如何在Docker中初始化数据库后启动flyway

Posted

技术标签:

【中文标题】如何在Docker中初始化数据库后启动flyway【英文标题】:How to start flyway after database initialization in Docker 【发布时间】:2019-01-30 17:23:33 【问题描述】:

我有以下 docker compose 文件(docker-compose-dev.yml):

version: '3'
services:
  my_sql_db:
    image: percona:latest
    container_name: my_sql_db
    environment:

      mysql_ROOT_PASSWORD: password
      MYSQL_DATABASE: abhs
    ports:
    - "3306:3306"

  migration:
    image: boxfuse/flyway:latest
    container_name: flyway_migration
    volumes:
      - ./flyway_scripts/src/main/resources/db/migration:/flyway/sql
    command: -url=jdbc:mysql://my_sql_db:3306/abhs?useUnicode=true&characterEncoding=utf8&useSSL=false -user=root -password=password migrate
    depends_on:
    - my_sql_db

并关注docker-compose.yml

version: '3'
services:
  migration:
    image: boxfuse/flyway:latest
    container_name: flyway_migration
    volumes:
     - ./flyway_scripts/src/main/resources/db/migration:/flyway/sql

然后我执行以下命令:

docker-compose -f docker-compose.yml -f docker-compose-dev.yml up

它会导致错误:

在日志中我看到以下内容:

my_sql_db    | Initializing database
flyway_migration  | Flyway Community Edition 5.1.4 by Boxfuse
flyway_migration  |
my_sql_db    | 2018-08-24T08:47:41.616694Z 0 [Warning] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.
my_sql_db    | 2018-08-24T08:47:41.616747Z 0 [Warning] 'NO_AUTO_CREATE_USER' sql mode was not set.
flyway_migration  | ERROR:
flyway_migration  | Unable to obtain connection from database (jdbc:mysql://my_sql_db:3306/abhs?useUnicode=true&characterEncoding=utf8&useSSL=false) for user 'root': Could not connect to address=(host=my_sql_db)(port=3306)(type=master) : Connection refused (Connection refused)
flyway_migration  | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
flyway_migration  | SQL State  : 08
flyway_migration  | Error Code : -1
flyway_migration  | Message    : Could not connect to address=(host=my_sql_db)(port=3306)(type=master) : Connection refused (Connection refused)
my_sql_db    | 2018-08-24T08:47:43.024690Z 0 [Warning] InnoDB: New log files created, LSN=45790
flyway_migration  |
my_sql_db    | 2018-08-24T08:47:43.443625Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
my_sql_db    | 2018-08-24T08:47:43.588008Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 5dc59a4f-a77a-11e8-b6cb-0242ac130002.
my_sql_db    | 2018-08-24T08:47:43.760654Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
my_sql_db    | 2018-08-24T08:47:44.518107Z 0 [Warning] CA certificate ca.pem is self signed.
my_sql_db    | 2018-08-24T08:47:44.925466Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
my_sql_db    | 2018-08-24T08:47:54.762213Z 1 [Warning] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode.
my_sql_db    | 2018-08-24T08:47:54.762517Z 1 [Warning] 'user' entry 'mysql.session@localhost' ignored in --skip-name-resolve mode.
my_sql_db    | 2018-08-24T08:47:54.762889Z 1 [Warning] 'user' entry 'mysql.sys@localhost' ignored in --skip-name-resolve mode.
my_sql_db    | 2018-08-24T08:47:54.763244Z 1 [Warning] 'db' entry 'performance_schema mysql.session@localhost' ignored in --skip-name-resolve mode.
my_sql_db    | 2018-08-24T08:47:54.763472Z 1 [Warning] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode.
my_sql_db    | 2018-08-24T08:47:54.763788Z 1 [Warning] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode.
my_sql_db    | 2018-08-24T08:47:54.763928Z 1 [Warning] 'tables_priv' entry 'user mysql.session@localhost' ignored in --skip-name-resolve mode.
my_sql_db    | 2018-08-24T08:47:54.764128Z 1 [Warning] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode.
my_sql_db    | Database initialized
my_sql_db    | MySQL init process in progress...
my_sql_db    | 2018-08-24T08:47:58.970290Z 0 [Warning] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.
my_sql_db    | 2018-08-24T08:47:58.970345Z 0 [Warning] 'NO_AUTO_CREATE_USER' sql mode was not set.
my_sql_db    | 2018-08-24T08:47:58.974061Z 0 [Note] mysqld (mysqld 5.7.22-22) starting as process 58 ...
my_sql_db    | 2018-08-24T08:47:58.999651Z 0 [Note] InnoDB: PUNCH HOLE support available
my_sql_db    | 2018-08-24T08:47:58.999685Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
my_sql_db    | 2018-08-24T08:47:58.999689Z 0 [Note] InnoDB: Uses event mutexes
my_sql_db    | 2018-08-24T08:47:58.999692Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
my_sql_db    | 2018-08-24T08:47:58.999695Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.8
my_sql_db    | 2018-08-24T08:47:58.999698Z 0 [Note] InnoDB: Using Linux native AIO
my_sql_db    | 2018-08-24T08:47:59.000153Z 0 [Note] InnoDB: Number of pools: 1
my_sql_db    | 2018-08-24T08:47:59.000426Z 0 [Note] InnoDB: Using CPU crc32 instructions
my_sql_db    | 2018-08-24T08:47:59.002306Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
my_sql_db    | 2018-08-24T08:47:59.006893Z 0 [Note] InnoDB: Completed initialization of buffer pool
my_sql_db    | 2018-08-24T08:47:59.013219Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
my_sql_db    | 2018-08-24T08:47:59.024242Z 0 [Note] InnoDB: Crash recovery did not find the parallel doublewrite buffer at /var/lib/mysql/xb_doublewrite
my_sql_db    | 2018-08-24T08:47:59.026263Z 0 [Note] InnoDB: Highest supported file format is Barracuda.
my_sql_db    | 2018-08-24T08:47:59.066469Z 0 [Note] InnoDB: Created parallel doublewrite buffer at /var/lib/mysql/xb_doublewrite, size 3932160 bytes
my_sql_db    | 2018-08-24T08:47:59.071752Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables
my_sql_db    | 2018-08-24T08:47:59.072052Z 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
my_sql_db    | 2018-08-24T08:47:59.422155Z 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
my_sql_db    | 2018-08-24T08:47:59.423325Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active.
my_sql_db    | 2018-08-24T08:47:59.423376Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active.
my_sql_db    | 2018-08-24T08:47:59.423900Z 0 [Note] InnoDB: Waiting for purge to start
my_sql_db    | 2018-08-24T08:47:59.474066Z 0 [Note] InnoDB: Percona XtraDB (http://www.percona.com) 5.7.22-22 started; log sequence number 2595255
my_sql_db    | 2018-08-24T08:47:59.474647Z 0 [Note] Plugin 'FEDERATED' is disabled.
my_sql_db    | 2018-08-24T08:47:59.499970Z 0 [Note] Found ca.pem, server-cert.pem and server-key.pem in data directory. Trying to enable SSL support using them.
my_sql_db    | 2018-08-24T08:47:59.500004Z 0 [Note] Skipping generation of SSL certificates as certificate files are present in data directory.
my_sql_db    | 2018-08-24T08:47:59.500382Z 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
my_sql_db    | 2018-08-24T08:47:59.501263Z 0 [Warning] CA certificate ca.pem is self signed.
my_sql_db    | 2018-08-24T08:47:59.522151Z 0 [Note] Skipping generation of RSA key pair as key files are present in data directory.
my_sql_db    | 2018-08-24T08:47:59.531657Z 0 [Note] InnoDB: Buffer pool(s) load completed at 180824  8:47:59

看起来 flyway 在数据库初始化之前启动,因此无法连接到数据库,我看到下面的错误。

我该如何解决这个问题?

附言

我在谷歌上搜索了类似的问题,并找到了以下建议:https://github.com/vishnubob/wait-for-it 但我是 docker 新手,我不明白如何将其放入我的 docker compose 文件中

附言2

我尝试将文件wait-fot-it.sh 放在撰写文件附近并执行:

command: ["./wait-for-it.sh", "mysql:3306", "--", "-url=jdbc:mysql://my_sql_db:3306/abhs?useUnicode=true&characterEncoding=utf8&useSSL=false -user=root -password=password migrate"]

但我返回错误:Invalid argument: ./wait-for-it.sh

附言 3

我尝试了“重复”主题的方法:

version: '3'
services:
  my_sql_db:
    image: percona:latest
    container_name: my_sql_db
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: abhs
    ports:
    - "3306:3306"
    healthcheck:
      test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
      timeout: 20s
      retries: 10
  migration:
    image: boxfuse/flyway:latest
    container_name: flyway_migration
    volumes:
      - ./flyway_scripts/src/main/resources/db/migration:/flyway/sql
    command: -url=jdbc:mysql://my_sql_db:3306/abhs?useUnicode=true&characterEncoding=utf8&useSSL=false -user=root -password=password migrate
    depends_on:
      my_sql_db:
        condition: service_healthy

但我看到以下错误:

$ docker-compose -f docker-compose.yml -f docker-compose-dev.yml up
The Compose file '.\docker-compose-dev.yml' is invalid because:
services.migration.depends_on contains an invalid type, it should be an array

附言 4

对于这种方法,我看到以下错误:

version: '3'
services:
  my_sql_db:
    image: percona:latest
    container_name: my_sql_db
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: abhs
    ports:
    - "3306:3306"
    healthcheck:
      test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
      timeout: 20s
      retries: 10
  migration:
    image: boxfuse/flyway:latest
    container_name: flyway_migration
    volumes:
      - ./flyway_scripts/src/main/resources/db/migration:/flyway/sql
    command: dockerize wait jdbc:mysql://my_sql_db:3306 -url=jdbc:mysql://my_sql_db:3306/abhs?useUnicode=true&characterEncoding=utf8&useSSL=false -user=root -password=password migrate
    depends_on:
      - my_sql_db

我看到以下错误:

flyway_migration  | ERROR: Invalid argument: dockerize

UPDATE_1

wait-for-it.sh 内容:

#!/usr/bin/env bash
#   Use this script to test if a given TCP host/port are available

cmdname=$(basename $0)

echoerr()  if [[ $QUIET -ne 1 ]]; then echo "$@" 1>&2; fi 

usage()

    cat << USAGE >&2
Usage:
    $cmdname host:port [-s] [-t timeout] [-- command args]
    -h HOST | --host=HOST       Host or IP under test
    -p PORT | --port=PORT       TCP port under test
                                Alternatively, you specify the host and port as host:port
    -s | --strict               Only execute subcommand if the test succeeds
    -q | --quiet                Don't output any status messages
    -t TIMEOUT | --timeout=TIMEOUT
                                Timeout in seconds, zero for no timeout
    -- COMMAND ARGS             Execute command with args after the test finishes
USAGE
    exit 1


wait_for()

    if [[ $TIMEOUT -gt 0 ]]; then
        echoerr "$cmdname: waiting $TIMEOUT seconds for $HOST:$PORT"
    else
        echoerr "$cmdname: waiting for $HOST:$PORT without a timeout"
    fi
    start_ts=$(date +%s)
    while :
    do
        if [[ $ISBUSY -eq 1 ]]; then
            nc -z $HOST $PORT
            result=$?
        else
            (echo > /dev/tcp/$HOST/$PORT) >/dev/null 2>&1
            result=$?
        fi
        if [[ $result -eq 0 ]]; then
            end_ts=$(date +%s)
            echoerr "$cmdname: $HOST:$PORT is available after $((end_ts - start_ts)) seconds"
            break
        fi
        sleep 1
    done
    return $result


wait_for_wrapper()

    # In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692
    if [[ $QUIET -eq 1 ]]; then
        timeout $BUSYTIMEFLAG $TIMEOUT $0 --quiet --child --host=$HOST --port=$PORT --timeout=$TIMEOUT &
    else
        timeout $BUSYTIMEFLAG $TIMEOUT $0 --child --host=$HOST --port=$PORT --timeout=$TIMEOUT &
    fi
    PID=$!
    trap "kill -INT -$PID" INT
    wait $PID
    RESULT=$?
    if [[ $RESULT -ne 0 ]]; then
        echoerr "$cmdname: timeout occurred after waiting $TIMEOUT seconds for $HOST:$PORT"
    fi
    return $RESULT


# process arguments
while [[ $# -gt 0 ]]
do
    case "$1" in
        *:* )
        hostport=($1//:/ )
        HOST=$hostport[0]
        PORT=$hostport[1]
        shift 1
        ;;
        --child)
        CHILD=1
        shift 1
        ;;
        -q | --quiet)
        QUIET=1
        shift 1
        ;;
        -s | --strict)
        STRICT=1
        shift 1
        ;;
        -h)
        HOST="$2"
        if [[ $HOST == "" ]]; then break; fi
        shift 2
        ;;
        --host=*)
        HOST="$1#*="
        shift 1
        ;;
        -p)
        PORT="$2"
        if [[ $PORT == "" ]]; then break; fi
        shift 2
        ;;
        --port=*)
        PORT="$1#*="
        shift 1
        ;;
        -t)
        TIMEOUT="$2"
        if [[ $TIMEOUT == "" ]]; then break; fi
        shift 2
        ;;
        --timeout=*)
        TIMEOUT="$1#*="
        shift 1
        ;;
        --)
        shift
        CLI=("$@")
        break
        ;;
        --help)
        usage
        ;;
        *)
        echoerr "Unknown argument: $1"
        usage
        ;;
    esac
