[英]Mysql replication in shared hosting
I need to do some mysql replication. 我需要做一些mysql复制。 Some informations : 一些信息:
Right now the system is using a revision number on every row of every table and we periodically checked for modification of these number (and update the corresponding rows). 现在系统在每个表的每一行使用修订号,我们定期检查这些号码的修改(并更新相应的行)。 It's quite slow. 这很慢。
What I'm thinking of is that every SELECT/INSERT/UPDATE query is logged in a specific table and the "slave server" periodically ask the "master server" for the content of this table and apply the corresponding queries. 我想到的是每个SELECT / INSERT / UPDATE查询都记录在一个特定的表中,“从服务器”定期向“主服务器”询问该表的内容并应用相应的查询。
What is your opinion on that idea ? 你对这个想法有什么看法?
I know it's not perfect, a server might be down before all the queries are propagated, but I want to minimize the possible problems, with as few lines of code as possible. 我知道它并不完美,服务器可能会在所有查询传播之前关闭,但我希望尽可能减少可能的问题,尽可能少的代码行。
What would be the best possible way to implement it ? 实施它的最佳方式是什么?
I successfully used a combination of triggers and a federated table to simulate replication of data from a MyISAM table on one server to a MyISAM table on a different server in a shared hosting environment. 我成功地使用了触发器和联合表的组合来模拟从一台服务器上的MyISAM表到共享托管环境中不同服务器上的MyISAM表的数据复制。
Any inserts / updates / deletes on my master table are replicated to my federated table on the same server via AFTER INSERT / AFTER UPDATE / AFTER DELETE triggers. 我的主表上的任何插入/更新/删除都通过AFTER INSERT / AFTER UPDATE / AFTER DELETE触发器复制到同一服务器上的联合表。 That federated table then pushes the changes to a table on a different server. 然后,该联合表将更改推送到另一台服务器上的表。
I can't take the credit for coming up with this approach as it was very helpfully documented by RolandoMySQLDBA on Server Fault: 由于RolandoMySQLDBA在服务器故障上非常有帮助地记录了这个方法,所以我不能不赞成这种方法:
Is a MySQL stored procedure able to insert/update to a remote backup MySQL server? MySQL存储过程是否能够插入/更新到远程备份MySQL服务器? . 。
Here are the steps I implemented: 以下是我实施的步骤:
On SERVER2... 在SERVER2上......
On SERVER1... 在SERVER1上......
I created a table (let's call it federated_table ) with columns which matched those in master_table , specifying a FEDERATED
storage engine and a CONNECTION
string to reference slave_table on SERVER2... CONNECTION='mysql://username:password@SERVER2:port/database/slave_table';
我创建了一个表(让我们称之为federated_table ),其列与master_table中的列匹配,指定了一个FEDERATED
存储引擎和一个CONNECTION
字符串,以引用SERVER2上的slave_table ... CONNECTION='mysql://username:password@SERVER2:port/database/slave_table';
I added AFTER INSERT
, AFTER UPDATE
and AFTER DELETE
triggers to master_table which contained SQL commands to... 我将AFTER INSERT
, AFTER UPDATE
和AFTER DELETE
触发器添加到master_table ,其中包含SQL命令...
INSERT INTO federated_table VALUES (NEW.id,NEW.title);
, ,
UPDATE federated_table SET id=NEW.id,title=NEW.title WHERE id=OLD.id;
and 和
DELETE FROM federated_table WHERE id=OLD.id;
respectively. 分别。
I hope that helps someone in a similar situation. 我希望能帮助处于类似情况的人。
Two ideas: 两个想法:
A cron that finds the max(ID)s in the backup database tables and then gets all the records in the main database beyond that. 一个cron,它在备份数据库表中找到max(ID)s,然后获取主数据库中的所有记录。
To include the suggestion from my comment, duplicating your writes directly to the 2nd database instead of writing the queries to a table. 要包含我的评论中的建议,请将您的写入直接复制到第二个数据库,而不是将查询写入表中。 This may cause a bit of overhead, but might be the easiest to implement. 这可能会导致一些开销,但可能是最容易实现的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.