- 主1
vi /etc/my.cnf
======================================================================
server-id=1 #主机ID(根据需求自行设置)
log-bin=mysql-bin # 启用二进制日志
auto-increment-increment = 2 #每次增长2
auto-increment-offset = 1 #设置自动增长的字段的偏移量
#两个可选参数(2选1):
binlog-do-db=test1 #需要同步的库
binlog-ignore-db=mysql,information_schema,performation_schema,sys #忽略不同步的库
======================================================================
重启mysql服务
/etc/init.d/mysqld restart
======================================================================
登录mysql
mysql -uroot -p
grant replication slave on *.* to 用户名@'主2IP' identified by '密码';
flush privileges;
show master status;(或show master status \G)#记录下二进制日志文件名和位置
进入主2进行操作
======================================================================
主2设置成功后
stop slave;
change master to master_host='主2IP',master_user='用户名',master_password='密码',master_log_file='mysql-bin.000001',master_log_pos=613;
(master_log_file和master_log_pos填上刚才记录下主1的二进制日志文件名和位置)
start slave;
show slave status\G(出现Slave_IO_Running: Yes Slave_SQL_Running: Yes 标识主1设置成功,即可测试主主复制)
======================================================================
主主复制配置成功,
注:主1和主2库要保持一致,主1备份还原到主2 - 主2
vi /etc/my.cnf
======================================================================
server-id=2 #主机ID(根据需求自行设置)
log-bin=mysql-bin # 启用二进制日志
auto-increment-increment = 2 #每次增长2
auto-increment-offset = 1 #设置自动增长的字段的偏移量
#两个可选参数(2选1):
binlog-do-db=test1 #需要同步的库
binlog-ignore-db=mysql,information_schema,performation_schema,sys #忽略不同步的库
======================================================================
重启mysql服务
/etc/init.d/mysqld restart
======================================================================
登录mysql
mysql -uroot -p
grant replication slave on *.* to 用户名@'主1IP' identified by '密码';
flush privileges;
show master status;(或show master status \G)#记录下二进制日志文件名和位置
stop slave;
change master to master_host='主1IP',master_user='用户名',master_password='密码',master_log_file='mysql-bin.000001',master_log_pos=613;
(master_log_file和master_log_pos填上刚才记录下主1的二进制日志文件名和位置)
start slave;
show slave status\G(出现Slave_IO_Running: Yes Slave_SQL_Running: Yes 标识主2设置成功)
返回主1进行操作
====================================================================== - 一从
vi /etc/my.cnf
======================================================================
[mysqld]
#追加以下内容
lower_case_table_names = 1 # 表名不区分大小写
server-id = 134
master_info_repository = table
relay_log_info_repository = table
======================================================================
重启mysql服务
/etc/init.d/mysqld restart
======================================================================
mysql -uroot -p
CHANGE MASTER TO
MASTER_HOST='主1IP',
MASTER_PORT=3306, MASTER_USER='用户',
MASTER_PASSWORD='密码',
MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=886 for channel '主1标识';
(master_log_file和master_log_pos填上刚才记录下主1的二进制日志文件名和位置)
CHANGE MASTER TO
MASTER_HOST='主2IP',
MASTER_PORT=3306, MASTER_USER='用户',
MASTER_PASSWORD='密码',
MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=154 for channel '主2标识';
(master_log_file和master_log_pos填上刚才记录下主2的二进制日志文件名和位置)
flush privileges;
start slave;
show slave status\G(出现Slave_IO_Running: Yes Slave_SQL_Running: Yes 标识主2设置成功) - 测试
进入随意主库,创建表,插入一条数据
CREATE TABLE `t_user` (
`id` varchar(32) NOT NULL COMMENT '主键ID',
`name` varchar(32) CHARACTER SET utf8mb4 NULL COMMENT '用户名称',
`code` varchar(32) CHARACTER SET utf8mb4 NULL COMMENT '用户编码',
`phone_number` varchar(300) CHARACTER SET utf8mb4 NULL COMMENT '电话号码',
`create_date` datetime NULL COMMENT '创建时间',
`update_date` datetime NULL COMMENT '修改时间',
PRIMARY KEY (`id`)
) COMMENT = '用户信息表';
INSERT INTO t_user (`id`, `name`, `code`, `phone_number`, `create_date`, `update_date`)
VALUES ('userId_4', '张三', '123456789', '123456789632', '2020-04-27 22:05:00', '2020-04-27 22:05:00');
从库查询,发现数据保持一致,表示主从复制成功
另一主库查询,发现数据保持一致,表示主主复制成功
以上都成功,表示两主一从搭建成功(使用方式可以选择两主中的一主对外进行写入,另外一主进行查询,一从作为备份)
【Mysql】集群配置(双主一从)
于 2021-10-26 00:24:29 首次发布