Toshimaru's Blog

Run and Connect to MySQL on Docker

I wanted to run MySQL v8.0.x on Docker aside from MySQL v5.7 running on my localhost, so I’ll give a instruction for it.

docker pull

First of all, let’s run docker pull and get MySQL v8.0 image.

$ docker pull mysql:8.0

8.0 means a tag. if it’s not specified, latest MySQL docker image well be used.

docker run

Then, run docker run.

$ docker run --name mysql8 -e MYSQL_ROOT_PASSWORD={your_password} -p 3306:3306 mysql:8.0
  • MYSQL_ROOT_PASSWORD is required, you can set whatever password you like
  • --name mysql option assigns a name(mysql) to the container
  • -p 3306:3306 option publishes a container’s 3306 port to the host
    • If you want to change port, change it to -p 3307:3306. Then, 3307 port is used

Simple docker run

Just setting MYSQL_ROOT_PASSWORD is enough. :)

$ docker run -e MYSQL_ROOT_PASSWORD={your_password} mysql:8.0
2020-01-07 16:06:52+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.18-1debian9 started.
2020-01-07 16:06:52+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2020-01-07 16:06:52+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.18-1debian9 started.
2020-01-07 16:06:52+00:00 [Note] [Entrypoint]: Initializing database files

docker ps

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
77ecf2eb0b98        mysql:8.0           "docker-entrypoint.s…"   8 seconds ago       Up 6 seconds>3306/tcp, 33060/tcp   mysql8

You can see:

  • image is mysql:8.0
  • Name is mysql8
  • Port setting is>3306/

Connect to MySQL

Finally, let’s connect to MySQL running on the docker.

$ mysql -h -u root -p
Enter password: {your_password}
ERROR 2059 (HY000): Authentication plugin 'caching_sha2_password' cannot be loaded: dlopen(/usr/local/Cellar/mysql/5.7.22/lib/plugin/, 2): image not found

But we’ve got an error. This is because MySQL server version and client version are different and those encryption methods are different.

Let’s connect to mysql using mysql client on the docker.

$ docker exec -it mysql8 bash

The following commands are run inside the docker.

root@xxx:/# mysql --version
mysql  Ver 8.0.17 for Linux on x86_64 (MySQL Community Server - GPL)
root@xxx:/# mysql -uroot -p
Enter password: {your_password}
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.17 MySQL Community Server - GPL

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


It works!

Stop MySQL container

$ docker stop mysql8

Remove created container

$ docker rm mysql8


Load more