How to update a table automatically as another table is updated on different mysql server?

余生颓废 提交于 2019-12-31 03:51:06

问题


Let's say I have two databases 'db1' & 'db2' on different mysql servers 'A' & 'B' respectively.

I want to check every 6 hours if there is any update found in 'table1' in 'db2', then the 'table1' in 'db1' will be automatically updated.

How can I do that with trigger or a cron job? and when it will be fired?


回答1:


You could do with with a cron job, simply could run a php file every minute.

this file can check for a new row in a table1 in db2 (saving current count of rows in a text file for comparison, and if new count > old count, then this can then update table1 in db1.

easy

but mysql replication as @Charles said in the comment would be better.




回答2:


I accepted kutF's answer and after you run that program Using keyword CASCADE in table creation in mysql. So if you update the parent table then the child table also get updated

CREATE TABLE parent
(
par_id INT NOT NULL,
PRIMARY KEY (par_id)
) TYPE = INNODB;

CREATE TABLE child
(
par_id INT NOT NULL,
child_id INT NOT NULL,
PRIMARY KEY (child_id),
FOREIGN KEY (par_id) REFERENCES parent (par_id) ON DELETE CASCADE
) TYPE = INNODB;


来源:https://stackoverflow.com/questions/13930975/how-to-update-a-table-automatically-as-another-table-is-updated-on-different-mys

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!