代码之家  ›  专栏  ›  技术社区  ›  Werzi2001

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

  •  1
  • Werzi2001  · 技术社区  · 8 年前

    我用的是 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 回复  |  直到 8 年前
        1
  •  3
  •   Werzi2001    8 年前

    似乎在这一点上 initdb 名为MySQL的脚本只能通过套接字使用,不能通过网络连接使用。因此flyway无法运行。我现在为flyway使用第二个docker容器,它连接到MySQL容器。这个很好用。