gtid简介
GTID是MySQL 5.6的新特性,其全称是Global Transaction Identifier,可简化MySQL的主从切换以及Failover。GTID用于在binlog中唯一标识一个事务。当事务提交时,MySQL Server在写binlog的时候,会先写一个特殊的Binlog Event,类型为GTID_Event,指定下一个事务的GTID,然后再写事务的Binlog。主从同步时GTID_Event和事务的Binlog都会传递到从库,从库在执行的时候也是用同样的GTID写binlog,这样主从同步以后,就可通过GTID确定从库同步到的位置了。也就是说,无论是级联情况,还是一主多从情况,都可以通过GTID自动找点儿,而无需像之前那样通过File_name和File_position找点儿了。
GTID 的组成部分:
-
GTID = source_id:transaction_id
-
source_id 正常即是 server_uuid,在第一次启动时生成(函数 generate_server_uuid),并持久化到 DATADIR/auto.cnf 文件里。
-
transaction_id 是顺序化的序列号(sequence number),在每台 MySQL 服务器上都是从 1 开始自增长的序列,是事务的唯一标识。例如:3E11FA47-71CA-11E1-9E33-C80AA9429562:23
-
GTID 的集合(GTIDs)可以用 source_id+transaction_id 范围表示,例如:3E11FA47-71CA-11E1-9E33-C80AA9429562:1-18
-
复杂一点的:如果这组 GTIDs 来自不同的 source_id,各组 source_id 之间用逗号分隔;如果事务序号有多个范围区间,各组范围之间用冒号分隔,例如:3E11FA47-71CA-11E1-9E33-C80AA9429562:1-5:11-18,2C256447-3F0D-431B-9A12-575BB20C1507:1-27
GTID 的概述
1、全局事物标识:global transaction identifieds。
2、GTID 事物是全局唯一性的,且一个事务对应一个 GTID。
3、一个 GTID 在一个服务器上只执行一次,避免重复执行导致数据混乱或者主从不一致。
4、GTID 用来代替classic的复制方法,不在使用 binlog+pos 开启复制。而是使用 master_auto_postion=1 的方式自动匹配 GTID 断点进行复制。
5、MySQL-5.6.5 开始支持的,MySQL-5.6.10 后开始完善。
6、在传统的 slave 端,binlog 是不用开启的,但是在 GTID 中,slave 端的 binlog 是必须开启的,目的是记录执行过的 GTID(强制);但是从 5.7.5 版本开始无需在 GTID 模式下启用参数 log_slave_updates
GTID 如何产生
GTID 的生成受 gtid_next 控制。
在 Master 上,gtid_next 是默认的 AUTOMATIC,即 GTID 在每次事务提交时自动生成。它从当前已执行的 GTID 集合(即 gtid_executed)中,找一个大于 0 的未使用的最小值作为下个事务 GTID。同时将 GTID 写入到 binlog(set gtid_next 记录),在实际的更新事务记录之前。
在 Slave 上,从 binlog 先读取到主库的 GTID(即 set gtid_next 记录),而后执行的事务采用该 GTID。
参考链接:
https://blog.51cto.com/qiuyt/2335664
修改配置文件
主库:
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
server-id = 2
log-bin = mysql_bin
gtid-mode = ON
enforce-gtid-consistency = true
从库:
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
server-id = 2
log-bin = mysql_bin
gtid-mode = ON
enforce-gtid-consistency = true
准备数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| ming |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
备份主库
[root@jun ~]# mysqldump -uroot -pming123 --all-databases --single-transaction --master-data=1 --flush-logs > all.a.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
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 restore GTIDs, pass --set-gtid-purged=OFF. To make a complete dump, pass --all-databases --triggers --routines --events.
[root@jun ~]# ls
all.a.sql all.sql anaconda-ks.cfg create httpd-2.4.37 httpd-2.4.37.tar.bz2 mysql.pam
[root@jun ~]# scp all.a.sql root@192.168.120.23:/opt
给从库授权
mysql> grant replication slave on *.* to 'ming'@'192.168.120.23' identified by 'ming123';
Query OK, 0 rows affected, 1 warning (0.00 sec)
在从库恢复数据
[root@localhost opt]# mysql -uroot -pming123 -e "source /opt/all.a.sql"
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@localhost opt]# mysql -uroot -pming123
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 3
Server version: 5.7.23-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 |
| ming |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
指向主库,gtid同步
mysql> change master to master_host='192.168.120.11',master_user='ming',master_password='ming123',mast_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.11 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.120.11
Master_User: ming
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql_bin.000004
Read_Master_Log_Pos: 154
Relay_Log_File: localhost-relay-bin.000004
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql_bin.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
//在主库添加一个jun库
mysql> create database if not exists jun;
Query OK, 1 row affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| jun |
| ming |
| mysql |
| performance_schema |
| sys |
+--------------------+
6 rows in set (0.00 sec)
//在从库查看已经同步
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| jun |
| ming |
| mysql |
| performance_schema |
| sys |
+--------------------+
6 rows in set (0.00 sec)