简体   繁体   English

(Docker+SpringBoot+Mysql) Mysql 连接被拒绝

[英](Docker+SpringBoot+Mysql) Mysql connection refused

Problem Description问题描述

Spring Boot(app) container cant connect to "mysql" container Spring Boot(应用程序)容器无法连接到“mysql”容器

Problem Output问题输出

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.引起:com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:无法创建到数据库服务器的连接。 Attempted reconnect 3 times.尝试重新连接 3 次。 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 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我正在更新 application.property 文件和 docker-compose up --build。

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).但是,您使用的是 MySQL 的最新 docker 映像(在撰写本文时为 8.0.x)。 So, the exception you got indicates that there is a compatibility issue between your MySQL connector and MySQL server version.因此,您得到的异常表明您的 MySQL 连接器和 MySQL 服务器版本之间存在兼容性问题。 Which version of MySQL Connector/J are you using in your Spring Boot application?您在 Spring Boot 应用程序中使用的是哪个版本的MySQL Connector/J You need to update the connector or downgrade the MySQL docker image version.您需要更新连接器或降级 MySQL docker 镜像版本。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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