简体   繁体   English

使用Django Webapp和MySQL进行多主数据库复制

[英]Multi-master database replication with Django webapp and MySQL

I am working on scaling out a webapp and providing some database redundancy for protection against failures and to keep the servers up when updates are needed. 我正在努力扩展Web应用程序并提供一些数据库冗余,以防止出现故障并在需要更新时保持服务器正常运行。 The app is still in development, so I have chosen a simple multi-master redundancy with two separate database servers to try and achieve this. 该应用程序仍在开发中,因此我选择了带有两个单独的数据库服务器的简单多主机冗余来尝试实现此目的。 Each server will have the Django code and host its own database, and the databases should be as closely mirrored as possible (updated within a few seconds). 每个服务器将具有Django代码并托管自己的数据库,并且数据库应尽可能紧密地镜像(在几秒钟内更新)。

I am trying to figure out how to set up the multi-master (master-master) replication between databases with Django and MySQL. 我试图弄清楚如何在使用Django和MySQL的数据库之间设置多主机(master-master)复制。 There is a lot of documentation about setting it up with MySQL only (using various configurations), but I cannot find any for making this work from the Django side of things. 有很多关于仅使用MySQL进行设置(使用各种配置)的文档,但是我找不到从Django方面进行这项工作的任何文档。

From what I understand, I need to approach this by adding two database entries in the Django settings (one for each master) and then write a database router that will specify which database to read from and which to write from. 据我了解,我需要通过在Django设置中添加两个数据库条目(每个主数据库一个)来解决此问题,然后编写一个数据库路由器,该路由器将指定从哪个数据库读取和从哪个数据库写入。 In this scenario, both databases should accept both reads and writes, and writes/updates should be mirrored over to the other database. 在这种情况下,两个数据库都应同时接受读取和写入,并且写入/更新应镜像到另一个数据库。 The logic in the router could simply use a round-robin technique to decide which database to use. 路由器中的逻辑可以简单地使用轮询技术来决定使用哪个数据库。 From there on, further configuration to set up the actual replication should be done through MySQL configuration. 从那里开始,应该通过MySQL配置完成进一步的配置以设置实际的复制。

Does this approach sound correct, and does anyone have any experience with getting this to work? 这种方法听起来是否正确,是否有人有使其工作的经验?

Your idea of the router is great! 您对路由器的想法很棒! I would add that you need automatically detect whether a databases is [slow] down. 我要补充一点,您需要自动检测数据库是否[运行缓慢]。 You can detect that by the response time and by connection/read/write errors. 您可以通过响应时间以及连接/读取/写入错误来检测到该错误。 And if this happens then you exclude this database from your round-robin list for a while, trying to connect back to it every now and then to detect if the databases is alive. 如果发生这种情况,则将您的数据库从循环列表中排除一段时间,尝试不时尝试重新连接至该数据库,然后检测数据库是否处于活动状态。

In other words the round-robin list grows and shrinks dynamically depending on the health status of your database machines. 换句话说,轮询列表根据数据库计算机的运行状况动态地增长和缩小。

The another important notice is that luckily you don't need to maintain this round-robin list common to all the web servers. 另一个重要的通知是,幸运的是,您不需要维护所有Web服务器通用的循环列表。 Each web server can store its own copy of the round-robin list and its own state of inclusion and exclusion of databases into this list. 每个Web服务器都可以存储自己的轮询列表的副本以及其包含和排除数据库到此列表中的状态。 This is just because a database server can be seen from one web server and can be not seen from another one due to local network problems. 这仅仅是因为由于本地网络问题,可以从一台Web服务器看到数据库服务器,而从另一台Web服务器看不到数据库服务器。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM