Flyway 无法从 Docker-Container 连接到 MySQL

Posted

技术标签:

【中文标题】Flyway 无法从 Docker-Container 连接到 MySQL【英文标题】:Flyway cannot connect to MySQL from Docker-Container 【发布时间】:2019-09-28 03:45:59 【问题描述】:

Flyway 尝试连接到 mysql Docker-Container。 Flyway 集成在 Spring Boot 项目中。 Spring Boot Project 和数据库都是从 docker-compose 开始的。

无法将数据库客户端连接到数据库容器。

在Flyway的错误日志中,有消息:

Access denied for user 'root'@'172.19.0.3' (using password: NO)

我尝试使用 *** 的几种解决方案来解决这个问题。 我试图更改 application.properties 文件。 还尝试使用 init.sql 创建一个新用户。 使用 docker-compose,我尝试设置 MySQL-root-password。

docker-compose.yml

version: '3'

services: 
  docker-mysql:
    image: mysql:latest
    container_name: docker-mysql
    command: --init-file /data/application/init.sql
    volumes:
        - .init.sql:/data/application/init.sql
    networks:
      - mt-network
    environment:
      - MYSQL_ROOT_PASSWORD=
      - MYSQL_DATABASE=chat
      - MYSQL_USER=thelegend27
      - MYSQL_PASSWORD=1234
      - MYSQL_ALLOW_EMPTY_PASSWOR=yes
  spring-boot-jpa-docker-webapp:
    image: chat
    depends_on:
      - docker-mysql
    links:
      - docker-mysql
    ports:
      - 8080:8080
    networks:
      - mt-network
    environment:
      - SPRING_DATASOURCE_URL=jdbc:mysql://docker-mysql:3306/chat?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
      - DATABASE_HOST_1=docker-mysql
      - DATABASE_HOST=localhost
      - DATABASE_USER=thelegend27
      - DATABASE_PASSWORD=1234
      - DATABASE_NAME=chat  
      - DATABASE_PORT=3306
networks:
  mt-network:
    driver: bridge

application.properties

#spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://docker-mysql:3306/chat?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=thelegend27
spring.datasource.password=1234

flyway.user=thelegend27
flyway.password=1234


logging.level.org.flywaydb=debug

错误日志

spring-boot-jpa-docker-webapp_1  | SQL State  : 28000
spring-boot-jpa-docker-webapp_1  | Error Code : 1045
spring-boot-jpa-docker-webapp_1  | Message    : Access denied for user 'root'@'172.19.0.3' (using password: NO)
spring-boot-jpa-docker-webapp_1  |
spring-boot-jpa-docker-webapp_1  |  at org.flywaydb.core.internal.jdbc.JdbcUtils.openConnection(JdbcUtils.java:60) ~[flyway-core-5.2.4.jar!/:na]
spring-boot-jpa-docker-webapp_1  |  at org.flywaydb.core.internal.database.DatabaseFactory.createDatabase(DatabaseFactory.java:72) ~[flyway-core-5.2.4.jar!/:na]
spring-boot-jpa-docker-webapp_1  |  at org.flywaydb.core.Flyway.execute(Flyway.java:1670) ~[flyway-core-5.2.4.jar!/:na]
spring-boot-jpa-docker-webapp_1  |  at org.flywaydb.core.Flyway.migrate(Flyway.java:1356) ~[flyway-core-5.2.4.jar!/:na]
spring-boot-jpa-docker-webapp_1  |  at org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer.afterPropertiesSet(FlywayMigrationInitializer.java:66) ~[spring-boot-autoconfigure-2.1.4.RELEASE.jar!/:2.1.4.RELEASE]
spring-boot-jpa-docker-webapp_1  |  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1837) ~[spring-beans-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
spring-boot-jpa-docker-webapp_1  |  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1774) ~[spring-beans-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
spring-boot-jpa-docker-webapp_1  |  ... 26 common frames omitted
spring-boot-jpa-docker-webapp_1  | Caused by: java.sql.SQLException: Access denied for user 'root'@'172.19.0.3' (using password: NO)
spring-boot-jpa-docker-webapp_1  |  at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
spring-boot-jpa-docker-webapp_1  |  at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
spring-boot-jpa-docker-webapp_1  |  at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
spring-boot-jpa-docker-webapp_1  |  at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
spring-boot-jpa-docker-webapp_1  |  at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
spring-boot-jpa-docker-webapp_1  |  at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
spring-boot-jpa-docker-webapp_1  |  at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199) ~[mysql-connector-java-8.0.15.jar!/:8.0.15]
spring-boot-jpa-docker-webapp_1  |  at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:136) ~[HikariCP-3.2.0.jar!/:na]
spring-boot-jpa-docker-webapp_1  |  at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:369) ~[HikariCP-3.2.0.jar!/:na]
spring-boot-jpa-docker-webapp_1  |  at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:198) ~[HikariCP-3.2.0.jar!/:na]
spring-boot-jpa-docker-webapp_1  |  at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:467) ~[HikariCP-3.2.0.jar!/:na]
spring-boot-jpa-docker-webapp_1  |  at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:541) ~[HikariCP-3.2.0.jar!/:na]
spring-boot-jpa-docker-webapp_1  |  at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) ~[HikariCP-3.2.0.jar!/:na]
spring-boot-jpa-docker-webapp_1  |  at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-3.2.0.jar!/:na]
spring-boot-jpa-docker-webapp_1  |  at org.flywaydb.core.internal.jdbc.JdbcUtils.openConnection(JdbcUtils.java:56) ~[flyway-core-5.2.4.jar!/:na]
spring-boot-jpa-docker-webapp_1  |  ... 32 common frames omitted

