docker mysql initdb 脚本无法连接到数据库

Posted

技术标签:

【中文标题】docker mysql initdb 脚本无法连接到数据库【英文标题】:docker mysql initdb script can't connect to database 【发布时间】:2018-10-29 08:38:58 【问题描述】:

我正在使用 mysql docker 容器,并将初始化脚本添加到 docker-entrypoint-initdb.d 中,如下所述:https://hub.docker.com/_/mysql/(初始化新实例)。

init 脚本应该使用 flyway 命令行工具来迁移数据库内容。奇怪的是,我收到一个错误,即 flyway 无法连接到数据库。为什么会这样?据我了解,此时数据库应该已启动并运行。

[...]
2018-05-18T20:52:45.558031Z 0 [Note] mysqld: ready for connections.
Version: '5.7.22'  socket: '/var/run/mysqld/mysqld.sock'  port: 0  MySQL Community Server (GPL)
Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
2018-05-18T20:52:50.940818Z 4 [Warning] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode.
2018-05-18T20:52:50.940841Z 4 [Warning] 'user' entry 'mysql.session@localhost' ignored in --skip-name-resolve mode.
2018-05-18T20:52:50.940864Z 4 [Warning] 'user' entry 'mysql.sys@localhost' ignored in --skip-name-resolve mode.
2018-05-18T20:52:50.940881Z 4 [Warning] 'db' entry 'performance_schema mysql.session@localhost' ignored in --skip-name-resolve mode.
2018-05-18T20:52:50.940886Z 4 [Warning] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode.
2018-05-18T20:52:50.940897Z 4 [Warning] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode.
2018-05-18T20:52:50.940926Z 4 [Warning] 'tables_priv' entry 'user mysql.session@localhost' ignored in --skip-name-resolve mode.
2018-05-18T20:52:50.940935Z 4 [Warning] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode.
mysql: [Warning] Using a password on the command line interface can be insecure.

/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/migrate.sh
Flyway Community Edition 5.0.7 by Boxfuse

ERROR: 
Unable to obtain connection from database (jdbc:mysql://localhost/db) for user 'root': Could not connect to address=(host=localhost)(port=3306)(type=master) : Connection refused (Connection refused)
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SQL State  : 08
Error Code : -1
Message    : Could not connect to address=(host=localhost)(port=3306)(type=master) : Connection refused (Connection refused)

【问题讨论】:

【参考方案1】:

似乎在 initdb 脚本被称为 MySQL 时只能通过套接字而不是通过网络连接。因此flyway无法运行。我现在将第二个 docker 容器用于连接到 MySQL 容器的 flyway。这很好用。

【讨论】:

以上是关于docker mysql initdb 脚本无法连接到数据库的主要内容,如果未能解决你的问题,请参考以下文章

Docker“docker-entrypoint-initdb.d”脚本未执行

Docker-compose mysql:导入 .sql

无法使用 docker 容器连接到 MySQL 数据库

从头盔图访问/docker-entrypoint-initdb.d

docker mysql容器启动后退出

如何从 docker 容器中的 python 脚本连接到 localhost 上的 mysql 数据库