简体   繁体   English

从 docker 容器连接到主机 mysql db 时连接被拒绝

[英]Connection refused when connecting to host mysql db from docker container

I am trying to connect to mysql server running on host machine (localhost) from a spring app running inside a docker container我正在尝试从在 docker 容器内运行的 spring 应用程序连接到在主机(localhost)上运行的 mysql 服务器

this is my Dockerfile这是我的 Dockerfile

FROM openjdk:11
ADD build/libs/demo-0.0.1-SNAPSHOT.jar demo.jar
EXPOSE 8000
RUN mkdir -p tmp/scripts
RUN mkdir -p tmp/logs
ENTRYPOINT ["java","-jar","/demo.jar"]

I am using Docker desktop for mac v2.3.0.3 with engine version 19.03.8我正在为 mac v2.3.0.3 使用 Docker 桌面,引擎版本为 19.03.8

This is may hikariCP datasource connection properties这可能是 hikariCP 数据源连接属性

spring.datasource.jdbcUrl=jdbc:mysql://**host.docker.internal:3306**/freshid
spring.datasource.username=sa
spring.datasource.password=test

When I build docker build -f Dockerfile -t demo.当我构建docker build -f Dockerfile -t demo. and run the image as docker run -p 8000:8000 demo并将图像运行为docker run -p 8000:8000 demo

I get the following error on application start up我在应用程序启动时收到以下错误

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
    at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835)
    at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455)
    at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
    at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199)
    at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138)
    at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:354)
    at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:202)
    at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:473)
    at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:554)
    ... 64 common frames omitted
Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151)
    at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167)
    at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:91)
    at com.mysql.cj.NativeSession.connect(NativeSession.java:152)
    at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:955)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:825)
    ... 72 common frames omitted
Caused by: java.net.ConnectException: Connection refused (Connection refused)
    at java.base/java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399)
    at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242)
    at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224)
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403)
    at java.base/java.net.Socket.connect(Socket.java:609)
    at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155)
    at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65)
    ... 75 common frames omitted

I uninstalled and re-installed the docker (19.03.8) and it worked.我卸载并重新安装了 docker (19.03.8) 并且它工作正常。 The final connection string is jdbc:mysql://host.docker.internal:3306/db .最终的连接字符串是jdbc:mysql://host.docker.internal:3306/db This does not conclude any technical correctness but it worked.这并不能得出任何技术正确性的结论,但它确实有效。 Thanks.谢谢。

I would suggest a simple solution.我会建议一个简单的解决方案。

Step 1: Turboshooting: Try to run springboot appliction in you machine, outside container.第 1 步:Turboshooting:尝试在您的机器、容器外部运行 springboot 应用程序。 If works fine, then there is no issue your application.如果工作正常,那么您的应用程序没有问题。 (Make sure while testing locally you change your DB connection string from host.docker.internal to localhost ) (确保在本地测试时将数据库连接字符串从host.docker.internallocalhost

Step 2: Please expose your DB port, that is 3306 by default in case of mysql when you run Docker Container with imperitive command步骤 2:当您使用命令式命令运行 Docker Container 时,请公开您的 DB 端口,默认情况下为3306 ,如果是 mysql

docker run -p 8000:8000 -p 3306:3306 demo

This should resolve your issue这应该可以解决您的问题

暂无
暂无

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

相关问题 连接拒绝从 docker 连接到 Elasticsearch docker 容器 - Connection Refused connecting from docker to Elasticsearch docker container Docker容器中的Java服务未连接到主机mysql - Java service in docker container not connecting to host mysql 从Selenium RemoteWebDriver与Selenium Docker Firefox容器连接时,如何解决“拒绝连接”错误? - How to fix “Connection Refused” error while connecting with Selenium Docker Firefox container from Selenium RemoteWebDriver? 连接到 jmeter 从站 docker 容器时出现连接被拒绝错误 - Connection refused error while connecting to jmeter slave docker container 如何:运行 java jar 的 Docker 容器连接到主机 mysql 服务器? - How to: Docker container running java jar connecting to host mysql server? java.net.connectexception 连接被拒绝(连接被拒绝):无法从 docker 容器连接到本地 rabbitmq - java.net.connectexception connection refused (connection refused) : not able to connect to local rabbitmq from docker container Docker容器中数据库容器和docker中的java容器之间的连接被拒绝 - Connection refused in Docker container between database container and java container in docker 可怕的 Java SpringBoot 应用程序未通过 Docker-compose java.net.ConnectException 连接到 MySQL:连接被拒绝 - The dreaded Java SpringBoot app not connecting to MySQL with Docker-compose java.net.ConnectException: Connection refused 无法在 docker 容器之间进行通信 - 连接被拒绝 - cannot communicate between docker container - connection refused (Docker+SpringBoot+Mysql) Mysql 连接被拒绝 - (Docker+SpringBoot+Mysql) Mysql connection refused
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM