os: ubuntu 16.04
db: mysql 5.7.25
规划如下:
192.168.56.92 node1 # mysql master
192.168.56.90 node2 # mysql slave
192.168.56.88 node3 # mysql slave
传统的基于 binlog position 复制的方式有个严重的缺点:如果slave连接master时指定的binlog文件错误或者position错误,会造成遗漏或者重复,很多时候前后数据是有依赖性的,这样就会出错而导致数据不一致。
从MYSQL5.6开始,mysql开始支持GTID复制。GTID 的全称是 global transaction id,表示的是全局事务ID。GTID的分配方式为uuid:trans_id,
其中:
uuid是每个mysql服务器都唯一的,记录在$datadir/auto.cnf中。
如果复制结构中,任意两台服务器uuid重复的话(比如直接冷备份时,auto.conf中的内容是一致的),在启动复制功能的时候会报错。这时可以删除auto.conf文件再重启mysqld。
基于gtid复制的好处
从上面可以看出,gtid复制的优点大致有:
保证同一个事务在某slave上绝对只执行一次,没有执行过的gtid事务总是会被执行。
不用像传统复制那样保证 binlog 的坐标准确,因为根本不需要 binlog 以及 position。
故障转移到新的master的时候很方便,简化了很多任务。
很容易判断 master 和 slave 的数据是否一致。只要 master 上提交的事务在 slave 上也提交了,那么一定是一致的。
当然,MySQL提供了选项可以控制跳过某些gtid事务,防止slave第一次启动复制时执行master上的所有事务而导致耗时过久。
虽然对于 row-based 和 statement-based 的格式都能进行 gtid 复制,但建议采用 row-based 格式。
详细过程略,可以参考<
# mysql -h 127.0.0.1 -P 3306 -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.25 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, 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 version();
+-----------+
| version() |
+-----------+
| 5.7.25 |
+-----------+
1 row in set (0.00 sec)
注意 server-id 不同
# vi /etc/mysql/mysql.conf.d/mysqld.cnf
##########################
# character set
##########################
character-set-server = utf8mb4
collation-server = utf8mb4_bin
##########################
# log bin
##########################
server-id = 1
log_bin = mysql-bin
binlog_format = row
sync_binlog = 1
expire_logs_days =7
binlog_cache_size = 128m
max_binlog_cache_size = 256m
max_binlog_size = 256m
master_info_repository=TABLE
relay_log_info_repository=TABLE
gtid_mode = on
enforce_gtid_consistency = on
replicate_ignore_db=mysql
replicate_ignore_db=information_schema
replicate_ignore_db=performation_schema
replicate_ignore_db=sys
##########################
# log relay
##########################
relay_log = mysql-relay-bin
relay_log_purge = on
relay_log_recovery = on
max_relay_log_size = 1G
请注意: 这里设置了
gtid_mode = on
enforce_gtid_consistency = on;
master slave 节点都需要修改 gtid_mode 和 enforce_gtid_consistency后分别重启mysql.
# systemctl restart mysql
#
# mysql -h 127.0.0.1 -P 3306 -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.7.25-log MySQL Community Server (GPL)
Copyright (c) 2000, 2019, 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 |
+------------------+----------+--------------+------------------+----------------------------------------+
| mysql-bin.000006 | 322 | | | 2124494a-3a2b-11e9-96e1-080027c780f8:1 |
+------------------+----------+--------------+------------------+----------------------------------------+
1 row in set (0.00 sec)
注意观察 Executed_Gtid_Set 这列.
master 节点创建复制用户
mysql> create user 'repl'@'192.168.56.%' identified by 'mysqlmysql';
mysql> grant replication slave,replication client on *.* to 'repl'@'192.168.56.%';
mysql> flush privileges;
mysql> show master status\G
slave 节点设置
mysql> stop slave;
mysql>
mysql> change master to
master_host='192.168.56.92',
master_port=3306,
master_user='repl',
master_password='mysqlmysql',
master_auto_position=1,
master_connect_retry=10;
mysql> start slave\G
mysql> show slave status\G
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Retrieved_Gtid_Set: 2124494a-3a2b-11e9-96e1-080027c780f8:1
Executed_Gtid_Set: 2124494a-3a2b-11e9-96e1-080027c780f8:1
Auto_Position: 1
注意 change master to 语句不能包含 master_log_file、master_log_pos
其中 master_auto_position 默认为 0(停用),需要显式设置为 1(启用)。
master 节点上查询信息
# mysql -h 127.0.0.1 -P 3306 -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.25 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, 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 processlist;
+----+------+---------------------+------+------------------+------+---------------------------------------------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+---------------------+------+------------------+------+---------------------------------------------------------------+------------------+
| 9 | root | localhost:42936 | NULL | Query | 0 | starting | show processlist |
| 10 | repl | 192.168.56.90:54142 | NULL | Binlog Dump GTID | 175 | Master has sent all binlog to slave; waiting for more updates | NULL |
| 11 | repl | 192.168.56.88:60942 | NULL | Binlog Dump GTID | 158 | Master has sent all binlog to slave; waiting for more updates | NULL |
+----+------+---------------------+------+------------------+------+---------------------------------------------------------------+------------------+
3 rows in set (0.00 sec)
注意 Command 已经显示为 Binlog Dump GTID
参考:
https://dev.mysql.com/doc/refman/5.7/en/replication-gtids.html
https://dev.mysql.com/doc/refman/5.7/en/change-master-to.html