十五、MySQL--主主复制
什么是主主复制?主主复制:两台机器互为主从关系主主复制实验项目名称:构建一个简单的主主复制集群项目环境:2c/4G的服务器(虚拟机),CentOS Linux release 7.9.2009 (Core)、MySQL5.7.34提示:masterA用的主机名叫master,master用的主机名为slave项目步骤:0.准备好两台安装好的机器,使用脚本一键部署好二进制的MySQLmaster:1
什么是主主复制?
主主复制:两台机器互为主从关系
主主复制实验
项目名称:构建一个简单的主主复制集群
项目环境:2c/4G的服务器(虚拟机),CentOS Linux release 7.9.2009 (Core)、MySQL5.7.34
提示:masterA用的主机名叫master,master用的主机名为slave
项目步骤:
0.准备好两台安装好的机器,使用脚本一键部署好二进制的MySQL
master:192.168.2.129
slave:192.168.2.204
1.关闭防火墙:
service firewalld stop
systemctl disable firewalld
2.在masterA机器上做一个完全备份,发给masterB恢复,从而达到数据初始一致
[root@master backup]# mysqldump -uroot -p'Sanchuang123#' --all-databases > /backup/all_db.sql
[root@master backup]# scp /backup/all_db.sql root@192.168.2.204:/root
[root@slave ~]# mysql -uroot -p'Sanchuang123#' < /root/all_db.sql
3.配置授权账户
在masterA上配置授权账户:
root@(none) 16:44 mysql>grant replication slave on *.* to 'masterA'@'%' identified by '123456';
在masterB上配置授权账户:
root@(none) 16:44 mysql> grant replication slave on *.* to 'masterB'@'%' identified by '123456';
4.配置my.cnf文件masterA:
[mysqld]
socket=/data/mysql/mysql.sock
log_bin
server_id = 1
auto-increment-offset=1
auto-increment-increment=2
port = 3306
open_files_limit = 8192
innodb_buffer_pool_size = 512M
character-set-server=utf8
masterB:[mysqld]
socket=/data/mysql/mysql.sock
log_bin
server_id = 2
auto-increment-offset=2
auto-increment-increment=2
port = 3306
open_files_limit = 8192
innodb_buffer_pool_size = 512M
character-set-server=utf8
5.重新启动两部机器的mysql服务service mysqld restart
6.拉取二进制日志的用户信息和日志文件的名称和位置号,启动slave服务
masterA作为从服务器拉取masterB的信息,在masterB上输入:
CHANGE MASTER TO MASTER_HOST='192.168.2.129',
MASTER_USER='masterB',
MASTER_PASSWORD='123456',
MASTER_PORT=3306,
MASTER_LOG_FILE='master-bin.000002',
MASTER_LOG_POS=194;
root@(none) 17:09 mysql>start slave;
Query OK, 0 rows affected (0.00 sec)root@(none) 17:09 mysql>show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.2.204
Master_User: masterB
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: slave-bin.000002
Read_Master_Log_Pos: 485
Relay_Log_File: master-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: slave-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
masterB作为从服务器拉取masterA的信息:
CHANGE MASTER TO MASTER_HOST='192.168.2.204',
MASTER_USER='masterA',
MASTER_PASSWORD='123456',
MASTER_PORT=3306,
MASTER_LOG_FILE='slave-bin.000002',
MASTER_LOG_POS=194;
root@(none) 17:03 mysql>start slave;
Query OK, 0 rows affected (0.00 sec)root@(none) 17:04 mysql>show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.2.129
Master_User: masterA
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000002
Read_Master_Log_Pos: 194
Relay_Log_File: slave-relay-bin.000002
Relay_Log_Pos: 321
Relay_Master_Log_File: master-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes7.验证效果
两台机器数据一致
在masterA中对t1表插入一条数据
在masterB中删除几条数据
可以看出masterA和masterB对表的操作共同影响了数据库,这就是主主复制成功的效果
更多推荐
所有评论(0)