本文最后更新于 428 天前,其中的信息可能已经过时,如有错误请发送邮件到 wuxianglongblog@163.com
| GTID的英文全称为"Global Transaction ID",即一个全局唯一的编号。GTID主要保证主从复制架构中一致性的问题。生产环境中强烈建议开启GTID,最好是在初始化数据库时就直接开启GTID哟~ |
| |
| 对于DDL语句来讲,一条语句对应一个GTID,但对于一个DML语句而言,一个已提交的事物对应一个GTID。 |
| |
| GTID由MySQL 5.6版本新加的特性,在MySQL 5.7版本中有所加强,在MySQL 8.0版本中已经开始主推GTID。值得注意的是,在mysql 5.7中的GTID,即使不开启也会有自动生成。 |
| |
| 标准的GTID格式:"GTID = server_uuid:transaction_id",例如: "ecaf563f-5345-11eb-a106-000c29820c67:5"。 |
| mysql> SELECT @@server_uuid; |
| +--------------------------------------+ |
| | @@server_uuid | |
| +--------------------------------------+ |
| | ecaf563f-5345-11eb-a106-000c29820c67 | |
| +--------------------------------------+ |
| 1 row in set (0.00 sec) |
| |
| mysql> |
| |
| 如下所示,表示未开启GTID时,我们查看到"Event_type=Anonymous_Gtid",而"Info=SET @@SESSION.GTID_NEXT= 'ANONYMOUS'",这是由于咱们未开启GTID,由MySQL自动生成的GTID信息。 |
| mysql> SHOW BINLOG EVENTS IN 'oldboyedu-mysqld-binary.000001'; |
| +----------------------------------+-----+----------------+-----------+-------------+------------------------------------------------+ |
| | Log_name | Pos | Event_type | Server_id | End_log_pos | Info | |
| +----------------------------------+-----+----------------+-----------+-------------+------------------------------------------------+ |
| | oldboyedu-mysqld-binary.000001 | 4 | Format_desc | 7 | 123 | Server ver: 5.7.31-log, Binlog ver: 4 | |
| | oldboyedu-mysqld-binary.000001 | 123 | Previous_gtids | 7 | 154 | | |
| | oldboyedu-mysqld-binary.000001 | 154 | Anonymous_Gtid | 7 | 219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' | |
| | oldboyedu-mysqld-binary.000001 | 219 | Query | 7 | 343 | CREATE DATABASE zabbix DEFAULT CHARSET utf8mb4 | |
| +----------------------------------+-----+----------------+-----------+-------------+------------------------------------------------+ |
| 4 rows in set (0.00 sec) |
| |
| mysql> |
| |
| 如下所示,表示已开启GTID时,我们查看到"Event_type=Gtid",而"Info=SET @@SESSION.GTID_NEXT= 'ecaf563f-5345-11eb-a106-000c29820c67:2'" |
| mysql> SHOW BINLOG EVENTS IN 'oldboyedu-mysqld-binary.000004'; |
| +----------------------------------+------+----------------+-----------+-------------+-------------------------------------------------------------------+ |
| | Log_name | Pos | Event_type | Server_id | End_log_pos | Info | |
| +----------------------------------+------+----------------+-----------+-------------+-------------------------------------------------------------------+ |
| | oldboyedu-mysqld-binary.000004 | 4 | Format_desc | 7 | 123 | Server ver: 5.7.31-log, Binlog ver: 4 | |
| | oldboyedu-mysqld-binary.000004 | 123 | Previous_gtids | 7 | 154 | | |
| | oldboyedu-mysqld-binary.000004 | 154 | Gtid | 7 | 219 | SET @@SESSION.GTID_NEXT= 'ecaf563f-5345-11eb-a106-000c29820c67:1' | |
| | oldboyedu-mysqld-binary.000004 | 219 | Query | 7 | 357 | CREATE DATABASE IF NOT EXISTS hive DEFAULT CHARACTER SET utf8mb4 | |
| | oldboyedu-mysqld-binary.000004 | 357 | Gtid | 7 | 422 | SET @@SESSION.GTID_NEXT= 'ecaf563f-5345-11eb-a106-000c29820c67:2' | |
| | oldboyedu-mysqld-binary.000004 | 422 | Query | 7 | 557 | CREATE DATABASE IF NOT EXISTS cdh DEFAULT CHARACTER SET utf8mb4 | |
| | oldboyedu-mysqld-binary.000004 | 557 | Gtid | 7 | 622 | SET @@SESSION.GTID_NEXT= 'ecaf563f-5345-11eb-a106-000c29820c67:3' | |
| | oldboyedu-mysqld-binary.000004 | 622 | Query | 7 | 736 | CREATE TABLE IF NOT EXISTS hive.user(id int) | |
| | oldboyedu-mysqld-binary.000004 | 736 | Gtid | 7 | 801 | SET @@SESSION.GTID_NEXT= 'ecaf563f-5345-11eb-a106-000c29820c67:4' | |
| | oldboyedu-mysqld-binary.000004 | 801 | Query | 7 | 869 | BEGIN | |
| | oldboyedu-mysqld-binary.000004 | 869 | Table_map | 7 | 916 | table_id: 112 (hive.user) | |
| | oldboyedu-mysqld-binary.000004 | 916 | Write_rows | 7 | 966 | table_id: 112 flags: STMT_END_F | |
| | oldboyedu-mysqld-binary.000004 | 966 | Xid | 7 | 997 | COMMIT | |
| | oldboyedu-mysqld-binary.000004 | 997 | Gtid | 7 | 1062 | SET @@SESSION.GTID_NEXT= 'ecaf563f-5345-11eb-a106-000c29820c67:5' | |
| | oldboyedu-mysqld-binary.000004 | 1062 | Query | 7 | 1174 | CREATE TABLE IF NOT EXISTS cdh.user(id int) | |
| +----------------------------------+------+----------------+-----------+-------------+-------------------------------------------------------------------+ |
| 15 rows in set (0.00 sec) |
| |
| mysql> |
| GTID复制的作用: |
| 主要保证主从复制中的高级特性。 |
| |
| GTID在MySQL 5.6版本中引入的新特性,但默认并没有开启。 |
| |
| GTID在MySQL 5.7版本中即使不开启,也有匿名的GTID记录。 |
| |
| GTID的优势: |
| (1)为主库的dump线程传输可以提供并行的解决方案; |
| (2)为从库的SQL线程可以提供并发"回放"; |
| (3)配置主从时方便,无需手动定位主库二进制日志文件名称及位置信息,而是交由MySQL自动去定位; |
| |
| 温馨提示: |
| MySQL 5.7.17+的版本以后几乎都是GTID模式了。 |
| |
| 我们知道配置基于GTID主从复制相对简单,只需设置"MASTER_AUTO_POSITION=1"参数,而无需定位主库的文件名称和位置点。而是交由MySQL去自动识别复制的起始点。 |
| |
| 基于GTID的主从复制,MySQL自动判断复制起始点的流程如下所示: |
| (1)在主库和从库都是刚刚初始化的数据库而言,从库的IO线程会读取中继日志的最后一个GTID事件,我们此时可以认为GTID的事件为"0",表示从来没有复制过,那么从库的I/O线程就会去主库的第一个GTID事件开始复制; |
| (2)但在生产环境中,很有可能主库已经运行很长时间了,因此需要对主库进行备份,而后在从库进行恢复,这个时候从库就会去查"GTID_PURGED"变量记录的信息,会根据"GTID_PURGED"记录的GTID事件继续往后复制; |
| |
| 举个例子: |
| 假设从主库中的备份文件中有这样"SET @@GLOBAL.GTID_PURGED='ecaf563f-5345-11eb-a106-000c29820c67:1-100';"一行记录,那么我们基于GTID下次请求的事务就是从第101个GTID开始复制,因为当前数据库已经存在了1-100啦。 |
| 有关GTID启用参数说明: |
| gtid-mode: |
| 将其值设置为"on"则表示开启GTID功能。默认值为"OFF",表示未开启。 |
| |
| enforce-gtid-consistency: |
| 启用强制GITD的一致性。即在启动MySQL实例之前,会校验GTID一致性的问题。默认值为"OFF",表示未开启。 |
| |
| 如下所示,表示启用MySQL数据库实例的实操: |
| [root@docker201.oldboyedu.com ~]# mysql -S /tmp/mysql23307.sock |
| Welcome to the MySQL monitor. Commands end with ; or \g. |
| Your MySQL connection id is 2 |
| Server version: 5.7.31-log MySQL Community Server (GPL) |
| |
| Copyright (c) 2000, 2020, 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> SELECT @@gtid_mode; |
| + |
| | @@gtid_mode | |
| + |
| | OFF | |
| + |
| 1 row in set (0.00 sec) |
| |
| mysql> |
| mysql> SELECT @@enforce_gtid_consistency; |
| + |
| | @@enforce_gtid_consistency | |
| + |
| | OFF | |
| + |
| 1 row in set (0.00 sec) |
| |
| mysql> |
| mysql> QUIT |
| Bye |
| [root@docker201.oldboyedu.com ~]# |
| [root@docker201.oldboyedu.com ~]# vim /oldboyedu/softwares/mysql23307/my.cnf |
| [root@docker201.oldboyedu.com ~]# |
| [root@docker201.oldboyedu.com ~]# tail -2 /oldboyedu/softwares/mysql23307/my.cnf |
| gtid-mode=on |
| enforce-gtid-consistency=true |
| [root@docker201.oldboyedu.com ~]# |
| [root@docker201.oldboyedu.com ~]# systemctl restart mysqld23307 |
| [root@docker201.oldboyedu.com ~]# |
| [root@docker201.oldboyedu.com ~]# mysql -S /tmp/mysql23307.sock |
| Welcome to the MySQL monitor. Commands end with ; or \g. |
| Your MySQL connection id is 2 |
| Server version: 5.7.31-log MySQL Community Server (GPL) |
| |
| Copyright (c) 2000, 2020, 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> |
| mysql> SELECT @@gtid_mode; |
| + |
| | @@gtid_mode | |
| + |
| | ON | |
| + |
| 1 row in set (0.00 sec) |
| |
| mysql> |
| mysql> SELECT @@enforce_gtid_consistency; |
| + |
| | @@enforce_gtid_consistency | |
| + |
| | ON | |
| + |
| 1 row in set (0.00 sec) |
| |
| mysql> |
| mysql> SHOW MASTER STATUS; |
| + |
| | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | |
| + |
| | oldboyedu-mysqld-binary.000004 | 154 | | | | |
| + |
| 1 row in set (0.00 sec) |
| |
| mysql> |
| mysql> CREATE DATABASE IF NOT EXISTS hive DEFAULT CHARACTER SET utf8mb4; # 注意哈,我们下面分别执行了DDL,DML语句,注意观察"Executed_Gtid_Set"字段信息 |
| Query OK, 1 row affected (0.00 sec) |
| |
| mysql> |
| mysql> SHOW MASTER STATUS; |
| + |
| | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | |
| + |
| | oldboyedu-mysqld-binary.000004 | 357 | | | ecaf563f-5345-11eb-a106-000c29820c67:1 | |
| + |
| 1 row in set (0.00 sec) |
| |
| mysql> |
| mysql> CREATE DATABASE IF NOT EXISTS cdh DEFAULT CHARACTER SET utf8mb4; |
| Query OK, 1 row affected (0.00 sec) |
| |
| mysql> |
| mysql> SHOW MASTER STATUS; |
| + |
| | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | |
| + |
| | oldboyedu-mysqld-binary.000004 | 557 | | | ecaf563f-5345-11eb-a106-000c29820c67:1-2 | |
| + |
| 1 row in set (0.01 sec) |
| |
| mysql> |
| mysql> CREATE TABLE IF NOT EXISTS hive.user(id int); |
| Query OK, 0 rows affected (0.05 sec) |
| |
| mysql> |
| mysql> SHOW MASTER STATUS; |
| + |
| | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | |
| + |
| | oldboyedu-mysqld-binary.000004 | 736 | | | ecaf563f-5345-11eb-a106-000c29820c67:1-3 | |
| + |
| 1 row in set (0.00 sec) |
| |
| mysql> |
| mysql> INSERT INTO hive.user VALUES(100),(200),(300); |
| Query OK, 3 rows affected (0.00 sec) |
| Records: 3 Duplicates: 0 Warnings: 0 |
| |
| mysql> |
| mysql> SHOW MASTER STATUS; |
| + |
| | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | |
| + |
| | oldboyedu-mysqld-binary.000004 | 997 | | | ecaf563f-5345-11eb-a106-000c29820c67:1-4 | |
| + |
| 1 row in set (0.00 sec) |
| |
| mysql> |
| mysql> CREATE TABLE IF NOT EXISTS cdh.user(id int); |
| Query OK, 0 rows affected (0.01 sec) |
| |
| mysql> |
| mysql> SHOW MASTER STATUS; |
| + |
| | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | |
| + |
| | oldboyedu-mysqld-binary.000004 | 1174 | | | ecaf563f-5345-11eb-a106-000c29820c67:1-5 | |
| + |
| 1 row in set (0.00 sec) |
| |
| mysql> |
| mysql> SHOW BINLOG EVENTS IN 'oldboyedu-mysqld-binary.000004'; # 还记得"SET @@SESSION.GTID_NEXT= 'ANONYMOUS'"这样的日志信息吗?这是由于咱们未开启GTID,由MySQL自动生成的GTID信息,开启后就会输出如下的信息。 |
| + |
| | Log_name | Pos | Event_type | Server_id | End_log_pos | Info | |
| + |
| | oldboyedu-mysqld-binary.000004 | 4 | Format_desc | 7 | 123 | Server ver: 5.7.31-log, Binlog ver: 4 | |
| | oldboyedu-mysqld-binary.000004 | 123 | Previous_gtids | 7 | 154 | | |
| | oldboyedu-mysqld-binary.000004 | 154 | Gtid | 7 | 219 | SET @@SESSION.GTID_NEXT= 'ecaf563f-5345-11eb-a106-000c29820c67:1' | |
| | oldboyedu-mysqld-binary.000004 | 219 | Query | 7 | 357 | CREATE DATABASE IF NOT EXISTS hive DEFAULT CHARACTER SET utf8mb4 | |
| | oldboyedu-mysqld-binary.000004 | 357 | Gtid | 7 | 422 | SET @@SESSION.GTID_NEXT= 'ecaf563f-5345-11eb-a106-000c29820c67:2' | |
| | oldboyedu-mysqld-binary.000004 | 422 | Query | 7 | 557 | CREATE DATABASE IF NOT EXISTS cdh DEFAULT CHARACTER SET utf8mb4 | |
| | oldboyedu-mysqld-binary.000004 | 557 | Gtid | 7 | 622 | SET @@SESSION.GTID_NEXT= 'ecaf563f-5345-11eb-a106-000c29820c67:3' | |
| | oldboyedu-mysqld-binary.000004 | 622 | Query | 7 | 736 | CREATE TABLE IF NOT EXISTS hive.user(id int) | |
| | oldboyedu-mysqld-binary.000004 | 736 | Gtid | 7 | 801 | SET @@SESSION.GTID_NEXT= 'ecaf563f-5345-11eb-a106-000c29820c67:4' | |
| | oldboyedu-mysqld-binary.000004 | 801 | Query | 7 | 869 | BEGIN | |
| | oldboyedu-mysqld-binary.000004 | 869 | Table_map | 7 | 916 | table_id: 112 (hive.user) | |
| | oldboyedu-mysqld-binary.000004 | 916 | Write_rows | 7 | 966 | table_id: 112 flags: STMT_END_F | |
| | oldboyedu-mysqld-binary.000004 | 966 | Xid | 7 | 997 | COMMIT /* xid=16 */ | |
| | oldboyedu-mysqld-binary.000004 | 997 | Gtid | 7 | 1062 | SET @@SESSION.GTID_NEXT= 'ecaf563f-5345-11eb-a106-000c29820c67:5' | |
| | oldboyedu-mysqld-binary.000004 | 1062 | Query | 7 | 1174 | CREATE TABLE IF NOT EXISTS cdh.user(id int) | |
| + |
| 15 rows in set (0.00 sec) |
| |
| mysql> |
| 三台虚拟机安装MySQL 5.7的发行版本,关闭防火墙即可。 |
| |
| 温馨提示: |
| 其实使用多实例配置GTID也可以模拟实现效果,但为了后面的MHA做准备,建议大家还是可以直接使用虚拟机来做实验。 |
| |
| 实验环境: |
| mysql201.oldboyedu.com: |
| 172.200.1.201(主库) |
| mysql202.oldboyedu.com: |
| 172.200.1.202(从库) |
| mysql203.oldboyedu.com: |
| 172.200.1.203(从库) |
| mysql201.oldboyedu.com主库生成配置文件: |
| [root@mysql201.oldboyedu.com ~] |
| > [mysql] |
| > prompt=mysql201 [\d]> |
| > socket=/tmp/mysql.sock |
| > |
| > [mysqld] |
| > server_id=201 |
| > bind-address=172.200.1.201 |
| > port=3306 |
| > basedir=/oldboyedu/softwares/mysql/mysql |
| > datadir=/oldboyedu/data/mysql |
| > log_bin=/oldboyedu/logs/mysql/binlog/mysql-bin |
| > socket=/tmp/mysql.sock |
| > secure-file-priv=/tmp |
| > autocommit=0 |
| > binlog_format=row |
| > |
| > gtid-mode=on |
| > enforce-gtid-consistency=true |
| > log-slave-updates=1 |
| > EOF |
| [root@mysql201.oldboyedu.com ~] |
| [root@mysql201.oldboyedu.com ~] |
| [mysql] |
| prompt=mysql201 [\d]> |
| socket=/tmp/mysql.sock |
| |
| [mysqld] |
| server_id=201 |
| bind-address=172.200.1.201 |
| port=3306 |
| basedir=/oldboyedu/softwares/mysql/mysql |
| datadir=/oldboyedu/data/mysql |
| log_bin=/oldboyedu/logs/mysql/binlog/mysql-bin |
| socket=/tmp/mysql.sock |
| secure-file-priv=/tmp |
| autocommit=0 |
| binlog_format=row |
| |
| gtid-mode=on |
| enforce-gtid-consistency=true |
| log-slave-updates=1 |
| [root@mysql201.oldboyedu.com ~] |
| [root@mysql201.oldboyedu.com ~] |
| State Recv-Q Send-Q Local Address:Port Peer Address:Port |
| LISTEN 0 128 *:22 *:* |
| LISTEN 0 80 [::]:3306 [::]:* |
| LISTEN 0 128 [::]:22 [::]:* |
| [root@mysql201.oldboyedu.com ~] |
| [root@mysql201.oldboyedu.com ~] |
| [root@mysql201.oldboyedu.com ~] |
| [root@mysql201.oldboyedu.com ~] |
| [root@mysql201.oldboyedu.com ~] |
| [root@mysql201.oldboyedu.com ~] |
| State Recv-Q Send-Q Local Address:Port Peer Address:Port |
| LISTEN 0 80 172.200.1.201:3306 *:* |
| LISTEN 0 128 *:22 *:* |
| LISTEN 0 128 [::]:22 [::]:* |
| [root@mysql201.oldboyedu.com ~] |
| |
| mysql202.oldboyedu.com从库生成配置文件: |
| [root@mysql202.oldboyedu.com ~] |
| > [mysql] |
| > prompt=mysql202 [\d]> |
| > socket=/tmp/mysql.sock |
| > |
| > [mysqld] |
| > server_id=202 |
| > bind-address=172.200.1.202 |
| > port=3306 |
| > basedir=/oldboyedu/softwares/mysql/mysql |
| > datadir=/oldboyedu/data/mysql |
| > log_bin=/oldboyedu/logs/mysql/binlog/mysql-bin |
| > socket=/tmp/mysql.sock |
| > secure-file-priv=/tmp |
| > autocommit=0 |
| > binlog_format=row |
| > |
| > gtid-mode=on |
| > enforce-gtid-consistency=true |
| > log-slave-updates=1 |
| > EOF |
| [root@mysql202.oldboyedu.com ~] |
| [root@mysql202.oldboyedu.com ~] |
| [mysql] |
| prompt=mysql202 [\d]> |
| socket=/tmp/mysql.sock |
| |
| [mysqld] |
| server_id=202 |
| bind-address=172.200.1.202 |
| port=3306 |
| basedir=/oldboyedu/softwares/mysql/mysql |
| datadir=/oldboyedu/data/mysql |
| log_bin=/oldboyedu/logs/mysql/binlog/mysql-bin |
| socket=/tmp/mysql.sock |
| secure-file-priv=/tmp |
| autocommit=0 |
| binlog_format=row |
| |
| gtid-mode=on |
| enforce-gtid-consistency=true |
| log-slave-updates=1 |
| [root@mysql202.oldboyedu.com ~] |
| [root@mysql202.oldboyedu.com ~] |
| [root@mysql202.oldboyedu.com ~] |
| [root@mysql202.oldboyedu.com ~] |
| [root@mysql202.oldboyedu.com ~] |
| [root@mysql202.oldboyedu.com ~] |
| State Recv-Q Send-Q Local Address:Port Peer Address:Port |
| LISTEN 0 80 172.200.1.202:3306 *:* |
| LISTEN 0 128 *:22 *:* |
| LISTEN 0 128 [::]:22 [::]:* |
| [root@mysql202.oldboyedu.com ~] |
| |
| mysql203.oldboyedu.com从库生成配置文件: |
| [root@mysql203.oldboyedu.com ~] |
| > [mysql] |
| > prompt=mysql203 [\d]> |
| > socket=/tmp/mysql.sock |
| > |
| > [mysqld] |
| > server_id=203 |
| > bind-address=172.200.1.203 |
| > port=3306 |
| > basedir=/oldboyedu/softwares/mysql/mysql |
| > datadir=/oldboyedu/data/mysql |
| > log_bin=/oldboyedu/logs/mysql/binlog/mysql-bin |
| > socket=/tmp/mysql.sock |
| > secure-file-priv=/tmp |
| > autocommit=0 |
| > binlog_format=row |
| > |
| > gtid-mode=on |
| > enforce-gtid-consistency=true |
| > log-slave-updates=1 |
| > EOF |
| [root@mysql203.oldboyedu.com ~] |
| [root@mysql203.oldboyedu.com ~] |
| [mysql] |
| prompt=mysql203 [\d]> |
| socket=/tmp/mysql.sock |
| |
| [mysqld] |
| server_id=203 |
| bind-address=172.200.1.203 |
| port=3306 |
| basedir=/oldboyedu/softwares/mysql/mysql |
| datadir=/oldboyedu/data/mysql |
| log_bin=/oldboyedu/logs/mysql/binlog/mysql-bin |
| socket=/tmp/mysql.sock |
| secure-file-priv=/tmp |
| autocommit=0 |
| binlog_format=row |
| |
| gtid-mode=on |
| enforce-gtid-consistency=true |
| log-slave-updates=1 |
| [root@mysql203.oldboyedu.com ~] |
| [root@mysql203.oldboyedu.com ~] |
| State Recv-Q Send-Q Local Address:Port Peer Address:Port |
| LISTEN 0 80 172.200.1.203:3306 *:* |
| LISTEN 0 128 *:22 *:* |
| LISTEN 0 128 [::]:22 [::]:* |
| [root@mysql203.oldboyedu.com ~] |
| |
| 有关GTID启用参数说明: |
| gtid-mode: |
| 将其值设置为"on"则表示开启GTID功能。默认值为"OFF",表示未开启。 |
| |
| enforce-gtid-consistency: |
| 启用强制GITD的一致性。即在启动MySQL实例之前,会校验GTID一致性的问题。默认值为"OFF",表示未开启。 |
| |
| log-slave-updates: |
| 强制从库记录二进制日志的一致性。换句话说,就是要求从库的GTID和主库的GTID要保持一致。 |
| |
| 温馨提示: |
| 在上面配置时"server_id","bind-address"并不相同哟,log_bin指定的上级目录必须存在且运行MySQL实例的用户必须有权限访问! |
| (1)清理环境,避免以前的一些数据干扰(比如已经存在的文件,用户权限等),我们准备用一个全新的数据库来实验(这样做有个好处就是可以省略使用mysqlbinlog工具进行备份恢复,因为3个数据库初始数据相同)。 |
| [root@mysql201.oldboyedu.com ~] |
| [root@mysql201.oldboyedu.com ~] |
| [root@mysql201.oldboyedu.com ~] |
| [root@mysql201.oldboyedu.com ~] |
| [root@mysql201.oldboyedu.com ~] |
| [root@mysql201.oldboyedu.com ~] |
| [root@mysql201.oldboyedu.com ~] |
| 总用量 0 |
| [root@mysql201.oldboyedu.com ~] |
| [root@mysql201.oldboyedu.com ~] |
| 总用量 0 |
| [root@mysql201.oldboyedu.com ~] |
| |
| (2)初始化数据库 |
| [root@mysql201.oldboyedu.com ~] |
| 2021-02-17T01:44:38.696093Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use |
| 2021-02-17T01:44:38.899253Z 0 [Warning] InnoDB: New log files created, LSN=45790 |
| 2021-02-17T01:44:38.931787Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. |
| 2021-02-17T01:44:39.277253Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: b2a02b77-70c1-11e |
| b-a390-000c29e297a4.2021-02-17T01:44:39.279812Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. |
| 2021-02-17T01:44:39.968279Z 0 [Warning] CA certificate ca.pem is self signed. |
| 2021-02-17T01:44:40.163229Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the |
| [root@mysql201.oldboyedu.com ~] |
| [root@mysql201.oldboyedu.com ~] |
| 总用量 110660 |
| -rw-r |
| -rw |
| -rw-r |
| -rw-r |
| -rw |
| -rw-r |
| -rw-r |
| -rw-r |
| -rw-r |
| drwxr-x |
| drwxr-x |
| -rw |
| -rw-r |
| -rw-r |
| -rw |
| drwxr-x |
| [root@mysql201.oldboyedu.com ~] |
| |
| (3)启动数据库 |
| 关于启动脚本的操作,我这里不做赘述,因为我是基于早期部署的MySQL环境使用的,详情可参考第一章节中"MySQL二进制安装"笔记。 |
| |
| [root@mysql201.oldboyedu.com ~] |
| 总用量 24 |
| -rw-r--r-- 1 mysql mysql 773 6月 2 2020 magic |
| -rwxr-xr-x 1 mysql mysql 1061 6月 2 2020 mysqld_multi.server |
| -rwxr-xr-x 1 mysql mysql 894 6月 2 2020 mysql-log-rotate |
| -rwxr-xr-x 1 mysql mysql 10576 6月 2 2020 mysql.server |
| [root@mysql201.oldboyedu.com ~] |
| [root@mysql201.oldboyedu.com ~] |
| [root@mysql201.oldboyedu.com ~] |
| [root@mysql201.oldboyedu.com ~] |
| [root@mysql201.oldboyedu.com ~] |
| |
| 在MySQL的脚本文件中指定MySQL数据的自定义安装路径: |
| [root@mysql201.oldboyedu.com ~] |
| [root@mysql201.oldboyedu.com ~] |
| [root@mysql201.oldboyedu.com ~] |
| datadir= |
| [root@mysql201.oldboyedu.com ~] |
| [root@mysql201.oldboyedu.com ~] |
| [root@mysql201.oldboyedu.com ~] |
| [root@mysql201.oldboyedu.com ~] |
| datadir=/oldboyedu/data/mysql |
| [root@mysql201.oldboyedu.com ~] |
| |
| 在MySQL的脚本文件中指定MySQL程序的自定义安装路径: |
| [root@mysql201.oldboyedu.com ~] |
| basedir= |
| [root@mysql201.oldboyedu.com ~] |
| [root@mysql201.oldboyedu.com ~] |
| [root@mysql201.oldboyedu.com ~] |
| [root@mysql201.oldboyedu.com ~] |
| basedir=/oldboyedu/softwares/mysql/mysql |
| [root@mysql201.oldboyedu.com ~] |
| |
| 重新加载启动脚本: |
| [root@mysql201.oldboyedu.com ~] |
| [root@mysql201.oldboyedu.com ~] |
| [root@mysql201.oldboyedu.com ~] |
| [root@mysql201.oldboyedu.com ~] |
| [root@mysql202.oldboyedu.com ~] |
| State Recv-Q Send-Q Local Address:Port Peer Address:Port |
| LISTEN 0 80 172.200.1.202:3306 *:* |
| LISTEN 0 128 *:22 *:* |
| LISTEN 0 128 [::]:22 [::]:* |
| [root@mysql202.oldboyedu.com ~] |
| [root@mysql201.oldboyedu.com ~]# ss -ntl |
| State Recv-Q Send-Q Local Address:Port Peer Address:Port |
| LISTEN 0 128 *:22 *:* |
| LISTEN 0 128 [::]:22 [::]:* |
| [root@mysql201.oldboyedu.com ~]# |
| [root@mysql201.oldboyedu.com ~]# systemctl start mysqld |
| [root@mysql201.oldboyedu.com ~]# |
| [root@mysql201.oldboyedu.com ~]# ss -ntl |
| State Recv-Q Send-Q Local Address:Port Peer Address:Port |
| LISTEN 0 80 172.200.1.201:3306 *:* |
| LISTEN 0 128 *:22 *:* |
| LISTEN 0 128 [::]:22 [::]:* |
| [root@mysql201.oldboyedu.com ~]# |
| [root@mysql201.oldboyedu.com ~]# systemctl enable mysqld |
| mysqld.service is not a native service, redirecting to /sbin/chkconfig. |
| Executing /sbin/chkconfig mysqld on |
| [root@mysql201.oldboyedu.com ~]# |
| 主库创建用于复制的用户: |
| [root@mysql201.oldboyedu.com ~] |
| Welcome to the MySQL monitor. Commands end with ; or \g. |
| Your MySQL connection id is 4 |
| Server version: 5.7.31-log MySQL Community Server (GPL) |
| |
| Copyright (c) 2000, 2020, 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. |
| |
| mysql201 [(none)]> |
| mysql201 [(none)]>SELECT user,host FROM mysql.user; |
| +---------------+-------------+ |
| | user | host | |
| +---------------+-------------+ |
| | mysql.session | localhost | |
| | mysql.sys | localhost | |
| | root | localhost | |
| +---------------+-------------+ |
| 3 rows in set (0.00 sec) |
| |
| mysql201 [(none)]> |
| mysql201 [(none)]>CREATE USER copy@'172.200.1.%' IDENTIFIED BY 'oldboyedu'; |
| Query OK, 0 rows affected (0.00 sec) |
| |
| mysql201 [(none)]> |
| mysql201 [(none)]>SELECT user,host FROM mysql.user; |
| +---------------+-------------+ |
| | user | host | |
| +---------------+-------------+ |
| | copy | 172.200.1.% | |
| | mysql.session | localhost | |
| | mysql.sys | localhost | |
| | root | localhost | |
| +---------------+-------------+ |
| 4 rows in set (0.00 sec) |
| |
| mysql201 [(none)]> |
| mysql201 [(none)]>SHOW GRANTS FOR copy@'172.200.1.%'; |
| +--------------------------------------------+ |
| | Grants for copy@172.200.1.% | |
| +--------------------------------------------+ |
| | GRANT USAGE ON *.* TO 'copy'@'172.200.1.%' | |
| +--------------------------------------------+ |
| 1 row in set (0.00 sec) |
| |
| mysql201 [(none)]> |
| mysql201 [(none)]>GRANT REPLICATION SLAVE ON *.* TO copy@'172.200.1.%'; |
| Query OK, 0 rows affected (0.01 sec) |
| |
| mysql201 [(none)]> |
| mysql201 [(none)]>SHOW GRANTS FOR copy@'172.200.1.%'; |
| +--------------------------------------------------------+ |
| | Grants for copy@172.200.1.% | |
| +--------------------------------------------------------+ |
| | GRANT REPLICATION SLAVE ON *.* TO 'copy'@'172.200.1.%' | |
| +--------------------------------------------------------+ |
| 1 row in set (0.00 sec) |
| |
| mysql201 [(none)]> |
| mysql201 [(none)]>QUIT |
| Bye |
| [root@mysql201.oldboyedu.com ~] |
| |
| 从库验证用户是否可以正常登录:(一定要验证一下,如果从库登录不了,说明您配置的用户权限有问题,请及时排查,否则在从库使用该用户复制时会报错哟~) |
| [root@mysql202.oldboyedu.com ~] |
| mysql: [Warning] Using a password on the command line interface can be insecure. |
| Welcome to the MySQL monitor. Commands end with ; or \g. |
| Your MySQL connection id is 6 |
| Server version: 5.7.31-log MySQL Community Server (GPL) |
| |
| Copyright (c) 2000, 2020, 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. |
| |
| mysql202 [(none)]> |
| mysql202 [(none)]>SHOW DATABASES; |
| +--------------------+ |
| | Database | |
| +--------------------+ |
| | information_schema | |
| +--------------------+ |
| 1 row in set (0.00 sec) |
| |
| mysql202 [(none)]> |
| mysql202 [(none)]>QUIT |
| Bye |
| [root@mysql202.oldboyedu.com ~] |
| (1)主库使用mysqlbinlog工具进行备份,而后在两个从库进行恢复 |
| 该步骤直接省略,原因很简单,因为我们三个数据库都重新格式化的数据库,因此3个数据库目前都没有新数据生成。但生产环境中一定不要忽略该步骤哟~ |
| |
| (2)mysql202.oldboyedu.com从库开启主从复制 |
| [root@mysql202.oldboyedu.com ~]# mysql |
| Welcome to the MySQL monitor. Commands end with ; or \g. |
| Your MySQL connection id is 3 |
| Server version: 5.7.31-log MySQL Community Server (GPL) |
| |
| Copyright (c) 2000, 2020, 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. |
| |
| mysql202 [(none)]> |
| mysql202 [(none)]>CHANGE MASTER TO |
| -> MASTER_HOST='172.200.1.201', |
| -> MASTER_USER='copy', |
| -> MASTER_PASSWORD='oldboyedu', |
| -> MASTER_PORT=3306, # 该参数可以省略,因为默认就是3306端口,生产环境中如果修改了3306端口的话,就得显式使用该参数了哟~ |
| -> MASTER_AUTO_POSITION=1; # 让从库自动判断二进制位置的起点,无需咱们人为手动指定主库的二进制文件名称及位置啦~ |
| Query OK, 0 rows affected, 2 warnings (0.30 sec) |
| |
| mysql202 [(none)]> |
| mysql202 [(none)]>START SLAVE; |
| Query OK, 0 rows affected (0.00 sec) |
| |
| mysql202 [(none)]> |
| mysql202 [(none)]>SHOW SLAVE STATUS\G |
| *************************** 1. row *************************** |
| Slave_IO_State: Waiting for master to send event |
| Master_Host: 172.200.1.201 |
| Master_User: copy |
| Master_Port: 3306 |
| Connect_Retry: 60 |
| Master_Log_File: mysql-bin.000003 |
| Read_Master_Log_Pos: 615 |
| Relay_Log_File: mysql202-relay-bin.000002 |
| Relay_Log_Pos: 828 |
| 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: |
| Last_Errno: 0 |
| Last_Error: |
| Skip_Counter: 0 |
| Exec_Master_Log_Pos: 615 |
| Relay_Log_Space: 1038 |
| 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: 201 |
| Master_UUID: b2a02b77-70c1-11eb-a390-000c29e297a4 |
| Master_Info_File: /oldboyedu/data/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: b2a02b77-70c1-11eb-a390-000c29e297a4:1-2 |
| Executed_Gtid_Set: b2a02b77-70c1-11eb-a390-000c29e297a4:1-2 |
| Auto_Position: 1 |
| Replicate_Rewrite_DB: |
| Channel_Name: |
| Master_TLS_Version: |
| 1 row in set (0.00 sec) |
| |
| mysql202 [(none)]> |
| |
| (3)mysql203.oldboyedu.com从库开启主从复制 |
| [root@mysql203.oldboyedu.com ~]# mysql -uroot -poldboyedu |
| mysql: [Warning] Using a password on the command line interface can be insecure. |
| Welcome to the MySQL monitor. Commands end with ; or \g. |
| Your MySQL connection id is 2 |
| Server version: 5.7.31-log MySQL Community Server (GPL) |
| |
| Copyright (c) 2000, 2020, 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. |
| |
| mysql203 [(none)]>USE mysql |
| 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 |
| mysql203 [mysql]> |
| mysql203 [mysql]>CHANGE MASTER TO |
| -> MASTER_HOST='172.200.1.201', |
| -> MASTER_USER='copy', |
| -> MASTER_PASSWORD='oldboyedu', |
| -> MASTER_PORT=3306, |
| -> MASTER_AUTO_POSITION=1; |
| Query OK, 0 rows affected, 2 warnings (0.02 sec) |
| |
| mysql203 [mysql]> |
| mysql203 [mysql]>START SLAVE; |
| Query OK, 0 rows affected (0.00 sec) |
| |
| mysql203 [mysql]> |
| mysql203 [mysql]>SHOW SLAVE STATUS\G |
| *************************** 1. row *************************** |
| Slave_IO_State: Waiting for master to send event |
| Master_Host: 172.200.1.201 |
| Master_User: copy |
| Master_Port: 3306 |
| Connect_Retry: 60 |
| Master_Log_File: mysql-bin.000003 |
| Read_Master_Log_Pos: 615 |
| Relay_Log_File: mysql203-relay-bin.000002 |
| Relay_Log_Pos: 828 |
| 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: |
| Last_Errno: 0 |
| Last_Error: |
| Skip_Counter: 0 |
| Exec_Master_Log_Pos: 615 |
| Relay_Log_Space: 1038 |
| 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: 201 |
| Master_UUID: b2a02b77-70c1-11eb-a390-000c29e297a4 |
| Master_Info_File: /oldboyedu/data/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: b2a02b77-70c1-11eb-a390-000c29e297a4:1-2 |
| Executed_Gtid_Set: b2a02b77-70c1-11eb-a390-000c29e297a4:1-2 |
| Auto_Position: 1 |
| Replicate_Rewrite_DB: |
| Channel_Name: |
| Master_TLS_Version: |
| 1 row in set (0.00 sec) |
| |
| mysql203 [mysql]> |
| |
| 温馨提示: |
| (1)基于GTID的主从复制,我们就不用太过于关心主库的二进制日志,从库的中继日志的文件名称和位置点了,而是应该关心"Retrieved_Gtid_Set"和"Executed_Gtid_Set"这两个参数; |
| (2)有关"Retrieved_Gtid_Set"和"Executed_Gtid_Set"这两个参数说明如下: |
| Retrieved_Gtid_Set: |
| 表示从库已经接收的GTID编号。 |
| Executed_Gtid_Set: |
| 表示从库已经执行的GITD编号。 |
| mysql201.oldboyedu.com主库查看GTID执行的位置点: |
| [root@mysql201.oldboyedu.com ~] |
| Welcome to the MySQL monitor. Commands end with ; or \g. |
| Your MySQL connection id is 12 |
| Server version: 5.7.31-log MySQL Community Server (GPL) |
| |
| Copyright (c) 2000, 2020, 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. |
| |
| mysql201 [(none)]> |
| mysql201 [(none)]>SHOW MASTER STATUS; |
| +------------------+----------+--------------+------------------+------------------------------------------+ |
| | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | |
| +------------------+----------+--------------+------------------+------------------------------------------+ |
| | mysql-bin.000003 | 615 | | | b2a02b77-70c1-11eb-a390-000c29e297a4:1-2 | |
| +------------------+----------+--------------+------------------+------------------------------------------+ |
| 1 row in set (0.00 sec) |
| |
| mysql201 [(none)]> |
| mysql201 [(none)]>QUIT |
| Bye |
| [root@mysql201.oldboyedu.com ~] |
| |
| mysql202.oldboyedu.com从库查看执行的位置点: |
| [root@mysql202.oldboyedu.com ~] |
| Retrieved_Gtid_Set: b2a02b77-70c1-11eb-a390-000c29e297a4:1-2 |
| Executed_Gtid_Set: b2a02b77-70c1-11eb-a390-000c29e297a4:1-2 |
| [root@mysql202.oldboyedu.com ~] |
| |
| mysql203.oldboyedu.com从库查看执行的位置点: |
| [root@mysql203.oldboyedu.com ~] |
| mysql: [Warning] Using a password on the command line interface can be insecure. |
| Retrieved_Gtid_Set: b2a02b77-70c1-11eb-a390-000c29e297a4:1-2 |
| Executed_Gtid_Set: b2a02b77-70c1-11eb-a390-000c29e297a4:1-2 |
| [root@mysql203.oldboyedu.com ~] |
| |
| 综上所述,我们很容易发现基于GTID的主从复制更容易判断从库是否有延迟。直接对比主从的"Executed_Gtid_Set"是否一致即可! |
| 我们先看下面命令的输出结果: |
| [root@mysql201.oldboyedu.com ~] |
| Warning: A partial dump from a server that has GTIDs will by default include the GTIDs of all transactions, even those that changed suppressed parts of the database. If you don't want to re |
| store GTIDs, pass |
| [root@mysql201.oldboyedu.com ~] |
| [root@mysql201.oldboyedu.com ~] |
| SET @@GLOBAL.GTID_PURGED='b2a02b77-70c1-11eb-a390-000c29e297a4:1-2'; |
| [root@mysql201.oldboyedu.com ~] |
| [root@mysql201.oldboyedu.com ~] |
| [root@mysql201.oldboyedu.com ~] |
| [root@mysql201.oldboyedu.com ~] |
| [root@mysql201.oldboyedu.com ~] |
| [root@mysql201.oldboyedu.com ~] |
| [root@mysql201.oldboyedu.com ~] |
| |
| 综上所述,我们不难发现,如果手动添加了" |
| |
| 如果不设置" |
| 经过下面四个步骤的操作都很简单,其目的是生成多个二进制文件,而且每个二进制文件都有有效的DDL和DML语句。 |
| |
| 第一次执行DDL及DML语句,并滚动二进制日志: |
| mysql> SHOW MASTER STATUS; |
| +----------------------------------+----------+--------------+------------------+------------------------------------------+ |
| | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | |
| +----------------------------------+----------+--------------+------------------+------------------------------------------+ |
| | oldboyedu-mysqld-binary.000004 | 1174 | | | ecaf563f-5345-11eb-a106-000c29820c67:1-5 | |
| +----------------------------------+----------+--------------+------------------+------------------------------------------+ |
| 1 row in set (0.00 sec) |
| |
| mysql> |
| mysql> CREATE DATABASE IF NOT EXISTS hdp DEFAULT CHARACTER SET utf8mb4; |
| Query OK, 1 row affected (0.28 sec) |
| |
| mysql> |
| mysql> CREATE TABLE IF NOT EXISTS hdp.t1(id int); |
| Query OK, 0 rows affected (0.01 sec) |
| |
| mysql> |
| mysql> BEGIN; |
| Query OK, 0 rows affected (0.00 sec) |
| |
| mysql> |
| mysql> INSERT INTO hdp.t1 VALUES(1),(3),(5),(7); |
| Query OK, 4 rows affected (0.00 sec) |
| Records: 4 Duplicates: 0 Warnings: 0 |
| |
| mysql> |
| mysql> INSERT INTO hdp.t1 VALUES(2),(4),(6),(8); |
| Query OK, 4 rows affected (0.00 sec) |
| Records: 4 Duplicates: 0 Warnings: 0 |
| |
| mysql> |
| mysql> COMMIT; |
| Query OK, 0 rows affected (0.05 sec) |
| |
| mysql> |
| mysql> FLUSH LOGS; |
| Query OK, 0 rows affected (0.01 sec) |
| |
| mysql> |
| mysql> SHOW MASTER STATUS; |
| +----------------------------------+----------+--------------+------------------+------------------------------------------+ |
| | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | |
| +----------------------------------+----------+--------------+------------------+------------------------------------------+ |
| | oldboyedu-mysqld-binary.000005 | 194 | | | ecaf563f-5345-11eb-a106-000c29820c67:1-8 | |
| +----------------------------------+----------+--------------+------------------+------------------------------------------+ |
| 1 row in set (0.00 sec) |
| |
| mysql> |
| |
| 第二次执行DDL及DML语句,并滚动二进制日志: |
| mysql> SHOW MASTER STATUS; |
| +----------------------------------+----------+--------------+------------------+------------------------------------------+ |
| | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | |
| +----------------------------------+----------+--------------+------------------+------------------------------------------+ |
| | oldboyedu-mysqld-binary.000005 | 194 | | | ecaf563f-5345-11eb-a106-000c29820c67:1-8 | |
| +----------------------------------+----------+--------------+------------------+------------------------------------------+ |
| 1 row in set (0.00 sec) |
| |
| mysql> |
| mysql> CREATE TABLE IF NOT EXISTS hdp.t2(id int); |
| Query OK, 0 rows affected (0.29 sec) |
| |
| mysql> |
| mysql> BEGIN; |
| Query OK, 0 rows affected (0.00 sec) |
| |
| mysql> |
| mysql> INSERT INTO hdp.t2 VALUES(10),(30),(50),(70); |
| Query OK, 4 rows affected (0.00 sec) |
| Records: 4 Duplicates: 0 Warnings: 0 |
| |
| mysql> |
| mysql> INSERT INTO hdp.t2 VALUES(20),(40),(60),(80); |
| Query OK, 4 rows affected (0.01 sec) |
| Records: 4 Duplicates: 0 Warnings: 0 |
| |
| mysql> |
| mysql> COMMIT; |
| Query OK, 0 rows affected (0.00 sec) |
| |
| mysql> |
| mysql> SHOW MASTER STATUS; |
| +----------------------------------+----------+--------------+------------------+-------------------------------------------+ |
| | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | |
| +----------------------------------+----------+--------------+------------------+-------------------------------------------+ |
| | oldboyedu-mysqld-binary.000005 | 731 | | | ecaf563f-5345-11eb-a106-000c29820c67:1-10 | |
| +----------------------------------+----------+--------------+------------------+-------------------------------------------+ |
| 1 row in set (0.00 sec) |
| |
| mysql> |
| mysql> FLUSH LOGS; |
| Query OK, 0 rows affected (0.29 sec) |
| |
| mysql> |
| mysql> SHOW MASTER STATUS; |
| +----------------------------------+----------+--------------+------------------+-------------------------------------------+ |
| | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | |
| +----------------------------------+----------+--------------+------------------+-------------------------------------------+ |
| | oldboyedu-mysqld-binary.000006 | 194 | | | ecaf563f-5345-11eb-a106-000c29820c67:1-10 | |
| +----------------------------------+----------+--------------+------------------+-------------------------------------------+ |
| 1 row in set (0.00 sec) |
| |
| mysql> |
| |
| 第三次执行DDL及DML语句,并滚动二进制日志: |
| mysql> SHOW MASTER STATUS; |
| +----------------------------------+----------+--------------+------------------+-------------------------------------------+ |
| | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | |
| +----------------------------------+----------+--------------+------------------+-------------------------------------------+ |
| | oldboyedu-mysqld-binary.000006 | 194 | | | ecaf563f-5345-11eb-a106-000c29820c67:1-10 | |
| +----------------------------------+----------+--------------+------------------+-------------------------------------------+ |
| 1 row in set (0.00 sec) |
| |
| mysql> |
| mysql> |
| mysql> |
| mysql> SHOW MASTER STATUS; |
| +----------------------------------+----------+--------------+------------------+-------------------------------------------+ |
| | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | |
| +----------------------------------+----------+--------------+------------------+-------------------------------------------+ |
| | oldboyedu-mysqld-binary.000006 | 194 | | | ecaf563f-5345-11eb-a106-000c29820c67:1-10 | |
| +----------------------------------+----------+--------------+------------------+-------------------------------------------+ |
| 1 row in set (0.00 sec) |
| |
| mysql> |
| mysql> CREATE TABLE IF NOT EXISTS hdp.t3(id int); |
| Query OK, 0 rows affected (0.01 sec) |
| |
| mysql> |
| mysql> BEGIN; |
| Query OK, 0 rows affected (0.00 sec) |
| |
| mysql> |
| mysql> INSERT INTO hdp.t3 VALUES(100),(300),(500),(700); |
| Query OK, 4 rows affected (0.00 sec) |
| Records: 4 Duplicates: 0 Warnings: 0 |
| |
| mysql> |
| mysql> INSERT INTO hdp.t3 VALUES(200),(400),(600),(800); |
| Query OK, 4 rows affected (0.00 sec) |
| Records: 4 Duplicates: 0 Warnings: 0 |
| |
| mysql> |
| mysql> COMMIT; |
| Query OK, 0 rows affected (0.00 sec) |
| |
| mysql> |
| mysql> SHOW MASTER STATUS; |
| +----------------------------------+----------+--------------+------------------+-------------------------------------------+ |
| | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | |
| +----------------------------------+----------+--------------+------------------+-------------------------------------------+ |
| | oldboyedu-mysqld-binary.000006 | 731 | | | ecaf563f-5345-11eb-a106-000c29820c67:1-12 | |
| +----------------------------------+----------+--------------+------------------+-------------------------------------------+ |
| 1 row in set (0.00 sec) |
| |
| mysql> |
| mysql> FLUSH LOGS; |
| Query OK, 0 rows affected (0.01 sec) |
| |
| mysql> |
| mysql> SHOW MASTER STATUS; |
| +----------------------------------+----------+--------------+------------------+-------------------------------------------+ |
| | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | |
| +----------------------------------+----------+--------------+------------------+-------------------------------------------+ |
| | oldboyedu-mysqld-binary.000007 | 194 | | | ecaf563f-5345-11eb-a106-000c29820c67:1-12 | |
| +----------------------------------+----------+--------------+------------------+-------------------------------------------+ |
| 1 row in set (0.00 sec) |
| |
| mysql> |
| |
| 第四次执行DDL及DML语句,并不滚动二进制日志,而是删除"hdp"数据库: |
| [root@docker201.oldboyedu.com ~]# mysql -S /tmp/mysql23307.sock |
| Welcome to the MySQL monitor. Commands end with ; or \g. |
| Your MySQL connection id is 5 |
| Server version: 5.7.31-log MySQL Community Server (GPL) |
| |
| Copyright (c) 2000, 2020, 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> |
| mysql> SHOW MASTER STATUS; |
| +----------------------------------+----------+--------------+------------------+-------------------------------------------+ |
| | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | |
| +----------------------------------+----------+--------------+------------------+-------------------------------------------+ |
| | oldboyedu-mysqld-binary.000007 | 194 | | | ecaf563f-5345-11eb-a106-000c29820c67:1-12 | |
| +----------------------------------+----------+--------------+------------------+-------------------------------------------+ |
| 1 row in set (0.00 sec) |
| |
| mysql> |
| mysql> CREATE TABLE IF NOT EXISTS hdp.t5(id int); |
| Query OK, 0 rows affected (0.01 sec) |
| |
| mysql> |
| mysql> BEGIN; |
| Query OK, 0 rows affected (0.00 sec) |
| |
| mysql> |
| mysql> INSERT INTO hdp.t5 VALUES(111),(222),(333),(444); |
| Query OK, 4 rows affected (0.00 sec) |
| Records: 4 Duplicates: 0 Warnings: 0 |
| |
| mysql> |
| mysql> INSERT INTO hdp.t5 VALUES(10),(20),(30),(40); |
| Query OK, 4 rows affected (0.00 sec) |
| Records: 4 Duplicates: 0 Warnings: 0 |
| |
| mysql> |
| mysql> COMMIT; |
| Query OK, 0 rows affected (0.00 sec) |
| |
| mysql> |
| mysql> SHOW MASTER STATUS; |
| +----------------------------------+----------+--------------+------------------+-------------------------------------------+ |
| | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | |
| +----------------------------------+----------+--------------+------------------+-------------------------------------------+ |
| | oldboyedu-mysqld-binary.000007 | 731 | | | ecaf563f-5345-11eb-a106-000c29820c67:1-14 | |
| +----------------------------------+----------+--------------+------------------+-------------------------------------------+ |
| 1 row in set (0.00 sec) |
| |
| mysql> |
| mysql> DROP DATABASE hdp; |
| Query OK, 4 rows affected (0.30 sec) |
| |
| mysql> |
| mysql> SHOW MASTER STATUS; |
| +----------------------------------+----------+--------------+------------------+-------------------------------------------+ |
| | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | |
| +----------------------------------+----------+--------------+------------------+-------------------------------------------+ |
| | oldboyedu-mysqld-binary.000007 | 885 | | | ecaf563f-5345-11eb-a106-000c29820c67:1-15 | |
| +----------------------------------+----------+--------------+------------------+-------------------------------------------+ |
| 1 row in set (0.00 sec) |
| |
| mysql> |
| |
| 在第四步骤我们删除了"hdp"数据库,那么来我们的操作就是得恢复"hdp"数据库中的数据,我们分为以下几个步骤来操作: |
| (1)确定起点的位置,我们得找到创建数据库的语句在二进制日志中的位置 |
| mysql> SHOW BINLOG EVENTS IN 'oldboyedu-mysqld-binary.000004'; |
| +----------------------------------+------+----------------+-----------+-------------+-------------------------------------------------------------------+ |
| | Log_name | Pos | Event_type | Server_id | End_log_pos | Info | |
| +----------------------------------+------+----------------+-----------+-------------+-------------------------------------------------------------------+ |
| | oldboyedu-mysqld-binary.000004 | 4 | Format_desc | 7 | 123 | Server ver: 5.7.31-log, Binlog ver: 4 | |
| | oldboyedu-mysqld-binary.000004 | 123 | Previous_gtids | 7 | 154 | | |
| | oldboyedu-mysqld-binary.000004 | 154 | Gtid | 7 | 219 | SET @@SESSION.GTID_NEXT= 'ecaf563f-5345-11eb-a106-000c29820c67:1' | |
| | oldboyedu-mysqld-binary.000004 | 219 | Query | 7 | 357 | CREATE DATABASE IF NOT EXISTS hive DEFAULT CHARACTER SET utf8mb4 | |
| | oldboyedu-mysqld-binary.000004 | 357 | Gtid | 7 | 422 | SET @@SESSION.GTID_NEXT= 'ecaf563f-5345-11eb-a106-000c29820c67:2' | |
| | oldboyedu-mysqld-binary.000004 | 422 | Query | 7 | 557 | CREATE DATABASE IF NOT EXISTS cdh DEFAULT CHARACTER SET utf8mb4 | |
| | oldboyedu-mysqld-binary.000004 | 557 | Gtid | 7 | 622 | SET @@SESSION.GTID_NEXT= 'ecaf563f-5345-11eb-a106-000c29820c67:3' | |
| | oldboyedu-mysqld-binary.000004 | 622 | Query | 7 | 736 | CREATE TABLE IF NOT EXISTS hive.user(id int) | |
| | oldboyedu-mysqld-binary.000004 | 736 | Gtid | 7 | 801 | SET @@SESSION.GTID_NEXT= 'ecaf563f-5345-11eb-a106-000c29820c67:4' | |
| | oldboyedu-mysqld-binary.000004 | 801 | Query | 7 | 869 | BEGIN | |
| | oldboyedu-mysqld-binary.000004 | 869 | Table_map | 7 | 916 | table_id: 112 (hive.user) | |
| | oldboyedu-mysqld-binary.000004 | 916 | Write_rows | 7 | 966 | table_id: 112 flags: STMT_END_F | |
| | oldboyedu-mysqld-binary.000004 | 966 | Xid | 7 | 997 | COMMIT | |
| | oldboyedu-mysqld-binary.000004 | 997 | Gtid | 7 | 1062 | SET @@SESSION.GTID_NEXT= 'ecaf563f-5345-11eb-a106-000c29820c67:5' | |
| | oldboyedu-mysqld-binary.000004 | 1062 | Query | 7 | 1174 | CREATE TABLE IF NOT EXISTS cdh.user(id int) | |
| | oldboyedu-mysqld-binary.000004 | 1174 | Gtid | 7 | 1239 | SET @@SESSION.GTID_NEXT= 'ecaf563f-5345-11eb-a106-000c29820c67:6' | |
| | oldboyedu-mysqld-binary.000004 | 1239 | Query | 7 | 1374 | CREATE DATABASE IF NOT EXISTS hdp DEFAULT CHARACTER SET utf8mb4 | |
| | oldboyedu-mysqld-binary.000004 | 1374 | Gtid | 7 | 1439 | SET @@SESSION.GTID_NEXT= 'ecaf563f-5345-11eb-a106-000c29820c67:7' | |
| | oldboyedu-mysqld-binary.000004 | 1439 | Query | 7 | 1549 | CREATE TABLE IF NOT EXISTS hdp.t1(id int) | |
| | oldboyedu-mysqld-binary.000004 | 1549 | Gtid | 7 | 1614 | SET @@SESSION.GTID_NEXT= 'ecaf563f-5345-11eb-a106-000c29820c67:8' | |
| | oldboyedu-mysqld-binary.000004 | 1614 | Query | 7 | 1682 | BEGIN | |
| | oldboyedu-mysqld-binary.000004 | 1682 | Table_map | 7 | 1726 | table_id: 113 (hdp.t1) | |
| | oldboyedu-mysqld-binary.000004 | 1726 | Write_rows | 7 | 1781 | table_id: 113 flags: STMT_END_F | |
| | oldboyedu-mysqld-binary.000004 | 1781 | Table_map | 7 | 1825 | table_id: 113 (hdp.t1) | |
| | oldboyedu-mysqld-binary.000004 | 1825 | Write_rows | 7 | 1880 | table_id: 113 flags: STMT_END_F | |
| | oldboyedu-mysqld-binary.000004 | 1880 | Xid | 7 | 1911 | COMMIT | |
| | oldboyedu-mysqld-binary.000004 | 1911 | Rotate | 7 | 1974 | oldboyedu-mysqld-binary.000005;pos=4 | |
| +----------------------------------+------+----------------+-----------+-------------+-------------------------------------------------------------------+ |
| 27 rows in set (0.00 sec) |
| |
| mysql> |
| |
| (2)确定结束的位置,我们得找到删除数据库的语句在二进制日志中的位置 |
| mysql> SHOW MASTER STATUS; |
| +----------------------------------+----------+--------------+------------------+-------------------------------------------+ |
| | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | |
| +----------------------------------+----------+--------------+------------------+-------------------------------------------+ |
| | oldboyedu-mysqld-binary.000007 | 885 | | | ecaf563f-5345-11eb-a106-000c29820c67:1-15 | |
| +----------------------------------+----------+--------------+------------------+-------------------------------------------+ |
| 1 row in set (0.00 sec) |
| |
| mysql> |
| mysql> SHOW BINLOG EVENTS IN 'oldboyedu-mysqld-binary.000007'; |
| +----------------------------------+-----+----------------+-----------+-------------+--------------------------------------------------------------------+ |
| | Log_name | Pos | Event_type | Server_id | End_log_pos | Info | |
| +----------------------------------+-----+----------------+-----------+-------------+--------------------------------------------------------------------+ |
| | oldboyedu-mysqld-binary.000007 | 4 | Format_desc | 7 | 123 | Server ver: 5.7.31-log, Binlog ver: 4 | |
| | oldboyedu-mysqld-binary.000007 | 123 | Previous_gtids | 7 | 194 | ecaf563f-5345-11eb-a106-000c29820c67:1-12 | |
| | oldboyedu-mysqld-binary.000007 | 194 | Gtid | 7 | 259 | SET @@SESSION.GTID_NEXT= 'ecaf563f-5345-11eb-a106-000c29820c67:13' | |
| | oldboyedu-mysqld-binary.000007 | 259 | Query | 7 | 369 | CREATE TABLE IF NOT EXISTS hdp.t5(id int) | |
| | oldboyedu-mysqld-binary.000007 | 369 | Gtid | 7 | 434 | SET @@SESSION.GTID_NEXT= 'ecaf563f-5345-11eb-a106-000c29820c67:14' | |
| | oldboyedu-mysqld-binary.000007 | 434 | Query | 7 | 502 | BEGIN | |
| | oldboyedu-mysqld-binary.000007 | 502 | Table_map | 7 | 546 | table_id: 116 (hdp.t5) | |
| | oldboyedu-mysqld-binary.000007 | 546 | Write_rows | 7 | 601 | table_id: 116 flags: STMT_END_F | |
| | oldboyedu-mysqld-binary.000007 | 601 | Table_map | 7 | 645 | table_id: 116 (hdp.t5) | |
| | oldboyedu-mysqld-binary.000007 | 645 | Write_rows | 7 | 700 | table_id: 116 flags: STMT_END_F | |
| | oldboyedu-mysqld-binary.000007 | 700 | Xid | 7 | 731 | COMMIT | |
| | oldboyedu-mysqld-binary.000007 | 731 | Gtid | 7 | 796 | SET @@SESSION.GTID_NEXT= 'ecaf563f-5345-11eb-a106-000c29820c67:15' | |
| | oldboyedu-mysqld-binary.000007 | 796 | Query | 7 | 885 | DROP DATABASE hdp | |
| +----------------------------------+-----+----------------+-----------+-------------+--------------------------------------------------------------------+ |
| 13 rows in set (0.00 sec) |
| |
| mysql> |
| |
| (3)综上所述,起始位置是GTID是6,结束位置的GTID是14。但是这些GTID范围是从'oldboyedu-mysqld-binary.000004'到'oldboyedu-mysqld-binary.000007',跨越了4个二进制日志文件,你也可以模拟更多的跨越文件。 |
| [root@docker201.oldboyedu.com ~] |
| [root@docker201.oldboyedu.com /oldboyedu/logs/mysql23307/binlog] |
| [root@docker201.oldboyedu.com /oldboyedu/logs/mysql23307/binlog] |
| 总用量 32 |
| -rw-r----- 1 mysql mysql 177 2月 4 17:54 oldboyedu-mysqld-binary.000001 |
| -rw-r----- 1 mysql mysql 177 2月 4 17:59 oldboyedu-mysqld-binary.000002 |
| -rw-r----- 1 mysql mysql 177 2月 4 18:00 oldboyedu-mysqld-binary.000003 |
| -rw-r----- 1 mysql mysql 1974 2月 4 20:46 oldboyedu-mysqld-binary.000004 |
| -rw-r----- 1 mysql mysql 794 2月 4 20:49 oldboyedu-mysqld-binary.000005 |
| -rw-r----- 1 mysql mysql 794 2月 4 20:51 oldboyedu-mysqld-binary.000006 |
| -rw-r----- 1 mysql mysql 885 2月 4 20:58 oldboyedu-mysqld-binary.000007 |
| -rw-r----- 1 mysql mysql 483 2月 4 20:51 oldboyedu-mysqld-binary.index |
| [root@docker201.oldboyedu.com /oldboyedu/logs/mysql23307/binlog] |
| [root@docker201.oldboyedu.com /oldboyedu/logs/mysql23307/binlog] |
| [root@docker201.oldboyedu.com /oldboyedu/logs/mysql23307/binlog] |
| [root@docker201.oldboyedu.com /oldboyedu/logs/mysql23307/binlog] |
| [root@docker201.oldboyedu.com /oldboyedu/logs/mysql23307/binlog] |
| -rw-r--r-- 1 root root 10563 2月 4 21:21 /tmp/recover_hdp.log |
| [root@docker201.oldboyedu.com /oldboyedu/logs/mysql23307/binlog] |
| [root@docker201.oldboyedu.com /oldboyedu/logs/mysql23307/binlog] |
| /tmp/recover_hdp.log: ASCII text |
| [root@docker201.oldboyedu.com /oldboyedu/logs/mysql23307/binlog] |
| [root@docker201.oldboyedu.com ~] |
| ; |
| ; |
| DELIMITER ; |
| |
| |
| ROLLBACK; |
| BINLOG ' |
| wcUbYA8HAAAAdwAAAHsAAAAAAAQANS43LjMxLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA |
| AAAAAAAAAAAAAAAAAADBxRtgEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA |
| Adx7KXY= |
| '; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| SET @@SESSION.GTID_NEXT= 'ecaf563f-5345-11eb-a106-000c29820c67:6'; |
| |
| |
| SET TIMESTAMP=1612442691; |
| SET @@session.pseudo_thread_id=3; |
| SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1; |
| SET @@session.sql_mode=1436549152; |
| SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1; |
| ; |
| SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=8; |
| SET @@session.lc_time_names=0; |
| SET @@session.collation_database=DEFAULT; |
| CREATE DATABASE IF NOT EXISTS hdp DEFAULT CHARACTER SET utf8mb4 |
| ; |
| |
| |
| SET @@SESSION.GTID_NEXT= 'ecaf563f-5345-11eb-a106-000c29820c67:7'; |
| |
| |
| SET TIMESTAMP=1612442705; |
| CREATE TABLE IF NOT EXISTS hdp.t1(id int) |
| ; |
| |
| |
| ; |
| SET @@SESSION.GTID_NEXT= 'ecaf563f-5345-11eb-a106-000c29820c67:8'; |
| |
| |
| SET TIMESTAMP=1612442755; |
| BEGIN |
| ; |
| |
| |
| |
| |
| |
| BINLOG ' |
| g+wbYBMHAAAALAAAAL4GAAAAAHEAAAAAAAEAA2hkcAACdDEAAQMAAecgHeU= |
| g+wbYB4HAAAANwAAAPUGAAAAAHEAAAAAAAEAAgAB//4BAAAA/gMAAAD+BQAAAP4HAAAAuA6ZDw== |
| '; |
| |
| |
| |
| |
| |
| BINLOG ' |
| jOwbYBMHAAAALAAAACEHAAAAAHEAAAAAAAEAA2hkcAACdDEAAQMAAeqfW1I= |
| jOwbYB4HAAAANwAAAFgHAAAAAHEAAAAAAAEAAgAB//4CAAAA/gQAAAD+BgAAAP4IAAAAsdj4qA== |
| '; |
| |
| |
| COMMIT; |
| |
| |
| SET @@SESSION.GTID_NEXT= 'AUTOMATIC' ; |
| |
| |
| BINLOG ' |
| luwbYA8HAAAAdwAAAHsAAAAAAAQANS43LjMxLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA |
| AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA |
| AUXcGTU= |
| '; |
| |
| |
| |
| |
| |
| SET @@SESSION.GTID_NEXT= 'ecaf563f-5345-11eb-a106-000c29820c67:9'; |
| |
| |
| SET TIMESTAMP=1612442943; |
| CREATE TABLE IF NOT EXISTS hdp.t2(id int) |
| ; |
| |
| |
| ; |
| SET @@SESSION.GTID_NEXT= 'ecaf563f-5345-11eb-a106-000c29820c67:10'; |
| |
| |
| SET TIMESTAMP=1612442966; |
| BEGIN |
| ; |
| |
| |
| |
| |
| |
| BINLOG ' |
| Vu0bYBMHAAAALAAAACICAAAAAHIAAAAAAAEAA2hkcAACdDIAAQMAAcTcqzs= |
| Vu0bYB4HAAAANwAAAFkCAAAAAHIAAAAAAAEAAgAB//4KAAAA/h4AAAD+MgAAAP5GAAAAalsFdg== |
| '; |
| |
| |
| |
| |
| |
| BINLOG ' |
| Yu0bYBMHAAAALAAAAIUCAAAAAHEAAAAAAAEAA2hkcAACdDEAAQMAAVIt8iA= |
| Yu0bYB4HAAAANwAAALwCAAAAAHEAAAAAAAEAAgAB//4UAAAA/igAAAD+PAAAAP5QAAAAs80wBg== |
| '; |
| |
| |
| COMMIT; |
| |
| |
| SET @@SESSION.GTID_NEXT= 'AUTOMATIC' ; |
| |
| |
| BINLOG ' |
| c+0bYA8HAAAAdwAAAHsAAAAAAAQANS43LjMxLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA |
| AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA |
| ASocZfA= |
| '; |
| |
| |
| |
| |
| |
| SET @@SESSION.GTID_NEXT= 'ecaf563f-5345-11eb-a106-000c29820c67:11'; |
| |
| |
| SET TIMESTAMP=1612443037; |
| CREATE TABLE IF NOT EXISTS hdp.t3(id int) |
| ; |
| |
| |
| ; |
| SET @@SESSION.GTID_NEXT= 'ecaf563f-5345-11eb-a106-000c29820c67:12'; |
| |
| |
| SET TIMESTAMP=1612443053; |
| BEGIN |
| ; |
| |
| |
| |
| |
| |
| BINLOG ' |
| re0bYBMHAAAALAAAACICAAAAAHIAAAAAAAEAA2hkcAACdDIAAQMAAX7qvgI= |
| re0bYB4HAAAANwAAAFkCAAAAAHIAAAAAAAEAAgAB//5kAAAA/iwBAAD+9AEAAP68AgAAvU3uVw== |
| '; |
| |
| |
| |
| |
| |
| BINLOG ' |
| uO0bYBMHAAAALAAAAIUCAAAAAHEAAAAAAAEAA2hkcAACdDEAAQMAARKIHIQ= |
| uO0bYB4HAAAANwAAALwCAAAAAHEAAAAAAAEAAgAB//7IAAAA/pABAAD+WAIAAP4gAwAAJhzRiw== |
| '; |
| |
| |
| COMMIT; |
| |
| |
| SET @@SESSION.GTID_NEXT= 'AUTOMATIC' ; |
| |
| |
| |
| BINLOG ' |
| y+0bYA8HAAAAdwAAAHsAAAABAAQANS43LjMxLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA |
| AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA |
| Aa7l0l8= |
| '; |
| |
| |
| |
| |
| |
| SET @@SESSION.GTID_NEXT= 'ecaf563f-5345-11eb-a106-000c29820c67:13'; |
| |
| |
| SET TIMESTAMP=1612443375; |
| CREATE TABLE IF NOT EXISTS hdp.t5(id int) |
| ; |
| |
| |
| ; |
| SET @@SESSION.GTID_NEXT= 'ecaf563f-5345-11eb-a106-000c29820c67:14'; |
| |
| |
| SET TIMESTAMP=1612443436; |
| BEGIN |
| ; |
| |
| |
| |
| |
| |
| BINLOG ' |
| LO8bYBMHAAAALAAAACICAAAAAHQAAAAAAAEAA2hkcAACdDUAAQMAAeFcLBE= |
| LO8bYB4HAAAANwAAAFkCAAAAAHQAAAAAAAEAAgAB//5vAAAA/t4AAAD+TQEAAP68AQAAVoG+sQ== |
| '; |
| |
| |
| |
| |
| |
| BINLOG ' |
| Pu8bYBMHAAAALAAAAIUCAAAAAHQAAAAAAAEAA2hkcAACdDUAAQMAAZc9OZA= |
| Pu8bYB4HAAAANwAAALwCAAAAAHQAAAAAAAEAAgAB//4KAAAA/hQAAAD+HgAAAP4oAAAAx3qcag== |
| '; |
| |
| |
| COMMIT; |
| SET @@SESSION.GTID_NEXT= 'AUTOMATIC' ; |
| DELIMITER ; |
| |
| ; |
| ; |
| [root@docker201.oldboyedu.com ~] |
| [root@docker201.oldboyedu.com ~] |
| -rw-r--r-- 1 root root 10563 2月 4 21:21 /tmp/recover_hdp.log |
| [root@docker201.oldboyedu.com ~] |
| [root@docker201.oldboyedu.com ~] |
| Welcome to the MySQL monitor. Commands end with ; or \g. |
| Your MySQL connection id is 6 |
| Server version: 5.7.31-log MySQL Community Server (GPL) |
| |
| Copyright (c) 2000, 2020, 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> |
| mysql> SHOW DATABASES; |
| +--------------------+ |
| | Database | |
| +--------------------+ |
| | information_schema | |
| | cdh | |
| | cmdb | |
| | hive | |
| | mysql | |
| | performance_schema | |
| | school | |
| | sys | |
| | world | |
| | oldboyedu | |
| | zabbix | |
| +--------------------+ |
| 11 rows in set (0.00 sec) |
| |
| mysql> |
| mysql> SOURCE /tmp/recover_hdp.log |
| Query OK, 0 rows affected (0.00 sec) |
| |
| ... |
| |
| Query OK, 0 rows affected (0.00 sec) |
| |
| mysql> |
| mysql> SHOW DATABASES; |
| +--------------------+ |
| | Database | |
| +--------------------+ |
| | information_schema | |
| | cdh | |
| | cmdb | |
| | hive | |
| | mysql | |
| | performance_schema | |
| | school | |
| | sys | |
| | world | |
| | oldboyedu | |
| | zabbix | |
| +--------------------+ |
| 11 rows in set (0.00 sec) |
| |
| mysql> |
| mysql> SELECT * FROM hdp.t5; |
| ERROR 1146 (42S02): Table 'hdp.t5' doesn't exist |
| mysql> |
| |
| 幂等性: |
| 就是用户对于同一个操作发起的一次或者多次请求的结果是一致的,不会因为多次点击而产生副作用。 |
| |
| 开启GTID后,MySQL恢复binlog时,重复GTID事务不会再执行了。也就是说,我们基于GTID对二进制日志进行截取后,想要直接使用截取后的日志进行恢复是不可行的,这是因为数据库日志中已经有GTID的存在,因此并不会执行截取的GTID事件。 |
| |
| 综上所述,想要解决GTID幂等性的问题,可以在使用mysqlbinlog工具截取日志时添加" |
| [root@docker201.oldboyedu.com /oldboyedu/logs/mysql23307/binlog] |
| [root@docker201.oldboyedu.com /oldboyedu/logs/mysql23307/binlog] |
| [root@docker201.oldboyedu.com /oldboyedu/logs/mysql23307/binlog] |
| [root@docker201.oldboyedu.com ~] |
| [root@docker201.oldboyedu.com ~] |
| -rw-r |
| -rw-r |
| [root@docker201.oldboyedu.com ~] |
| [root@docker201.oldboyedu.com ~] |
| ; |
| ; |
| DELIMITER ; |
| |
| |
| ROLLBACK; |
| BINLOG ' |
| wcUbYA8HAAAAdwAAAHsAAAAAAAQANS43LjMxLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA |
| AAAAAAAAAAAAAAAAAADBxRtgEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA |
| Adx7KXY= |
| '; |
| ; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| SET TIMESTAMP=1612442691; |
| SET @@session.pseudo_thread_id=3; |
| SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1; |
| SET @@session.sql_mode=1436549152; |
| SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1; |
| ; |
| SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=8; |
| SET @@session.lc_time_names=0; |
| SET @@session.collation_database=DEFAULT; |
| CREATE DATABASE IF NOT EXISTS hdp DEFAULT CHARACTER SET utf8mb4 |
| ; |
| |
| |
| |
| SET TIMESTAMP=1612442705; |
| CREATE TABLE IF NOT EXISTS hdp.t1(id int) |
| ; |
| |
| |
| |
| SET TIMESTAMP=1612442755; |
| BEGIN |
| ; |
| |
| |
| |
| |
| |
| BINLOG ' |
| g+wbYBMHAAAALAAAAL4GAAAAAHEAAAAAAAEAA2hkcAACdDEAAQMAAecgHeU= |
| g+wbYB4HAAAANwAAAPUGAAAAAHEAAAAAAAEAAgAB//4BAAAA/gMAAAD+BQAAAP4HAAAAuA6ZDw== |
| '; |
| |
| |
| |
| |
| |
| BINLOG ' |
| jOwbYBMHAAAALAAAACEHAAAAAHEAAAAAAAEAA2hkcAACdDEAAQMAAeqfW1I= |
| jOwbYB4HAAAANwAAAFgHAAAAAHEAAAAAAAEAAgAB//4CAAAA/gQAAAD+BgAAAP4IAAAAsdj4qA== |
| '; |
| |
| |
| COMMIT; |
| |
| |
| |
| |
| BINLOG ' |
| luwbYA8HAAAAdwAAAHsAAAAAAAQANS43LjMxLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA |
| AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA |
| AUXcGTU= |
| '; |
| ; |
| |
| |
| |
| |
| SET TIMESTAMP=1612442943; |
| CREATE TABLE IF NOT EXISTS hdp.t2(id int) |
| ; |
| |
| |
| |
| SET TIMESTAMP=1612442966; |
| BEGIN |
| ; |
| |
| |
| |
| |
| |
| BINLOG ' |
| Vu0bYBMHAAAALAAAACICAAAAAHIAAAAAAAEAA2hkcAACdDIAAQMAAcTcqzs= |
| Vu0bYB4HAAAANwAAAFkCAAAAAHIAAAAAAAEAAgAB//4KAAAA/h4AAAD+MgAAAP5GAAAAalsFdg== |
| '; |
| |
| |
| |
| |
| |
| BINLOG ' |
| Yu0bYBMHAAAALAAAAIUCAAAAAHEAAAAAAAEAA2hkcAACdDEAAQMAAVIt8iA= |
| Yu0bYB4HAAAANwAAALwCAAAAAHEAAAAAAAEAAgAB//4UAAAA/igAAAD+PAAAAP5QAAAAs80wBg== |
| '; |
| |
| |
| COMMIT; |
| |
| |
| |
| |
| BINLOG ' |
| c+0bYA8HAAAAdwAAAHsAAAAAAAQANS43LjMxLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA |
| AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA |
| ASocZfA= |
| '; |
| ; |
| |
| |
| |
| |
| SET TIMESTAMP=1612443037; |
| CREATE TABLE IF NOT EXISTS hdp.t3(id int) |
| ; |
| |
| |
| |
| SET TIMESTAMP=1612443053; |
| BEGIN |
| ; |
| |
| |
| |
| |
| |
| BINLOG ' |
| re0bYBMHAAAALAAAACICAAAAAHIAAAAAAAEAA2hkcAACdDIAAQMAAX7qvgI= |
| re0bYB4HAAAANwAAAFkCAAAAAHIAAAAAAAEAAgAB//5kAAAA/iwBAAD+9AEAAP68AgAAvU3uVw== |
| '; |
| |
| |
| |
| |
| |
| BINLOG ' |
| uO0bYBMHAAAALAAAAIUCAAAAAHEAAAAAAAEAA2hkcAACdDEAAQMAARKIHIQ= |
| uO0bYB4HAAAANwAAALwCAAAAAHEAAAAAAAEAAgAB//7IAAAA/pABAAD+WAIAAP4gAwAAJhzRiw== |
| '; |
| |
| |
| COMMIT; |
| |
| |
| |
| |
| |
| BINLOG ' |
| y+0bYA8HAAAAdwAAAHsAAAABAAQANS43LjMxLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA |
| AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA |
| Aa7l0l8= |
| '; |
| ; |
| |
| |
| |
| |
| SET TIMESTAMP=1612443375; |
| CREATE TABLE IF NOT EXISTS hdp.t5(id int) |
| ; |
| |
| |
| |
| SET TIMESTAMP=1612443436; |
| BEGIN |
| ; |
| |
| |
| |
| |
| |
| BINLOG ' |
| LO8bYBMHAAAALAAAACICAAAAAHQAAAAAAAEAA2hkcAACdDUAAQMAAeFcLBE= |
| LO8bYB4HAAAANwAAAFkCAAAAAHQAAAAAAAEAAgAB//5vAAAA/t4AAAD+TQEAAP68AQAAVoG+sQ== |
| '; |
| |
| |
| |
| |
| |
| BINLOG ' |
| Pu8bYBMHAAAALAAAAIUCAAAAAHQAAAAAAAEAA2hkcAACdDUAAQMAAZc9OZA= |
| Pu8bYB4HAAAANwAAALwCAAAAAHQAAAAAAAEAAgAB//4KAAAA/hQAAAD+HgAAAP4oAAAAx3qcag== |
| '; |
| |
| |
| COMMIT; |
| DELIMITER ; |
| |
| ; |
| ; |
| [root@docker201.oldboyedu.com ~] |
| [root@docker201.oldboyedu.com ~] |
| -rw-r--r-- 1 root root 7.9K 2月 4 21:42 /tmp/recover_hdp2.log |
| -rw-r--r-- 1 root root 11K 2月 4 21:42 /tmp/recover_hdp.log |
| [root@docker201.oldboyedu.com ~] |
| [root@docker201.oldboyedu.com ~] |
| Welcome to the MySQL monitor. Commands end with ; or \g. |
| Your MySQL connection id is 7 |
| Server version: 5.7.31-log MySQL Community Server (GPL) |
| |
| Copyright (c) 2000, 2020, 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> |
| mysql> SHOW DATABASES; |
| +--------------------+ |
| | Database | |
| +--------------------+ |
| | information_schema | |
| | cdh | |
| | cmdb | |
| | hive | |
| | mysql | |
| | performance_schema | |
| | school | |
| | sys | |
| | world | |
| | oldboyedu | |
| | zabbix | |
| +--------------------+ |
| 11 rows in set (0.00 sec) |
| |
| mysql> |
| mysql> SET sql_log_bin=0; |
| Query OK, 0 rows affected (0.00 sec) |
| |
| mysql> |
| mysql> SOURCE /tmp/recover_hdp2.log; |
| Query OK, 0 rows affected (0.00 sec) |
| |
| ... |
| |
| Query OK, 0 rows affected (0.00 sec) |
| |
| mysql> |
| mysql> SET sql_log_bin=1; |
| Query OK, 0 rows affected (0.00 sec) |
| |
| mysql> |
| mysql> SHOW DATABASES; |
| +--------------------+ |
| | Database | |
| +--------------------+ |
| | information_schema | |
| | cdh | |
| | cmdb | |
| | hdp | |
| | hive | |
| | mysql | |
| | performance_schema | |
| | school | |
| | sys | |
| | world | |
| | oldboyedu | |
| | zabbix | |
| +--------------------+ |
| 12 rows in set (0.00 sec) |
| |
| mysql> |
| mysql> SELECT * FROM hdp.t5; |
| +------+ |
| | id | |
| +------+ |
| | 111 | |
| | 222 | |
| | 333 | |
| | 444 | |
| | 10 | |
| | 20 | |
| | 30 | |
| | 40 | |
| +------+ |
| 8 rows in set (0.00 sec) |
| |
| mysql> |
| mysql> |
| mysqlbinlog |
| |
| 相关参数说明: |
| |
| 跳过GTID信息的截取。 |
| |
| |
| 只截取关心的GTID范围。 |
| |
| |
| 排除指定GTID事件。 |
| 报错原因: |
| 主库未开启GTID |
| |
| 解决方案: |
| cat > /oldboyedu/etc/mysql57/3307/my.cnf <<EOF |
| [mysqld] |
| ... |
| |
| # 将其值设置为"on"则表示开启GTID功能。默认值为"OFF",表示未开启。 |
| gtid-mode=ON |
| # 启用强制GITD的一致性。即在启动MySQL实例之前,会校验GTID一致性的问题。默认值为"OFF",表示未开启。 |
| enforce-gtid-consistency=ON |
| # 强制从库记录二进制日志的一致性。换句话说,就是要求从库的GTID和主库的GTID要保持一致。 |
| log-slave-updates=1 |
| EOF |
| 报错原因: |
| 从库未开启GTID |
| |
| 解决方案: |
| cat > /oldboyedu/etc/mysql57/3307/my.cnf <<EOF |
| [mysqld] |
| ... |
| gtid-mode=ON |
| enforce-gtid-consistency=ON |
| EOF |
| 报错原因: |
| gtid-mode和enforce-gtid-consistency这两个参数必须同时开启! |
| |
| 解决方案: |
| cat > /oldboyedu/etc/mysql57/3307/my.cnf <<EOF |
| [mysqld] |
| ... |
| gtid-mode=ON |
| enforce-gtid-consistency=ON |
| EOF |