Anna Klein
Anna Klein

Reputation: 2171

Connect to MySql Docker-Container from Intellij's Database Tool

I have created a docker MySQL container like this

docker run --detach --name internal-mysql -p 6604:3306 -e MYSQL_ROOT_PASSWORD=user_pass -e MYSQL_DATABASE=internal -e MYSQL_USER=user -e MYSQL_PASSWORD=user_pass mysql

The container is running fine

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e802cd30e6a2 mysql "docker-entrypoint.s…" 42 seconds ago Up 41 seconds 33060/tcp, 0.0.0.0:6604->3306/tcp internal-mysql

Within the container I have checked the user host and it looks fine doesn't it?

+------------------+-----------+
| user             | host      |
+------------------+-----------+
| root             | %         |
| user             | %         |
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+

but when I try to connect to that DB from IntelliJ or my Spring-Boot app I always receive the same error

enter image description here

Of course I tried to google and figured out that for some cases it helped to figure out the container ip via docker inspect -f "{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}" internal-mysql

which resulted in 172.17.0.2

I have tried to connect via jdbc:mysql://172.17.0.2:3306/internal-mysql

I have tried it with JDBC MYSQL Driver 8.0.21 and 5.1 with both no success.

Any idea please?

Upvotes: 0

Views: 2052

Answers (1)

Michał Krzywański
Michał Krzywański

Reputation: 16900

Since you published 3306 port of mysql container on port 6604 of your host - you should use 6604 port when trying to connect to your mysql docker container from the host.

Upvotes: 2

Related Questions