简体   繁体   English

Flyway无法从Docker-Container连接到MySQL

[英]Flyway cannot connect to MySQL from Docker-Container

Flyway tries to connect to MySQL Docker-Container. Flyway尝试连接到MySQL Docker-Container。 Flyway is integrated in a Spring Boot Project. Flyway集成在Spring Boot项目中。 Spring Boot Project and the database are starting from a docker-compose. Spring Boot Project和数据库都是从docker-compose开始的。

There is no possibility to connect with the database-client to the database-container. 无法将数据库客户端连接到数据库容器。

In the error-log of Flyway, there is the message: 在Flyway的错误日志中,显示以下消息:

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

I've tried to solve the problem with several solutions from Stackoverflow. 我试图用Stackoverflow的几种解决方案解决问题。 I have tried to change up the application.properties file. 我试图更改application.properties文件。 Also tried to create a new user with a init.sql. 还尝试使用init.sql创建一个新用户。 With docker-compose, I tried to set the MySQL-root-password. 使用docker-compose,我尝试设置MySQL-root-password。

docker-compose.yml 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 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 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

How can I connect to the MySQL-container? 如何连接到MySQL容器? Is something wrong in my docker-compose-file? 我的docker-compose-file中有问题吗? Or do we need to open the 3306-port? 还是我们需要打开3306端口?

Since Spring Boot 2.x, the flyway configuration keys need to be prefixed with spring. 从Spring Boot 2.x开始, flyway配置键需要以 spring. 为前缀 spring. :

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

Without that prefix, your settings are ignored and it defaults to the root user and an empty password. 没有该前缀,您的设置将被忽略,并且默认为root用户和一个空密码。

Setting the empty MYSQL_ROOT_PASSWORD environment variable would have worked except for a typo: 设置空的MYSQL_ROOT_PASSWORD环境变量将起作用,但输入错误除外:

- MYSQL_ALLOW_EMPTY_PASSWOR=yes

should be: 应该:

- MYSQL_ALLOW_EMPTY_PASSWORD=yes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM