gpt4 book ai didi

mysql - Flyway 无法从 Docker-Container 连接到 MySQL

转载 作者:行者123 更新时间:2023-11-30 21:31:10 24 4
gpt4 key购买 nike

Flyway 尝试连接到 MySQL Docker-Container。 Flyway 集成在一个 Spring Boot 项目中。 Spring Boot 项目和数据库是从 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

应用程序属性

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

关于mysql - Flyway 无法从 Docker-Container 连接到 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56074108/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com