Master-A:192.168.15.233
Master-B:192.168.15.235
2:master服务器上授权用户
Master-A
mysql> grant replication slave,file on *.* to 'repl'@'192.168.15.235' identified by 'repl';
Master-B
mysql> grant replication slave,file on *.* to 'repl'@'192.168.15.233' identified by 'repl';
3:配置master的my.cf文件
配置master-A的my.cf文件
server-id = 1
log-bin=mysql-bin
binlog-do-db=mobi
binlog-ignore-db=mysql
binlog-ignore-db=test
log-slave-updates
slave-skip-errors
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1
配置master-B的my.cf文件
server-id = 2
log-bin=mysql-bin
binlog-do-db=mobi
binlog-ignore-db=mysql
binlog-ignore-db=test
log-slave-updates
slave-skip-errors
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=2
参数说明:
多主互备和主从复制有一些区别,因为多主中 都可以对服务器有写权限,所以设计到自增长重复问题
出现的问题(多主自增长ID重复)
1:首先我们通过A,B的test表结构
2:掉A,在B上对数据表test(存在自增长ID)执行插入操作,返回插入ID为1
3:后停掉B,在A上对数据表test(存在自增长ID)执行插入操作,返回的插入ID也是1
4:然后 我们同时启动A,B,就会出现主键ID重复
解决方法:
我们只要保证两台服务器上插入的自增长数据不同就可以了
如:A查奇数ID,B插偶数ID,当然如果服务器多的话,你可以定义算法,只要不同就可以了
在这里我们在A,B上加入参数,以实现奇偶插入
A:my.ini上加入参数
auto_increment_offset = 1
auto_increment_increment = 2
这样A的auto_increment字段产生的数值是:1, 3, 5, 7, …等奇数ID了
B:my.ini上加入参数
auto_increment_offset = 2
auto_increment_increment = 2
这样B的auto_increment字段产生的数值是:2, 4, 6, 8, …等偶数ID了
可以看出,你的auto_increment字段在不同的服务器之间绝对不会重复,所以Master-Master结构就没有任何问题了。当然,你还可以使用3台,4台,或者N台服务器,只要保证auto_increment_increment = N 再设置一下auto_increment_offset为适当的初始值就可以了,那样,我们的MySQL可以同时有几十台主服务器,而不会出现自增长ID重复。
在这里我们说的是2台MYSQL服务器,你也可以扩展到多台,实现方法类似
A -> B -> C-> D ->A
这样一个环形的备份结构就形成了,最后可要记住 自增长ID(主键)要设计好哦,否则会出错的。
4:master-A 和master-B锁表库
mysql> flush tables with read lock\G (锁库表)
master-A 和master-B下分别显示库信息
master-A:
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 | 180 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
master-B:
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000002 | 189| | |
+------------------+----------+--------------+------------------+
5:设置master change to
设置连接MASTER MASTER_LOG_FILE为对方库的File,MASTER_LOG_POS为对方库的Position
master-A:
mysql> slave stop;
mysql> change master to
-> master_host='192.168.15.233',
-> master_user='repl,
-> master_password='repl',
-> master_log_file=' mysql-bin.000002',
-> master_log_pos=189;
mysql> slave start;
master-B:
mysql> slave stop;
mysql> change master to
-> master_host='192.168.15.253',
-> master_user='repl,
-> master_password='repl',
-> master_log_file=' mysql-bin.000002',
-> master_log_pos=189;
mysql> slave start;
测试完成!
转自:http://88fly.blog.163.com/blog/static/122680390201132815747901/