title: Mysql-Gtid複制實戰&問題總結
date: 2019-07-23 10:15:45
categories: Mysql
實踐一遍MYSQL的GTID複制,記錄總結整個過程,包括:
- 手動配置GTID主備的方法,重要參數
- GTID正常複制場景
- GTID異常複制場景
- 如何修複GTID複制錯誤
- GTID與備份恢複
- 相關參數
1 Gtid複制配置實戰
1.1 環境
使用端口、環境變量隔離兩個資料庫
部署 | IP | PORT |
---|---|---|
主庫 | 127.0.0.1 | 5404 |
從庫 | 5405 |
1.2 安裝MYSQL(省略)
1.3 主備配置
master
[mysqld]
innodb_buffer_pool_size = 128M
basedir = /home/mingjie.gmj/databases/mysql5404
datadir = /home/mingjie.gmj/databases/data/mydata5404
port = 5404
server_id = 06700004
socket = /home/mingjie.gmj/databases/data/mydata5404/mysql5404.sock
join_buffer_size = 128M
sort_buffer_size = 2M
read_rnd_buffer_size = 2M
log_bin_trust_function_creators = 1
log-bin = /home/mingjie.gmj/databases/data/mydata5404/mysql-bin.log
log-bin-index = /home/mingjie.gmj/databases/data/mydata5404/master-log-bin.index
log-error = /home/mingjie.gmj/databases/data/mydata5404/master-error.log
relay-log = /home/mingjie.gmj/databases/data/mydata5404/slave-relay.log
relay-log-info-file = /home/mingjie.gmj/databases/data/mydata5404/slave-relay-log.info
relay-log-index = /home/mingjie.gmj/databases/data/mydata5404/slave-relay-log.index
master-info-file = /home/mingjie.gmj/databases/data/mydata5404/master.info
log-slave-updates = 1
#read_only = ON
#master_info_repository = TABLE
#relay_log_info_repository = TABLE
binlog_format = ROW
gtid_mode = ON
enforce-gtid-consistency = 1
log-slave-updates = 1
max_binlog_size = 500M
binlog_checksum = CRC32
slave
[mysqld]
innodb_buffer_pool_size = 128M
basedir = /home/mingjie.gmj/databases/mysql5405
datadir = /home/mingjie.gmj/databases/data/mydata5405
port = 5405
server_id = 06700005
socket = /home/mingjie.gmj/databases/data/mydata5405/mysql5405.sock
join_buffer_size = 128M
sort_buffer_size = 2M
read_rnd_buffer_size = 2M
log_bin_trust_function_creators = 1
log-bin = /home/mingjie.gmj/databases/data/mydata5405/mysql-bin.log
log-bin-index = /home/mingjie.gmj/databases/data/mydata5405/master-log-bin.index
log-error = /home/mingjie.gmj/databases/data/mydata5405/master-error.log
relay-log = /home/mingjie.gmj/databases/data/mydata5405/slave-relay.log
relay-log-info-file = /home/mingjie.gmj/databases/data/mydata5405/slave-relay-log.info
relay-log-index = /home/mingjie.gmj/databases/data/mydata5405/slave-relay-log.index
master-info-file = /home/mingjie.gmj/databases/data/mydata5405/master.info
log-slave-updates = 1
read_only = ON
master_info_repository = TABLE
relay_log_info_repository = TABLE
binlog_format = ROW
gtid_mode = ON
enforce-gtid-consistency = 1
log-slave-updates = 1
max_binlog_size = 500M
binlog_checksum = CRC32
1.4 配置主庫
配置主庫并寫入資料
/home/mingjie.gmj/databases/mysql5404/scripts/mysql_install_db --basedir=/home/mingjie.gmj/databases/mysql5404 --datadir=/home/mingjie.gmj/databases/data/mydata5404 --user=mingjie.gmj
/bin/sh /home/mingjie.gmj/databases/mysql5404/bin/mysqld_safe --defaults-file=/home/mingjie.gmj/databases/data/mydata5404/my.cnf &
建立測試表,複制賬号
注意:一定把匿名使用者删了,要不然連不上!
$ mysql -uroot test
mysql> create table tbl01(id int, info text);
Query OK, 0 rows affected (0.02 sec)
mysql> insert into tbl01 values (1, '001');
Query OK, 1 row affected (0.00 sec)
mysql> insert into tbl01 values (2, '001');
Query OK, 1 row affected (0.00 sec)
mysql> insert into tbl01 values (3, 'info');
Query OK, 1 row affected (0.00 sec)
mysql> grant replication slave, replication client on *.* to repl@'127.0.0.%' identified by '333';
Query OK, 0 rows affected (0.00 sec)
mysql> delete from mysql.user where user='';
Query OK, 2 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
導出資料
mysqldump -uroot --all-databases --default-character-set=utf8 -R -q --all-databases --triggers --routines --events --master-data=2 --single-transaction > /tmp/5404.sql
1.5 配置備庫
配置備庫并寫入資料
/home/mingjie.gmj/databases/mysql5405/scripts/mysql_install_db --basedir=/home/mingjie.gmj/databases/mysql5405 --datadir=/home/mingjie.gmj/databases/data/mydata5405 --user=mingjie.gmj
/bin/sh /home/mingjie.gmj/databases/mysql5405/bin/mysqld_safe --defaults-file=/home/mingjie.gmj/databases/data/mydata5405/my.cnf &
建立複制賬号
mysql> grant replication slave, replication client on *.* to repl@'127.0.0.%' identified by '333';
Query OK, 0 rows affected (0.00 sec)
重置gtid并導入資料,不重置會報錯
ERROR 1840 (HY000) at line 24: @@GLOBAL.GTID_PURGED can only be set when @@GLOBAL.GTID_EXECUTED is empty.
mysql> show global variables like '%GTID%';
+---------------------------------+----------------------------------------+
| Variable_name | Value |
+---------------------------------+----------------------------------------+
| binlog_gtid_simple_recovery | OFF |
| enforce_gtid_consistency | ON |
| gtid_executed | 15348f20-ad20-11e9-8bc7-00163f00f11a:1 |
| gtid_mode | ON |
| gtid_owned | |
| gtid_purged | |
| simplified_binlog_gtid_recovery | OFF |
+---------------------------------+----------------------------------------+
7 rows in set (0.00 sec)
mysql> reset master;
Query OK, 0 rows affected (0.01 sec)
mysql> show global variables like '%GTID%';
+---------------------------------+-------+
| Variable_name | Value |
+---------------------------------+-------+
| binlog_gtid_simple_recovery | OFF |
| enforce_gtid_consistency | ON |
| gtid_executed | |
| gtid_mode | ON |
| gtid_owned | |
| gtid_purged | |
| simplified_binlog_gtid_recovery | OFF |
+---------------------------------+-------+
7 rows in set (0.00 sec)
mysql -uroot --default-character-set=utf8 < /tmp/5404.sql
啟動複制
位點在導出的dump檔案中!
change master to master_host='127.0.0.1',
master_user='repl',
master_password='333',
master_port=5404,
master_log_file='mysql-bin.000001',
master_log_pos=1327;
或者自動找位點
change master to master_host='127.0.0.1',
master_user='repl',
master_password='333',
master_port=5404,
MASTER_AUTO_POSITION=1;
1.6 驗證狀态
檢視備庫狀态
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 127.0.0.1
Master_User: repl
Master_Port: 5404
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 2175
Relay_Log_File: slave-relay.000002
Relay_Log_Pos: 1256
Relay_Master_Log_File: mysql-bin.000001
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: 2175
Relay_Log_Space: 1456
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: 6700004
Master_UUID: 673619b8-ad1a-11e9-8ba2-00163f00f11a
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 the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: 673619b8-ad1a-11e9-8ba2-00163f00f11a:6-8
Executed_Gtid_Set: 673619b8-ad1a-11e9-8ba2-00163f00f11a:1-8
Auto_Position: 1
1 row in set (0.00 sec)
正常的複制線程狀态
mysql> select * from information_schema.processlist where user<>'root'\G
*************************** 1. row ***************************
ID: 7
USER: system user
HOST:
DB: NULL
COMMAND: Connect
TIME: 404
STATE: Waiting for master to send event
INFO: NULL
*************************** 2. row ***************************
ID: 8
USER: system user
HOST:
DB: NULL
COMMAND: Connect
TIME: 273
STATE: Slave has read all relay log; waiting for the slave I/O thread t
INFO: NULL
2 rows in set (0.01 sec)
2 GTID複制測試
2.1 正常複制
繼續上面環境主庫上執行
mysql> create database tdb;
Query OK, 1 row affected (0.00 sec)
mysql> use tdb;
Database changed
mysql> CREATE TABLE `test1` (`id` int(11) DEFAULT NULL,`count` int(11) DEFAULT NULL);
Query OK, 0 rows affected (0.01 sec)
mysql> insert into test1 values(1,1);
Query OK, 1 row affected (0.00 sec)
從庫已經執行成功,檢查從庫的狀态
從庫Retrieved_Gtid_Set增加了4個事務ID,Executed_Gtid_Set執行了4個事務ID
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 127.0.0.1
Master_User: repl
Master_Port: 5404
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 2753
Relay_Log_File: slave-relay.000002
Relay_Log_Pos: 1834
Relay_Master_Log_File: mysql-bin.000001
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: 2753
Relay_Log_Space: 2034
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: 6700004
Master_UUID: 673619b8-ad1a-11e9-8ba2-00163f00f11a
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 the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: 673619b8-ad1a-11e9-8ba2-00163f00f11a:6-11
Executed_Gtid_Set: 673619b8-ad1a-11e9-8ba2-00163f00f11a:1-11
Auto_Position: 1
2.2 異常複制場景1——主庫日志reset落後于備庫
主庫執行
reset master
,插入新資料
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 151 | | | |
+------------------+----------+--------------+------------------+-------------------+
mysql> insert into test1 values (8,8);
Query OK, 1 row affected (0.01 sec)
mysql> insert into test1 values (8,9);
Query OK, 1 row affected (0.01 sec)
mysql> insert into test1 values (8,10);
Query OK, 1 row affected (0.00 sec)
mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000001
Position: 877
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set: 673619b8-ad1a-11e9-8ba2-00163f00f11a:1-3
1 row in set (0.00 sec)
備庫複制中斷
mysql> show slave status\G
*************************** 1. row ***************************
...
...
Last_IO_Errno: 1236
Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Slave has more GTIDs than the master has, using the master's SERVER_UUID. This may indicate that the end of the binary log was truncated or that the last binary log file was lost, e.g., after a power or disk failure when sync_binlog != 1. The master may or may not have rolled back transactions that were already replica'
...
...
Retrieved_Gtid_Set: 673619b8-ad1a-11e9-8ba2-00163f00f11a:6-11
Executed_Gtid_Set: 673619b8-ad1a-11e9-8ba2-00163f00f11a:1-11
Auto_Position: 1
1 row in set (0.00 sec)
備庫
reset master
ysql> reset master;
Query OK, 0 rows affected (0.01 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 151 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
mysql> show slave status\G
*************************** 1. row ***************************
...
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
...
Retrieved_Gtid_Set: 673619b8-ad1a-11e9-8ba2-00163f00f11a:1-3
Executed_Gtid_Set: 673619b8-ad1a-11e9-8ba2-00163f00f11a:1-3
Auto_Position: 1
1 row in set (0.00 sec)
2.3 異常複制場景2——主備不一緻備庫redo失敗
主庫表中資料比備庫多
mysql> select * from test1;
+------+
| i |
+------+
| 1 |
| 2 |
| 30 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
+------+
mysql> insert into test1 values (10);
Query OK, 1 row affected (0.00 sec)
mysql> update test1 set i=300 where i=30;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> insert into test1 values (100);
Query OK, 1 row affected (0.00 sec)
備庫失敗
mysql> select * from test1;
+------+
| i |
+------+
| 1 |
| 2 |
| 4 |
| 5 |
| 7 |
| 8 |
| 9 |
| 10 |
+------+
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 127.0.0.1
Master_User: repl
Master_Port: 5404
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 1579
Relay_Log_File: slave-relay.000003
Relay_Log_Pos: 1309
Relay_Master_Log_File: mysql-bin.000001
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: 1032
Last_Error: Could not execute Update_rows event on table tdb.test1; Can't find record in 'test1', Error_code: 1032; handler error HA_ERR_END_OF_FILE; the event's master log mysql-bin.000001, end_log_pos 1311
Skip_Counter: 0
Exec_Master_Log_Pos: 1099
Relay_Log_Space: 1989
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: 1032
Last_SQL_Error: Could not execute Update_rows event on table tdb.test1; Can't find record in 'test1', Error_code: 1032; handler error HA_ERR_END_OF_FILE; the event's master log mysql-bin.000001, end_log_pos 1311
Replicate_Ignore_Server_Ids:
Master_Server_Id: 6700004
Master_UUID: 673619b8-ad1a-11e9-8ba2-00163f00f11a
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State:
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp: 190723 20:13:15
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: 673619b8-ad1a-11e9-8ba2-00163f00f11a:1-6
Executed_Gtid_Set: 673619b8-ad1a-11e9-8ba2-00163f00f11a:1-4
Auto_Position: 1
1 row in set (0.00 sec)
資料已經不一緻了!正常應該備庫重新搭,這裡做下實驗跳過GTID試試。
5号事務是失敗的,6号事務是OK的,是以來跳過5号事務!
注意:跳過誰就設誰的位置!
mysql> SET @@SESSION.GTID_NEXT= '673619b8-ad1a-11e9-8ba2-00163f00f11a:5';
mysql> STOP SLAVE;
Query OK, 0 rows affected (0.00 sec)
mysql> SET @@SESSION.GTID_NEXT= '673619b8-ad1a-11e9-8ba2-00163f00f11a:5';
Query OK, 0 rows affected (0.00 sec)
mysql> BEGIN; COMMIT;
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql> SET SESSION GTID_NEXT = AUTOMATIC;
Query OK, 0 rows affected (0.00 sec)
mysql> START SLAVE;
Query OK, 0 rows affected (0.00 sec)
備庫ok
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 127.0.0.1
Master_User: repl
Master_Port: 5404
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 1579
Relay_Log_File: slave-relay.000004
Relay_Log_Pos: 685
Relay_Master_Log_File: mysql-bin.000001
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: 1579
Relay_Log_Space: 2523
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: 6700004
Master_UUID: 673619b8-ad1a-11e9-8ba2-00163f00f11a
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 the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: 673619b8-ad1a-11e9-8ba2-00163f00f11a:1-6
Executed_Gtid_Set: 673619b8-ad1a-11e9-8ba2-00163f00f11a:1-6
&nbs