done

if [[ "$HOST" == "" || "$PORT" == "" ]]; then
    echoerr "Error: you need to provide a host and port to test."
    usage
fi

TIMEOUT=$TIMEOUT:-15
STRICT=$STRICT:-0
CHILD=$CHILD:-0
QUIET=$QUIET:-0

# check to see if timeout is from busybox?
# check to see if timeout is from busybox?
TIMEOUT_PATH=$(realpath $(which timeout))
if [[ $TIMEOUT_PATH =~ "busybox" ]]; then
        ISBUSY=1
        BUSYTIMEFLAG="-t"
else
        ISBUSY=0
        BUSYTIMEFLAG=""
fi

if [[ $CHILD -gt 0 ]]; then
    wait_for
    RESULT=$?
    exit $RESULT
else
    if [[ $TIMEOUT -gt 0 ]]; then
        wait_for_wrapper
        RESULT=$?
    else
        wait_for
        RESULT=$?
    fi
fi

if [[ $CLI != "" ]]; then
    if [[ $RESULT -ne 0 && $STRICT -eq 1 ]]; then
        echoerr "$cmdname: strict mode, refusing to execute subprocess"
        exit $RESULT
    fi
    exec "$CLI[@]"
else
    exit $RESULT
fi

附言 5

我也试过这个:

