Home > Database > Mysql Tutorial > body text

Why Does My Dockerized Spring Boot App Get \'Communications Link Failure\' When Connecting to MySQL?

DDD
Release: 2024-11-02 18:11:29
Original
223 people have browsed it

Why Does My Dockerized Spring Boot App Get

Dockerized Spring Boot, MySQL, and Hibernate: Troubleshooting "Communications Link Failure"

When running a Spring Boot application with MySQL within a Docker container, it's possible to encounter the error "Communications link failure" due to connection issues between the app and the database. This can be resolved by ensuring that the correct database hostname is used in the application's configuration.

In the provided configuration, the createConnection method uses the localhost hostname in the JDBC URL. This refers to the host machine, which is not where the MySQL Docker image is running.

To fix the issue, the Docker Compose file should be modified to use the MySQL image's hostname in the Spring Boot application's environment configuration. Here's the updated Docker Compose file:

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
Copy after login

By specifying the environment variable SPRING_DATASOURCE_URL in the Spring Boot application's Docker image, the hostname of the MySQL container (docker-mysql) will be used in the JDBC connection. This resolves the communication issue and allows the application to connect to the database successfully.

The above is the detailed content of Why Does My Dockerized Spring Boot App Get \'Communications Link Failure\' When Connecting to MySQL?. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!