Tobias Lukoschek
Tobias Lukoschek

Reputation: 405

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).

I pulled the latest version of mysql with:

docker pull mysql/mysql-server:latest

and started this (and the myadmin container with the following:

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'.

Here you can see the docker check:

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:

    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

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?

Upvotes: 7

Views: 18590

Answers (2)

Tobias Lukoschek
Tobias Lukoschek

Reputation: 405

The first problem was that the port mapping must be one of the first parameters in the run statement of docker. 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).

And last but not least: The useSSL param was set to false (not necessary on the local machine).

Upvotes: 5

grapes
grapes

Reputation: 8636

You need to setup Docker networking for that stuff to to work together. 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:

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.

Now to connect between services you use their names and generic ports, like mysql1:3306 and myadmin:80.

To connect to services from host you use localhost and exposed ports: localhost:1234 and localhost:8081 appropriately.

Upvotes: 2

Related Questions