version: '3'
services:
  my_sql_db:
    image: percona:latest
    container_name: my_sql_db
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: abhs
    ports:
    - "3306:3306"
    healthcheck:
      test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
      timeout: 20s
      retries: 10
  migration:
    image: boxfuse/flyway:latest
    container_name: flyway_migration
    volumes:
     - ./flyway_scripts/src/main/resources/db/migration:/flyway/sql
    entrypoint: ["wait-for-it.sh", "mysql:3306", "--", "docker-entrypoint.sh"]      
    command: -url=jdbc:mysql://my_sql_db:3306/abhs?useUnicode=true&characterEncoding=utf8&useSSL=false -user=root -password=password migrate
    depends_on:
      - my_sql_db

导致错误:

正在创建 flyway_migration ... 错误

ERROR: for flyway_migration  Cannot start service migration: OCI runtime create failed: container_linux.go:348: starting container process caused "exec: \"wait-for-it.sh\": executable file not found in $PATH": unknown

ERROR: for migration  Cannot start service migration: OCI runtime create failed: container_linux.go:348: starting container process caused "exec: \"wait-for-it.sh\": executable file not found in $PATH": unknown
Encountered errors while bringing up the project.

【问题讨论】:

如果要指定容器之间的依赖关系,为什么要使用两个单独的撰写文件?另外:您当前启动它的方式,第二个文件首先启动 @Jeroen Steenbeeke,我的同事做到了。他这样做是因为他认为我们想为开发环境使用自己的数据库,而我们将为其他环境使用独立数据库 Docker-compose check if mysql connection is ready的可能重复 等待它是一个“旧”的解决方案。现在您可以使用健康检查。见上面的链接。 @lvthillo,我正在阅读 cmets 并看到以下内容: 1.mysqladmin ping 如果服务器正在运行但尚未接受连接,将返回误报。 2:仅供 2017 年的人们参考:版本 3+ 不支持depends_on 下的条件 【参考方案1】:

