Cannot connect to MySQL from other container

I have a MySQL container running (version 8, but tried with 5.x) where I can access the port 3307 from an external Java program (not in docker). That’s OK for a test to make sure the database is accessible in at least one case, but I need it to accessible from another docker container.


However, for whatever reason, need help with this, I cannot get my service running in one docker container to talk to the MySQL container running in docker.

My ports configuration for MySQL in docker-compose.yml is simply:

ports:
- 3307:3306

When docker loads MySQL via the docker-compose file, the last line says:

“[System] [MY-011323] [Server] X Plugin ready for connections. Socket: ‘/var/run/mysqld/mysqlx.sock’ bind-address: ‘::’ port: 33060”

the line above that, says:

“[Server] /usr/sbin/mysqld: ready for connections. Version: ‘8.0.13’ socket: ‘/var/run/mysqld/mysqld.sock’ port: 3306 MySQL Community Server - GPL.”


Yet, regardless of what ports I use in the JDBC connection string, I get:

“Could not create connection to database server. Attempted reconnect 3 times. Giving up.”

I’ve tried:

“jdbc:mysql://0.0.0.0:33060/ZipCodeLookup?useSSL=false&” + “serverTimezone=America/New_York&user=root&password=thepass&autoReconnect=true”;

“jdbc:mysql://192.168.0.1:33060/ZipCodeLookup?useSSL=false&” + “serverTimezone=America/New_York&user=root&password=thepass&autoReconnect=true”;

“jdbc:mysql://localhost:33060/ZipCodeLookup?useSSL=false&” + “serverTimezone=America/New_York&user=root&password=thepass&autoReconnect=true”;

“jdbc:mysql://localhost:3307/ZipCodeLookup?useSSL=false&” + “serverTimezone=America/New_York&user=root&password=thepass&autoReconnect=true”;

“jdbc:mysql://localhost:3306/ZipCodeLookup?useSSL=false&” + “serverTimezone=America/New_York&user=root&password=thepass&autoReconnect=true”;

and other combinations.

I appear to be confused what the “bind address” is and how to properly use it. I’ve searched on Stackoverflow, but the examples there are confusing.

Would appreciate advice on how to configure mysql access from another container.

Thanks very much.

  • mike

You should have both the containers running on the same network and declare a host-name for the database. That way, docker will take care of the ip-address lookup for you.

The easiest way to do that is to use a docker-compose.yml file and start the containers as services. This is an example from another answer I posted earlier.

version: "3"
services:
  db:
    image: mysql
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: TopSecretPassword
  adminer:
    image: adminer
    restart: always
    ports:
      - 8088:8080  
  image: tomcat
    ports: 
      - "8080:8080"
    volumes:
      - ./target/DockerExample.war:/usr/local/tomcat/webapps/ROOT.war
      - ./target/DockerExample:/usr/local/tomcat/webapps/ROOT

This will create 3 services and if you access adminer on port 8088, you will see that it finds a MySQL-server on host db. That is the hostname you can use in your other application as well. So instead of an ip, you just use db as you would use any FQDN to a server.

Thanks for your help. Very useful.

Here’s my docker-compose file:

version: ‘3’
services:
app:
build:
context: .
dockerfile: Dockerfile
ports:
- “8089:8080”
volumes:
- /Users/mike/Library/apache-tomcat-9.0.7/conf/tomcat-users.xml:/usr/local/tomcat/conf/tomcat-users.xml
depends_on:
- db
db:
image: mysql
container_name: test-mysql-docker
ports:
- 3307:3306
volumes:
- ./ZipCodeLookup.sql:/docker-entrypoint-initdb.d/ZipCodeLookup.sql
environment:
MYSQL_ROOT_PASSWORD: “thepass”

How would you modify it so the app can access mysql?

Thanks,

  • mike

PS I’m not sure “db” would resolve in a JDBC connection string.

I made a note of the internal network created on docker startup and also tried …

“jdbc:mysql://miketest_default:3306/ZipCodeLookup?useSSL=false&” + “serverTimezone=America/New_York&user=root&password=thepass&autoReconnect=true”;

“jdbc:mysql://miketest_default:3307/ZipCodeLookup?useSSL=false&” + “serverTimezone=America/New_York&user=root&password=thepass&autoReconnect=true”;

“jdbc:mysql://miketest_default:8080/ZipCodeLookup?useSSL=false&” + “serverTimezone=America/New_York&user=root&password=thepass&autoReconnect=true”;

“jdbc:mysql://miketest_default:8089/ZipCodeLookup?useSSL=false&” + “serverTimezone=America/New_York&user=root&password=thepass&autoReconnect=true”;

“jdbc:mysql://miketest_default:33060/ZipCodeLookup?useSSL=false&” + “serverTimezone=America/New_York&user=root&password=thepass&autoReconnect=true”;

SOLVED.

Problem was that I wasn’t actually removed cached versions of the build WAR file so none of my multiple experiments was actually using new builds. Need to use the “rmi” argument with docker down I’m told. In any case, the correct working JDBC was to use the mysql container name from the docker-compose file above and port 3306.

Hope this helps someone else.

Thanks to all.

2 Likes

Hi,

Can you please show us how you solved your issue? for example in this url jdbc:mysql://miketest_default:3306/ZipCodeLookup?useSSL=false&” + “serverTimezone=America/New_York&user=root&password=thepass&autoReconnect=true”; what is miketest_default, is it the name of network or container?

Thanks.

miketest_default will be the container name of mysql docker.

Yes this was very helpful indeed.

IT WORKED FOR US ALSO AFTER SPENDING A LOT OF HOURS TRYING TO FIGURE OUT WAS WAS WRONG.