mysql 双主配置

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/w329636271/article/details/80211512

主机master1执行以下操作:


my.ini中 添加


log-bin=mysql-bin
log-bin-index=mysql-bin.index
server-id = 1  #--服务Id,和slave 的server-id不一样
sync_binlog=1
binlog_format=mixed
binlog-do-db = db1 #--做主从同步的数据库名
binlog-do-db = db2
binlog-do-db = db3
binlog-ignore-db = mysql
binlog-ignore-db = performance_schema
binlog-ignore-db = information_schema

auto-increment-offset=1
auto-increment-increment=2
log-slave-updates=on
执行命令

重启 master1


添加master2的用户权限

GRANT REPLICATION SLAVE ON *.*TO 'master2'@'127.0.0.1' IDENTIFIED BY 'master2';
 ---- 从master2 访问master1的账号密码 ip为master2服务器的ip 
GRANT REPLICATION SLAVE ON *.*TO '账号'@'Slave地址' IDENTIFIED BY '密码';//账号为slave使用的账号
FLUSH PRIVILEGES;

SHOW MASTER STATUS;

其中File和position需要在master2中用到


主机master2执行以下操作:

my.ini中 添加
log-bin=mysql-bin

server-id=2         
log-bin = mysql-bin
relay-log-index = slave-relay-bin.index
relay-log = slave-relay-bin

sync_master_info = 1
sync_relay_log = 1
sync_relay_log_info = 1

log-bin=mysql-bin
log-bin-index=mysql-bin.index
sync_binlog=1
binlog_format=mixed
binlog-do-db = db1
binlog-do-db = db2
binlog-do-db = db3
binlog-ignore-db = mysql
binlog-ignore-db = performance_schema
binlog-ignore-db = information_schema


auto-increment-offset=2 
auto-increment-increment=2
log-slave-updates=on

其中主要是 auto-increment-offset 不同 还有server-id不能相同
注释 auto-increment-offset这个为自动增长时候的初始化值  auto-increment-increment为步长.
引用
双主的配置,其实就是做两次主从,主要是解决双主自增 id 不能重复的问题,用 auto-increment-increment 和 auto-increment-offset 来避免出现混乱。

auto-increment-increment 写主Master机器的数量,aotu-increment-offset 是默认的开始值,如果是 4 太机器做主,
auto-increment-increment = 4
aotu-increment-offset 后面的值,四个机器分别是 1 2 3 4,就能避开重复。
一样重启服务器
重启mastert2


配置master2的主服务器.
CHANGE MASTER TO
MASTER_HOST='127.0.0.1',
 MASTER_USER='master2',
MASTER_PASSWORD='master2',
 MASTER_PORT=3306,
MASTER_LOG_FILE='mysql-bin.000004',
MASTER_LOG_POS=284;
 其中Master_Log_File为master1 显示的File 和position两个字段.
启动主从复制
START SLAVE;




GRANT REPLICATION SLAVE ON *.*TO 'master1'@'127.0.0.1' IDENTIFIED BY 'master1';
---- 从服务器 访问master1的账号密码

FLUSH PRIVILEGES;


SHOW MASTER STATUS




master1 执行以下操作:

CHANGE MASTER TO
MASTER_HOST='127.0.0.1',
 MASTER_USER='master1',
MASTER_PASSWORD='master1',
 MASTER_PORT=3340,
MASTER_LOG_FILE='mysql-bin.000003',
MASTER_LOG_POS=1189;

启动主从复制
START SLAVE;

配置完成.


1.其中 如果需要重置binlog的File 和 position
执行以下

RESET MASTER ;

2.其中碰见了 position无法移动 是因为没有 use db;

不好意思,我的理解有误。
不是不支持,是需要指定default database;
楼主是不是在执行语句之前执行了use语句来切换数据库,如果这样的话应该只需要
SQL code


use malldb;

重新指定一下默认的数据库就可以了。


3.显示binlog的内容

SHOW BINLOG EVENTS IN 'mysql-bin.000004';


4.flush logs;
可以让binlog 重新另起一个文件.

猜你喜欢

转载自blog.csdn.net/w329636271/article/details/80211512