Dockerize / wait-for-it.sh

对于错误:

Invalid argument: ./wait-for-it.sh

Invalid argument: dockerize

这是因为 Flyway 容器的 entrypoint 是 flyway 可执行文件,并且您指定的 command 的内容作为参数附加到入口点。因此,实际上,容器正在运行以下内容:

flyway dockerize ...

flyway wait-for-it.sh ...

这些都不是Flyway command line 的有效参数。

入口点需要更新,就像您在P.S.5 中所做的那样。但是,您随后遇到了错误:

"wait-for-it.sh": executable file not found in $PATH"

这是因为 wait-for-it.sh(和 dockerize)在 Flyway 容器中不可用。

您可以创建一个扩展 Flyway 容器的 Dockerfile,然后创建 ADDCOPY 脚本,例如:

FROM boxfuse/flyway:latest

RUN mkdir /flyway/bin

ADD wait-for-it.sh /flyway/bin/wait-for-it.sh

RUN chmod 755 /flyway/bin/wait-for-it.sh

或者挂载一个包含脚本/可执行文件的卷:

version: '3'
services:
  ...
  migration:
    image: boxfuse/flyway:latest
    container_name: flyway_migration
    volumes:
     - ../sql:/flyway/sql
     - ../bin:/flyway/bin
    entrypoint: ["/flyway/bin/dockerize", "-wait", "tcp://my_sql_db:3306", "-timeout", "15s", "--", "flyway"]      
    ...

本地目录../bin 包含dockerize(或wait-for-it.sh)的位置。

这应该足以让 dockerize / wait-for-it.sh 正常工作。但是,这两个工具只检查端口是否可用,而不是检查数据库本身是否实际上已准备好为请求提供服务。

撰写 v2.1

也就是说,使用 docker-compose v2.1 depends_on: condition 语法可能是一种合理的方法。正如您在 cmets 中提到的那样,该语法已在 v3 和很多 people are unhappy about it 中删除。

然而,正如一位 Docker 开发人员在comment on that issue 中所说:

如果您不打算使用 swarm 服务,则没有理由使用 v3 格式。

自定义健康检查脚本

另一种方法是扩展 Flyway 容器以添加自定义 MySQL 健康检查脚本,类似于 docker compose documentation 中显示的 Postgres 脚本:

#!/bin/bash
# wait-for-mysql.sh

set -e

host="$1"
shift
cmd="$@"

until MYSQL_PWD=$MYSQL_ROOT_PASSWORD /usr/bin/mysql --host="$host" --user="root" --execute "SHOW DATABASES;"; do
  >&2 echo "MySQL is unavailable - sleeping"
  sleep 1
done

>&2 echo "MySQL is up - executing command"
exec $cmd

然后创建一个 Dockerfile 来扩展 Flyway,安装 MySQL 客户端并添加这个脚本:

FROM boxfuse/flyway:latest

RUN apt-get update && \
    apt-get install -y mysql-client && \
    mkdir /flyway/bin

ADD wait-for-mysql.sh /flyway/bin/wait-for-mysql.sh

RUN chmod 755 /flyway/bin/wait-for-mysql.sh

然后您可以在撰写文件中使用自定义 Flyway 图像:

version: '3'
services:
  my_sql_percona:
    ...
  migration:
    build: ./flyway_mysql_client
    container_name: flyway_migration
    environment:
      MYSQL_ROOT_PASSWORD: password
    volumes:
     - ../sql:/flyway/sql
    entrypoint: ["bash", "/flyway/bin/wait-for-mysql.sh", "my_sql_percona", "--", "flyway"]      
    command: -url=jdbc:mysql://my_sql_db:3306/abhs?useUnicode=true&characterEncoding=utf8&useSSL=false -user=root -password=password migrate
    depends_on:
      - my_sql_percona