MySQL 日志

docker-mysql                     | 2019-05-10T08:39:28.600028Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
docker-mysql                     | 2019-05-10T08:39:28.600195Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.16) starting as process 1
docker-mysql                     | 2019-05-10T08:39:29.618709Z 0 [System] [MY-010229] [Server] Starting crash recovery...
docker-mysql                     | 2019-05-10T08:39:29.630817Z 0 [System] [MY-010232] [Server] Crash recovery finished.
docker-mysql                     | 2019-05-10T08:39:29.717531Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
docker-mysql                     | 2019-05-10T08:39:29.720926Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
docker-mysql                     | 2019-05-10T08:39:29.777044Z 6 [ERROR] [MY-000061] [Server] 1105  Bootstrap file error, return code (1). Nearest query: ''.
docker-mysql                     | 2019-05-10T08:39:29.777578Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.16'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.
docker-mysql                     | 2019-05-10T08:39:29.819539Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060

如何连接到 MySQL 容器? 我的 docker-compose-file 有问题吗? 还是需要打开3306端口?

【问题讨论】:

从日志spring-boot-jpa-docker-webapp_1 | Message : Access denied for user 'root'@'172.19.0.3' (using password: NO)您的应用程序仍在使用根凭据对数据库进行身份验证,请先检查您的应用程序。 您的数据库是否允许访问该端口?我会将 PORT : 添加到撰写文件的数据库设置中。在你的情况下,也许 EXPOSE 而不是 PORT 就足够了 【参考方案1】:

从 Spring Boot 2.x 开始,flyway configuration keys need to be prefixed 和 spring.

spring.flyway.user=thelegend27
spring.flyway.password=1234

没有该前缀,您的设置将被忽略,默认为 root 用户和空密码。

设置空的 MYSQL_ROOT_PASSWORD 环境变量会起作用,除非有错字:

- MYSQL_ALLOW_EMPTY_PASSWOR=yes

应该是:

- MYSQL_ALLOW_EMPTY_PASSWORD=yes

【讨论】:

以上是关于Flyway 无法从 Docker-Container 连接到 MySQL的主要内容,如果未能解决你的问题,请参考以下文章

Docker Compose - Flyway - 无法从数据源获取 Jdbc 连接

如何从 Flyway 迁移名称中删除前缀?

Heroku Postgres 升级后 Flyway 无法连接到数据库

如何从 IntelliJ IDEA 中的模型类生成 Flyway 迁移脚本?

将 flyway 版本 1.5 升级到 3.2

Flyway 无法使用 Snowflake JDBC