12

I am working with PhpStorm 2018.3.4, Docker, MySQL and Ubuntu. I tried unsuccessfully to configure MySQL with the Docker container network_mysql.

First, I have tried this configuration :

First configuration

It gave me this error :

Result of the first configuration

Then, I tried this :

Second configuration

This one gave me this other error.

Result of the second configuration

Am I missing something? Is there another place where I must configure something?

docker ps output :

docker ps

Here docker network ls :

docker network ls

For the command docker inspect network_mysql, here is a link to the description : https://pastebin.com/9LmeAkc8

Here is a docker-compose.yml configuration : https://pastebin.com/DB4Eye4y

I tried to put - "3306:3306" in addition to the wex_server_proxy section with no avail.

The file to modify was this one :

https://pastebin.com/TPBQNCDZ

I added the ports section, opening the 3306 port :) And then, it works.

lionelp
  • 443
  • 1
  • 7
  • 21
  • use your mysql container name in connection host – Muhammad Hamza Younas Mar 06 '19 at 16:21
  • you can see the container name by this command docker ps or docker container ls – Muhammad Hamza Younas Mar 06 '19 at 16:23
  • typo `mysql:///` three slashes, should be 2 ? – Alex Mar 06 '19 at 16:26
  • Can you share your container configuration? – Nico Haase Mar 06 '19 at 16:26
  • Possible duplicate of [Connect to Docker MySQL container from localhost?](https://stackoverflow.com/questions/32360687/connect-to-docker-mysql-container-from-localhost) – Nico Haase Mar 06 '19 at 16:27
  • The container name is `network_mysql`. But i see '/network_mysql` in the Docker section of PHPStorm. It is for that reason that i tested `/network_mysql` and then got `mysql:///`. We have a layout with Docker so it is a bit complicated, i am not the creator of this layout. – lionelp Mar 06 '19 at 18:09
  • post your output when `docker ps` ? And I am very certain you don't need three slashes. – Alex Mar 06 '19 at 18:11
  • I just added the output of `docker ps`. – lionelp Mar 06 '19 at 18:15
  • and `docker network ls` please? – Alex Mar 06 '19 at 18:34
  • and `docker inspect network_mysql` sorry, for doing that one by one, but that is all info required to understand how to access your mysql container. Do you have any other tool connected to that DB except phpstorm? – Alex Mar 06 '19 at 18:42
  • by the way you don't need `network` at the end of connection link but just: `jdbc:mysql://network_mysql:3306` – Alex Mar 06 '19 at 18:44
  • I added the `docker network ls` but for `docker inspect network_mysql` it shows too much information on the project like password so ... no :) It is PhpStorm that puts `network` at the end when we give it the database name. – lionelp Mar 06 '19 at 20:15

3 Answers3

21

Solution

I notice that you are not mapping the mysql container port out. If you did, you would see this from the docker ps command:

...   0.0.0.0:3306->3306/tcp       network_mysql

Post not mapped out for mysql container

The container network_mysql is attached to a bridge type network called tmp_wex_net. This means that the container is not accesible from the host, by it's container name.

I appears that you are using a docker-compose.yml definition for the stack. In order to be able to access the container from the host, you need to use the ports section of your compose definition for this container:

serivces:
  mysql:
    ...
    ports:
      - "3306:3306"
    ...

If you are starting it with docker run, then you can acomplish the same thing with:

docker run -p 3306:3306 --name network_mysql --network="tmp_wex_net" -d mysql

And then use localhost in the hostname of your connection settings in PHPStorm. Like this:

Host: localhost

Port: 3306

Database: network


The problem

The reason that you are not able to connect, is that the host name network_mysql that you specify in the connection settings, does not resolve to any host that your machines knows of.

The container name of a docker container, is not a DNS name that the docker host can resolve.

If you have not specified any network for your mysql container, then it is connected to the default bridge network. And if you have created a new network, without specifying the type - it will also default to the bridge driver.

In order to access the container from the host, you need to either:

  1. Connect the container to the host network
  2. Or from a container on a bridge network, map the port to the host like suggested in the solution above. You can then address the specifically mapped port on that container with localhost:<portnum> from the host machine.
Andreas Lorenzen
  • 3,810
  • 1
  • 24
  • 26
  • I added the `docker inspect network_mysql` command via a link in my original post. – lionelp Mar 07 '19 at 09:34
  • I can see that your container is in the 'tmp_wex_net' network which is a bridge network. Please try my instructions in the bottom of my answer. I think I will move them to the top. – Andreas Lorenzen Mar 07 '19 at 09:49
  • I edited my answer to fit the new information, and to make the solution appear in the top of the answer. Let me know if the suggested solution works out for you. – Andreas Lorenzen Mar 07 '19 at 10:18
  • I will discuss of your solution with the one who created the overlay. – lionelp Mar 07 '19 at 11:32
  • I changed something to the Docker configuration file (that i just added to my post) but it did not change anything. – lionelp Mar 07 '19 at 14:19
  • Port 443 does not have anything to do with what you are trying to do. And the containers specified in the docker-compose.yml definition that you have added, do not have anything to do with the mysql container. You will have to show the docker-compose.yml where you define the `mysql` service, and add `- "3306:3306"` to the `ports` section in there. – Andreas Lorenzen Mar 07 '19 at 14:26
  • Whoops, i meant 3306 ah ah. I edited my post ! I will try to find other Docker configuration files. – lionelp Mar 07 '19 at 15:00
  • Ok i found the good section in another Docker file. Thanks for all :D – lionelp Mar 07 '19 at 15:03
3

For everyone who has setup mysql manually in a docker image:

chances are you must configure mysql to accept incoming connections also from the network interface which docker creates/uses to communicate with the host (along with port forwarding).

in my case, i added the following to /etc/mysql/my.cnf to the end of the file:

[mysqld]
bind-address    = 0.0.0.0

With that mysql listens on all network interfaces.

Luc
  • 31
  • 1
0

My solution: I forwarded ports from localhost to remote: ssh -R 3306:localhost:3306 root@remote_host_ip and the connection was successful.

Benjamin Hubbard
  • 2,797
  • 22
  • 28