当前位置: 首页 > 知识库问答 >
问题:

运行在docker上的Springboot微服务无法连接到mysql

饶承宣
2023-03-14

例外情况是:

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
app_1  |    at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)
app_1  |    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
app_1  |    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:836)
app_1  |    at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456)
app_1  |    at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)
app_1  |    at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:197)
app_1  |    at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:121)
app_1  |    at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:358)
app_1  |    at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206)
app_1  |    at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:477)
app_1  |    at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:560)
app_1  |    ... 69 more
app_1  | Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
app_1  | 
app_1  | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
app_1  |    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
app_1  |    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
app_1  |    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
app_1  |    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
app_1  |    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
app_1  |    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105)
app_1  |    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151)
app_1  |    at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167)
app_1  |    at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:91)
app_1  |    at com.mysql.cj.NativeSession.connect(NativeSession.java:144)
app_1  |    at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:956)
app_1  |    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:826)
app_1  |    ... 77 more
app_1  | Caused by: java.net.ConnectException: Connection refused (Connection refused)
app_1  |    at java.net.PlainSocketImpl.socketConnect(Native Method)
app_1  |    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
app_1  |    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
app_1  |    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
app_1  |    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
app_1  |    at java.net.Socket.connect(Socket.java:589)
app_1  |    at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155)
app_1  |    at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65)
app_1  |    ... 80 more

docker-compose文件是:

version: '3'

services:
  db:
    image: mysql:5.7
    restart: always
    environment:
      - MYSQL_DATABASE=inward
      # So you don't have to use root, but you can if you like
      - MYSQL_USER=root
      # You can use whatever password you like
      - MYSQL_PASSWORD=unroot
      # Password for root access
      - MYSQL_ROOT_PASSWORD=unroot
    ports:
      # <Port exposed> : < MySQL Port running inside container>
      - 3307:3306

  app:
    build:
      context: ./
      dockerfile: Dockerfile
    environment:
      SPRING_DATASOURCE_URL: jdbc:mysql://db:3306/inward?allowPublicKeyRetrieval=true&useSSL=false
    depends_on:
      - db
    working_dir: /app
    command: [sh, -c, 'mkdir -p ~/logs/; cd /src ; mvn clean spring-boot:run -Dspring.profiles.active=dev -DLOG_DIR=/root/logs/ -DLOG_FILE=hubstamper.log']
    ports:
      - "8080:8080"
    volumes:
      - "${HOME}/.m2:/root/.m2"

多克菲乌勒

FROM maven:3.5-jdk-8-alpine
COPY . /app
jdbcUrl=jdbc:mysql://db:3306/inward?allowPublicKeyRetrieval=true&useSSL=false
dataSource.user=root
dataSource.password=unroot
dataSource.cachePrepStmts=true
dataSource.prepStmtCacheSize=250
dataSource.prepStmtCacheSqlLimit=2048
driverClassName=com.mysql.jdbc.Driver
dataSource.driverClassName=com.mysql.jdbc.Driver

共有1个答案

闽阳州
2023-03-14

我可以用docker-compose文件解决这个问题

services:
  db:
    image: mysql:5.7
    restart: always
    environment:
      - MYSQL_DATABASE=inward
      # So you don't have to use root, but you can if you like
      - MYSQL_USER=root
      # You can use whatever password you like
      - MYSQL_PASSWORD=unroot
      # Password for root access
      - MYSQL_ROOT_PASSWORD=unroot
    ports:
      # <Port exposed> : < MySQL Port running inside container>
      - 3307:3306

  app:
    build:
      context: ./
      dockerfile: Dockerfile
    depends_on:
      - db
    working_dir: /app
    command: [sh, -c, 'mkdir -p ~/logs/; cd /src ; mvn clean spring-boot:run -Dspring.profiles.active=local -DLOG_DIR=/root/logs/ -DLOG_FILE=hubstamper.log']
    ports:
      - 8080:8080
    volumes:
      - "${HOME}/.m2:/root/.m2"

和应用程序-local.properties文件

jdbcUrl=jdbc:mysql://db:3306/inward?allowPublicKeyRetrieval=true&useSSL=false
dataSource.user=root
dataSource.password=unroot
dataSource.cachePrepStmts=true
dataSource.prepStmtCacheSize=250
dataSource.prepStmtCacheSqlLimit=2048
driverClassName=com.mysql.jdbc.Driver
dataSource.driverClassName=com.mysql.jdbc.Driver

和docker文件

FROM maven:3.5-jdk-8-alpine
COPY . /app
 类似资料:
  • 我有发现服务:https://github.com/Naresh-Chaurasia/API-MicroServices-Kafka/tree/master/Microservices-CQRS-SAGA-Kafka/DiscoveryService 我有产品服务:https://github.com/Naresh-Chaurasia/API-MicroServices-Kafka/tree/ma

  • 我正在docker容器中运行sql server` 所有这些都成功完成了。现在,当我尝试使用Java连接到数据库时,我得到了错误。 192.168.56.101是我的虚拟机的ip。我正在虚拟机上运行Linux。 我收到通信链路故障错误。 我在谷歌上搜索并找到了一些关于以下链接的解决方案,但没有一个对我有用。 使用JDBC和MySQL解决“通信链路故障” 如何连接docker中作为容器运行的MySQ

  • $docker容器运行--名称云--网络雇员-e mysql_root_password=root-e mysql_database=bootdb-d mysql:5.7 spring boot应用程序在本地-我试图在java应用程序和mysql容器之间建立连接: 配置属性: null spring-boot-starter-data-jpa mysql-connector-java-versio

  • 我的公司让我运行用openshift部署的微服务项目。 现有服务成功连接到具有此属性的rabbitmq服务, 现在我正在用我的笔记本电脑开发新服务,还没有部署到 OpenShift,现在我正在尝试连接到与其他代理相同的代理,所以我为 RabbitMQ 服务创建了新路由,将 5672 作为它的端口 这是我的路线YAML: 当我;我正在尝试将我的新服务与此属性连接: 我的服务未能设置连接。 如何解决这

  • 我遵循了Microsoft的官方文档,并安装了SQL Server Docker image 因此,我有一个SQL服务器映像在IP地址的Docker上运行172.17.0.2 我还可以使用带有虚拟密码的sqlcmd轻松连接到它 问题是我无法通过SSMS连接到它: 当然,在发布这个问题之前,我阅读了其他StackOverflow帖子,我尝试了多次登录: 本地主机,1433 如果localhost不能

  • 我尝试将我的服务(auth-service)连接到service Registry的Eureka服务器。Eureka服务器正在运行,但当我尝试连接身份验证服务时,什么也没有发生。 Eureka服务器的URL:http://localhost:8761/ DS副本:本地主机 启动身份验证服务时的消息: 发现服务pom