简体   繁体   English

MySQL无需停机即可更新整个数据库

[英]MySQL update whole database without downtime

I have a large database that needs to be rebuilt every 24h. 我有一个大型数据库,需要每24小时重建一次。 The database is built using a custom script on a server that puls data from different files. 该数据库是使用服务器上的自定义脚本构建的,该脚本可对来自不同文件的数据进行处理。 The problem is that the whole process takes 1 min to complete and that is 1 min downtime because we need to drop the whole database in order to rebuild it (there is no other way than to drop it). 问题在于整个过程需要1分钟才能完成,而停机时间则是1分钟,因为我们需要删除整个数据库才能重建它(除了删除它,别无其他方法)。

At first, we planned to build a temporary database, and drop the original and then rename the temporary to the original name but MySQL doesn't support database renaming. 最初,我们计划构建一个临时数据库,删除原始数据库,然后将临时数据库重命名为原始名称,但是MySQL不支持数据库重命名。

The second approach was to dump .sql file from temp database and import it to main(original) database but that also causes downtime. 第二种方法是从temp数据库中转储.sql文件并将其导入到main(原始)数据库中,但这也会导致停机。

What is the best way to do this? 做这个的最好方式是什么?

Here is something that I do. 这是我要做的。 It doesn't result in zero downtime but could finish in less than a second. 它不会导致零停机,但可以在不到一秒钟的时间内完成。

Create a database that only has interface elements to your real database. 创建一个仅包含与实际数据库的接口元素的数据库。 In my case, it only contains view definitions, and all user queries go through this database. 就我而言,它仅包含视图定义,并且所有用户查询都通过该数据库。

Create a new database each night. 每晚创建一个新的数据库。 When it is done, then update the view definitions to refer to the new database. 完成后,请更新视图定义以引用新数据库。 I would recommend either turning off user access to the database containing the views while you are updating them or deleting all of the views and recreating them -- this prevents partial access to the old database. 我建议您在更新视图时关闭用户对包含视图的数据库的访问,或者删除所有视图并重新创建它们-这样可以防止部分访问旧数据库。 Because creating views is fast, this should be a very fast operation. 因为创建视图的速度很快,所以这应该是非常快的操作。

We do all of this through a job. 我们通过一项工作来完成所有这些工作。 In fact, before changing the production views, we test the view creation on another database to be sure they are all working. 实际上,在更改生产视图之前,我们在另一个数据库上测试视图创建以确保它们都可以正常工作。

Obviously, if you use alter view instead of requiring consistency across all the views, then there is no downtime, just a brief period of inconsistency. 显然,如果您使用alter view而不是要求所有视图之间都具有一致性,那么就不会停机,只有一小段时间的不一致。

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

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