Mysql-Gtid复制实战&问题总结

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
从库 127.0.0.1 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
上一篇:SVG系列二


下一篇:强制SQL Server执行计划使用并行提升在复杂查询语句下的性能