MySQL GTID 主从同步

概念

从MySQL5.6.5开始新增了一种基于GTID的复制方式。通过GTID保证了每个在主库上提交的事务在集群中有一个唯一的ID;

这种方式强化了数据库的主备一致性故障恢复以及容错能力

借助GTID,在发生主备切换的情况下,MySQL的其他从库可以自动在新主库上找到正确的复制位置,这大大简化了复杂复制拓扑下集群的维护,也减少了人为设置复制位置发生误操作的风险。

什么是GITD?

GTID(global transaction ID)是对于一个已提交事务的编号,并且是一个全局唯一的编号,GTID实际上是由UUID+TID组成的。其中UUID是一个MySQL实例的唯一标识,TID代表了该实例上已经提交的事务数量,并且随着事务提交单调递增。例子:03a1eb63-c21a-11ec-b07f-000c2987bea6:1-25,冒号分割前边为UUID,后边为TID。可以使用show master status实时查看当前事务执行数。

GTID的工作原理:

  1. 当一个事务在主库端执行并提交时,产生GTID,一同记录到binlog日志中。

  1. binlog传输到slave,并存储到salve的relaylog后,读取这个GTID的这个值设置GTID——next变量,即告诉slave,下一个要执行的GTID值。

  2. sql线程从relaylog中获取GTID,然后对比slave端的binlog是否有该GTID。

  3. 如果有记录说明该GTID的事务已经执行,slave会忽略。

  4. 如果没有记录,slave会执行该GTID事务,并记录到该GTID到自身的binlog,在读取该事务前会检查其他session持有该GTID,确保不被重复执行。

  5. 在解析过程中会判断是否有主键,如果没有就用二级索引,如果就用全部扫描

GTID比传统复制的优势:

更简单的实现failover,不需要找log_file和log_Pos;

更简单的搭建主从复制;

比传统复制更安全;

GTID是连续没有空洞的,因此主从库出现数据冲突时,可以用添加空事务的方式进行跳过。

配置一主一从GTID

数据库角色       ip             MySQL版本      有无数据
主数据库(home1)   192.168.10.11     MySQL5.7        无
从数据库(home2)   192.168.10.12     MySQL5.7        无

在主数据库中创建一个同步账户授权给从数据库使用

[root@home1 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.24 MySQL Community Server (GPL)
​
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
​
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
​
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
​
mysql> grant replication slave on *.* to 'weme'@'192.168.10.%' identified by '#自己设置密码';
Query OK, 0 rows affected, 1 warning (0.00 sec)
​
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
​
mysql> exit
Bye
​

配置主库文件

[root@home1 ~]# vim /etc/my.cnf
​
[mysqld]
port=3306
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
skip-name-resolve
symbolic-links=0
​
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
skip-name-resolve
​
log-bin=mysql-bin                        #开启二进制日志
server-id=11                                 #服务器ID,必须唯一
gtid-mode=on                              #开启gtid模式
enforce-gtid-consistency=on       #强制gtid一致性,开启后对特定的create table不支持
binlog-format=row                       #默认为mixed混合模式,更改成row复制,为了数据一致性
log-slave-updates=1                    #从库binlog记录主库同步的操作日志
skip-slave-start=1                         #跳过slave复制线程
[root@home1 ~]# systemctl restart mysqld
[root@home1 ~]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2022-11-24 21:16:03 EST; 8s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 990 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 973 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 994 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─994 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
​
Nov 24 21:16:03 home1 systemd[1]: Starting MySQL Server...
Nov 24 21:16:03 home1 systemd[1]: Started MySQL Server.

查看GTID模式状态

[root@home1 ~]# mysql -uroot -p#自己的密码 -e 'show variables like "%GTID%";'
mysql: [Warning] Using a password on the command line interface can be insecure.
+----------------------------------+-----------+
| Variable_name                    | Value     |
+----------------------------------+-----------+
| binlog_gtid_simple_recovery      | ON        |
| enforce_gtid_consistency         | ON        |
| gtid_executed_compression_period | 1000      |
| gtid_mode                        | ON        |
| gtid_next                        | AUTOMATIC |
| gtid_owned                       |           |
| gtid_purged                      |           |
| session_track_gtids              | OFF       |
+----------------------------------+-----------+
​

配置从库文件

[root@home2 ~]# vim /etc/my.cnf
​
[mysqld]
port=3306
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
​
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
skip-name-resolve
​
log-bin=mysql-bin
server-id=12
gtid-mode=on  
enforce-gtid-consistency=on          
binlog-format=row
log-slave-updates=1
skip-slave-start=1
[root@home2 ~]# systemctl restart mysqld
[root@home2 ~]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2022-11-24 21:23:34 EST; 8s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 929 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 912 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 933 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─933 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
​
Nov 24 21:23:33 home2 systemd[1]: Starting MySQL Server...
Nov 24 21:23:34 home2 systemd[1]: Started MySQL Server.
​

查看GTID模式状态

[root@home2 ~]# mysql -uroot -p#自己的密码 -e 'show variables like "%GTID%";'
mysql: [Warning] Using a password on the command line interface can be insecure.
+----------------------------------+-----------+
| Variable_name                    | Value     |
+----------------------------------+-----------+
| binlog_gtid_simple_recovery      | ON        |
| enforce_gtid_consistency         | ON        |
| gtid_executed_compression_period | 1000      |
| gtid_mode                        | ON        |
| gtid_next                        | AUTOMATIC |
| gtid_owned                       |           |
| gtid_purged                      |           |
| session_track_gtids              | OFF       |
+----------------------------------+-----------+
​

开启主从同步

[root@home2 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.24-log MySQL Community Server (GPL)
​
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
​
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
​
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
​
mysql> change master to master_host='192.168.10.11',master_user='weme',master_password='#主数据库的密码',master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.40 sec)
​
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
​
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.10.11
                  Master_User: weme
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: home2-relay-bin.000002
                Relay_Log_Pos: 367
        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: 154
              Relay_Log_Space: 574
              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: 048d3f7f-6af4-11ed-b7b2-000c292094f4
             Master_Info_File: /var/lib/mysql/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: 
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)
​

