1

I have seen numerous answers to this issue, and I am trying to solve mine today. ( I had already spent 2+ days on that, but still clueless).

Here is my situation:

  1. It is on my Mac 10.13.1

  2. The MySQL is running with one docker container, and I have all these privileges set.

  3. docker exec -it mysql bash ==> cd /etc => where I have found a my.cnf file, in which I have added bind-address=127.0.0.1

  4. I have a local MySQL Workbench, which is able to make connections to this MySQL server, 127.0.0.1:3306 and gets the result from the database.

  5. But, when my application was started in 2nd Docker container, I was not able to make it connected to the MySQL database, which is on a different container. This is the part the command I have used during the app startup:

    docker run -d ... -e DB_URL=jdbc:mysql://127.0.0.1:3306/mydatabase --name myApp  --restart=always --link mysql myApp_t:latest
    

But once myApp is started, I can find the msg from its logs as:

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.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_111]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_111]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_111]
...

Any clues what else I may be doing ?

1 Answer 1

0

This is more a docker question than a MySQL/JDBC connections. Your second docker container can't reach your first because the 127.0.0.1 there points to the container itself, and not the first container.

First of all, you shouldn't configure the bind_address in the first container to be 127.0.0.1. You either need to leave it out or set it to 0.0.0.0. You probably didn't restart MySQL, otherwise you wouldn't have been able to connect from MySQL Workbench on your own machine either.

The ability to connect through 127.0.0.1 from your machine to an application running within docker is only available on your machine, not between containers.

Your command line shows you linked the myApp container to the mysql container (using --link mysql). That means that as the connection URL you should use:

jdbc:mysql://mysql:3306/mydatabase

That is: replace 127.0.0.1 with mysql as that is used as the host name of your mysql container to communicate between those two containers.

2
  • Thanks So much for the tips. It does give me the correct direction to solve my problem. Plus, I have just realized that my jdbc driver was of old version, while the downloaded mysql is of version 8+. Once I have both corrected, my case is working now ! May 15, 2018 at 21:18
  • @user3595231 If my answer helped you solve it, then please accept it by clicking on the checkmark. May 16, 2018 at 7:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.