通信链路故障,Spring Boot + MySql +Docker + Hibernate

我正在使用 spring boot、hibernate &数据库.在运行应用程序时,它按预期运行良好.但是在制作 docker-compose 文件并使用 mysql docker 镜像运行应用程序 docker 镜像时,它会出现此错误.

<块引用>

错误com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败java.net.ConnectException: 连接被拒绝.

私有连接 createConnection() 抛出 SQLException{DriverManager.registerDriver(new com.mysql.jdbc.Driver());String mysqlUrl = "jdbc:mysql://localhost/database?autoReconnect=true&useSSL=false";Connection connection = DriverManager.getConnection(mysqlUrl, "root", "root");返回连接;}

<块引用>

Application.properties

spring.datasource.url=jdbc:mysql://localhost/database?autoReconnect=true&useSSL=falsespring.datasource.username=root

spring.datasource.password=root

请指导我如何解决这个问题.

**docker-compose.yml**版本:'3'服务:码头工人-mysql:图像:mysql:5.7环境:- MYSQL_ROOT_PASSWORD=root- MYSQL_DATABASE=数据库- MYSQL_USER=root- MYSQL_PASSWORD=root端口:- 3307:3306应用程序:图片:应用程序:最新端口:- 8091:8091依赖于取决于:- docker-mysql

解决方案

问题是由于在 jdbc url 中引用了 localhost.

以下配置应该可以工作.

**docker-compose.yml**版本:'3'服务:码头工人-mysql:图像:mysql:5.7环境:- MYSQL_ROOT_PASSWORD=root- MYSQL_DATABASE=数据库- MYSQL_USER=root- MYSQL_PASSWORD=root端口:- 3307:3306应用程序:图片:应用程序:最新端口:- 8091:8091环境:SPRING_DATASOURCE_URL: jdbc:mysql://docker-mysql:3306/database?autoReconnect=true&useSSL=false依赖于取决于:- docker-mysql

I'm working with spring boot, hibernate & MySql. While running the application it is running well as per expectation . But while making the docker-compose file and running the app docker image with mysql docker image it gives this error.

Error com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure java.net.ConnectException: Connection refused.

private Connection createConnection() throws SQLException 
{
        DriverManager.registerDriver(new com.mysql.jdbc.Driver());
        String mysqlUrl = "jdbc:mysql://localhost/database?autoReconnect=true&useSSL=false";
        Connection connection = DriverManager.getConnection(mysqlUrl, "root", "root");
        return connection;
}

Application.properties

spring.datasource.url=jdbc:mysql://localhost/database?autoReconnect=true&useSSL=false spring.datasource.username=root

spring.datasource.password=root

Please guide me how to tackle this.

**docker-compose.yml**

version: '3'

services:
  docker-mysql:
    image: mysql:5.7
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=database
      - MYSQL_USER=root
      - MYSQL_PASSWORD=root
    ports:
      - 3307:3306

  app:
    image: app:latest
    ports:
       - 8091:8091
    depends_on:
       - docker-mysql

解决方案

Issue is due to reference of localhost in the jdbc url.

Below configuration should work.

**docker-compose.yml**

version: '3'

services:
  docker-mysql:
    image: mysql:5.7
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=database
      - MYSQL_USER=root
      - MYSQL_PASSWORD=root
    ports:
      - 3307:3306

  app:
    image: app:latest
    ports:
       - 8091:8091
    environment:
      SPRING_DATASOURCE_URL: jdbc:mysql://docker-mysql:3306/database?autoReconnect=true&useSSL=false
    depends_on:
       - docker-mysql

相关文章