简体   繁体   English

如何使用从主机到 docker mysql 服务器的 JDBC 连接

[英]How to use JDBC connection from host to a docker mysql server

I am new in docker and I try to connect to a mysql container from the local machine (host).我是 docker 新手,我尝试从本地机器(主机)连接到 mysql 容器。

I pulled the latest version of mysql with:我使用以下命令提取了最新版本的 mysql:

docker pull mysql/mysql-server:latest

and started this (and the myadmin container with the following:并启动它(以及具有以下内容的 myadmin 容器:

docker run --name mysql1 -e MYSQL_ROOT_PASSWORD=root -d mysql --default-authentication-plugin=mysql_native_password -P 3306 -h localhost
docker run --name myadmin -d --link mysql1:db -p 8081:80 phpmyadmin/phpmyadmin

I can access the phpmyadmin on my local browser (with localhost:8081) and I created a DB named 'userDB'.我可以在本地浏览器上访问 phpmyadmin(使用 localhost:8081),并创建了一个名为“userDB”的数据库。

Here you can see the docker check:在这里您可以看到 docker 检查:

docker ps
CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS                            NAMES
03126272c0e3        phpmyadmin/phpmyadmin   "/run.sh supervisord…"   7 minutes ago       Up 7 minutes        9000/tcp, 0.0.0.0:8081->80/tcp   myadmin
c8d032921d7c        mysql                   "docker-entrypoint.s…"   8 minutes ago       Up 8 minutes        3306/tcp, 33060/tcp              mysql1

On my local JavaEE application (running on an GlassFish Webserver) I try to connect this mysql database with the following commands:在我的本地 JavaEE 应用程序(在 GlassFish Web 服务器上运行)上,我尝试使用以下命令连接这个 mysql 数据库:

    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/userDB", "root", "root");
        return con;
    } catch (Exception ex) {
        System.out.println("Database.getConnection() Error -->"
                + ex.getMessage());
        return null;
    }

and got the following exception: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure并得到以下异常:com.mysql.jdbc.exceptions.jdbc4.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.

What mistake do I do?我犯了什么错误? How can i connect my Java application with the mysql docker container?如何将我的 Java 应用程序与 mysql docker 容器连接?

You need to setup Docker networking for that stuff to to work together.您需要为这些东西设置 Docker 网络才能协同工作。 I don't review docker-compose here, you can do it on your own, but if using generic docker CLI, it will look like following:我不在这里回顾docker-compose ,你可以自己做,但如果使用通用的docker CLI,它将如下所示:

First you create user-defined bridge network首先创建用户定义的桥接网络

docker network create foo

Then you start your containers attached to this net然后你启动你的容器连接到这个网络

docker run --network=foo --name mysql1 -e MYSQL_ROOT_PASSWORD=root -d mysql --default-authentication-plugin=mysql_native_password -P 3306 -h "0.0.0.0"
docker run --network=foo --name myadmin -p 8081:80 phpmyadmin/phpmyadmin

Notice: I changed localhost to 0.0.0.0 to allow remote connections and removed link argument - it is oboslete.注意:我将localhost更改为0.0.0.0以允许远程连接并删除了link参数 - 它已过时。

Now to connect between services you use their names and generic ports, like mysql1:3306 and myadmin:80 .现在要在服务之间进行连接您可以使用它们的名称和通用端口,例如mysql1:3306myadmin:80

To connect to services from host you use localhost and exposed ports : localhost:1234 and localhost:8081 appropriately.要从主机连接到服务您可以适当地使用localhost公开端口localhost:1234localhost:8081

The first problem was that the port mapping must be one of the first parameters in the run statement of docker.第一个问题是端口映射必须是docker的run语句中的第一个参数之一。 This is the final run statement:这是最后的运行语句:

docker run -p 3306:3306 --name mysql1 -e MYSQL_ROOT_PASSWORD=root -d mysql --default-authentication-plugin=mysql_native_password -h 127.0.0.1

The next problem was to update the JDBC driver in maven (to the newest).下一个问题是更新 maven 中的 JDBC 驱动程序(到最新的)。

And last but not least: The useSSL param was set to false (not necessary on the local machine).最后但并非最不重要的是:useSSL 参数设置为 false(在本地机器上不需要)。

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

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