站点图标 Linux-技术共享

MySQL 5.7 基于 GTID 的主从复制实践

环境准备

MySQL配置

image-20201110163302003
 

must params

server_id = 100

enforce_gtid_consistency = on

gtid_mode = on

binlog

log_bin = mysqlbin

log_slave_updates = 1

binlog_format = row

relay log

skip_slave_start = 1

 

需要注意的是,若配置多台MySQL从服务器,确保server_id唯一

 

must params

server_id = 101

enforce_gtid_consistency = on

gtid_mode = on

binlog

log_bin = mysqlbin

binlog_format = row

master_info_repository = TABLE

relay log

relay_log_info_repository = TABLE

 

 

systemctl restart mysqld

 

gtid_mode 被设置为on,即为开启成功

image-20201110165848669

若使用的是三个数据库,数据不一样,需要进行数据的备份,导入

 

备份

mysqldump --single-transaction --master-data=2 --triggers --routines --all-databases -uroot -p > all.sql

导出

mysqldump -u username -p dbname > dbname.sql

导入

mysqldump -u username -p dbname < dbname.sql

 

配置账号

创建用于复制的账号

 

mysql> create user repl@'192.168.43.%' identified by '123456Gao!';

Query OK, 0 rows affected (0.01 sec)

mysql> grant replication slave on . to repl@'192.168.43.%';

Query OK, 0 rows affected (0.06 sec)

 

进行数据库连接

 

mysql> change master to master_host = '192.168.43.54',

  -> master_user = 'repl',

  -> master_password = '123456Gao!',

  -> master_auto_position = 1

  -> ;

Query OK, 0 rows affected, 2 warnings (0.07 sec)

 

image-20201110172707416
image-20201110172725502
 

start slave;

 

查看状态

确保下图红框的两个内容状态为yes,

在主库创建数据库,然后从库参看是否正确同步的该数据库

 

主库创建

create table test_gtid;

从库查看

show databases;

 

可能遇到的问题

 

mysql -urepl -p123456Gao! -h192.168.43.54

退出移动版