MySQL on Docker

This is a short recipe for starting the MySQL on Docker. I have Windows 10 machine now and run Docker natively (no Linux VM).

Run the following command:

docker run --name dimitar-mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=yes -v C:/DockerContainersData/mysql:/var/lib/mysql -p 2000:3306 -d mysql
  • –name: give it whatever you want
  • -e MYSQL_ALLOW_EMPTY_PASSWORD=yes: allow empty root password
  • -v C:/DockerContainersData/mysql:/var/lib/mysql: to keep the data persistent, this says store the mysql data in this folder, in our case the C:/DockerContainersData/mysql. I haven’t tested much yet, but running another version of mysql inside docker, with this same setup, should give you the exact same data
  • -p 2000:3306: map the port 3306 from the docker container to port 2000 on host machine
  • -d: Detached mode: Run container in the background
  • mysql: name of the docker image

That’s it. After this, running docker ps should show the newly created container. If you do not see it, run docker ps -a, find the image and see the logs with docker logs container ID.

Check out the docker docs: https://docs.docker.com/engine/reference/run/.

Now, if Windows won’t let you connect, then you have some more settings to change inside the MySQL:

docker ps
docker exec -it 78e2d32a7add bash
mysql
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '';

In short, find the container, run bash inside it, change the root password and let it connect from ‘%’ (anywhere). After this, you can use MySQL Workbench to connect from Windows to MySQL inside docker (port is 2000 here!). If MySQL Workbench starts giving you issues, complaining about creating schemas and DDL, just update it to newer version.