Containerizing — Installing mysql database on Docker

Ari Nurcahya
3 min readJan 13, 2021

Recently I’ve got the problem when I tried to migrating to microservice on my job. I cannot to trying the apps that I want to migrate to microservice because the database use mysql5.7 but on my local machine I’m using mysql 8. so there so many problems facing to me. so I uninstalled my mysql 8 then trying to installing mysql 5.7 on my machine, but when it was finished my mysql database won’t to run after restarting my machine. so I asked to my friend “hello bro, what version of mysql that installed on your machine?” then he told me, he used docker for the mysql. and he said because of only for local development, so he used mysql installed on docker. and I asked “Is good to install mysql on Docker? I was read about some article that told me ‘don’t install’ mysql on Docker” and He said it is good because you used mysql only for local development.

Firstly you must install docker on your machine, here I’m using macOS version of docker. after you successfully installed docker to your machine continue the step.

type the code bellow

docker run \
-d \
--name=mysqldbv5.7 \
-p 3306:3306 \
--volume=/Users/nurcahyaari/Documents/docker-volumes/mysqldb-dir:/var/lib/mysql \
mysql/mysql-server:5.7

what is that?

if you don’t know what is / in the last of sentences. the backslashed (/) meaning we want create a newline

first we must run the docker with docker run, in this case we will creating a brand new container.

then -d mean that we want create a background process for our container. — name=mysqldbv5.7 mean that we want create a new container called mysqldbv5.7 and you can named it as you like, example name it as dbmysql, db, or whatever.

then -p 3306:3306 mean that we want to expose the container our machine, because the default act of docker is isolated, so you can accessing your docker container directly from your localhost, or your machine, so I exposed the docker to 3306 port. maybe you confuse why the port is the same? first in the right side is the mysql port from the docker, then in the left side is the exposed port to be and accessed port for our machine.

after that I used docker volume for data persistence. — volume=/Users/nurcahyaari/Documents/docker-volumes/mysqldb-dir:/var/lib/mysql. it mean that we use volume from the host machine at the /Users/nurcahyaari/Documents/docker-volumes/mysqldb-dir directory, and used for the /var/lib/mysql directory from the docker container

and the last we want create the container from the mysql images so we must define the image that we want to use, here the command mysql/mysql-server:5.7

Yay we created a brand new container… but You still cannot open your mysql instance from your container.

you must setup your password. type the code below:

docker logs mysqldbv5.7 2>&1 | grep GENERATED

when it successfully your terminal will printing this code

then open your mysql instance

docker exec -it mysqldbv5.7 mysql -uroot -p

then type the password that you created from the code above. after that you must change your password

alter user 'root'@'localhost' identified by 'password';

where the ‘password’ meaning your new password, if you type like the code above, your mysql password will be password

then type

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

after that try to restart your container, and try to connect to your mysql instance again

docker exec -it mysqldbv5.7 mysql -uroot -p

oke thats all. if you have any question let me answering, thanks

--

--