查看主从库的信息是否同步

查看主库

[root@home1 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.24-log MySQL Community Server (GPL)
​
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
​
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
​
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
​
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.31 sec)
​

查看从库

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (1.68 sec)
​
​

测试主从同步

在主库下执行一下SQL语句

mysql> CREATE DATABASE BOOK;
Query OK, 1 row affected (0.01 sec)
​
mysql> USE BOOK;
Database changed
mysql> CREATE TABLE book(name char(66),price int,pages int);
Query OK, 0 rows affected (0.01 sec)
​
mysql> INSERT INTO book(name,price,pages) VALUES('Linux','30','666');
Query OK, 1 row affected (0.37 sec)
​
mysql> INSERT INTO book(name,price,pages) VALUES('Cloud Computing','60','666');
Query OK, 1 row affected (0.01 sec)
​
mysql> INSERT INTO book(name,price,pages) VALUES('Operation System','80','666');
Query OK, 1 row affected (0.00 sec)
​
mysql> INSERT INTO book(name,price,pages) VALUES('Artificial Intelligence','166','666');
Query OK, 1 row affected (0.00 sec)
​
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| BOOK               |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)
​
mysql> select * from BOOK.book;
+-------------------------+-------+-------+
| name                    | price | pages |
+-------------------------+-------+-------+
| Linux                   |    30 |   666 |
| Cloud Computing         |    60 |   666 |
| Operation System        |    80 |   666 |
| Artificial Intelligence |   166 |   666 |
+-------------------------+-------+-------+
4 rows in set (0.00 sec)
​

在从库上查看数据是否同步过来

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| BOOK               |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)
​
​
mysql> select * from BOOK.book;
+-------------------------+-------+-------+
| name                    | price | pages |
+-------------------------+-------+-------+
| Linux                   |    30 |   666 |
| Cloud Computing         |    60 |   666 |
| Operation System        |    80 |   666 |
| Artificial Intelligence |   166 |   666 |
+-------------------------+-------+-------+
4 rows in set (0.00 sec)
​

查看主库状态

mysql> show master status;
+------------------+----------+--------------+------------------+------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                        |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000001 |     1619 |              |                  | 048d3f7f-6af4-11ed-b7b2-000c292094f4:1-6 |
+------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)
​

查看从库状态

mysql> show master status;
+------------------+----------+--------------+------------------+------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                        |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000001 |     1583 |              |                  | 048d3f7f-6af4-11ed-b7b2-000c292094f4:1-6 |
+------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)
​

从库停掉主从同步

mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)
​
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.10.11
                  Master_User: weme
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 1619
               Relay_Log_File: home2-relay-bin.000002
                Relay_Log_Pos: 1832
        Relay_Master_Log_File: mysql-bin.000001
             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: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 1619
              Relay_Log_Space: 2039
              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: 11
                  Master_UUID: 048d3f7f-6af4-11ed-b7b2-000c292094f4
             Master_Info_File: /var/lib/mysql/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: 048d3f7f-6af4-11ed-b7b2-000c292094f4:1-6
            Executed_Gtid_Set: 048d3f7f-6af4-11ed-b7b2-000c292094f4:1-6
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)
​

在主库创建数据后,从库开启主从同步查看是否同步

