MySQL使用Replication实现主主同步复制【20210719】

发表于 2021-07-17
阅读 66

介绍

介绍

福哥今天打算给大家讲一讲MySQL的Replication功能,使用Replication功能来实现双主机相互同步复制数据,可以大大提高系统的健壮性,两台主机都可以用来作为数据写入服务器使用。

主主同步复制的配置难度远远超过主从同步复制,因为两台机器都会相互同步对方的用户提交的操作,一个不小心有可能会造成两边的机器的数据都被清空的可怕情况出现。

配置

配置文件里面必须包含的参数包括:

  • server_id

  • log-bin

  • log-bin-index

  • binlog-do-db

  • auto_increment_increment

  • auto_increment_offset

配置文件里面可选的参数包括:

  • relay-log

  • relay-log-index

  • expire_logs_days

  • max_binlog_size

  • binlog-format

  • slave-skip-errors

  • master_info_repository

  • relay_log_info_repository

配置文件里面的server_id必须是唯一的,也就是说每个MySQL实例都必须设置一个唯一的server_id。

主机A

这是主机A的配置文件。

server_id=131415001
log-bin=mysql-bin
log-bin-index=mysql-bin.index
relay-log=relay-bin
relay-log-index=relay-bin.index
binlog-do-db=test
expire_logs_days=10
max_binlog_size=1G
binlog-format=row
auto_increment_increment=2
auto_increment_offset=1
slave-skip-errors=all
master_info_repository=TABLE
relay_log_info_repository=TABLE

主机B

这是主机B的配置文件。

server_id=131415002
log-bin=mysql-bin
log-bin-index=mysql-bin.index
relay-log=relay-bin
relay-log-index=relay-bin.index
binlog-do-db=test
expire_logs_days=10
max_binlog_size=1G
binlog-format=row
auto_increment_increment=2
auto_increment_offset=2
slave-skip-errors=all
master_info_repository=TABLE
relay_log_info_repository=TABLE

授权

为了方便福哥的grant语句面对任意客户端开放了,大家实际使用环境可以根据自己的情况设置授权客户端主机,或者通过网络端口控制客户端访问。

主机A

主机A添加授权同步用户sync_user。

grant replication slave, replication client on *.* to sync_user@`%` identified by 'sync_pass';

主机B

主机B添加授权同步用户sync_user。

grant replication slave, replication client on *.* to sync_user@`%` identified by 'sync_pass';

操作

停止主机A同步

首先避免出现问题,我们先把主机A从其他主机同步的进程停止了。

stop slave;

停止主机B同步

首先避免出现问题,我们还要把主机B从其他主机同步的进程停止了。

stop slave;

设置主机A同步参数

设置主机A从那个主机同步,包括主机、端口、用户名、密码等等。

change master to master_host = 'hostB', master_port = 3306, master_user = 'sync_user', master_password = 'sync_pass';

设置主机B同步参数

设置主机B从那个主机同步,包括主机、端口、用户名、密码等等。

change master to master_host = 'hostA', master_port = 3306, master_user = 'sync_user', master_password = 'sync_pass';

从主机A导出同步数据

现在从主机A导出一份完整的数据,记得增加锁表参数避免数据差异。

mysqldump -uroot -pabcdef -h hostA test > test-utf8-210717.sql --default-character-set=utf8 --extended-insert=false --master-data --lock-all-tables

导入同步数据导主机B

主机A导出的数据传到主机B上面,将数据导入进来。

重新创建test库

如果主机B没有test库就创建它,如果主机B有test库就先删除了再重写创建它。

drop database if exists test;
create database test default charset utf8;

导入数据到test库

主机B上面导入同步数据到test库。

mysql -uroot -pabcdef -h hostB test < test-utf8-210717.sql --default-character-set=utf8

完成A到B的同步

现在到主机B上面执行启动同步命令。

start slave;

查看主机B的日志信息

主机B上面执行查看日志信息命令。

show master status;

home/topic/2021/0719/12/21796b8c34c7eccd0dc15549039be9f7.png

设置主机A的同步信息

使用主机B的日志信息配置主机A的同步信息。

change master to master_log_file = 'mysql-bin.000003', master_log_pos = 2228;

完成B到A的同步

现在到主机A上面执行启动同步命令。

start slave;

测试

测试主机A到主机B

主机A上面插入一条数据。

home/topic/2021/0719/12/9f67655c4dd24011f777e3b69faf19b9.png

主机B上面可以看到这条数据。

home/topic/2021/0719/12/258b35281ae9cb5321813b99f4d61e21.png

测试主机B到主机A

主机B上面插入一条数据。

home/topic/2021/0719/12/5654e941c94eb2a4822ccb7e595dd88d.png

主机A上面可以看到这条数据。

home/topic/2021/0719/12/cdf97e58247b3ac15644df5d3d10be4e.png

总结

今天福哥带着童鞋们学习了使用MySQL的Replication功能实现了两台MySQL服务器之间双向同步复制数据的功能配置,主主同步的优势就是任意一个机器坏了都可以立即用另外一个机器顶上。

鬼谷子叔叔

跟福哥学编程吧~~
日志
212
浏览
1626