Run MySQL from docker and allow access from outside container

Run MySQL from docker and allow access from outside container

By default MySQL deployed in docker has no access outside of docker network. This quick guide shows how to enable access to MySQL database from outside network.

First run MySQL from docker:

$ docker run -d --name=dd-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=TempPassword mysql/mysql-server

After Mysql container is running login to via docker command:

$ sudo docker exec -it dd-mysql /bin/bash

Login to MySQL database providing user name and password set during container creation:

$ mysql -u root -pTempPassword

Then switch to the “mysql” database:

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

Set the host to “%” so it can be possible to login from any address:

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

At the end flush privileges to have it working:

Query OK, 0 rows affected (0.03 sec)

That is it. Test it by login from your local machine or from machine from your local network. If you don’t have mysql client install it (example from Ubuntu):

$ sudo apt install mysql-client-core-8.0
$ mysql -u root -pTempPassword -h localhost

Or you can use application like Mysql Workbench.

Leave a Reply