mysql> CREATE DATABASE `testbookdb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
Query OK, 1 row affected (0.00 sec)
​
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| BOOK               |
| mysql              |
| performance_schema |
| sys                |
| testbookdb         |
+--------------------+
6 rows in set (0.00 sec)
​
mysql> use testbookdb;
Database changed
mysql> CREATE TABLE book1 (bTypeId int,bName char(16),price int,publishing char(16));
Query OK, 0 rows affected (0.04 sec)
​
mysql> INSERT INTO book1(bTypeId,bName,price,publishing) VALUES('1','《Linux从入门到精通》','66',' 电子工业出版社');
Query OK, 1 row affected (0.00 sec)
​
mysql> INSERT INTO book1(bTypeId,bName,price,publishing) VALUES('2','《云计算趋势》','68','人民邮电出版社');
Query OK, 1 row affected (0.00 sec)
​
mysql> INSERT INTO book1(bTypeId,bName,price,publishing) VALUES('3','《操作系统设计与实现》','90','机械工业出版社');
Query OK, 1 row affected (0.00 sec)
​
mysql> INSERT INTO book1(bTypeId,bName,price,publishing) VALUES('4','《高性能MySQL》','71','清华大 学出版社');
Query OK, 1 row affected (0.00 sec)
​
mysql> INSERT INTO book1(bTypeId,bName,price,publishing) VALUES('5','《高性能MySQL2》','72','清华大学出版社');
Query OK, 1 row affected (0.00 sec)
​
mysql> INSERT INTO book1(bTypeId,bName,price,publishing) VALUES('6','《高性能MySQL3》','73','清华大学出版社');
Query OK, 1 row affected (0.00 sec)
​
mysql> INSERT INTO book1(bTypeId,bName,price,publishing) VALUES('7','《高性能MySQL4》','74','清华大学出版社');
Query OK, 1 row affected (0.00 sec)
​
mysql> INSERT INTO book1(bTypeId,bName,price,publishing) VALUES('8','《高性能MySQL5》','75','清华大学出版社');
Query OK, 1 row affected (0.00 sec)
​
mysql> INSERT INTO book1(bTypeId,bName,price,publishing) VALUES('9','《高性能MySQL6》','76','清华大学出版社');
Query OK, 1 row affected (0.00 sec)
​
mysql> INSERT INTO book1(bTypeId,bName,price,publishing) VALUES('10','《高性能MySQL7》','77','清华 大学出版社');
Query OK, 1 row affected (0.00 sec)
​
mysql> INSERT INTO book1(bTypeId,bName,price,publishing) VALUES('11','《高性能MySQL8》','78','清华 大学出版社');
Query OK, 1 row affected (0.00 sec)
​
mysql> INSERT INTO book1(bTypeId,bName,price,publishing) VALUES('12','《高性能MySQL9》','79','清华 大学出版社');
Query OK, 1 row affected (0.00 sec)
​
mysql> CREATE TABLE book2 (name char(16),price int,pages int);
Query OK, 0 rows affected (0.01 sec)
​
mysql> INSERT INTO book2(name,price,pages) VALUES('《Linux从入门到精通》','30','666');
Query OK, 1 row affected (0.00 sec)
​
mysql> INSERT INTO book2(name,price,pages) VALUES('《云计算趋势》','60','666');
Query OK, 1 row affected (0.00 sec)
​
mysql> INSERT INTO book2(name,price,pages) VALUES('《操作系统设计与实现》','80','666');
Query OK, 1 row affected (0.01 sec)
​
mysql> INSERT INTO book2(name,price,pages) VALUES('《高性能MySQL》','166','666');
Query OK, 1 row affected (0.01 sec)
​
mysql> show tables;
+----------------------+
| Tables_in_testbookdb |
+----------------------+
| book1                |
| book2                |
+----------------------+
2 rows in set (0.00 sec)
​
mysql> select * from book1;
+---------+-----------------------------------+-------+-----------------------+
| bTypeId | bName                             | price | publishing            |
+---------+-----------------------------------+-------+-----------------------+
|       1 | 《Linux从入门到精通》             |    66 | 电子工业出版社        |
|       2 | 《云计算趋势》                    |    68 | 人民邮电出版社        |
|       3 | 《操作系统设计与实现》            |    90 | 机械工业出版社        |
|       4 | 《高性能MySQL》                   |    71 | 清华大学出版社        |
|       5 | 《高性能MySQL2》                  |    72 | 清华大学出版社        |
|       6 | 《高性能MySQL3》                  |    73 | 清华大学出版社        |
|       7 | 《高性能MySQL4》                  |    74 | 清华大学出版社        |
|       8 | 《高性能MySQL5》                  |    75 | 清华大学出版社        |
|       9 | 《高性能MySQL6》                  |    76 | 清华大学出版社        |
|      10 | 《高性能MySQL7》                  |    77 | 清华大学出版社        |
|      11 | 《高性能MySQL8》                  |    78 | 清华大学出版社        |
|      12 | 《高性能MySQL9》                  |    79 | 清华大学出版社        |
+---------+-----------------------------------+-------+-----------------------+
12 rows in set (0.00 sec)
​
mysql> select * from book2;
+-----------------------------------+-------+-------+
| name                              | price | pages |
+-----------------------------------+-------+-------+
| 《Linux从入门到精通》             |    30 |   666 |
| 《云计算趋势》                    |    60 |   666 |
| 《操作系统设计与实现》            |    80 |   666 |
| 《高性能MySQL》                   |   166 |   666 |
+-----------------------------------+-------+-------+
4 rows in set (0.00 sec)
​

查看主库状态

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                         |
+------------------+----------+--------------+------------------+-------------------------------------------+
| mysql-bin.000001 |     7388 |              |                  | 048d3f7f-6af4-11ed-b7b2-000c292094f4:1-25 |
+------------------+----------+--------------+------------------+-------------------------------------------+
1 row in set (0.00 sec)
​

查看从库状态

mysql> show master status;
+------------------+----------+--------------+------------------+------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                        |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000001 |     1583 |              |                  | 048d3f7f-6af4-11ed-b7b2-000c292094f4:1-6 |
+------------------+----------+--------------+------------------+------------------------------------------+
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_IO_State: Waiting for master to send event
                  Master_Host: 192.168.10.11
                  Master_User: weme
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 7388
               Relay_Log_File: home2-relay-bin.000003
                Relay_Log_Pos: 6223
        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: 7388
              Relay_Log_Space: 8108
              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: 048d3f7f-6af4-11ed-b7b2-000c292094f4
             Master_Info_File: /var/lib/mysql/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: 048d3f7f-6af4-11ed-b7b2-000c292094f4:1-25
            Executed_Gtid_Set: 048d3f7f-6af4-11ed-b7b2-000c292094f4:1-25
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)
​
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| BOOK               |
| mysql              |
| performance_schema |
| sys                |
| testbookdb         |
+--------------------+
6 rows in set (0.01 sec)
​
mysql> use testbookdb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
​
Database changed
mysql> show tables;
+----------------------+
| Tables_in_testbookdb |
+----------------------+
| book1                |
| book2                |
+----------------------+
2 rows in set (0.00 sec)
​
mysql> select * from book1;
+---------+-----------------------------------+-------+-----------------------+
| bTypeId | bName                             | price | publishing            |
+---------+-----------------------------------+-------+-----------------------+
|       1 | 《Linux从入门到精通》             |    66 | 电子工业出版社        |
|       2 | 《云计算趋势》                    |    68 | 人民邮电出版社        |
|       3 | 《操作系统设计与实现》            |    90 | 机械工业出版社        |
|       4 | 《高性能MySQL》                   |    71 | 清华大学出版社        |
|       5 | 《高性能MySQL2》                  |    72 | 清华大学出版社        |
|       6 | 《高性能MySQL3》                  |    73 | 清华大学出版社        |
|       7 | 《高性能MySQL4》                  |    74 | 清华大学出版社        |
|       8 | 《高性能MySQL5》                  |    75 | 清华大学出版社        |
|       9 | 《高性能MySQL6》                  |    76 | 清华大学出版社        |
|      10 | 《高性能MySQL7》                  |    77 | 清华大学出版社        |
|      11 | 《高性能MySQL8》                  |    78 | 清华大学出版社        |
|      12 | 《高性能MySQL9》                  |    79 | 清华大学出版社        |
+---------+-----------------------------------+-------+-----------------------+
12 rows in set (0.00 sec)
​
mysql> select * from book2;
+-----------------------------------+-------+-------+
| name                              | price | pages |
+-----------------------------------+-------+-------+
| 《Linux从入门到精通》             |    30 |   666 |
| 《云计算趋势》                    |    60 |   666 |
| 《操作系统设计与实现》            |    80 |   666 |
| 《高性能MySQL》                   |   166 |   666 |
+-----------------------------------+-------+-------+
4 rows in set (0.00 sec)
​
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                         |
+------------------+----------+--------------+------------------+-------------------------------------------+
| mysql-bin.000001 |     7112 |              |                  | 048d3f7f-6af4-11ed-b7b2-000c292094f4:1-25 |
+------------------+----------+--------------+------------------+-------------------------------------------+
1 row in set (0.00 sec)
​

由此,MySQL GTID主从同步搭建完成。

你可能感兴趣的:(数据库,mysql,运维)