简体   繁体   English

Docker - MySQL 和 Java 容器连接错误

[英]Docker - MySQL and Java container connectivity error

I am trying to do a simple task of creating a microservice with JAVA and MySQL.我正在尝试使用 JAVA 和 MySQL 创建一个微服务的简单任务。 I am using docker-compose on Windows 10 with Docker Desktop.我在 Windows 10 上使用 docker-compose 和 Docker 桌面。

Client: Docker Engine - Community
Version:           19.03.5
API version:       1.40
Server: Docker Engine - Community
Engine:
Version:          19.03.5
API version:      1.40 (minimum version 1.12)

My docker-compose.yml is我的 docker-compose.yml 是

version: '3.1'
services:
  db:
    #image: mysql:5.7.22
    image: mysql:latest
    ports: ["3306:3306"]
    hostname: db
    environment:
        - MYSQL_ROOT_PASSWORD=root
        - MYSQL_DATABASE=Users
    container_name: mysqldatabase
  web:
    build: docker-mysql-connector 
    image: docker-mysql-connector
    hostname: web
    tty: true
    depends_on:
      - db
    links:
      - db:db

My JAVA code to check the connectivity is我检查连接性的 JAVA 代码是

package com.prasad.docker.mysql;

import java.net.InetAddress;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Map;

public class MySQLConnection {
    public static void main(String[] args) throws Exception {
        String ipAddr = InetAddress.getLocalHost().getHostName();
        System.out.println("Printing IP address of the host " + ipAddr);
        Map<String, String> env = System.getenv();
        for (String envName : env.keySet()) {
            System.out.format("%s=%s%n", envName, env.get(envName));
        }
        Thread.sleep(10000);

        boolean connected = false;
        while (!connected) {
            try {

                String url = "jdbc:mysql://db:3306/Users?autoReconnect=false&useSSL=false";
                String user = "root";
                String password = "root";
                System.out.println("Connecting to URL " + url);
                Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
                Connection conn = DriverManager.getConnection(url, user, password);
                System.out.println("Connection was successful");
                connected = true;
            } catch (Exception e) {
                System.err.println("Error connecting to database");
                e.printStackTrace();
                Thread.sleep(5000);
            }
        }

    }
}

I get the following error when I output the log of web container where my JAVA ode is running当我 output 运行 JAVA ode 的 web 容器的日志时出现以下错误

Connecting to URL jdbc:mysql://db:3306/Users?autoReconnect=false&useSSL=false
Error connecting to database
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
       at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:590)
       at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:57)
       at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:1606)
       at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:633)
       at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:347)
       at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:219)
       at java.sql.DriverManager.getConnection(DriverManager.java:664)
       at java.sql.DriverManager.getConnection(DriverManager.java:247)
       at com.prasad.docker.mysql.MySQLConnection.main(MySQLConnection.java:34)
Caused by: com.mysql.cj.core.exceptions.CJCommunicationsException: Communications link failure

I am able to test the connection successfully from MySQL Workbench and the MySQL database inside the container.我能够从容器内的 MySQL Workbench 和 MySQL 数据库成功测试连接。 I get the connectivity error only from JAVA code.我只从 JAVA 代码中得到连接错误。 I tried using latest version and v5.7.22 of MySQL database.我尝试使用 MySQL 数据库的最新版本和 v5.7.22。 Same error in both cases.两种情况下的错误相同。 Any help appreciated任何帮助表示赞赏

Have you mentioned that the instance is for localhost?您是否提到该实例适用于本地主机?

version: '3.1'
services:
  db:
    #image: mysql:5.7.22
    image: mysql:latest
    ports: ["3306:3306"]
    hostname: db
    environment:
        - MYSQL_ROOT_PASSWORD=root
        - MYSQL_DATABASE=Users
    container_name: mysqldatabase
  web:
    build: docker-mysql-connector 
    image: docker-mysql-connector
    hostname: web
    tty: true
    depends_on:
      - db
    links:
      - db:db
    environment:
      SPRING_DATASOURCE_URL: jdbc:mysql://docker-mysql:3306/database?autoReconnect=true&useSSL=false

Also adding network_mode: "host" will also help.另外添加network_mode: "host"也会有所帮助。

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

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