MySQL学习笔记07基于GTID的复制

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL学习笔记07基于GTID的复制相关的知识,希望对你有一定的参考价值。

1.1.1. 相关概念

 

(1)GTID

GTIDGlobal Transaction Identifier的缩写。GTID是一个跟提交的事务有关的标识符,由提交事务所在的原始mysqlUUID和事务的编号组成;因此,每个GTID在每个参与的MySQL中都是唯一的,而且由GTID可以取得该事务所在的原始MySQL以及事务在原始MySQL上的编号。

 

GTID格式如下:

GTID = MySQL原始UUD:事务编号

GTID的例子如下:

GTID=a2392929-6dfb-11e7-b294-000c29b1c103:1

 

一个GTID序列可以简写为:

GTID序列 = MySQL原始UUD:开始事务编号-结束事务编号

例如:

GTID=a2392929-6dfb-11e7-b294-000c29b1c103:1-5

 

 

(2)mysql.gtid_executed表。

在启用了GTID选项之后,MySQL将提交的事务的GTID记录到mysql.executed数据表中。根据GTID记录,在基于GTID的复制体系中,slave无需记录masterBinary Log文件和位置。

gtid_executed数据表的结构如下:

mysql> show create table mysql.gtid_executed;

| Table         | Create Table                                                                                                                                                                                                                                                                                                                                                                             | gtid_executed | CREATE TABLE `gtid_executed` (

  `source_uuid` char(36) NOT NULL COMMENT ‘uuid of the source where the transaction was originally executed.‘,

  `interval_start` bigint(20) NOT NULL COMMENT ‘First number of interval.‘,

  `interval_end` bigint(20) NOT NULL COMMENT ‘Last number of interval.‘,

  PRIMARY KEY (`source_uuid`,`interval_start`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1 |

 

 

(3)基于GTID的复制。

在数据库中每个已经提交的事务,都有一个唯一的GTID。这个特点在用于复制时,可以代替Binary Log的文件名和位置,。通过比较masterslave的最后执行的GTID,即可知道下一个GTID,从而不必指定Binary Log文件名和位置。

由于GTID跟事务密切相关,因此基于GTID的复制,只适用于支持事务的存储引擎,比如InnoDB;对于不支持事务的存储引擎,比如MyISAM,则不支持。

 

 

1.1.2. 部署基于GTID的主从复制

 

目标:实现基于GTID的主从复制。

master192.168.197.111

slave:  192.168.197.112

 

(1)修改master上的MySQL的配置文件。

做以下修改:

(a)设置一个唯一的server_uuid

(b)启用GTID模式。

 

[mysqld]

gtid_mode=ON

enforce-gtid-consistency=true

log-bin=mysql-bin

server-id=111

sync_binlog=1

innodb_flush_log_at_trx_commit=1

 

 

(2)修改masterserver-uuid

修改auto.cnf文件。

sudo cat /opt/mysql/data/auto.cnf

[auto]

server-uuid=a2392929-6dfb-11e7-b294-000c29b1c111

 

 

(3)master上创建用于复制的用户。

mysql> create user ‘repl‘@‘%‘ identified by ‘123456‘;

Query OK, 0 rows affected (0.02 sec)

 

mysql> grant replication slave  on *.* to ‘repl‘@‘%‘;

Query OK, 0 rows affected (0.01 sec)

 

mysql> flush privileges;

Query OK, 0 rows affected (0.01 sec)

 

 

(4)修改slave上的MySQL配置文件。

 

[mysqld]

gtid_mode=ON

enforce-gtid-consistency=true

log-bin=mysql-bin

server-id=112

innodb_flush_log_at_trx_commit=1

sync_binlog=1

log-slave-updates=true

replicate-wild-ignore-table=mysql.%

replicate-wild-ignore-table=information_schema.%

replicate-wild-ignore-table=performance_schema.%

replicate-wild-ignore-table=sys.%

 

其中log-slave-updates的作用是在slave上启用日志功能,从而使得slave能够作为其它MySQL服务的master,即组建多层级的master-slave复制体系。

 

 

(5)修改slaveserver-uuid

sudo cat /opt/mysql/data/auto.cnf

[auto]

server-uuid=a2392929-6dfb-11e7-b294-000c29b1c112

 

 

(6)重新启动masterslave上的MySQL服务。

 

 

(7)建立master-slave复制关系。

 

如果slave上之前部署了基于Binary Log的复制,则需要先完全清除掉这种复制的痕迹。

mysql> stop slave;

Query OK, 0 rows affected (0.01 sec)

 

mysql> reset slave all;

Query OK, 0 rows affected (0.00 sec)

 

mysql> reset master;

Query OK, 0 rows affected, 1 warning (0.04 sec)

 

 

 

再在slave上执行以下命令建立复制关系。

mysql> CHANGE MASTER TO

    ->  MASTER_HOST = ‘192.168.197.111‘,

    ->  MASTER_PORT = 3306,

    ->  MASTER_USER = ‘repl‘,

    ->  MASTER_PASSWORD = ‘123456‘,

    ->  MASTER_AUTO_POSITION = 1;

Query OK, 0 rows affected, 2 warnings (0.02 sec)

 

 

查看复制状态如下:

mysql> show slave status\G

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

               Slave_IO_State:

                  Master_Host: 192.168.197.111

                  Master_User: repl

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File:

          Read_Master_Log_Pos: 4

               Relay_Log_File: 112-relay-bin.000001

                Relay_Log_Pos: 4

        Relay_Master_Log_File:

             Slave_IO_Running: No

            Slave_SQL_Running: No

              Replicate_Do_DB:

          Replicate_Ignore_DB:

           Replicate_Do_Table:

       Replicate_Ignore_Table:

      Replicate_Wild_Do_Table:

  Replicate_Wild_Ignore_Table: mysql.%,information_schema.%,performance_schema.%,sys.%

                   Last_Errno: 0

                   Last_Error:

                 Skip_Counter: 0

          Exec_Master_Log_Pos: 0

              Relay_Log_Space: 194

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

               Last_SQL_Error:

  Replicate_Ignore_Server_Ids:

             Master_Server_Id: 0

                  Master_UUID:

             Master_Info_File: /opt/mysql/data/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:

               Master_SSL_Crl:

           Master_SSL_Crlpath:

           Retrieved_Gtid_Set:

            Executed_Gtid_Set: a2392929-6dfb-11e7-b294-000c29b1c111:1-6,

a2392929-6dfb-11e7-b294-000c29b1c112:1-2

                Auto_Position: 1

         Replicate_Rewrite_DB:

                 Channel_Name:

           Master_TLS_Version:

1 row in set (0.00 sec)

 

 

 

(8)slave上启动复制。

mysql> show processlist;

+----+------+-----------+------+---------+------+----------+------------------+

| Id | User | Host      | db   | Command | Time | State    | Info             |

+----+------+-----------+------+---------+------+----------+------------------+

|  3 | root | localhost | NULL | Query   |    0 | starting | show processlist |

+----+------+-----------+------+---------+------+----------+------------------+

1 row in set (0.00 sec)

 

mysql> start slave;

Query OK, 0 rows affected (0.00 sec)

 

mysql> show processlist;

+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+

| Id | User        | Host      | db   | Command | Time | State                                                  | Info             |

+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+

|  3 | root        | localhost | NULL | Query   |    0 | starting                                               | show processlist |

|  4 | system user |           | NULL | Connect |    3 | Waiting for master to send event                       | NULL             |

|  5 | system user |           | NULL | Connect |  207 | Slave has read all relay log; waiting for more updates | NULL             |

+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+

 

 

在建立了复制关系后,可以进一步实际验证复制关系是否能够正常运作。

最简单的验证方式是在master上创建一些数据,然后在slave上查看是否有这些数据,同时查看复制状态是否正常。

master上创建数据:

mysql> create database test;

Query OK, 1 row affected (0.02 sec)

 

mysql> use test;

Database changed

mysql> create table data (name2 varchar(100));

Query OK, 0 rows affected (0.07 sec)

 

mysql> insert into data (name2) values (‘001‘) , (‘002‘) , (‘003‘);

Query OK, 3 rows affected (0.04 sec)

Records: 3  Duplicates: 0  Warnings: 0

 

mysql> select * from data;

+-------+

| name2 |

+-------+

| 001   |

| 002   |

| 003   |

+-------+

3 rows in set (0.00 sec)

 

master上查看复制状态:

mysql> show master status\G

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

             File: mysql-bin.000002

         Position: 1350

     Binlog_Do_DB:

 Binlog_Ignore_DB:

Executed_Gtid_Set: a2392929-6dfb-11e7-b294-000c29b1c111:1-6

1 row in set (0.00 sec)

 

 

slave上查看数据:

mysql> use test;

Database changed

mysql> select * from data;

+-------+

| name2 |

+-------+

| 001   |

| 002   |

| 003   |

+-------+

3 rows in set (0.00 sec)

slave上的数据与master上完全一致。

 

slave上查看复制状态:

 

mysql> show slave status\G

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

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.197.111

                  Master_User: repl

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000003

          Read_Master_Log_Pos: 194

               Relay_Log_File: 112-relay-bin.000003

                Relay_Log_Pos: 407

        Relay_Master_Log_File: mysql-bin.000003

             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: mysql.%,information_schema.%,performance_schema.%,sys.%

                   Last_Errno: 0

                   Last_Error:

                 Skip_Counter: 0

          Exec_Master_Log_Pos: 194

              Relay_Log_Space: 2021

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

                  Master_UUID: a2392929-6dfb-11e7-b294-000c29b1c111

             Master_Info_File: /opt/mysql/data/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: a2392929-6dfb-11e7-b294-000c29b1c111:1-6

            Executed_Gtid_Set: a2392929-6dfb-11e7-b294-000c29b1c111:1-6

                Auto_Position: 1

         Replicate_Rewrite_DB:

                 Channel_Name:

           Master_TLS_Version:

1 row in set (0.00 sec)

至此,基于GTID的复制关系已经成功建立了。

 

以上是关于MySQL学习笔记07基于GTID的复制的主要内容,如果未能解决你的问题,请参考以下文章

mysql基于GTID的复制方式

MySQL 5.7基于GTID及多线程主从复制

Mysql 主从复制之半同步复制(基于gtid)

MySQL学习笔记16分组复制的几个常见问题以及解决办法

深入MySQL复制:基于GTID复制

mysql5.7.26 基于GTID的主从复制环境搭建