简体   繁体   English

如何在不同数据库中的两个表之间同步数据

[英]How to synchronize data between two tables in different databases

My problem statement is as below:我的问题陈述如下:

So, we have a system A where we get things from system C.所以,我们有一个系统 A,我们从系统 C 获取东西。 Records coming in from system C are dumped into database of system A. One main aspect of that incoming record is it has Status field with values as (Synchronised, Not Available, Unable to synchronize).从系统 C 传入的记录被转储到系统 A 的数据库中。传入记录的一个主要方面是它的状态字段的值为(已同步、不可用、无法同步)。

Also, we have system B in place where records from system A are saved.此外,我们有系统 B 用于保存系统 A 的记录。

Now the main point here is as soon as there is update in system A's records status the same should be updated in system B's database table.现在这里的要点是,一旦系统 A 的记录状态有更新,系统 B 的数据库表中也应该更新。

Hence whenever client queries for data from system B it will have the most recent status.因此,每当客户端从系统 B 查询数据时,它将具有最新状态。

JUST FYI: I'm working in .NET Core Web API based projects both system A & B are based on that technology and databases for those individual projects are based on MySQL. JUST FYI: I'm working in .NET Core Web API based projects both system A & B are based on that technology and databases for those individual projects are based on MySQL.

So far I'm still in research mode and till now i have explored on Dotmim sync framework, Trigger based approach , The best technology to synchronize data between different database schemas?到目前为止,我仍处于研究模式,直到现在我已经探索了 Dotmim 同步框架、 基于触发器的方法在不同数据库模式之间同步数据的最佳技术?

Haven't found such concrete and best solution as of now and looking for some fresh pointers or suggestions.到目前为止还没有找到如此具体和最佳的解决方案,并正在寻找一些新的指针或建议。

Any help is welcomed.欢迎任何帮助。

Would be great to see the code piece or know about the things that you used很高兴看到代码片段或了解您使用的东西

Note that below answer works for same definition of the table on both servers.请注意,以下答案适用于两台服务器上表的相同定义。

On Master Server.在主服务器上。

Create a replication user with replication grant access:创建具有复制授权访问权限的复制用户:

mysql> select user,host from mysql.user where user='replication_ip_log_2';
+----------------------+---------------+
| user                 | host          |
+----------------------+---------------+
| replication_ip_log_2 | xxx.xx.xx.xx |
+----------------------+---------------+

mysql> show grants for `replication_ip_log_2`@`xxx.xx.xx.xx`;
+--------------------------------------------------------------------------+
| Grants for replication_ip_log_2@xxx.xx.xx.xx                            |
+--------------------------------------------------------------------------+
| GRANT REPLICATION SLAVE ON *.* TO 'replication_ip_log_2'@'xxx.xx.xx.xx' |
+--------------------------------------------------------------------------+


FLUSH TABLES WITH READ LOCK; --- Carefully this will lock all tables 
SHOW MASTER STATUS; --- And copy the values of the result of the last command somewhere.

You need the file and position您需要文件和 position

File             | Position
mysql-bin.091820 |  7039359

Without closing the connection to the client (because it would release the read lock) issue the command to get a dump of the master:在不关闭与客户端的连接的情况下(因为它会释放读锁)发出命令以获取主服务器的转储:

mysqldump db_name table_name > table_name.sql

Do not release the lock, if the dump has not ended yet.如果转储尚未结束,请不要释放锁。

UNLOCK TABLES;

Now copy the dump file to the slave using scp or your preferred tool.现在使用 scp 或您喜欢的工具将转储文件复制到从站。

On Slave Server.在从服务器上。

Add replicate-do-table=db_name.table_name and after the change is made restart mysql service on Slave.添加 replicate-do-table=db_name.table_name 并在更改完成后重新启动 Slave 上的 mysql 服务。

sudo grep -ir "ip_log2" /etc/mysql/
/etc/mysql/mysql.conf.d/mysqld.cnf:replicate-do-table=db_name.ip_log2

Open a connection to mysql and type:打开与 mysql 的连接并键入:

STOP SLAVE;
RESET SLAVE;

CHANGE REPLICATION SOURCE TO
SOURCE_HOST='source_server_ip',
SOURCE_USER='replica_user',
SOURCE_PASSWORD='password',
SOURCE_LOG_FILE='mysql-bin.091820', ---the data tou saved from master 
SOURCE_LOG_POS=7039359;

Load masters data dump with this console command:使用此控制台命令加载主数据转储:

mysql -uroot -p < /root/mysqldump.sql
START SLAVE;
SHOW SLAVE STATUS;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何同步存储在不同数据库中的两个表 - How to synchronize two tables stored in different databases 从两个不同的表中搜索数据,这两个表在不同的数据库中 - Search data from two different tables that two tables in different databases 需要在来自不同服务器Postgresql数据库的两个表之间同步数据 - Need to sync data between two tables from different servers Postgresql databases 比较2个不同数据库中的2个表 - Compare between 2 tables in 2 different databases nHibernate同步两个数据库 - nHibernate synchronize two databases 如何使用 Linq 在 C# 中连接两个不同的数据库表? - How to join two different databases' tables in C# with Linq? 在两个不同的mysql数据库中的两个表之间复制大量行 - Copying a large amount of rows between two tables in two different mysql databases 如何同步2个数据库 - How to synchronize 2 databases 同步两个 dbcontexts 的数据(不同的提供者) - Synchronize data of two dbcontexts (Different providers) 如何在一个简单但完整的c#项目中从两个不同的sql server数据库连接两个表? - How to join two tables from two different sql server databases in a simple but complete c# project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM