How to Connect MySQL Workbench to Running MySQL Inside Docker

Connecting to MySQL container from Mysqlworkbench

You need to use localhost ( instead the container IP (in your case at hostname

Connect to mysql container using Mysql Workbench

I found the solution, and I hope it will help someone:

It that MySQL changing their authentication style after relese 8.0
Running older versions solve the problem:

docker run --name my-db -p 3306:3306 -v
/e/DockerData/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=admin -e
MYSQL_DATABASE=testDB -d mysql:5.7.22

How to access mysql running in docker container from host machine?

It's because you've changed your username password. First you set it during the creation, as:

CREATE USER 'demo_java' IDENTIFIED BY 'java';

But then you override it:

grant all on *.* to 'demo_java'@'%' identified by '1234';

Please, see this SO entry for more details.

Simply remove password change from your scripts. You would have something like this:

CREATE USER 'demo_java' IDENTIFIED BY 'java';
grant all on *.* to 'demo_java'@'%';
CREATE DATABASE hello_java CHARACTER SET utf8 COLLATE utf8_general_ci;


DriverManager.getConnection("jdbc:mysql://localhost:3306/hello_java", "demo_java", "java");

should work fine.

Regarding connecting as the root, please see another SO entry, after starting up new container, there are some connection restrictions by default in MySQL. If you want to be able to connect as the root, you'll have to change its priviliges like this:

update mysql.user set host = '%' where user='root';

then you'll have to restart your MySQL container and you should be able to connect as the root as well.

Docker Container cant connect to MySQL database

No information does not help solving your problem but a wild guess:

By default docker containers are joining a virtual network separate from host named bridge.

You can't reach host by localhost or, because this is pointing to your docker container itself. To reach host directly either let container use hosts IP by --network=host (with some disadvantages) or use host.docker.internal as DNS-Name instead of an IP.

BUT you should not take the way over host, connect directly to the mySQL-container by using the alias or IP or the container. You'll get that by docker inspect <containername>. No need to map ports then..

How to connect MySQL with Docker using Navicat or MySQL Workbench?

Do try host as mysql instead of localhost from the connected container. If you are on windows try as host.

If still having issue try retest with mysql-compose.yml

version: "3.1"

image: mysql:5.7.22
command: "--innodb_use_native_aio=0"
restart: always
- .env
- "3306:3306"
- /var/database/docker/common:/var/lib/mysql
- mysqlnet
- mysqlnetfpm

driver: bridge
driver: bridge

Note: Before running new mysql container don't forgot to delete the older mysqldata files.

Now you can use the username as root, password as root and for the internal containers(attached containers) use the host as mysql.

If you are try to connect from outside the docker do try with host as localhost or or your public docker ip.

How to connect to MySQL database cluster

In your docker run command, you can use -p 3306:3306 (or any available port). Then you can use <host>:<port> from Workbench or Dbeaver connection URL.

Related Topics

Leave a reply