这是使用mysql实时双向同步动态数据的最佳方式 [英] Which is the best way to bi-directionally synchronize dynamic data in real time using mysql

查看:198
本文介绍了这是使用mysql实时双向同步动态数据的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是场景。 2 web服务器在两个不同的位置有两个mysql数据库具有相同的表。表中的数据也预期实时相同。

Here is the scenario. 2 web servers in two separate locations having two mysql databases with identical tables. The data within the tables is also expected to be identical in real time.

这里是问题。如果任一位置中的用户同时将新记录输入到相同的表中,如下面的两个第一表所示,其中每个表中的第三记录已由不同的人同时输入。表中的数据不再相同。这是保持数据保持实时相同的最佳方式,如下表所示,无论更新发生在哪里?这样,在下面的插图中,而不是在每个表中结束3行,新记录被双向复制,它们被插入两个表中,以创建2个相同的表,这次有4列?

Here is the problem. if a user in either location simultaneously enters a new record into identical tables, as illustrated in the two first tables below, where the third record in each table has been entered simultaneously by the different people. The data in the tables is no longer identical. Which is the best way to maintain that the data remains identical in real time as illustrated in the third table below regardless of where the updates take place? That way in the illustrations below instead of ending up with 3 rows in each table, the new records are replicated bi-directionally and they are inserted in both tables to create 2 identical tables again with 4 columns this time?

Server A in Location A
==============

Table Names
| ID| NAME  |
|-----------|
| 1 | Tom   |
| 2 | Scott |
|-----------|
| 3 | John  |
|-----------|

Server B in Location B
==============
Table Names
| ID| NAME  |
|-----------|
| 1 | Tom   |
| 2 | Scott |
|-----------|
| 3 | Peter |
|-----------|


Expected Scenario
===========
Table Names
| ID| NAME  |
|-----------|
| 1 | Tom   |
| 2 | Scott |
| 3 | Peter |
| 4 | John  |
|-----------|


推荐答案

复制没有太多的性能你的数据库在两个主人。

There isn't much performance to be gained from replicating your database on two masters. However there is a nifty bit of failover if you code your application correct.

Master-Master设置与Slave-Master设置基本相同,但两个从设备都已启动

Master-Master setup is essentially the same as the Slave-Master setup, but has both Slaves started and an important change to your config files on each box.

主MySQL 1:

auto_increment_increment = 2
auto_increment_offset = 1 



Master MySQL 2:

auto_increment_increment = 2
auto_increment_offset = 2

这两个参数确保当两个服务器由于某种原因而争用主键时,它们不会复制并终止复制。不是递增1,任何自动递增字段将默认递增2.在一个框上,它将从1开始偏移并运行序列1 3 5 7 9 11 13等。在第二个框上,它将开始偏移2并沿着2 4 6 8 10 12等运行。从当前测试,自动增量似乎采取下一个自由数,而不是之前剩下的一个。例如。如果服务器1插入前3条记录(1 3和5),当服务器2插入第4个时,将给出6的键(不是2,不使用)。

These two parameters ensure that when two servers are fighting over a primary key for some reason, they do not duplicate and kill the replication. Instead of incrementing by 1, any auto-increment field will by default increment by 2. On one box it will start offset from 1 and run the sequence 1 3 5 7 9 11 13 etc. On the second box it will start offset at 2 and run along 2 4 6 8 10 12 etc. From current testing, the auto increment appears to take the next free number, not one that has left before. E.g. If server 1 inserts the first 3 records (1 3 and 5), when Server 2 inserts the 4th, it will be given the key of 6 (not 2, which is left unused).

一旦你设置了,启动它们作为奴隶。
然后检查两个都工作正常,连接到这两个机器,并执行命令 SHOW SLAVE STATUS ,你应该注意, Slave_IO_Running Slave_SQL_Running 应对每个框都说YES。

Once you've set that up, start both of them up as Slaves. Then to check both are working ok, connect to both machines and perform the command SHOW SLAVE STATUS and you should note that both Slave_IO_Running and Slave_SQL_Running should both say "YES" on each box.

当然,在表中创建几个记录,并确保一个框只插入奇数主键,另一个框只递增偶数主键。

Then of course, create a few records in a table and ensure one box is only inserting odd numbered primary keys and the other is only incrementing even numbered ones.

然后进行所有测试,以确保您可以在每个框上执行所有标准应用程序,并将其复制到另一个。

Then do all the tests to ensure that you can perform all the standard applications on each box with it replicating to the other.

这是相对简单的一旦它走了。
但是正如已经提到的,MySQL确实不鼓励它,并建议您在编写应用程序代码时注意这一功能。

It's relatively simple once it's going. But as has been mentioned, MySQL do discourage it and advise that you ensure you are mindful of this functionality when writing your application code.

编辑:如果你确保偏移是正确的等等,理论上可以添加更多的主器件。你可能更切合实际,添加一些额外的奴隶。

I suppose it's theoretically possible to add more masters if you ensure that the offsets are correct and so on. You might more realistically though, add some additional slaves.

这篇关于这是使用mysql实时双向同步动态数据的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