I am using mysql server inside docker container and able to access inside docker. How to create connection in mysql workbench running on my local(Host Machine).
15 Answers
By default after deployment MySQL has following connection restrictions:
mysql> select host, user from mysql.user;
+-----------+---------------+
| host | user |
+-----------+---------------+
| localhost | healthchecker |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+---------------+
4 rows in set (0.00 sec)
Apparently, for security purposes, you will not be able to connect to it from outside of the docker container. If you need to change that to allow root to connect from any host (say, for development purposes), do the following:
Start your mysql image with all port mappings required:
docker run -p 3306:3306 --name=mysql57 -d mysql/mysql-server:5.7
or, if the complete port mapping is required:
docker run -p 3306:3306 -p 33060:33060 --name=mysql57 -d mysql/mysql-server:5.7
If this is the fresh installation - grab the default password:
docker logs mysql57 2>&1 | grep GENERATED
Connect using
mysql
client directly to the mysqld in docker:docker exec -it mysql57 mysql -uroot -p
If this is a fresh installation you will be asked to change the password using
ALTER USER
command. Do it.Run SQL:
update mysql.user set host = '%' where user='root';
Quit the
mysql
client.Restart the container:
docker restart mysql57
Now you will be able to connect from MySQL Workbench to
host: `0.0.0.0`
port: `3306`
After all the changes the query will show:
select host, user from mysql.user;
+-----------+---------------+
| host | user |
+-----------+---------------+
| % | root |
| localhost | healthchecker |
| localhost | mysql.session |
| localhost | mysql.sys |
+-----------+---------------+

- 1,850
- 17
- 24
-
2thank you, this answer helped me a lot. Only changing the password was not documente here, found the answer here: https://stackoverflow.com/a/33511149/8439926 – AO19 Apr 29 '19 at 19:34
-
2Thank U Andrey, today I was having this problem, I already had about 3 days trying to connect, and I just used the ip 0.0.0.0 and port 3306 from your tip and it worked, but I use mysqlsh. – LandiLeite Aug 03 '20 at 17:06
-
2In my experienced ignorance I never thought `0.0.0.0` would be correct ... but it was ! – Jono Sep 01 '20 at 03:47
-
Thanks Andrey, this allowing '%' access just slipped my mind. – Ali Jibran Oct 13 '20 at 06:58
-
When I run other applications in docker, only providing my ipv4 addess instead of localhost allows me to connect. Right now I am not able to connect to mysql:latest docker container using workbench. Is that because of this connection restriction or my own error? – Murtaza Haji Nov 27 '20 at 18:42
-
Sorry, @MurtazaHaji , I don't know what is the cause of your problem connecting to the `mysql:latest`. Could be a number of things, including network issues. – Andrey Lebedenko Dec 02 '20 at 19:01
-
Thanks for the detailed explanation. Very helpful. – Lone Ronin Mar 08 '21 at 20:34
-
How can we confirm that `0.0.0.0` is actually tied to `%` for `root`? – klewis Aug 28 '23 at 19:19
You have to do few configuration in you docker container. Please follow the following steps.
Specify mysql configuration block in your docker-compose.yml. I have following mysql block under services object in my docker-compose.yml file.
services: db: image: mysql volumes: - "./.data/db:/var/lib/mysql" environment: MYSQL_ROOT_PASSWORD: root MYSQL_DATABASE: mydb MYSQL_USER: user MYSQL_PASSWORD: pass ports: 42333:3306
Restart docker container and run following commands to get to the bash shell in the mysql container
docker ps docker exec -it <mysql container name> /bin/bash
Inside the container, to connect to mysql command line type,
mysql -u root -p
Use MYSQL_ROOT_PASSWORD as specified in the docker-compose.yml . Execute following commands to create new user.
create user 'user'@'%' identified by 'pass'; grant all privileges on *.* to 'user'@'%' with grant option; flush privileges;
The percent sign (%) means all ip's. Restart the docker container.
In your MySQL Workbench provide the connection details. Use MYSQL_PASSWORD as specified in your docker-compose.yml file.
You should now be able to connect to your mysql container.

