MySQL5.7配置基于GTID的复制及GTID回退到传统模式的方法

  • 2019 年 10 月 5 日
  • 筆記

MySQL5.7下配置GTID复制的方法:

环境:

CentOS6.8X86_64

MySQL Community 5.7.17

node1:192.168.2.171 主库

node2:192.168.2.172 从库

修改主库和从库的配置文件,加入红色部分的配置项:

主库:

[mysqld]

log-bin=mysql-bin

binlog_format= ROW

gtid-mode = ON

enforce_gtid_consistency = ON

master-info-repository=TABLE

relay-log-info-repository=TABLE

从库:

[mysqld]

log-bin=mysql-bin

binlog_format= ROW

log_slave_updates = ON

gtid-mode = ON

enforce_gtid_consistency = ON

master-info-repository=TABLE

relay-log-info-repository=TABLE

重启主库和从库,使上面的配置生效。

然后在node1上导出全量的数据并传到node2:

mysqldump -uroot -proot -q–single-transaction -A  > /root/all.sql

scp /root/all.sql [email protected]:/root

在node2上导入: mysql -uroot -proot < /root/all.sql

然后,还要在node1的主库创建个复制权限的账号:

> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'repluser'@'192.168.2.%' IDENTIFIED BY 'Abcd@1234';

在slave上配置change master to指向(如下6行代码):

> change master to

  master_HOST='192.168.2.171',

  master_PORT=3306,

  master_USER='repluser',

  master_PASSWORD='Abcd@1234',

  master_AUTO_POSITION=1;

> start slave;

> show slave statusG  结果如下:

***************************1. row ***************************

               Slave_IO_State: Waiting formaster to send event

                  Master_Host: 192.168.2.171

                  Master_User: repluser

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql.000006

          Read_Master_Log_Pos: 786

               Relay_Log_File:node2-relay-bin.000002

                Relay_Log_Pos: 991

        Relay_Master_Log_File: mysql.000006

             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: 786

              Relay_Log_Space: 1198

              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:101

                  Master_UUID:e2deedc8-ed36-11e6-b826-000c29f17302

             Master_Info_File:mysql.slave_master_info

                    SQL_Delay: 0

          SQL_Remaining_Delay: NULL

      Slave_SQL_Running_State: Slave has readall 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:e2deedc8-ed36-11e6-b826-000c29f17302:1-3

            Executed_Gtid_Set:e2deedc8-ed36-11e6-b826-000c29f17302:1-3

                Auto_Position: 1

         Replicate_Rewrite_DB:

                 Channel_Name:

           Master_TLS_Version:

这样,我们的基于GTID的复制就配置完成了。

GTID复制转成传统模式的方法:

如果之前启用过了GTID,那么就不能不能再使用传统的change master to的方式了,会报错,如下:

ERROR 1776 (HY000): Parameters MASTER_LOG_FILE, MASTER_LOG_POS,RELAY_LOG_FILE and RELAY_LOG_POS cannot be set when MASTER_AUTO_POSITION isactive.

要转换成传统模式,需要在my.cnf里面注释掉下面2行:

gtid-mode=ON

enforce_gtid_consistency = ON

然后重启MySQL。

解决方法:

> stop slave;

> show slave statusG

记录下当前复制到哪里了,如下图红色部分:

> change master to MASTER_AUTO_POSITION=0;      # 设置为0关闭这个选项,这个选项是GTID复制才用到的。

> CHANGE MASTER TO

    MASTER_HOST = '192.168.2.171',

    MASTER_USER='repluser',

    MASTER_PASSWORD='Abcd@1234',

    MASTER_PORT=3306,

    MASTER_LOG_FILE='mysql.000006',

    MASTER_LOG_POS=786,

    MASTER_CONNECT_RETRY=10;

> start slave;

> show slave statusG 验证下是否IO/SQL都是YES状态。如下图,我们已经将GTID复制的转成传统模式了。