Problem Description
Spring Boot(app) container cant connect to “mysql” container
Problem Output
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
application.properties
spring.datasource.username = root
spring.datasource.url = jdbc:mysql://mysql:3306/fms?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false
spring.datasource.password = manager@123
docker-compose.yml
version: '3.7'
services:
mysql:
image: mysql:latest
restart: always
command: --default-authentication-plugin=mysql_native_password
ports:
- "33061:3306"
networks:
- spring-boot-mysql-network
environment:
MYSQL_DATABASE: fms
MYSQL_ROOT_PASSWORD: manager@123
volumes:
- ./database_storage:/docker-entrypoint-initdb.d
app:
build:
context: .
dockerfile: app.Dockerfile
ports:
- "8091:8080"
networks:
- spring-boot-mysql-network
depends_on:
- mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin
restart: always
depends_on:
- mysql
environment:
PMA_HOST: database
PMA_PORT: 3306
ports:
- "9091:80"
networks:
spring-boot-mysql-network:
driver: bridge
3
Answers
I had done a silly mistake.
Your settings look correct to me. However, you are using the latest docker image of MySQL (which is 8.0.x at the time of writing). So, the exception you got indicates that there is a compatibility issue between your MySQL connector and MySQL server version. Which version of MySQL Connector/J are you using in your Spring Boot application? You need to update the connector or downgrade the MySQL docker image version.
Could you try updating mysql connector version to
mysql-connector-java-8.0.11