1.什么是GTID?
GTID(Global Transaction ID)是对于一个已提交事务的编号,并且是一个全局唯一的编号;
GTID实际上是由UUID+TID组成的。其中UUID是一个MySQL实例的唯一标识。TID代表了该实例上已经提交的事务数量,并且随着事务提交单调递增;
#查看本数据库实例的uuid号:
[email protected] [(none)]>select @@server_uuid;
+--------------------------------------+
| @@server_uuid |
+--------------------------------------+
| 83373570-fe03-11e6-bb0a-000c29c1b8a9 |
+--------------------------------------+
#也可以通过系统层面查看uudi:
[[email protected] data]# cat /data/mysql/mysql3306/data/auto.cnf
[auto]
server-uuid=83373570-fe03-11e6-bb0a-000c29c1b8a9
#linux中可以通过uuidgen产生随机uuid,mysql中可以通过select uuid()产生;
如:
[[email protected] ~]# uuidgen
eceac2d7-4878-429b-81ca-e6aea02b1739
[email protected] [(none)]>select uuid();
+--------------------------------------+
| uuid() |
+--------------------------------------+
| bc959381-1c89-11e7-8786-000c29c1b8a9 |
+--------------------------------------+
GTID的限制
(1)不支持非事物引擎
(2)不支持create table ... select语句复制(主库直接报错)
ERROR 1786 (HY000): Statement violates GTID consistency: CREATE TABLE ... SELECT.
(3)不支持一个sql同时更新一个事物引擎和非事物引擎的表;
(4)在一个复制组中,必须要求统一开启GTID或是关闭GTID;
(5)开启GTID需要重启(5.7支持在线切换);
(6)开启GTID后就不再使用原来的传统的复制方式;
(7)对于create temporary table和drop temporary talbe语句不支持(不报错,但是也不存在表);
(8)不支持sql_slave_skip_counter;
2.环境配置
master | slave | |
数据库版本 | 5.7.16 | 5.7.16 |
IP | 192.168.91.18 | 192.168.91.20 |
serverid | 330618 | 330620 |
端口号 | 3306 | 3306 |
3.配置文件参数设置
(1)master:
配置文件中设置:
server-id = 330618
binlog_format = row
log-bin = /data/mysql3306/logs/mysql-bin
#GTID
gtid_mode=on
enforce-gtid-consistency=on
(2)slave:
配置文件中设置:
server-id = 330620
binlog_format = row
relay-log=relay-bin
relay-log-index=relay-bin.index
read_only = on
#复制进程就不会随着数据库的启动而启动
skip_slave_start=1
#如果这个从库还有从库,需要开启这个参数
log_slave_updates=0
#GTID
gtid_mode=on
enforce-gtid-consistency=on
4.主库创建用户
master:
创建rep用户:
create user [email protected]‘192.168.91.%‘ identified by ‘147258‘;
grant replication slave on *.* to [email protected]‘192.168.91.%‘;
flush privileges;
5.备份还原初始化
(1)主库备份数据库:
mysqldump -uroot -p147258 --single-transaction --master-data=2 -A > /tmp/master.sql
scp master.sql [email protected]:/tmp/
(2)把备份文件maser.sql,还原到slave:
mysql -uroot -p147258 < master.sql
#注意:
备份文件中有这么一条命令:
SET @@GLOBAL.GTID_PURGED=‘83373570-fe03-11e6-bb0a-000c29c1b8a9:1-10908‘;
在还原的时候使用,表示从库还原之后GTID就会达到10908,在10908之前的事物不需要同步过来,从10909开始同步事物;
6.从库 master to
(1)添加主库信息到从库slave:
change master to
master_host=‘192.168.91.18‘,
master_port=3306,
master_user=‘rep‘,
master_password=‘147258‘,
master_auto_position=1;
(2)启动从库
[email protected] [(none)]>start slave;
(3)查看主库信息
[email protected] [testdb]>show master status;
+------------------+----------+--------------+------------------+------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000006 | 1120 | | | f4b6894e-c7fd-11e6-aaf8-000c29aacb77:1-5 |
+------------------+----------+--------------+------------------+------------------------------------------+
(4)查看从库信息
[email protected] [(none)]>show slave hosts;
+-----------+------+------+-----------+--------------------------------------+
| Server_id | Host | Port | Master_id | Slave_UUID |
+-----------+------+------+-----------+--------------------------------------+
| 330621 | | 3306 | 330618 | 5af344c7-c861-11e6-ad80-000c290f28e2 |
| 330620 | | 3306 | 330618 | 31ba9bcb-c861-11e6-ad7f-000c29cc71ad |
+-----------+------+------+-----------+--------------------------------------+
(5)查看复制状态
slave:
[email protected] [testdb]>show slave status\G
7.测试
master:
[email protected] [(none)]>use testdb;
[email protected] [testdb]>create table t1(id int,name char(10));
[email protected] [testdb]>insert into t1 values(1,‘aaa‘),(2,‘bbb‘);
slave:
[email protected] [testdb]>select * from t1;
+------+------+
| id | name |
+------+------+
| 1 | aaa |
| 2 | bbb |
+------+------+