数据库主从复制实战
2019-01-22 本文已影响13人
朱溪江
环境:准备四台虚拟机
vim /etc/hosts
master1 10.18.46.80
master2 10.18.46.81
slave1 10.18.46.82
slave1 10.18.46.83
一、一主一从(1)
主:master1 作为主mysql,master2作为从mysql
修改mysql 主配置文件
1、准备数据 主master1:
create database master1db;
create table master1db.master1tab(name char(50));
insert into master1db.master1tab values (1111);
2、开启二进制日志 主master1:
vim /etc/my.cnf
log_bin
server-id=1
systemctl restart mysqld 重启生效
3、创建授权用户 master1:
grant replication slave, replication client on *.*
to 'rep'@'192.168.239.%' identified by 'QianFeng@123';
4、备份master数据库的数据 master1:
mysqldump -p'Zhutong@123' --all-databases --single-transaction --master-data=2 \
--flush-logs > `date +%F`-msyql-all.sql
scp -r 2019-01-22-mysql-all.sql master2:/tmp 发送给另一台主机
查看二进制日志分割点:
CHANGE MASTER TO MASTER_LOG_FILE='localhost-bin.000002', MASTER_LOG_POS=154;
5、准备数据 备份完之后 主数据库继续有数据写入
insert into master1db.master1tab values (33333333);
insert into master1db.master1tab values (44444);
从 master2:
测试rep用户是否可用:msyql -h master1 -urep -p'Zhutong@123'
1、配置从服务器序号
vim /etc/my.cnf
server-id=2
systemctl restart mysqld 重启数据库使配置生效
2、恢复数据 和主master1同步
mysql> set sql_log_bin=0;
mysql> source /tmp/2019-01-22-mysql-all.sql
3、设置主服务器:
mysql> change master to
master_host='master1',
master_user='rep',
master_password='Zhutong@123',
master_log_file='localhost-bin.000002',
master_log_pos=154;
注意:二进制日志的位置,是备份文件的位置
4、启动从设备
mysql > start slave;
5、查看启动状态(IO-YES/SQL-YES)
mysql > show slave status\G;
至此,在主服务器(master1)更新数据,观察从服务器(master2)是否变化
一主一从(2)
实验2与上一个实验需求基本相同经。master1 作为主mysql,master2 作为从mysql。
不同之处,使用了
“gtid_mode=ON
enforce_gtid_consistency=1”
该属性自动记录position位置。不需要手动指定了。
环境:重置master2数据库
systemctl stop mysqld
rm -rf /var/lib/mysql/*
systemctl restart mysqld
grep password /var/log/mysqld.log
进入数据库修改密码为'Zhutong@123'
主master1:
1、启动二进制日志,服务器id,gtid
vim /etc/my.cnf
log_bin
server-id=1
gtid_mode=ON
enforce_gtid_consistency=1
systemctl restart mysqld 重启数据库使配置生效
2、授权复制用户rep
mysql > grant replication slave,replication client on *.* to 'rep'@'192.168.239.%' identified by 'Zhutong@123';
flush privileges;
3、备份master数据库的数据 master1:
mysqldump -p'Zhutong@123' --all-databases --single-transaction --master-data=2 \
--flush-logs > `date +%F`-mysql-all.sql
scp -r 2019-01-22-mysql-all.sql master2:/tmp 发送给另一台主机
4、#模拟备份点到灾难点的数据库变更操作
insert into master1db.master1tab values(666666);
从master2:
1、测试rep 用户是否可用
mysql -h master1 -urep -p'Zhutong@123'
预防账户问题,防火请和selinux必须关闭,否则会失败
2、启动二进制日志,服务器ID,GTID
vim /etc/my.cnf
log_bin
server-id=2
gtid_mode=ON
enforce_gtid_consistency=1
systemctl restart mysqld 测试配置是否有问题
3、手动同步数据
mysql > set sql_log_bin=0;
mysql > source /tmp/2019-01-22-mysql-all.sql;
mysql > select * from master1db.master1tab ; 此时的数据会更新为备份点之后最新的数据,区别于第一种一主一从(备份完更新的数据没有,得从二进制的日志备份点导入)
4、设置主服务器
mysql> change master to
master_host='master1',
master_user='rep',
master_password='Zhutong@123',
master_auto_position=1;
start slave;
show slave status\G;
5、 返回主服务器(master1)更新数据,在从服务器(master2)观察是否同步。
二、双主(互为主从)增加写服务器的利用率 一写一主只有%50的写效率 而双主%100
目前:已经设置master1为master2的主服务器,
只需设置master2为master1的主服务器.
1、设置 master2为master1的主服务器
在mster2上进行授权 :
mysql > grant replication slave,replication client on *.* to 'rep'@'192.168.239.%' identified by 'Zhutong@123';
mysql > flush privileges;
master1:
mysql> change master to
master_host='master2',
master_user='rep',
master_password='Zhutong@123',
master_auto_position=1;
start slave;
show slave status\G;
如果状态不佳,重启以下master2的mysqld
再去master2上。启动和停止从属状态
start slave
stop slave
测试:双方同步成功,双主设置完成
三、双主双从
1、首先同步四台数据库 因为已经两台双主了,所以只需要备份任意一台master1/2 上的数据即可
master1 :
mysqldump -p'Zhutong@123' --all-databases --single-transaction --master-data=2 \
--flush-logs > `date +%F`-mysql-all.sql
scp -r 2019-01-22-mysql-all.sql slave1:/tmp/
scp -r 2019-01-22-mysql-all.sql slave2:/tmp/
slave1: mysql -p'Zhutong@123' </tmp/2019-01-22-mysql-all.sql
slave2: mysql -p'Zhutong@123' </tmp/2019-01-22-mysql-all.sql
2、启动从服务器ID,GTID:
slave1:
vim /etc/my.cnf
[mysqld]
server-id=3
gtid_mode=ON
enforce_gtid_consistency=1
master-info-repository=TABLE
relay-log-info-repository=TABLE
systemctl restart mysqld
slave2:
vim /etc/my.cnf
[mysqld]
server-id=4
gtid_mode=ON
enforce_gtid_consistency=1
master-info-repository=TABLE
relay-log-info-repository=TABLE
systemctl restart mysqld
3、设置主服务器:
slave1:
mysql> change master to
master_host='master1',
master_user='rep',
master_password='Zhutong@123',
master_auto_position=1 for channel 'master1';
mysql> change master to
master_host='master2',
master_user='rep',
master_password='Zhutong@123',
master_auto_position=1 for channel 'master2';
start slave;
show slave status\G;
slave2:
mysql> change master to
master_host='master1',
master_user='rep',
master_password='Zhutong@123',
master_auto_position=1 for channel 'master1';
mysql> change master to
master_host='master2',
master_user='rep',
master_password='Zhutong@123',
master_auto_position=1 for channel 'master2';
start slave;
show slave status\G;