gpt4 book ai didi

java - Docker compose spring boot 无法连接到 mysql db

转载 作者:行者123 更新时间:2023-12-01 19:35:09 26 4
gpt4 key购买 nike

我有连接到 mysql 数据库的 spring boot 应用程序。如果我以经典方式在本地运行它,它就可以正常工作。但是当我尝试执行 dockerise 时 - 数据库成功启动,但应用程序看不到数据库。

我的 Dockerfile:

FROM openjdk:11-jdk-slim
ADD start-local.sh /start-local.sh
RUN chmod +x start-local.sh
ENTRYPOINT [ "sh", "-c", "./start-local.sh" ]

我的start-local.sh(我添加它是为了我的应用程序等待数据库启动):

#!/bin/sh

echo "Waiting for the database server to start"
echo "********************************************************"
apt-get update && apt-get install -y netcat
while ! nc -z mysql 3306; do sleep 3; done
echo "******** Database Server has started"
echo "Starting account service"
java -jar /mnt/service-2.4.0.jar

最后是我的 docker-compose.yml 文件:

version: '2'
services:
mysql:
image: mysql:5.7.23
environment:
- MYSQL_ROOT_PASSWORD=root
entrypoint:
sh -c "echo 'CREATE DATABASE IF NOT EXISTS books; CREATE DATABASE IF NOT EXISTS notifications' > /docker-entrypoint-initdb.d/init.sql;/usr/local/bin/docker-entrypoint.sh --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --sql_mode="""
ports:
- "3306:3306"

account_app:
image: service_img
environment:
- "SPRING_PROFILES_ACTIVE=local"
ports:
- "8083:8083"
volumes:
- "/build/libs/:/mnt"

我执行的内容:

  • 首先,我构建了我的 service_img
  • 然后我执行 docker-compose up。在日志中,我看到数据库启动日志,在我的应用程序启动日志之后,然后出现错误:

| 2019-09-23 09:51:14.006 DEBUG [account-service,,,] 286 --- [
main] c.m.l.c.c.d.p.DatasourceConfiguration : Creating datasource
account_app_1 | 2019-09-23 09:51:14.014 INFO [account-service,,,] 286 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting... account_app_1 | 2019-09-23 09:51:15.222 ERROR [account-service,,,] 286 --- [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception during pool initialization. account_app_1 | account_app_1 | com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure account_app_1 | account_app_1 | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. account_app_1 | at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-java-8.0.15.jar!/:8.0.15] account_app_1 | at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-java-8.0.15.jar!/:8.0.15] account_app_1 | at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835) ~[mysql-connector-java-8.0.15.jar!/:8.0.15] account_app_1 | at com.mysql.cj.jdbc.ConnectionImpl.(ConnectionImpl.java:455) ~[mysql-connector-java-8.0.15.jar!/:8.0.15] account_app_1 | at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240) ~[mysql-connector-java-8.0.15.jar!/:8.0.15] account_app_1 | at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199) ~[mysql-connector-java-8.0.15.jar!/:8.0.15] The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. account_app_1 | at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-java-8.0.15.jar!/:8.0.15] account_app_1 | at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-java-8.0.15.jar!/:8.0.15] account_app_1 | at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835) ~[mysql-connector-java-8.0.15.jar!/:8.0.15] account_app_1 | at com.mysql.cj.jdbc.ConnectionImpl.(ConnectionImpl.java:455) ~[mysql-connector-java-8.0.15.jar!/:8.0.15] account_app_1 | at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240) ~[mysql-connector-java-8.0.15.jar!/:8.0.15] account_app_1 | at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199) ~[mysql-connector-java-8.0.15.jar!/:8.0.15] account_app_1 | at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:136) ~[HikariCP-3.2.0.jar!/:na] account_app_1 | at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:369) ~[HikariCP-3.2.0.jar!/:na] account_app_1 | at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:198) ~[HikariCP-3.2.0.jar!/:na] account_app_1 | at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:467) ~[HikariCP-3.2.0.jar!/:na] account_app_1 | at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:541) ~[HikariCP-3.2.0.jar!/:na] account_app_1 | ... 206 common frames omitted account_app_1 | Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure account_app_1 | account_app_1 | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. account_app_1 | at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na] account_app_1 | at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:na] account_app_1 | at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na] account_app_1 | at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490) ~[na:na] account_app_1 | at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) ~[mysql-connector-java-8.0.15.jar!/:8.0.15] account_app_1 | at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105) ~[mysql-connector-java-8.0.15.jar!/:8.0.15] account_app_1 | at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151) ~[mysql-connector-java-8.0.15.jar!/:8.0.15] account_app_1 | at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167) ~[mysql-connector-java-8.0.15.jar!/:8.0.15] account_app_1 | at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:91) ~[mysql-connector-java-8.0.15.jar!/:8.0.15] account_app_1 | at com.mysql.cj.NativeSession.connect(NativeSession.java:152) ~[mysql-connector-java-8.0.15.jar!/:8.0.15] account_app_1 | at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:955) ~[mysql-connector-java-8.0.15.jar!/:8.0.15] account_app_1 | at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:825) ~[mysql-connector-java-8.0.15.jar!/:8.0.15] account_app_1 |
... 214 common frames omitted account_app_1 | Caused by: java.net.ConnectException: Connection refused (Connection refused) account_app_1 | at java.base/java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:na] account_app_1 | at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399) ~[na:na] account_app_1 | at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242) ~[na:na] account_app_1 | at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224) ~[na:na] account_app_1 | at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403) ~[na:na] account_app_1 | at java.base/java.net.Socket.connect(Socket.java:591) ~[na:na] account_app_1 | at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155) ~[mysql-connector-java-8.0.15.jar!/:8.0.15] account_app_1 | at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65) ~[mysql-connector-java-8.0.15.jar!/:8.0.15] account_app_1 |
... 217 common frames omitted account_app_1 |

有什么建议吗?

最佳答案

当运行撰写“堆栈”时,docker 使用自己的 DNS 机制。

这意味着,如果您在 account_app 容器内针对主机名 mysql 执行 DNS 查询,该查询将返回名为 的容器的 IP 地址mysql(可能以堆栈名称为前缀)

简而言之:将 Spring 应用程序配置为连接到 mysql 数据库,而不是在本地主机上,而是在 mysql 上,然后就可以开始了。

您可能可以设置一些环境变量来配置它。

关于java - Docker compose spring boot 无法连接到 mysql db,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58059893/

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