「MySQL」- 搭建主从复制(基于 GTID 复制)

  CREATED BY JENKINSBOT

第一步、修改主库配置

主库配置文件:

[mysqld]
gtid_mode=ON
enforce-gtid-consistency=ON

log_bin = /var/log/mysql/binlog
binlog_format = ROW
expire_logs_days = 30
max_binlog_size = 500M
sync_binlog = 1

创建用于复制的用户,从库将使用该用户进行复制:

GRANT REPLICATION SLAVE ON *.* TO 'replication'@'%' IDENTIFIED BY 'XXXXXXXX';

至此启动数据服务即可。

第二步、修改从库配置

从库配置文件:

[mysqld]
gtid_mode=ON
enforce-gtid-consistency=ON

启动数据库服务。在启动从库时,应该指定 –skip-slave-start 选项,因为此时从库还没有指定好复制参数。

第三步、启用从库复制

CHANGE MASTER TO master_host = 'host', master_port = 'port',
    master_user = 'username', master_password = 'password',
    MASTER_AUTO_POSITION = 1;

START SLAVE;

参考文献

MySQL 5.7 Reference Manual/Setting Up Replication Using GTIDs