1. 程式人生 > >mysql主從不同步處理過程分享

mysql主從不同步處理過程分享

背景  8月7日15:58收到報障資料庫出現不同步:資料庫共四臺,分別為10.255.70.11,10.255.70.12,10.255.70.13,10.255.70.14(ip為虛擬ip)

資料庫結構為:

              

故障時不同步現為:(1)70.11和70.13之間主主不同步 ,(2)70.11和70.12之間主從不同步,(3)70.11和70.14之間主從是同步的

(1)由於my.cnf檔案中有slave-skip-errors=all配置,所以在出現不同步錯誤時跳過,檢查同步引數Slave_IO_Running: Yes/Slave_SQL_Running: Yes均為yes,實際資料是不同步的

(2)70.11和70.12之間不同步,同步引數為Slave_IO_Running: NO/Slave_SQL_Running: Yes,報錯1062,截圖如下:

 

在70.12上操作如下:

mysql>stop slave ;

mysql>SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1 

mysql>slave start

檢視資料同步引數:發現已經同步

至此70.11和70.12的主從不同步問題已經解決,需要注意的是:跳過1062這個事務之後,雖然以後主從資料庫是同步的,但是不同步期間的資料將不再重新同步,即:不同步期間從庫的資料不完整

現在只存在70.11和70.13之間的主主資料庫不同步,在此期間發現70.11的庫db1資料丟失了,因為70.11和70.12,70.14的資料是同步的,所以這三個庫全部資料丟失

解決辦法思路如下:

(1)先將現有業務切至70.13上,將危害減少到最小

(2)首先恢復70.11的資料:檢視資料庫最近一次備份是當天凌晨3點半,3點半到下午15:58的資料通過binlog日誌進行恢復

(3)備份70.11的資料,匯入70.12/13/14的資料庫,並作主主/主從同步

具體操作:

恢復8月7日凌晨3點半之前的資料:

[root@7011]# gunzip db1_20190807.sql.gz
[root@7011]# mysql -h 10.255.70.11 -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2039
Server version: 5.7.17-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
|db1 |
+--------------------+
11 rows in set (0.00 sec)

mysql> use db1;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed, 1 warning
mysql> source db1.sql.gz;

通過binlog日誌恢復凌晨3:30~~16:30之間的資料:

(1)將時間段內的binlog日誌轉化為mysql可識別的語句

[root@7011]# mysqlbinlog  --no-defaults  --database=db1 --start-datetime='2019-08-07 03:30:00' --stop-datetime='2019-08-17 16:30:00' mysql-bin.000034 >temp20190807.sql
[root@7011]# ll temp20190807.sql
-rw-r--r-- 1 root root 655391529 8月   7 20:56 temp20190807.sql

(2)刪除binlog日誌中的drop語句

[root@7011]# sed -i -e '/DROP/d' temp20190807.sql

(3)資料恢復

[root@7011]# mysql -h 10.255.70.11 -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2039
Server version: 5.7.17-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| db1 |
+--------------------+
11 rows in set (0.00 sec)

mysql> use db1;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed, 1 warning
mysql> source temp20190807.sql;

至此:70.11的資料恢復完全

接下來進行70.12和13,14的資料同步:

(1)備份70.11的資料庫

[root@7011 ~]# mysqldump -h 10.255.70.11 -uroot  -p  -R ottdb1 | gzip > /tmp/db120190807.sql.gz

(2)將70.11的備份資料copy給70.12/13/14

[root@7011 tmp]# scp db120190807.sql.gz 10.255.70.12:/tmp/
The authenticity of host '10.255.70.12 (10.255.70.12)' can't be established.
RSA key fingerprint is aa:56:33:d3:aa:a8:af:a3:a9:c9:6e:26:6b:05:7f:b3.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.255.70.12' (RSA) to the list of known hosts.
[email protected]'s password: 
ottdb120190807.sql.gz        

[root@7011 tmp]# scp db120190807.sql.gz 10.255.70.13:/tmp/
The authenticity of host '10.255.70.13 (10.255.70.13)' can't be established.
RSA key fingerprint is aa:56:33:d3:aa:a8:af:a3:a9:c9:6e:26:6b:05:7f:b3.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.255.70.13' (RSA) to the list of known hosts.
[email protected]'s password: 
ottdb120190807.sql.gz   

[root@7011 tmp]# scp db120190807.sql.gz 10.255.70.14:/tmp/
The authenticity of host '10.255.70.14 (10.255.70.14)' can't be established.
RSA key fingerprint is aa:56:33:d3:aa:a8:af:a3:a9:c9:6e:26:6b:05:7f:b3.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.255.70.14' (RSA) to the list of known hosts.
[email protected]'s password: 
ottdb120190807.sql.gz                              

(3)恢復70.12/13/14資料,以70.12為例,13和14也進行如下操作:

[root@7012]# mysql -h 10.255.70.12 -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2039
Server version: 5.7.17-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| db1 |
+--------------------+
11 rows in set (0.00 sec)

mysql> use db1;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed, 1 warning
mysql> source db120180807.sql;

(4)記錄70.11和70.13的file和position

[root@7011]# mysql -h 10.255.70.11 -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2039
Server version: 5.7.17-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show master status;
+------------------------+-----------+--------------+------------------+-------------------+
| File                   | Position  | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------------+-----------+--------------+------------------+-------------------+
| mysql-bin.000048         278032567 |    db1       | mysql            |                   |
+------------------------+-----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql>

[root@7013]# mysql -h 10.255.70.13 -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2039
Server version: 5.7.17-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show master status;
+------------------------+-----------+--------------+------------------+-------------------+
| File                   | Position  | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------------+-----------+--------------+------------------+-------------------+
| mysql-bin.000055         622032567 |    db1       | mysql            |                   |
+------------------------+-----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql>

(5)實現70.11和70.13之間的主主同步,具體步驟如下:

在70.11上執行:

mysql> change master to master_host='10.255.70.13',master_user='root',master_password='123456',master_log_file='mysql-bin.000055',master_log_pos=622032567;

在70.13上執行:

mysql> change master to master_host='10.255.70.11',master_user='root',master_password='123456',master_log_file='mysql-bin.000048',master_log_pos=278032567;

資料庫同步完成,檢視資料庫同步情況

[root@7011]# mysql -h 10.255.70.11 -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2039
Server version: 5.7.17-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.255.70.13
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000049
          Read_Master_Log_Pos: 612239326
               Relay_Log_File: mysq-relay-bin.000014
                Relay_Log_Pos: 1301
        Relay_Master_Log_File: mysql-bin.000049
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: db1
          Replicate_Ignore_DB: mysql

接下來進行70.11和70.12和14的主從同步:

在70.12和70.14上執行:

mysql> change master to master_host='10.255.70.11',master_user='root',master_password='123456',master_log_file='mysql-bin.000048',master_log_pos=278032567;

檢視資料庫同步情況:

[root@7012]# mysql -h 10.255.70.12 -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2039
Server version: 5.7.17-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.255.70.11
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000049
          Read_Master_Log_Pos: 612239326
               Relay_Log_File: mysq-relay-bin.000014
                Relay_Log_Pos: 1301
        Relay_Master_Log_File: mysql-bin.000049
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: db1
          Replicate_Ignore_DB: mysql

資料庫恢復完成,修改業務地址到VIP地址,此次故障解決

資料同步過程中報錯處理:

   Last_IO_Errno: 1236
                Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'
處理方式:

flush logs;
show master status;

記下File, Position

重新進行同步

 Got fatal error 1236 from master when reading data from binary log

&n