ÔõÑùÔÚLinuxÉÏÉèÖø߿ÉÓõÄÊý¾Ý¿âÖ÷´Ó¸´ÖƼà¿Ø
ÔõÑùÔÚlinuxÉÏÉèÖø߿ÉÓõÄÊý¾Ý¿âÖ÷´Ó¸´ÖƼà¿Ø
СÐò£º
ÔÚÏÖ´úµÄÊÖÒÕÇéÐÎÖУ¬Êý¾Ý¿âÊÇÒ»¸öÒªº¦×é¼þ£¬Ðí¶àÓ¦ÓóÌÐòÒÀÀµÓÚËüÃÇ¡£³öÓÚ¿ÉÓÃÐÔºÍÊý¾Ý±£»¤µÄ˼Á¿£¬Êý¾Ý¿âµÄ¸ß¿ÉÓÃÐÔºÍÖ÷´Ó¸´Öƶ¼ÊǺÜÊÇÖ÷ÒªµÄ¹¦Ð§¡£±¾ÎĽ«ÏÈÈÝÔõÑùÔÚlinuxÉÏÉèÖø߿ÉÓõÄÊý¾Ý¿âÖ÷´Ó¸´ÖƼà¿Ø£¬Í¨¹ýʾÀý´úÂëÀ´ÑÝʾ²Ù×÷°ì·¨¡£
Ö÷´Ó¸´ÖƵÄÊÂÇéÔÀí£º
Ö÷´Ó¸´ÖÆÊÇÒ»ÖÖ³£¼ûµÄÊý¾Ý¿â¸´ÖÆÒªÁ죬ÆäÖÐÒ»¸öÊý¾Ý¿âЧÀÍÆ÷×÷ΪÖ÷ЧÀÍÆ÷£¨Master£©£¬¶øÆäËûЧÀÍÆ÷Ôò×÷Ϊ´ÓЧÀÍÆ÷£¨Slave£©¡£Ö÷ЧÀÍÆ÷ÎüÊÕµ½µÄд²Ù×÷½«±»¸´ÖƵ½´ÓЧÀÍÆ÷¡£ÕâÖּܹ¹ÌṩÁËÊý¾ÝÈßÓà¡¢¶ÁдÊèÉ¢ºÍ¹ÊÕϻָ´µÄÀûÒæ¡£
ÉèÖÃÖ÷ЧÀÍÆ÷£º
Ê×ÏÈ£¬ÎÒÃÇÐèҪװÖÃÊý¾Ý¿âЧÀÍÆ÷¡£±¾ÎÄÒÔMySQLΪÀý¡£
×°ÖÃMySQLЧÀÍÆ÷£º
sudo apt update sudo apt install mysql-server
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
ÉèÖÃÖ÷ЧÀÍÆ÷£º
±à¼MySQLÉèÖÃÎļþ£º
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
ÔÚÎļþÖÐÕÒµ½ÒÔÏÂÐУ¬²¢¾ÙÐÐÐ޸ģº
bind-address = 0.0.0.0 server-id = 1 log_bin = /var/log/mysql/mysql-bin.log
µÇ¼ºó¸´ÖÆ
ÖØÆôMySQLЧÀÍ£º
sudo systemctl restart mysql
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
ÉèÖôÓЧÀÍÆ÷£º
×°ÖÃMySQLЧÀÍÆ÷£º
sudo apt update sudo apt install mysql-server
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
ÉèÖôÓЧÀÍÆ÷£º
±à¼MySQLÉèÖÃÎļþ£º
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
ÔÚÎļþÖÐÕÒµ½ÒÔÏÂÐУ¬²¢¾ÙÐÐÐ޸ģº
bind-address = 0.0.0.0 server-id = 2 log_bin = /var/log/mysql/mysql-bin.log relay_log = /var/log/mysql/mysql-relay-bin.log
µÇ¼ºó¸´ÖÆ
ÖØÆôMySQLЧÀÍ£º
sudo systemctl restart mysql
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
ÉèÖÃÖ÷´Ó¹Øϵ£º
ÔÚÖ÷ЧÀÍÆ÷ÉϽ¨ÉèÒ»¸öÓÃÓÚ¸´ÖƵÄÓû§£º
mysql -u root -p GRANT REPLICATION SLAVE ON *.* TO 'replication_user'@'%' IDENTIFIED BY 'password'; FLUSH PRIVILEGES; EXIT;
µÇ¼ºó¸´ÖÆ
ÔÚ´ÓЧÀÍÆ÷ÉÏÉèÖÃÖ÷ЧÀÍÆ÷ÐÅÏ¢£º
mysql -u root -p CHANGE MASTER TO MASTER_HOST='Ö÷ЧÀÍÆ÷µÄIPµØµã', MASTER_USER='replication_user', MASTER_PASSWORD='password'; START SLAVE; EXIT;
µÇ¼ºó¸´ÖÆ
ÑéÖ¤Ö÷´Ó¸´ÖÆÊÇ·ñÕý³£ÊÂÇ飺
ÔÚÖ÷ЧÀÍÆ÷ÉϽ¨ÉèÒ»¸öÊý¾Ý¿âºÍ±í£¬²¢²åÈëһЩÊý¾Ý¡£È»ºó£¬ÔÚ´ÓЧÀÍÆ÷ÉÏÑéÖ¤ÊÇ·ñÄܹ»¿´µ½ÏìÓ¦µÄÊý¾Ý¡£
ÉèÖüà¿Ø£º
ΪÁËÈ·±£Êý¾Ý¿âÖ÷´Ó¸´ÖƵĸ߿ÉÓÃÐÔ£¬ÎÒÃÇÐèÒª¼à¿ØÆä״̬£¬²¢ÊµÊ±·¢Ã÷ºÍ´¦Öóͷ£¹ÊÕÏ¡£ÏÂÃæÊÇÒ»¸öʹÓÃPython±àдµÄ¼òÆÓ¼à¿Ø¾ç±¾¡£
×°ÖÃËùÐèµÄPython°ü£º
sudo apt update sudo apt install python3-pip pip3 install mysql-connector-python pip3 install smtplib
µÇ¼ºó¸´ÖÆ
½¨Éèmonitor.pyÎļþ£¬²¢½«ÒÔÏ´úÂ븴ÖƵ½ÎļþÖУº
import smtplib import mysql.connector from email.mime.text import MIMEText def send_email(message): sender = "your_email@gmail.com" receiver = "recipient_email@gmail.com" subject = "Database Replication Alert" msg = MIMEText(message) msg['Subject'] = subject msg['From'] = sender msg['To'] = receiver try: with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp: smtp.login(sender, "your_password") smtp.sendmail(sender, receiver, msg.as_string()) print("Email sent successfully!") except Exception as e: print("Email failed to send: " + str(e)) def monitor_replication(): try: connection = mysql.connector.connect(host="localhost", user="replication_user", password="password", database="test") cursor = connection.cursor() cursor.execute("SELECT COUNT(*) FROM my_table") result = cursor.fetchone() cursor.close() connection.close() print("Replication is working fine, number of records: " + str(result[0])) except mysql.connector.Error as error: message = "Replication failed: " + str(error) print(message) send_email(message) if __name__ == "__main__": monitor_replication()
µÇ¼ºó¸´ÖÆ
ÐÞ¸Ämonitor.pyÖеÄÉèÖÃÐÅÏ¢£¬°üÀ¨·¢¼þÈ˺ÍÊÕ¼þÈ˵ÄÓÊÏäµØµã£¬ÒÔ¼°·¢¼þÈ˵ÄÓÊÏäÃÜÂë¡£
ÔËÐÐmonitor.py¾ç±¾£¬¿ÉÒÔ½«Æä¼ÓÈë׼ʱʹÃüÖУ¬ÒÔ°´ÆÚ¼à¿ØÊý¾Ý¿âÖ÷´Ó¸´ÖƵÄ״̬¡£
½áÂÛ£º
ͨ¹ýÒÔÉÏ°ì·¨£¬ÎÒÃÇ¿ÉÒÔÔÚLinuxÉÏÉèÖø߿ÉÓõÄÊý¾Ý¿âÖ÷´Ó¸´ÖƼà¿Ø¡£Ò»Á¬¼à¿ØÊý¾Ý¿âµÄ״̬¹ØÓÚ¹ÊÕϻָ´ºÍ¿ÉÓÃÐÔÊÇÖÁ¹ØÖ÷ÒªµÄ¡£Ê¹ÓÃʾÀý´úÂ룬ÎÒÃÇ¿ÉÒÔʵʱ·¢Ã÷²¢´¦Öóͷ£Êý¾Ý¿âÖ÷´Ó¸´ÖƵÄÎÊÌ⣬´Ó¶øÈ·±£ÓªÒµµÄƽÎÈÔËÐС£
ÒÔÉϾÍÊÇÔõÑùÔÚLinuxÉÏÉèÖø߿ÉÓõÄÊý¾Ý¿âÖ÷´Ó¸´ÖƼà¿ØµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