- 1,107
- 1
- 12
- 10
-
17
-
The IP shown above is my vagrant box IP address. In your case you can find it in Vagrantfile in your vagrant folder. – Krishna Feb 27 '19 at 02:46
-
2The IP is often stored in the .env file, depending on your set up. For example `DB_HOST=127.0.0.1 DB_PORT=33061` @Krishna, your answer about where to obtain the IP seems odd because folks often use Docker _instead_ of Vagrant, AFAIK. Also, if you're setting up as root or if you otherwise get an error message when trying to create the user because one already exists, just set up with a new user, say "workbench". (You could also edit the user's IP, e.g.). Otherwise, this is terrific. Thanks! – Jeremy L. Mar 03 '19 at 05:34
-
-
You can get the IP address by `ifconfig getifaddr en0` to get the IP address. The `docker container inspect container_name` outputs the IP address does not work for me. – htlbydgod Nov 24 '22 at 23:06
Suppose you have the next content of your docker-compose
file:
database:
image: mysql:5.6
volumes:
- dbdata:/var/lib/mysql
environment:
- "MYSQL_DATABASE=homestead"
- "MYSQL_USER=homestead"
- "MYSQL_PASSWORD=secret"
- "MYSQL_ROOT_PASSWORD=secret"
ports:
- "33061:3306"