这种方法的缺点是您需要使用自定义健康检查脚本为每个容器扩展每个容器的每个依赖项。

码头工人栈

v2.1 的depends_on: condition 语法似乎已被删除,取而代之的是 v3 中的restart policies。但是,它们嵌套在 deploy 部分下,其中:

仅在使用 docker stack deploy 部署到 swarm 时生效,被 docker-compose up 和 docker-compose run 忽略。

因此,另一个选择是放弃 docker-compose 并在 docker swarm 上运行,如下所示:

向 Flyway 容器添加 on-failure 重启策略:

version: '3'
services:
  my_sql_percona:
    ...
  migration:
    image: boxfuse/flyway:latest
    ...
    depends_on:
      - my_sql_percona
    deploy:
      restart_policy:
        condition: on-failure

创建一个 swarm 集群(在本例中为单节点):

docker swarm init --advertise-addr <your-ip-address>

部署服务:

docker stack deploy --compose-file docker-compose.yml flyway_mysql

Flyway 容器将在每次退出时由 swarm 重新启动 一个错误,直到它最终成功退出。

虽然这似乎可行,但我不确定在这种情况下它是否是最佳方法。例如,如果 Flyway 容器由于迁移脚本中的错误而退出,swarm 将继续重新启动容器,即使它永远不会成功。

总结

我用这五种不同的方法创建了一个repository。

就个人而言,我认为我会使用 v2.1 方法,因为运行状况检查与数据库容器本身一起保存,并且不会在依赖它的每个容器中重复。不过,我不需要使用 swarm 服务,所以选择适合你的。 :-)

【讨论】:

@dockerize 我在哪里可以定义 docker compose 版本? docker-compose.yml 的第一行,例如:version: '2.1'version: '3'。这声明了您正在使用的语法版本,而不是 docker-compose 的版本。 @g*** 我使用docker stack 而不是docker-compose 添加了另一个解决方案,并更新了我的repository 示例和更多详细信息。 抱歉耽搁了。我尝试使用 helthcheck 应用解决方案,但它不起作用 您尝试过我存储库中的版本吗?那对你有用吗? 1) git clone git@github.com:andyrbell/flyway-wait-for-mysql.git 2) cd flyway-wait-for-mysql/docker-compose-2.1/ 3) docker-compose up【参考方案2】:

使用flyway 5.2.0,您可以添加参数connectRetries,该参数指定flyway在1秒间隔内尝试重新连接的最大次数。

command: -connectRetries=20 -url=jdbc:mysql://my_sql_db:3306/abhs?useUnicode=true&characterEncoding=utf8&useSSL=false -user=root -password=password migrate

Flyway Docs

【讨论】:

不错,非常简单的解决方案【参考方案3】:

或者,您可以创建一个migrate.dev.sh 并使用docker run .. boxfuse/flyway:latest 等。

这是一个示例文件

$FLYWAY_PASSWORD=$PGPASSWORD
$FLYWAY_URL=jdbc:postgresql://$DB_HOST:$PORT/mydb
$FLYWAY_USER=postgres

docker run \
  --rm \
  -e FLYWAY_PASSWORD=$FLYWAY_PASSWORD \
  -e FLYWAY_URL=$FLYWAY_URL \
  -e FLYWAY_USER=$FLYWAY_USER \
  -e FLYWAY_SCHEMAS=$FLYWAY_SCHEMAS \
  -v $(pwd)/sql:/flyway/sql \
   boxfuse/flyway:latest $1

然后你可以这样称呼它

migrate.dev.sh info 要么 migrate.dev.sh migrate

假设您的迁移位于 ./sql/

【讨论】:

以上是关于如何在Docker中初始化数据库后启动flyway的主要内容,如果未能解决你的问题,请参考以下文章

如何一起使用Spring启动,JOOQ和Flyway?

在 Spring Boot 应用程序上使用 Flyway 时如何在 H2 中加载初始数据?

如何在不同的(依赖的)容器启动后对 docker 容器运行 .sql 脚本?

flyway的快速入门教程

flyway迁移后运行data.sql文件

flyway 在初始模式创建后不创建模式