Home > Database > Mysql Tutorial > How to Fix 'Communications Link Failure” Errors When Connecting to MySQL in a Dockerized Spring Boot Application?

How to Fix 'Communications Link Failure” Errors When Connecting to MySQL in a Dockerized Spring Boot Application?

Mary-Kate Olsen
Release: 2024-11-02 05:38:29
Original
238 people have browsed it

How to Fix “Communications Link Failure” Errors When Connecting to MySQL in a Dockerized Spring Boot Application?

Communications Link Failure in Spring Boot, MySQL, Docker, and Hibernate

Problem:
When running a Spring Boot application with Hibernate and MySQL using Docker Compose, an error occurs with the following message:

Communications link failure
java.net.ConnectException: Connection refused
Copy after login

Details:
The error occurs when the application tries to establish a connection to the MySQL database. The connection URL in jdbc:mysql://localhost/database refers to the local host, which is not the correct address within the Docker container.

Solution:
To resolve this issue, the connection URL in the JDBC connection and the Spring Boot configuration (application.properties) should be updated to point to the MySQL container's address.

JDBC Connection:

<code class="java">private Connection createConnection() throws SQLException {
    DriverManager.registerDriver(new com.mysql.jdbc.Driver());
    String mysqlUrl = "jdbc:mysql://docker-mysql:3306/database?autoReconnect=true&useSSL=false";
    Connection connection = DriverManager.getConnection(mysqlUrl, "root", "root");
    return connection;
}</code>
Copy after login

Spring Boot Configuration (application.properties):

<code class="properties">spring.datasource.url=jdbc:mysql://docker-mysql:3306/database?autoReconnect=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=root</code>
Copy after login

Docker Compose Configuration (docker-compose.yml):
Ensure that the Docker Compose configuration correctly defines the port mapping for the MySQL container:

<code class="yaml">services:
  docker-mysql:
    image: mysql:5.7
    ports:
      - 3307:3306</code>
Copy after login

and that the application container depends on the MySQL container:

<code class="yaml">app:
    image: app:latest
    ports:
      - 8091:8091
    depends_on:
      - docker-mysql</code>
Copy after login

The above is the detailed content of How to Fix 'Communications Link Failure” Errors When Connecting to MySQL in a Dockerized Spring Boot Application?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template