Home > Database > Mysql Tutorial > body text

mysql 双向同步的键值冲突问题的解决方法分享

WBOY
Release: 2016-06-07 18:05:34
Original
962 people have browsed it

多主互备和主从复制有一些区别,因为多主中都可以对服务器有写权限,所以设计到自增长重复问题

出现的问题(多主自增长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.cnf上加入参数

auto_increment_offset = 1
auto_increment_increment = 2
这样A的auto_increment字段产生的数值是:1, 3, 5, 7, …等奇数ID了

B:my.cnf上加入参数

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 重复。
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!