Flyway无法从Docker-Container连接到MySQL

时间:2019-05-10 08:59:08

标签: mysql spring-boot docker docker-compose flyway

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)

我试图用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

错误日志

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端口?

1 个答案:

答案 0 :(得分:0)

从Spring Boot 2.x开始,flyway configuration keys need to be prefixedspring.

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

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

设置空的MYSQL_ROOT_PASSWORD环境变量将有效,除了错字:

- MYSQL_ALLOW_EMPTY_PASSWOR=yes

应为:

- MYSQL_ALLOW_EMPTY_PASSWORD=yes