使用pt-table-sync进行双向同步

时间:2013-10-09 03:33:42

标签: mysql synchronization

我正在尝试使用以下命令双向同步两个数据库(一个本地和一个远程):

pt-table-sync --print --bidirectional --conflict-column * 
--conflict-comparison newest --databases my_db 
h=localhost,u=root,p=my_pass, h=ip_remote_server

显示错误:

DBI connect(';host=Aplicaciones;mysql_read_default_group=client','',...) 
failed: Unknown MySQL server host 'Aplicaciones' (2) at 
/usr/bin/pt-table-sync line 2208

他们拥有与here所说明的数据库相同的用户名和密码?

我不理解documentation

我希望你能帮助我。感谢。

1 个答案:

答案 0 :(得分:2)

最后我成功了。

1 .- 用户名,密码和数据库名称必须相同。

2 .- 授予用户远程连接到其他服务器的权限。您可以关注this guide

3 .- 运行以下脚本:

服务器本地:

GRANT SELECT, REPLICATION CLIENT, SHOW DATABASES, SUPER, PROCESS ON *.* TO 'your_user'@'localhost' IDENTIFIED BY 'your_password';

服务器远程:

GRANT SELECT, REPLICATION CLIENT, SHOW DATABASES, SUPER, PROCESS ON *.* TO 'your_user'@'your_real_ip_local' IDENTIFIED BY 'your_password';

4 .- 测试案例:表'颜色'

服务器本地:

+----------+--------+--------+---------+---------------------+
| id_color | name   | status | deleted | date_register       |
+----------+--------+--------+---------+---------------------+
|        1 | Negro  |      1 |       0 | 2012-01-26 00:35:19 |
|        2 | Blue   |      1 |       0 | 2012-01-26 00:35:19 |
|        3 | Gray   |      1 |       0 | 2012-01-26 00:35:19 |
|        4 | Silver |      1 |       0 | 2012-01-26 00:35:19 |
|        5 | Tan    |      1 |       0 | 2012-01-26 00:35:19 |
|        6 | White  |      1 |       0 | 2012-04-05 14:14:37 |
+----------+--------+--------+---------+---------------------+

服务器远程:

+----------+----------+--------+---------+---------------------+
| id_color | name     | status | deleted | date_register       |
+----------+----------+--------+---------+---------------------+
|        1 | Black    |      1 |       0 | 2012-01-26 00:35:19 |
|        2 | Blue     |      1 |       0 | 2012-01-26 00:35:19 |
|        3 | Gray     |      1 |       0 | 2012-01-26 00:35:19 |
|        4 | Silver   |      1 |       0 | 2012-01-26 00:35:19 |
|        5 | Tan      |      1 |       0 | 2012-01-26 00:35:19 |
|        6 | White    |      1 |       0 | 2012-04-05 14:14:37 |
|        7 | Amarillo |      1 |       0 | 2013-10-19 01:25:08 |
+----------+----------+--------+---------+---------------------+

5 .- 运行命令:

$ pt-table-sync --print --bidirectional --conflict-column 'name' --conflict-comparison newest h=ip_your_server_remote,u=your_user,p=your_password,D=your_db,t=color h=localhost

6 .- 结果:

/*ip_your_server_remote*/ UPDATE `your_db`.`color` SET `name`='Negro', `status`='1', `deleted`='0', `date_register`='2012-01-26 00:35:19' WHERE `id_color`='1' LIMIT 1;
/*localhost*/ INSERT INTO `your_db`.`color`(`id_color`, `name`, `status`, `deleted`, `date_register`) VALUES ('7', 'Amarillo', '1', '0', '2013-10-19 01:25:08');

要直接执行结果,您可以通过选项--print更改选项--execute

我希望它对某人有用。