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

Communications link failure , Spring Boot + MySql +Docker + Hibernate(通信链路故障,Spring Boot + MySql +Docker + Hibernate)
本文介绍了通信链路故障,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

这篇关于通信链路故障,Spring Boot + MySql +Docker + Hibernate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)