gpt4 book ai didi

java - 在 mysql 上使用 docker-compose 时遇到问题

转载 作者:行者123 更新时间:2023-12-01 18:26:43 24 4
gpt4 key购买 nike

我在尝试使用 Docker-compose 连接 MySQL 数据库时遇到问题。我的应用程序启动,直到尝试通过 docker 连接到数据库,而我可以从 IDE 连接到同一个数据库。当我从 IDE 运行它并使用运行 Docker 的 MySQL 时,我可以使用 spring.datasource.url=jdbc:mysql://localhost:3306/exchanger 连接到它、添加、删除等。但是从 docker-compose 启动我的应用程序似乎会导致连接错误。我尝试了不同的 datasource.url,但没有任何帮助。

这是我的文件:

Dockerfile:

FROM openjdk:8u201-jdk-alpine3.9
ADD target/exchanger-0.0.1-SNAPSHOT.jar .
EXPOSE 8080
CMD java -jar exchanger-0.0.1-SNAPSHOT.jar

Docker-compose.yml

version: '3'                          
services:

db:
container_name: db
image: mysql
environment:
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: exchanger
MYSQL_USER: user
ports:
- "3306:3306"
- "33060:33060"
restart : always
volumes:
- ./mysql-data:/var/lib/mysql


exchanger:
build: ./
ports:
- "8080:8080"
depends_on:
- db

应用程序属性:

server.port=8080
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL57Dialect
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://db:3306/exchanger
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create-drop
logging.level.org.hibernate.SQL=info

控制台日志

Starting db ... done
Starting exchanger_exchanger_1 ... done
Attaching to db, exchanger_exchanger_1
db | 2020-02-13 23:28:02+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.19-1debian9 started.
db | 2020-02-13 23:28:02+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
db | 2020-02-13 23:28:02+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.19-1debian9 started.
db | 2020-02-13T23:28:02.638670Z 0 [Warning] [MY-010139] [Server] Changed limits: max_open_files: 1024 (requested 8161)
db | 2020-02-13T23:28:02.638684Z 0 [Warning] [MY-010142] [Server] Changed limits: table_open_cache: 431 (requested 4000)
db | 2020-02-13T23:28:02.968321Z 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.
db | 2020-02-13T23:28:02.968508Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.19) starting as process 1
db | 2020-02-13T23:28:05.415554Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
db | 2020-02-13T23:28:05.461398Z 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.
db | 2020-02-13T23:28:05.504622Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.19' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.
db | 2020-02-13T23:28:05.777882Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060
exchanger_1 |
exchanger_1 | . ____ _ __ _ _
exchanger_1 | /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
exchanger_1 | ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
exchanger_1 | \\/ ___)| |_)| | | | | || (_| | ) ) ) )
exchanger_1 | ' |____| .__|_| |_|_| |_\__, | / / / /
exchanger_1 | =========|_|==============|___/=/_/_/_/
exchanger_1 | :: Spring Boot :: (v2.2.4.RELEASE)
exchanger_1 |
exchanger_1 | 2020-02-13 23:28:06.865 INFO 1 --- [ main] p.e.exchanger.ExchangerApplication : Starting ExchangerApplication v0.0.1-SNAPSHOT on f2c5f2d1f4c1 with PID 1 (/exchanger-0.0.1-SNAPSHOT.jar started by root in /)
exchanger_1 | 2020-02-13 23:28:06.873 INFO 1 --- [ main] p.e.exchanger.ExchangerApplication : No active profile set, falling back to default profiles: default
exchanger_1 | 2020-02-13 23:28:09.180 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
exchanger_1 | 2020-02-13 23:28:09.341 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 138ms. Found 3 JPA repository interfaces.
exchanger_1 | 2020-02-13 23:28:10.311 INFO 1 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
exchanger_1 | 2020-02-13 23:28:10.944 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
exchanger_1 | 2020-02-13 23:28:10.976 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
exchanger_1 | 2020-02-13 23:28:10.976 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.30]
exchanger_1 | 2020-02-13 23:28:11.128 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
exchanger_1 | 2020-02-13 23:28:11.128 INFO 1 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 4140 ms
exchanger_1 | 2020-02-13 23:28:11.752 INFO 1 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
exchanger_1 | 2020-02-13 23:28:11.911 INFO 1 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {5.4.10.Final}
exchanger_1 | 2020-02-13 23:28:12.181 INFO 1 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
exchanger_1 | 2020-02-13 23:28:12.410 INFO 1 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
exchanger_1 | 2020-02-13 23:28:13.660 ERROR 1 --- [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception during pool initialization.
exchanger_1 |
exchanger_1 | com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
exchanger_1 |
exchanger_1 | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
exchanger_1 | at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
exchanger_1 | at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
exchanger_1 | at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:836) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
exchanger_1 | at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
exchanger_1 | at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
exchanger_1 | at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:197) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
exchanger_1 | at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-3.4.2.jar!/:na]
exchanger_1 | at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:354) ~[HikariCP-3.4.2.jar!/:na]
exchanger_1 | at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:202) ~[HikariCP-3.4.2.jar!/:na]
exchanger_1 | at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:473) [HikariCP-3.4.2.jar!/:na]
exchanger_1 | at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:554) [HikariCP-3.4.2.jar!/:na]
exchanger_1 | at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) [HikariCP-3.4.2.jar!/:na]
exchanger_1 | at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) [HikariCP-3.4.2.jar!/:na]
exchanger_1 | at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) [hibernate-core-5.4.10.Final.jar!/:5.4.10.Final]
exchanger_1 | at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:180) [hibernate-core-5.4.10.Final.jar!/:5.4.10.Final]
...

最佳答案

我已经解决了这个问题

第一次设置数据库时出现问题。我认为创建 MySQL 数据库时出现问题,整个卷 ./myslq-data 错误。

我刚刚删除了目录 mysql-data 并再次创建了 run docker-compose。它再次创造了音量并且效果很好。

关于java - 在 mysql 上使用 docker-compose 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60218105/

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