環境規劃
192.168.31.178 |
Master/Slave |
192.168.31.179 |
Master/Slave |
192.168.31.178 配置
gtid-mode=ON
enforce_gtid_consistency=on
log-slave-updates=true
master-info-repository=TABLE
relay-log-info-repository=TABLE
relay_log = /data/mysql/relay.log
relay_log_index = /data/mysql/relay_log.index
report-port = 3306
sync-master-info=1
slave_parallel_workers=5
binlog-checksum=CRC32
master-verify-checksum=1
slave-sql-verify-checksum=1
binlog-rows-query-log_events=1
auto-increment-increment = 2
auto-increment-offset = 1
slave-skip-errors = all
skip-slave-start = 1 #防止複製隨着mysql啓動而自動啓動
slave-parallel-type = LOGICAL_CLOCK
server-id = 1
192.168.31.179 配置
gtid-mode=ON
enforce_gtid_consistency=on
log-slave-updates=true
master-info-repository=TABLE
relay-log-info-repository=TABLE
relay_log = /data/mysql/relay.log
relay_log_index = /data/mysql/relay_log.index
report-port = 3306
sync-master-info=1
slave_parallel_workers=5
binlog-checksum=CRC32
master-verify-checksum=1
slave-sql-verify-checksum=1
binlog-rows-query-log_events=1
auto-increment-increment = 2
auto-increment-offset = 2
slave-parallel-type = LOGICAL_CLOCK
slave-skip-errors = all
skip-slave-start = 1 #防止複製隨着mysql啓動而自動啓動
server-id = 11
創建同步用戶和備份最新數據的數據庫
兩臺數據庫都要執行
grant replication slave on *.* to 'ops'@'192.168.10.%' identified by 'ops123';
flush privileges;
最新數據庫執行備份最新的數據
mysqldump -uroot -p --single-transaction --default-character-set=utf8 --master-data=2 --flush-logs --triggers --routines --events --all-databases > all.sql
在對應的從庫執行導入sql
scp all.sql 192.168.1.179:/data/
切換到179服務器導入sql文件
source /data/all.sql
連接主從
192.168.1.178
CHANGE MASTER TO
MASTER_HOST = '192.168.31.179',
MASTER_PORT = 3306,
MASTER_USER = 'ops',
MASTER_PASSWORD = 'ops123',
MASTER_AUTO_POSITION = 1;
192.168.1.179
CHANGE MASTER TO
MASTER_HOST = '192.168.31.178',
MASTER_PORT = 3306,
MASTER_USER = 'ops',
MASTER_PASSWORD = 'ops123',
MASTER_AUTO_POSITION = 1;
測試環境
192.168.31.178
MySQL [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.31.179
Master_User: ops
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000007
Read_Master_Log_Pos: 38729
Relay_Log_File: relay.000006
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql-bin.000007
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: 38729
Relay_Log_Space: 954
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: 11
Master_UUID: 64d5d956-a3e7-11ea-9725-000c29e5421a
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set: c6617d6e-a3e4-11ea-bb0d-000c29f6218f:1-8
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
192.168.31.179
MySQL [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.31.178
Master_User: ops
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000012
Read_Master_Log_Pos: 38769
Relay_Log_File: relay.000006
Relay_Log_Pos: 38942
Relay_Master_Log_File: mysql-bin.000012
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: 38769
Relay_Log_Space: 39529
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
Master_UUID: c6617d6e-a3e4-11ea-bb0d-000c29f6218f
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: c6617d6e-a3e4-11ea-bb0d-000c29f6218f:3-8
Executed_Gtid_Set: c6617d6e-a3e4-11ea-bb0d-000c29f6218f:1-8
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
Retrieved_Gtid_Set: c6617d6e-a3e4-11ea-bb0d-000c29f6218f:3-8 #表示收到的事務數
Executed_Gtid_Set: c6617d6e-a3e4-11ea-bb0d-000c29f6218f:1-8 #表示執行完的事務數
如果主從報錯 需要從這兩個參數去重新啓動規劃主從
假如主從複製出現錯誤
當在 slave 上執行 show slave status\G
Retrieved_Gtid_Set: $UUID:3-9
Executed_Gtid_Set: $UUID:1-8
此時 Slave_SQL_Running: No
上面的信息表明:
slave 收到了 UUID:3-9 個事務,執行成功 UUID:1-8,1-8表示已經執行完成了。在這裏出現了錯誤,也就是說執行 UUID:1-9時出現了錯誤。所以我們應該要跳過下一個事務即 9;
解決方法:
按照下列步驟執行
mysql> stop slave;
mysql> set gtid_next='$UUID:9';
mysql> begin;
mysql> commit;
mysql> set gtid_next='automatic';
mysql> start slave;
在跳過之前,分析一下 Binlog 並且記錄下來,分析是否可以跳過。跳過之後,看一下主從數據是否一致,是否需要修復數據等等,請謹慎操作。可以使用mysqlcheck來修復數據