如果Docker的MariaDB容器可连接,并完成了所有迁移工作

“mariadb镜像的官方页面位于https://hub.docker.com/_/mariadb”

MySQL初始化完成前不要建立连接。

根据这一项,表示在诸多事项结束之前将不接受连接,因此如果想要使用docker-compose之类的同时启动多个容器进行利用的话,请注意。

image.png

那么让我们来实际观察一下 Mariadb 容器的行为吧。

$ mkdir /tmp/mariadb
$ cd /tmp/mariadb
$ vim docker-compose.yml
version: '2'
services:
  mariadb:
    image: mariadb:latest
    environment:
      - MYSQL_USER=user
      - MYSQL_PASSWORD=password
      - MYSQL_RANDOM_ROOT_PASSWORD=yes
    volumes:
      - ./migrations:/docker-entrypoint-initdb.d

我将存放一些只是执行迁移并休眠的 SQL 语句。

$ mkdir migrations
$ vim migrations/00-sleep.sql
select sleep(3);

不知怎么的,假设有多个情况,我会事先复制下来。

$ cp migrations/00-sleep.sql migrations/01-sleep.sql

悠然地启动。

$ docker-compose up

这样一来,日志就会以这种方式流动。

mariadb_1  | Initializing database
mariadb_1  |
(略)
mariadb_1  |
mariadb_1  | Database initialized
mariadb_1  | MySQL init process in progress...
(略)
mariadb_1  | 2018-12-27  6:24:38 0 [Note] mysqld: ready for connections.
(略)
mariadb_1  |
mariadb_1  | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/00-sleep.sql
mariadb_1  | sleep(3)
mariadb_1  | 0
mariadb_1  |
mariadb_1  |
mariadb_1  | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/01-sleep.sql
mariadb_1  | sleep(3)
mariadb_1  | 0
mariadb_1  |
mariadb_1  |
mariadb_1  | 2018-12-27  6:24:47 0 [Note] mysqld (initiated by: unknown): Normal shutdown
(略)
mariadb_1  | 2018-12-27  6:24:48 0 [Note] mysqld: Shutdown complete
mariadb_1  |
mariadb_1  |
mariadb_1  | MySQL init process done. Ready for start up.
mariadb_1  |
(略)
mariadb_1  | 2018-12-27  6:24:49 0 [Note] mysqld: ready for connections.
mariadb_1  | Version: '10.3.11-MariaDB-1:10.3.11+maria~bionic'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  mariadb.org binary distribution

根据这个看起来的样子,似乎

    1. 启动并运行初始化处理

 

    1. 准备好连接

 

    1. 执行数据迁移

 

    1. 关闭一次

 

    再次启动并准备好连接

听起来不错。

换句话说,如果想从其他容器中使用mariadb,例如使用dockerize的wait等待mariadb容器的3306端口打开,但仅仅等待并不完整,还需要检查是否真正完成了最新的迁移SQL。否则,根据上述流程,在迁移之前或者迁移进行中,mariadb就准备好连接了,如果此时开始运行其他容器的自动测试等操作就会出问题。我一直这样想。

我先放下来。

一度放下来。

暂时放下。

先放下来。

$ docker-compose down

在实际中,何时能接受来自外部的连接?通过编辑docker-compose.yml文件,添加一个名为”wait”的容器。

$ vim docker-compose.yml
version: '2'
services:
  mariadb:
    image: mariadb:latest
    environment:
      - MYSQL_USER=user
      - MYSQL_PASSWORD=password
      - MYSQL_RANDOM_ROOT_PASSWORD=yes
    volumes:
      - ./migrations:/docker-entrypoint-initdb.d
  # ↓ 追加
  wait:
    image: jwilder/dockerize:latest
    depends_on:
      - mariadb
    command: dockerize -wait tcp://mariadb:3306 -timeout 30s echo GOAL!

在这里,我决定使用以下指令并提取出行:通过等待 Docker 化的 wait 来等待 MariaDB 端口打开,一旦打开就显示 GOAL。

dockerize -wait tcp://mariadb:3306 -timeout 30s echo GOAL!

现在开始。

$ docker-compose up
mariadb_1  | Initializing database
wait_1     | 2018/12/27 07:09:19 Waiting for: tcp://mariadb:3306
wait_1     | 2018/12/27 07:09:19 Problem with dial: dial tcp 172.26.0.2:3306: getsockopt: connection refused. Sleeping 1s
(略)
mariadb_1  |
mariadb_1  | Database initialized
mariadb_1  | MySQL init process in progress...
(略)
mariadb_1  | 2018-12-27  7:09:22 0 [Note] mysqld: ready for connections.
(略)
mariadb_1  | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/00-sleep.sql
wait_1     | 2018/12/27 07:09:25 Problem with dial: dial tcp 172.26.0.2:3306: getsockopt: connection refused. Sleeping 1s
wait_1     | 2018/12/27 07:09:26 Problem with dial: dial tcp 172.26.0.2:3306: getsockopt: connection refused. Sleeping 1s
wait_1     | 2018/12/27 07:09:27 Problem with dial: dial tcp 172.26.0.2:3306: getsockopt: connection refused. Sleeping 1s
mariadb_1  | sleep(3)
mariadb_1  | 0
mariadb_1  |
mariadb_1  |
mariadb_1  | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/01-sleep.sql
wait_1     | 2018/12/27 07:09:28 Problem with dial: dial tcp 172.26.0.2:3306: getsockopt: connection refused. Sleeping 1s
wait_1     | 2018/12/27 07:09:29 Problem with dial: dial tcp 172.26.0.2:3306: getsockopt: connection refused. Sleeping 1s
wait_1     | 2018/12/27 07:09:30 Problem with dial: dial tcp 172.26.0.2:3306: getsockopt: connection refused. Sleeping 1s
mariadb_1  | sleep(3)
mariadb_1  | 0
mariadb_1  |
mariadb_1  |
mariadb_1  | 2018-12-27  7:09:30 0 [Note] mysqld (initiated by: unknown): Normal shutdown
(略)
mariadb_1  | 2018-12-27  7:09:31 0 [Note] mysqld: Shutdown complete
mariadb_1  |
mariadb_1  |
mariadb_1  | MySQL init process done. Ready for start up.
mariadb_1  |
(略)
mariadb_1  | 2018-12-27  7:09:32 0 [Note] mysqld: ready for connections.
mariadb_1  | Version: '10.3.11-MariaDB-1:10.3.11+maria~bionic'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  mariadb.org binary distribution
wait_1     | 2018/12/27 07:09:32 Connected to tcp://mariadb:3306
wait_1     | GOAL!
wait_1     | 2018/12/27 07:09:32 Command finished successfully.

通过此举,真是太感激了。

    1. 启动并进行初始化处理

 

    1. 准备连接(但仍无法从外部连接)

 

    1. 运行迁移

 

    1. 进行一次关机

 

    再次启动并准备连接(此时终于能从外部连接,并显示“GOAL!”)

我意识到这个时机。

简而言之,只要连接正常,迁移工作就已经全部完成了,不需要考虑任何复杂的事情。这很容易理解。

广告
将在 10 秒后关闭
bannerAds