DEV Community

Z. QIU
Z. QIU

Posted on • Updated on

Installation of Mysql in Docker (Ubuntu 18.04)

Info: This post is one of the articles in context of this post: Huawei Cloud ECS server notes

Just now, I installed MySql on my cloud server using Docker.

1) Create launcher file and mapping folders:

cd ~/launchers
touch mysql_docker_22345.sh
cd ~/docker
mkdir mysql_22345
Enter fullscreen mode Exit fullscreen mode

2) Launch Mysql container using cmd line with initial password for root user (this will take some time because docker needs to pull the image from its server):

docker run --name mysql_docker_22345 -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306  -v /home/jemaloQ/docker/mysql_22345/mysql_conf/:/etc/mysql/  -v /home/jemaloQ/docker/mysql_22345/data:/var/lib/mysql -d mysql:5.7
Enter fullscreen mode Exit fullscreen mode

3) Check if MySql container is working correctly:

docker ps -a | grep mysql
Enter fullscreen mode Exit fullscreen mode

4) If container is running, then we need to change the password config. We enter the inside of the container and login MySql as root:

docker exec -it mysql_docker_22345 mysql -u root -p

Enter fullscreen mode Exit fullscreen mode

then type the initial password and type enter.

5) Once we enter successfully MySql console inside the container, we change the password:

set password for 'root'=password('jemaloQ*#!');
Enter fullscreen mode Exit fullscreen mode

if everything goes Ok, it shall yields output like:

Query OK, 0 rows affected, 1 warning (0.00 sec)
Enter fullscreen mode Exit fullscreen mode

6) Now we copy the following script into our launch file:

docker rm -f mysql_docker_22345
docker run -it -d \
 --name mysql_docker_22345 \
 -p 22345:3306 \
 -v /home/jemaloQ/docker/mysql_22345/mysql_conf/:/etc/mysql/ \
 -v /home/jemaloQ/docker/mysql_22345/data:/var/lib/mysql \
 mysql:5.7
Enter fullscreen mode Exit fullscreen mode

7) Re-launch our Mysql service in container:

sh ~/launchers/mysql_docker_22345.sh
Enter fullscreen mode Exit fullscreen mode

8) Re-check if Mysql container is working correctly

docker ps -a | grep mysql
Enter fullscreen mode Exit fullscreen mode

Now try connecting it from Heidi:

Alt Text

Enjoy!

Top comments (0)