把数据库192.168.4.53也配置为192.168.4.51的从服务器
配置从服务器192.168.4.53
vim /etc/my.cnf
[mysqld]
server_id=53
systemctl restart mysqld
确保和主库数据一致
#192.168.4.51 (主库数据)
mysqldump -uroot -p123456 --master-data -B db2 db1 >/root/twodb.sql
scp /root/twodb.sql [email protected]:/root
#192.168.4.53(从服务器)
mysql -uroot -p123456 < /root/twodb.sql
mysql -uroot -p123456 -e "show databases" #查看是否恢复成功
#查看主库日志信息和偏移量
grep master51 /root/twodb.sql
指定主库信息
mysql>
change master to
master_host="192.168.4.51", #主库的IP地址
master_user="repluser", #主库授权用户
master_password="123456", #授权用户密码
master_log_file='master51.000002' , #主库日志文件,这个文件在主库使用show master status; 查看日志名与偏移量
master_log_pos=441;
启动slave程序
mysql> start slave;
查看状态信息
show slave status \G;
客户端连接主服务器51访问数据
在从服务器53 本机可以查看到同样的数据
客户端
mysql -u admin -h 192.168.4.51 -p123456
insert into db1.a values(2);
insert into db1.a values(3);
insert into db2.user values(5,"alive");
从服务器:192.168.4.53
mysql>
select * from db1.a;
select * from db2.user;
在这个实验中53作为主服务器,由于53做了上个实验的从服务器,所以先清除53的从服务器配置
rm -rf /var/lib/mysql/master.info
rm -rf /var/lib/mysql/relay-log.info
rm -rf /var/lib/mysql/sql3-relay-bin.*
systemctl restart mysqld
mysql -uroot -p123456 -e "show slave status" #没有输出信息说明重置成功
#为了保持主从库数据一致,我们刚刚在53数据库进行了一些数据的备份,现在删除他们,确保数据一致
mysql -u root -p123456
drop database db1;
drop database db2;
配置步骤:
vim /etc/my.cnf
[mysqld]
server_id=53
log_bin=master53
systemctl restart mysqld
mysql -uroot -p123456
grant replication slave on *.* to repluser@"%" identified by "123456";
show master status;
因为54服务器在这里即做主库,又作从库,所以这里需要给54做主库的配置和从库的配置
友情提醒:各位开始配置时记得关闭selinux和防火墙,或者防火墙开启3306端口
配置过程
vim /etc/my.cnf
[mysqld]
server_id=54
log-bin=master54
log_slave_updates #允许级联复制,要做主从从配置,必须要有这个
systemctl restart mysqld
mysql -uroot -p123456
grant replication slave on *.* to repluser@"%" identified by "123456";
show master status; #查看主库配置
show slave status \G; #查看从库配置
change master to
master_host="192.168.4.53",
master_user="repluser",
master_password="123456",
master_log_file="master53.000001",
master_log_pos=441;
start slave;
show slave status \G;
配置步骤:
vim /etc/my.cnf
[mysqld]
server_id=55
systemctl restart mysqld
mysql -u root -p123456
show slave status;
change master to
master_host="192.168.4.54",
master_user="repluser",
master_password="123456",
master_log_file="master54.000001",
master_log_pos=441;
start slave;
show slave status \G;
测试步骤:
192.168.4.53
mysql> grant all on db1.* to tom@"%" identified by "123456";
客户端连接主服务器53,测试
mysql -h192.168.4.53 -utom -p123456
show grants;
show databases;
create database db1;
create table db1.user(id int,name char(10));
insert into db1.user values(1,"tom");
从服务器在本机查看数据
#192.168.4.54
mysql -uroot -p123456 -e "select * from db1.user"
#192.168.4.55
mysql -uroot -p123456 -e "select * from db1.user"
主主结构就是两个服务器之间互为主从
192.168.4.56是192.168.4.57的主服务器,也是192.168.4.57的从服务器
192.168.4.57是192.168.4.56的主服务器,也是192.168.4.56的从服务器
配置步骤:
#192.168.4.56
vim /etc/my.cnf
log-bin=master56
server_id=56
systemctl restart mysqld
mysql -uroot -p123456
show master status;
grant replication slave on *.* to repluser@"%" identified by "123456";
#192.168.4.57
vim /etc/my.cnf
log-bin=master57
server_id=57
systemctl restart mysqld
mysql -uroot -p123456
show master status;
#192.168.4.57
mysql>
show slave status \G;
change master to
master_host="192.168.4.56",
master_user="repluser",
master_password="123456",
master_log_file="master56.000001",
master_log_pos=154;
start slave ;
show slave status \G;
#192.168.4.56
mysql>
show slave status \G;
change master to
master_host="192.168.4.57",
master_user="repluser",
master_password="123456",
master_log_file="master57.000001",
master_log_pos=154;
start slave ;
show slave status \G;
测试步骤:
#192.168.4.56
msyql>grant all on db1.* to tom@"%" identified by "123456";
#192.168.4.57
msyql>grant all on db1.* to lucy@"%" identified by "123456";
客户端连接进行测试
#192.168.4.56
mysql -h192.168.4.56 -utom -p123456
show grants;
show databases;
create database db1;
create table db1.user( id int ,name char(10));
insert into db1.user values(1,"tom");
#192.168.4.57
mysql -h192.168.4.57 -ulucy -p123456
show grants;
show databases;
insert into db1.user values(2,"lucy");
本次实验在数据库56,57上进行,56和57在之前做过一个主主结构的主从,所以56和57,既是主库又是从库
查看是否允许动态加载模块
使用数据库管理员root用户访问服务
mysql>install plugin rpl_Semi_sync_master
SONAME "semisync_master.so"; #在主服务器上执行,rpl_Semi_sync_主服务器模块,模块名为semisync_master.so
mysql> install plugin rpl_Semi_sync_slave
SONAME "semisync_slave.so"; #在从服务器上执行
mysql> select plugin_name,plugin_Status from information_schema.plugins where plugin_name like '%semi%'; #查看
半同步复制,默认关闭
mysql>
set global rpl_semi_sync_master_enabled=1; #主服务器执行
set global rpl_semi_sync_slave_enabled=1; #从服务器执行
show variables like "rpl_semi_sync_%_enabled"; #查看
vim /etc/my.cnf
[mysqld]
#主的配置
plugin-load=rpl_semi_sync_master=semisync_master.so
rpl_semi_sync_master_enabled=1
#从的配置
plugin-load=rpl_semi_sync_slave=semisync_slave.so
rpl_semi_sync_slave_enabled=1
是master和slave两种角色时
vim /etc/my.cnf
[mysqld]
plugin-load="rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
rpl_semi_sync_master_enabled=1
rpl_semi_sync_slave_enabled=1
systemctl restart mysqld
mysql -uroot -p123456 -e'show variables like "rpl_semi_sync_%_enabled"'