通过心跳线,实现主数据库(master)、从数据库(slave)数据同步
环境:centos7
master:10.70.20.78
slave:10.70.20.146
两台虚拟机分别安装MySQL
或:一套虚拟机安装MySQL后,使用虚拟机克隆功能
注意事项:
虚拟机克隆会导致,两台主从数据库的uuid相同,需要修改
1.1修改从数据库虚拟机的uuid
vim /var/lib/mysql/auto.cnf
这里谁便修改,任意一个字符
1.2重新启动从数据库
systemctl restart mysqld
2.1、(master设备)修改配置文件
vi /etc/my.cnf
2.2、(master设备)登录数据库
mysql -u root -p
2.3、创建共享用户
create user 'repl'@'%' identified by '123456'; #创建用户
grant replication slave on *.* to 'repl'@'%'; #授权用户
flush privileges;
flush tables with read lock; #锁定数据库表暂时无法写服务
2.4、记录记录binlog值
show master status; #查看binlog文件值与pos值,记住文件名和pos值
3.1、(slave设备)修改配置文件
vi /etc/my.cnf
server-id=2 #服务器的标识,要求每个节点id不一致
log-bin=mysql-bin2 #日志文件名称,每个节点名称不一致
log-slave-updates
slave-skip-errors=all
sync_binlog=1
auto_increment_increment=2 #需要将两台服务器的auto_increment_increment增长量都配置为2
auto_increment_offset=2 #把auto_increment_offset分别配置为1和2,这样可以避免两台服务器同时做更新时,自增长字段的值之间发生冲突。
max_connections = 3000
max_allowed_packet = 64M
default_authentication_plugin = mysql_native_password #MySQL8.0.4开始,默认身份认证开始改变。因为之前,MySQL的密码认证插件是“mysql_native_password”,而现在使用的是“caching_sha2_password”。
3.2、(slave设备)登录数据库
mysql -u root -p
3.3、创建共享用户
create user 'repl'@'%' identified by '123456'; #创建用户
grant replication slave on *.* to 'repl'@'%'; #授权用户
flush privileges;
flush tables with read lock; #锁定数据库表暂时无法写服务
3.4、记录记录binlog值
show master status; #查看binlog文件值与pos值,记住文件名和pos值
4、设置共享
4.1、master设备登录数据库操作
注:host配置为slave的IP,这里注意log_file与master_log_pos值都要对应对应slave的show master status;
change master to master_host='10.70.20.146', master_user='repl', master_password='123456',master_port=3306, master_log_file='mysql-bin2.000002', master_log_pos=157;
4.2、slave设备登录数据库操作
注:host配置为master的IP,这里注意log_file与master_log_pos值都要对应对应master的show master status;
change master to master_host='10.70.20.78', master_user='repl', master_password='123456',master_port=3306, master_log_file='mysql-bin1.000002', master_log_pos=834;
5、开启同步(master库、slave库)
mysql -u root -p #登录数据库
unlock tables; #解锁写
start slave; #开启同步
5.1、查看(master库)数据是否正常同步
show slave status\G #查看数据库是否正常同步
5.2、查看(slave库)数据是否正常同步
show slave status\G #查看数据库是否正常同步