简体   繁体   中英

(Docker+SpringBoot+Mysql) Mysql connection refused

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

I had done a silly mistake.

I was updating my application.property file and docker-compose up --build.But I never repackaged war file.So I was reading my old war file and thus reading my old property file

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.

您可以尝试将 mysql 连接器版本更新为mysql-connector-java-8.0.11

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM