mysql双主配置及其注意事项

主库配置

[mysqld]
server-id       = 1
log-bin=mysql1-bin
#salve-net-timeout默认是3600秒,缩短时间是为了防止双YES的假象
slave-net-timeout=60
auto_increment_offset=2
auto_increment_increment=2
如果要指定同步或不同步哪些库,可使用如下参数
#binlog-do-db=osyunweidb   #需要同步的数据库名,如果有多个数据库,可重复此参数,每个数据库一行
#binlog-ignore-db=mysql    #不同步mysql系统数据库

从库配置

[mysqld]
server-id       = 2
log-bin=mysql2-bin
#salve-net-timeout默认是3600秒,缩短时间是为了防止双YES的假象
slave-net-timeout=60
auto_increment_offset=1
auto_increment_increment=2

主键冲突

多主和主从有一点区别:因为在多主中都有对服务器有写的权限,所以会造成主键冲突。从而导致同步失败。所以需要保证自增长的数据不同。使用auto_increment_offsetauto_increment_increment来解决。

auto_increment_offset
auto_increment_increment

这两个参数的作用:

  • 控制自增列auto_incremnet的行为
  • 用于master_master之间的复制,防止出现重复值

auto_increment_increment:自增值的自增量

auto_increment_offset: 自增值的偏移量

一般设置:

auto_increment_offset=1 偏移量从开始,依次增加

auto_increment_offset=N 有几台主服务器,就设置为N,这样就可以保证他们之间的主键不冲突。

主从同步故障解决办法:

  1. 适用于数据相差不大。要求不严格的情况。

在主库上锁表 flush tables with read lock

在从库上执行:

stop slave;
#跳过错误的步骤,可以改变后面的数字,实现多次跳转
set  global sql_slave_skip_counter =1;
start slave;
show slave status\G;
解锁表 unlock tables;

2.重做,实现完全同步。适用于要求数据完全统一的情况下:

1. 在主库上锁表
2.进行主库数据备份
3.查看master的状态
4.将备份文件拷贝到从库
######################
5.停止从库的状态
6.导入备份的数据库
7.设置主从同步
8.开启从同步
9.查看同步的状态
10.在master上解锁

不同版本做主从报错问题:

配置:

master1 mysql:5.6
    master2 mysql: 5.5
  1. 在master1做slave,master2做主数据看时成功
  2. 在master1做master,master2做从数据库时报错
Got fatal error 1236 from master when reading data from binary log:
'Slave can not handle replication events with the checksum that master is configured to log; 
the first event 'mysql-bin.000001' at 5115510, the last event read from './mysql-bin.000001' at 5115510,
the last byte read from './mysql-bin.000001' at 120.'

查询资料发现当mysql版本为5.6时:
这个错误一般出现在master5.6,slave在低版本的情况下。这是由于5.6使用了crc32做binlog的checksum;当一个event被写入binary log(二进制日志)的时候,checksum也同时写入binary log,然后event通过网络传输到从服务器(slave)之后,再在从服务器中对其进行验证并写入从服务器的relay log。

由于每一步都记录了event和checksum,所以看报错就知道是无法checksum。
解决: 在master1配置文件中设置binlog_checksum =none;重启,然后重新进行连接就好了。


转载于:https://blog.51cto.com/dianel/2092172