mysql 双主(互为主从)


先要确保两个数据库的数据一致

1、master1上操作,修改my.cnf配置文件

[mysqld]
#必须唯一
server_id = 1
#开启binlog日志
log-bin = mysql-bin
relay-log=relay-bin
#要进行主从的数据库
binlog-do-db=coin
#指定自增id值从几开始
auto_increment_offset=1
#指定自增值的步长
auto_increment_increment=2
#设置清除log的天数
expire_logs_days=30

2、在master1上操作: 创建同步账号并授权 (此账号会给到master2做slave账号)


CREATE USER 'repl'@'%' IDENTIFIED WITH 'mysql_native_password' BY '123456';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';

flush privileges;

3、master1上操作:记下master1上的bin-log位置(此位置会给到master2做为同步起始位置)

show master status;

结果如下:

4、master2上操作,修改my.cnf配置文件

#必须唯一
server_id = 2
#开启binlog日志
log-bin = mysql-bin
relay-log=relay-bin
#要进行主从的数据库
binlog-do-db=coin
#指定自增id值从几开始
auto_increment_offset=2
#指定自增值的步长
auto_increment_increment=2
#设置清除log的天数
expire_logs_days=30

5、在master2上操作: 创建同步账号并授权 (此账号会给到master1做slave账号)

grant replication slave on *.* to 'replication'@'%';
grant replication slave on *.* to 'replication'@'%' identified by '123456';

flush privileges;

6、master2上操作:记下master1上的bin-log位置(此位置会给到master2做为同步起始位置)

show master status;

结果如下:

7、在master2上操作:master2作为从库备份master1的数据

stop slave;
change master to master_host='192.168.50.49',master_user='replication',master_password='123456',master_port=4301,master_log_file='mysql-bin.000001',master_log_pos=154;
start slave;

用  show slave status\G查看同步状态

8、在master1上操作:master1作为从库备份master2的数据

stop slave;
change master to master_host='192.168.50.49',master_user='replication',master_password='123456',master_port=4302,master_log_file='mysql-bin.000001',master_log_pos=606;
start slave;

用  show slave status\G查看同步状态

查看binlog日志指令

show binlog events [IN 'log_name'] [FROM pos] [LIMIT [offset,] row_count];

例如  show binlog events in 'mysql-bin.000012' FROM 12664382 limit 50;

设置binlog日志有效期(自动清理binlog日志)

show variables like 'expire_logs_days';

set global expire_logs_days = 30; #设置binlog多少天过期

跳过错误:

stop slave;
SET GLOBAL SQL_SLAVE_SKIP_COUNTER =1;
start slave;

show slave status\G

posted @   abowu  阅读(202)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示