简体   繁体   English

并发数据库创建

[英]Concurrent database creation

My web app requires some initial database scripts to be executed during installation. 我的Web应用程序需要在安装过程中执行一些初始数据库脚本。 These are just some CREATE TABLE scripts that need to be run. 这些只是一些需要运行的CREATE TABLE脚本。 I'm working on dockerizing it. 我正在努力将其Docker化。

I've been trying to use the Docker 1.12's swarm mode to create multiple container replicas of my web app which use a shared database. 我一直在尝试使用Docker 1.12的swarm模式来创建我的Web应用程序的多个容器副本,这些副本使用共享数据库。 (An Oracle 12c or MySQL instance). (Oracle 12c或MySQL实例)。

The problem is that when I start multiple copies of the container, they all try to run the SQL scripts simultaneously and fail. 问题是,当我启动容器的多个副本时,它们都尝试同时运行SQL脚本而失败。 I tried using Flyway to handle the database migrations but to no avail. 我尝试使用Flyway处理数据库迁移,但无济于事。

What should be the way to handle such a situation? 如何处理这种情况?

Generally, DB migration jobs should be versioned with every new script executing only after all the earlier ones are done. 通常,只有在完成所有较早版本的脚本之后,才应对数据库迁移作业进行版本控制,使每个新脚本都执行。 With this as a precondition one can either design a tool that runs those jobs one after another if not already run or one lets the jobs run always, but designs them in a way to check that they have not been done before. 以此为前提,可以设计一种工具(如果尚未运行)一个接一个地运行这些作业,或者让一个作业始终运行,但可以通过设计方式检查它们之前是否曾执行过。

Otherwise, all application data are at risk of being wiped off at next deployment. 否则,所有应用程序数据都有在下次部署时被擦除的风险。 You should be able to deploy any time keeping and reusing the existing data (with docker or without). 您应该能够部署任何时间来保存和重用现有数据(使用docker或不使用docker)。

In your "simple" case a DB check of the following sort would help: 在您的“简单”情况下,以下数据库检查会有所帮助:

create table IF NOT EXISTS thing (col1 int not null, col2 int not null, age int not null );

Thanks @Drew for prompting me to put my comment into an answer. 感谢@Drew提示我将评论放入答案中。

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

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