- 735
- 8
- 15
-
2Best answer!! You have to look at the MySQL settings in the docker-compose.yml file. Just use them – Ranjan Fadia May 19 '21 at 14:53
-
That's the right approach! I'd just advise on moving sensitive data from `environment` to an external .env file and add it in the docker-compose.yml file with `env_file: .env` – Paulo Cunha Oct 20 '22 at 14:20
-
-
This is the best answer. Just changing the external port did the trick. – VictorEspina Jun 23 '23 at 14:59
2 docker-related conditions:
first, your docker run must map the mysql port to an host port:
docker run -p host:container
(for instance: docker run -d -p 3306:3306 tutum/mysql
)
second, if you are using docker in a VM (docker-machine, with boot2docker), you need to use the ip of
docker-machine ip <VMname>
, with the host mapped port.http://$(docker-machine ip <VMname>):hostPort
If you need to use localhost
, you would need to do some port forwarding at the VirtualBox level:
VBoxManage controlvm "boot2docker-vm" natpf1 "tcp-port3306,tcp,,3306,,3306"
VBoxManage controlvm "boot2docker-vm" natpf1 "udp-port3306,udp,,3306,,$3306"
(controlvm
if the VM is running, modifyvm
is the VM is stopped)
(replace "boot2docker-vm
" by the name of your vm: see docker-machine ls
)
2 mysql-related conditions:
As illustrated in
nkratzke/EasyMySQL/Dockerfile
, you need to enable remote access:# Enable remote access (default is localhost only, we change this # otherwise our database would not be reachable from outside the container) RUN sed -i -e"s/^bind-address\s*=\s*127.0.0.1/bind-address = 0.0.0.0/" /etc/mysql/my.cnf
You need to create users when startig your database in your docker image.
See for instancenkratzke/EasyMySQL/start-database.sh
, which is called by theDockerfile CMD
:/usr/sbin/mysqld & sleep 5 echo "Creating user" echo "CREATE USER '$user' IDENTIFIED BY '$password'" | mysql --default-character-set=utf8 echo "REVOKE ALL PRIVILEGES ON *.* FROM '$user'@'%'; FLUSH PRIVILEGES" | mysql --default-character-set=utf8 echo "GRANT SELECT ON *.* TO '$user'@'%'; FLUSH PRIVILEGES" | mysql --default-character-set=utf8 echo "finished"
-
Docker is running on my local machine. and it has 172.17.42.1 IP address. When I run $select user(); It gives +----------------+ | user() | +----------------+ | root@localhost | +----------------+ 1 row in set (0.00 sec) and show variable gives port | 3306 – Pawan Kumar Nov 20 '15 at 13:11
-
@PawanSharma are you on Linux? If yes, what was the `docker run` exact command you use to start your containerized mysql? What version of docker are you using? – VonC Nov 20 '15 at 13:12
-
Yes I am on Ubuntu mchine nad docker version is Docker version 1.8.2, build 0a8c2e3. I am able to access mysql inside docker container. – Pawan Kumar Nov 20 '15 at 13:15
-
@PawanSharma OK. what was the docker run exact command you use to start your containerized mysql? – VonC Nov 20 '15 at 13:16
-
We are using docker container for multiple tools so we created a container with multiconfig files here we use command docker run -v "$XYZPATH":/apps/src/external:ro -t -i $EXPOSE_PORTS $DOCKER_REGISTRY/xxxxx:latest "$@ – Pawan Kumar Nov 20 '15 at 13:20
-
@PawanSharma I do not see `-p x:y` port mapping there. Plus, I have edited the answer to illustrate that your mysql image must be well configured to authorized remote access (allowing mysql workbench to contact it) – VonC Nov 20 '15 at 13:23
-
Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/95704/discussion-between-pawan-sharma-and-vonc). – Pawan Kumar Nov 20 '15 at 13:23
-
-
1Can you explain this? https://github.com/docker-library/mysql/issues/274#issuecomment-650040320 I used my ipv4 address with port 33060 , it didnt allow me , but when I created new container with -p 3306:3306 I was able to connect. – Murtaza Haji Nov 27 '20 at 19:09
-
@MurtazaHaji Only a published port would be accessible/visible from host: https://docs.docker.com/engine/reference/commandline/run/#publish-or-expose-port--p---expose – VonC Nov 27 '20 at 20:33
I was trying to connect from Mysql Workbench
but it wasn't allowing me. Turned out, I forgot to mention the port
. Here is the complete command to run and then connect from workbench:
Step 1 - Run docker container:
docker run --name mysql8 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=testdb -e MYSQL_USER=admin -e MYSQL_PASSWORD=root -d mysql:8.0.20
Explanation of the above command:
Mysql v8.0.20 (the image name)
mysql:8.0.20
Run as a detached container (ctrl + c won't stop the container)
-d
Container name
--name mysql8
Port expose
(external port on host machine : internal port of the container)
-p 3306:3306
Set environment variables
-e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=testdb -e MYSQL_USER=admin -e MYSQL_PASSWORD=root
You can see live logs of the container by -f means follow
:
docker logs mysql8 -f
Step 2 - Connect from Mysql Workbench:
host: localhost
port: 3306
user: admin
password: root

- 12,254
- 4
- 46
- 66
-
-
@MurtazaHaji Maybe the port isn't available, or any other issue. Check the logs `docker logs mysql8 -f` – Shaharyar Jul 23 '21 at 19:55
-
`ERROR 1396 (HY000) at line 1: Operation CREATE USER failed for 'root'@'%' ` – Murtaza Haji Jul 23 '21 at 20:05
-
Looks like you're trying to add a new user with the same name, while it already exists. Destroy your existing container, and create a fresh one using the commands mentioned in my answer. **Note: You will lose all the data you have in the container if you don't know the external volume address**. – Shaharyar Jul 23 '21 at 20:15
Specify your configuration
docker-compose.yml
. More details here. Example:version: '3.1' services: mysql: image: mysql:5.6 container_name: test-mysql ports: - 3306:3306 restart: always environment: MYSQL_ROOT_PASSWORD: password
- Run this comands
docker-compose up
and another terminal rundocker ps
to see your container. - Access your docker:
docker exec -it test-mysql bash
- Inside the container, to connect to mysql command line type,run
mysql -u root -p
. - Create a new user
- Finally config your MySQL Workbench

- 173
- 5
I found a video that showed another way to get this to work. You can specify the IP address when passing in the port number. That is, something like -p 127.0.0.1:3307:3306
instead of just -p 3307:3306
I've never seen that before
https://www.youtube.com/watch?v=20om-9Gwuc0#t=7m
Example start command
docker run -d -e MYSQL_ROOT_PASSWORD=test --name mysql8 -p 127.0.0.1:3307:3306 mysql:8
Then I was able to use MYSQL Workbench to connect to 127.0.0.1
at port 3307
.

- 1,136
- 11
- 15
@Krishna's answer worked but with a minor change - user was added as follows
create user 'user'@'%' IDENTIFIED WITH mysql_native_password BY 'pass';
see Authentication plugin 'caching_sha2_password' cannot be loaded

- 53
- 1
- 7
I got solution for this by setting field value in Hostname: 127.0.0.1 (Localhost), port by default 3306 with your creds.

- 1,511
- 4
- 15
- 18
-
2
-
@VonC Here i am referring workbench fields like *hostname*, *port*, *username* and *password* required for connecting mysql server. – Pawan Kumar Feb 20 '16 at 07:48
-
The explain of this reason on https://serverfault.com/questions/295285/mysql-cannot-connect-via-localhost-only-127-0-0-1?rq=1 – richardhell Jan 18 '23 at 13:37
On my case, I just needed to expose the port
docker run -p 33061:3306 <rest of command>

- 320
- 5
- 15
I followed instructions shown in mysql docker hub. wrote this docker-compose.yml
version: '3.1'
services:
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_ROOT_PASSWORD: example
ports:
- 3306:3306
Go to mysql workbench when you are making connection make sure to clear password in the parameters. now enter password(in my case example
)
I was able to docker describe
the mysql container, which held its IP address. I entered that in the Workbench connection screen, and it worked!

- 648
- 9
- 25