Install MySQL 8 using Docker

Installing and using MySQL with Docker is simple and straight forward.

Step 1: Get latest MySQL docker image
$ docker pull mysql/mysql-server:latest

Step 2: Verify the image
$ docker images

Step 3: Create Docker container from downloaded image
$ docker run -d -p 3307:3306 –name=docmysql -d mysql/mysql-server:latest

The default mysql port 3306 from docker container is mapped to port 3307 on host machine. (This is in case if you have another mysql instance running on your host machine. If this is the first instance you can use -p 3306:3306).

Step 4: Get root password. Docker creates a random password and its stored in docker logs.
$ docker logs docmysql 2>&1 | grep GENERATED

Step 5: Login to Docker Shell using root password

$ docker exec -it docmysql mysql -uroot -p

Step 6: Change root password in mysql

mysql> ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘somesecurepassword’;

Step 7: (optional step) Create secondary users

mysql> Create user ‘newuser’@’%’ identified WITH mysql_native_password by ‘newpassword’;

mysql> GRANT ALL PRIVILEGES ON . TO ‘newuser’@’%’;

mysql_native_password is important if you are using different or older mysql workbench client to work with MySQL 8.

Step 8: Exit from mysql shell and connect to mysql from client.

Hostname : localhost

User : root or newuser

Password : updated password.

Port : 3307 (or 3306 default)

mysql workbench - docker



Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: