简体   繁体   English

无法在docker中使用jdbc连接到mysql

[英]Unable to connect to mysql using jdbc in docker

I have the following docker-compose:我有以下 docker-compose:

version: '3.1'
services:
  db:
    container_name: db
    image: mysql
    environment:
      - MYSQL_ROOT_PASSWORD=password
      - MYSQL_DATABASE=world
    volumes:
      - ./mysql-db/:/docker-entrypoint-initdb.d
    networks:
      - my-network
  app:
    depends_on:
      - db
    container_name: app
    build: App/
    networks:
      - my-network
networks:
  my-network:
    driver: bridge

This builds the mysql image and uses a local file to create the database.这将构建mysql图像并使用本地文件创建数据库。 I am able to connect to the database through a database client on my host machine.我可以通过主机上的数据库客户端连接到数据库。 I know the db is running and working with those credentials on port 3306 .我知道数据库正在运行并使用端口 3306 上的这些凭据

App/Dockerfile : App/Dockerfile

# Build stage
FROM maven:latest AS build
COPY src /app/src
COPY pom.xml /app
# need to assemble to package in plugins
RUN mvn -f /app/pom.xml clean compile assembly:single

# Package stage
FROM openjdk:latest
COPY --from=build /app/target/seMethods-1.0-jar-with-dependencies.jar /usr/local/lib/build.jar
ENTRYPOINT ["java", "-jar", "/usr/local/lib/build.jar"]

This builds the jar file using maven.这将使用 maven 构建 jar 文件。

App/src/App.java


// sql imports
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class App 
{
  public static void main( String[] args )
  {
    try {
      String dbUrl = "jdbc:mysql://db:3306/world";
      Connection con = DriverManager.getConnection(dbUrl,"root","password");

      String testStatement = "SELECT * FROM city;";
      PreparedStatement preparedTest = con.prepareStatement(testStatement);
      ResultSet rs = preparedTest.executeQuery();

      while(rs.next()){
        System.out.println(rs.getRow());
      }

    } catch (Exception e) {
      // handle any errors
      System.out.println(String.format("Error: %s", e));
    }
  }
}

When my docker-compose runs, the containers are created although my app stops with the following:当我的 docker-compose 运行时,尽管我的应用程序停止并显示以下内容,但容器已创建:

Error: 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.

How can I connect my db container to app ?如何将我的db容器连接到app

You are trying to access a ressource outside your app docker container without having set ports on it.您正在尝试访问应用程序 docker 容器之外的资源,而无需在其上设置端口。 By default as you likely know docker container are insulated from the system, thus you can not access port 3306 from inside the container while you can from your host machine.默认情况下,您可能知道 docker 容器与系统绝缘,因此您无法从容器内部访问端口 3306,而您可以从主机访问端口。 Add the ports to the docker compose file.将端口添加到 docker compose 文件中。

Solved.解决了。

When creating the db image, the init file used to generate the inital database took a few seconds to complete.创建 db 映像时,用于生成初始数据库的 init 文件需要几秒钟才能完成。 Adding a Thread.sleep() hotfix to the start of my java app allowed the database tables to be created and then I am able to connect.在我的 java 应用程序的开头添加Thread.sleep()修补程序允许创建数据库表,然后我就可以连接了。

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

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