Flyway无法从Docker容器连接到MySQL

4

Flyway尝试连接MySQL Docker容器。 Flyway集成在Spring Boot项目中。 Spring Boot项目和数据库从docker-compose启动。

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

Flyway的错误日志中出现以下消息:

Original Answer翻译成: 最初的回答

Access denied for user 'root'@'172.19.0.3' (using password: NO)
我尝试过从Stackoverflow上找到几种解决方案来解决问题。 我已经尝试过更改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

Errorlog

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-log

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文件有什么问题吗? 还是说我们需要打开3306端口?

原始答案: "Original Answer"


从日志中 spring-boot-jpa-docker-webapp_1 | Message : Access denied for user 'root'@'172.19.0.3' (using password: NO) 可以看出,您的应用程序仍在使用 root 凭据来进行数据库身份验证,请先检查您的应用程序。 - nivhty
你的数据库是否允许访问端口?我会在compose文件的数据库设置中添加PORT <外部端口>: <MySQL标准端口>。也许在你的情况下,使用EXPOSE而不是PORT就足够了。 - user8914226
1个回答

阿里云服务器只需要99元/年,新老用户同享,点击查看详情
0
自从Spring Boot 2.x以来,flyway配置键需要加上前缀spring.:
spring.flyway.user=thelegend27
spring.flyway.password=1234

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

设置空的MYSQL_ROOT_PASSWORD环境变量本来是有效的,除了一个拼写错误:

- MYSQL_ALLOW_EMPTY_PASSWOR=yes
应该是:

应该是:

- MYSQL_ALLOW_EMPTY_PASSWORD=yes

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,