如何同步两个 MySQL 表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15577073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to sync two MySQL tables?
提问by GarouDan
If I have a table (lets call it orders
) on one server of mine, named, for example, local
. And I have this same table one another server of mine, named, for example, remote
.
如果我在我的一台orders
服务器上有一张桌子(我们称之为),例如命名为local
. 我的另一台服务器也有同一张桌子,例如,名为remote
.
My problem is, what is the best way to sync these two tables?
我的问题是,同步这两个表的最佳方法是什么?
I would like a solution that replaces a registry if the local is different of the remote one. And insert the registry if it doesn't exist on the local table.
如果本地与远程不同,我想要一种替换注册表的解决方案。如果本地表上不存在注册表,则插入注册表。
I had tried using dump a dump command similar to this one, but didn't worked as expected:
我曾尝试使用 dump 类似于这个命令的转储命令,但没有按预期工作:
/usr/bin/mysqldump --defaults-file=~/my/conf.cnf --skip-opt --skip-add-locks --default-character-set=latin1 --disable-keys --no-create-db --no-create-info --dump-date --compress --quick --replace --where='date > DATE_SUB(NOW(), INTERVAL 1 DAY)' mydb orders >> /backup/myDump
How can I do this? How could I do a script to do this?
我怎样才能做到这一点?我怎么能做一个脚本来做到这一点?
采纳答案by Bill Karwin
pt-table-sync can do this: http://www.percona.com/doc/percona-toolkit/2.1/pt-table-sync.html
pt-table-sync 可以做到这一点:http: //www.percona.com/doc/percona-toolkit/2.1/pt-table-sync.html
See also other SO questions and answersmentioning pt-table-sync.
另请参阅提及 pt-table-sync 的其他 SO 问题和答案。
回答by BlackTea
I am solving similar problem of syncing two tables constantly.
我正在解决不断同步两个表的类似问题。
Among all the scripts (of which most are old) I've found this actively developed app (looks promising)
在所有脚本(其中大多数是旧的)中,我发现了这个积极开发的应用程序(看起来很有希望)
https://github.com/mrjgreen/db-sync
https://github.com/mrjgreen/db-sync
I'll try it and maybe later I'll write an example.
我会尝试一下,也许稍后我会写一个例子。
回答by Joni
Arguably the"best" way is to use MySQL replication, documented here: http://dev.mysql.com/doc/refman/5.5/en/replication.html
可以说“最好”的方法是使用 MySQL 复制,记录在这里:http: //dev.mysql.com/doc/refman/5.5/en/replication.html