生產環境臨時性忽略MySQL主從複製模擬故障實驗
阿新 • • 發佈:2020-08-17
1、主伺服器(192.168.43.100)
(1)安裝資料庫
# yum install -y mariadb-server # systemctl restart mariadb # mysql_secure_installation
(2)配置my.cnf檔案
# vim /etc/my.cnf [mysqld] server-id=1 log-bin=/data/logbin/mysql-bin # systemctl restart mariadb
(3)建立使用者並授權
[root@node1 ~]# mysql -uroot -p000000
# MariaDB [(none)]> grant replication slave on *.* to repluser@'192.168.43.%' identified by 'centos';
(4)備份資料庫
# mysqldump -uroot -p000000 -A --single-transaction --master-data=1 -F > /data/all.sql
(5)拷貝備份檔案過從主機
# scp /data/all.sql 192.168.43.200:/data/
2、從伺服器(192.168.43.200)
(1)安裝資料庫
# yum install -y mariadb-server # systemctl restart mariadb # mysql_secure_installation
(2)往all.sql備份檔案新增主從複製相關資訊
# vim /data/all.sql CHANGE MASTER TO MASTER_HOST='192.168.43.100', MASTER_USER='repluser', MASTER_PASSWORD='centos', MASTER_PORT=3306, MASTER_LOG_FILE='mariadb-bin.000002', MASTER_LOG_POS=245;
(3)配置my.cnf檔案
# vim /etc/my.cnf [mysqld] server-id=2 read-only # systemctl restart mariadb
(4)匯入資料
# mysql -uroot -p000000 < /data/all.sql
(5)啟動slave
mysql -uroot -p000000 MariaDB [(none)]> start slave; MariaDB [(none)]> show processlist; +----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+----------+ | Id | User | Host | db | Command | Time | State | Info | Progress | +----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+----------+ | 3 | root | localhost | NULL | Query | 0 | NULL | show processlist | 0.000 | | 6 | system user | | NULL | Connect | 1017 | Waiting for master to send event | NULL | 0.000 | | 7 | system user | | NULL | Connect | 1017 | Slave has read all relay log; waiting for the slave I/O thread to update it | NULL | 0.000 | +----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+----------+ 3 rows in set (0.00 sec) MariaDB [(none)]> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.43.100 Master_User: repluser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mariadb-bin.000002 Read_Master_Log_Pos: 407 Relay_Log_File: mariadb-relay-bin.000003 Relay_Log_Pos: 531 Relay_Master_Log_File: mariadb-bin.000002 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 407 Relay_Log_Space: 1275 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 1 row in set (0.00 sec)
3、模擬故障
當在從資料庫伺服器誤操作的情況下執行了一條建立資料庫的語句,正好主的資料庫伺服器也操作了這條語句。
MariaDB [(none)]> create database db2;
則會發生如下報錯
MariaDB [(none)]> show status slave\G ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'slave' at line 1 MariaDB [(none)]> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.43.100 Master_User: repluser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mariadb-bin.000002 Read_Master_Log_Pos: 407 Relay_Log_File: mariadb-relay-bin.000002 Relay_Log_Pos: 612 Relay_Master_Log_File: mariadb-bin.000002 Slave_IO_Running: Yes Slave_SQL_Running: No Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 1007 Last_Error: Error 'Can't create database 'db2'; database exists' on query. Default database: 'db2'. Query: 'create database db2' Skip_Counter: 0 Exec_Master_Log_Pos: 326 Relay_Log_Space: 989 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: NULL Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 1007 Last_SQL_Error: Error 'Can't create database 'db2'; database exists' on query. Default database: 'db2'. Query: 'create database db2' Replicate_Ignore_Server_Ids: Master_Server_Id: 1 1 row in set (0.00 sec)
由於做了主從架構,主節點做的操作資料,只會單向的同步給從節點;從節點所做的操作並不會返回給主節點。所以當主節點也執行了從節點一模一樣的語句時,則會提示db2資料庫檔案已存在發生衝突。
故:主從複製服務已經不能夠正常運轉。
通常 Last_Errno: 1007程式碼提示,都是指物件已存在。
臨時性解決方法:
雖說上面的例子造成的故障,通過檢視報錯可以快速定位到故障所在。但是在生產環境中,也許不會像我們想象中那麼簡單,可以暫時選擇性忽略;因為有大量的事務在往主伺服器讀寫,如果從伺服器不能夠及時正常的同步資料,則會造成一定的資料丟失。等後續再去排查故障
mysql> STOP SLAVE; mysql> SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1; # 從伺服器忽略1個主伺服器的複製事件 mysql> START SLAVE;