How to Run MySQL Database Server using Docker Container in Linux Operating System

Posted on

Introduction

This is an article where the main focus is just to run MySQL Database Server using docker container. It is an alternative to be able to run MySQL Database Server without having to install it in the first place. The preparation is simple and fast relying on the available internet connection. The implementation of providing MySQL Database Server’s service is done using a docker container in Linux operating system. Moreover, the implementation is in Linux Ubuntu 19.10. The arrangement of this article is exist in several parts. The process for running MySQL Database Server using Docker Container exist in two parts. The first part is the configuration step. The last one is the testing step to test whether the MySQL Database Server is running in the docker container and it exist for further access.

 

Configuration Step

This part is a part for the configuration step of the docker container. In order to run MySQL Database Server, just perform the following sequence of actions :

1. Create a file with the name of docker-compose.yml. Just place the file in any folder or path exist in the operating system. In this article, for an example the file exist in ‘/home/user/docker-compose/0-db’.

2. Fill the content of the file ‘docker-compose.yml’ with the following configuration :

version: '2'
services:
  db:
    image: mysql
    volumes:
      - "./data/db:/var/lib/mysql"
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}

3. Create another file with the name of ‘.env’. This file will be useful to define the value of the environment variables needed in the ‘docker-compose.yml’ file. Modify and adjust the value of the environment variables accordingly as follow :

MYSQL_ROOT_PASSWORD=password
MYSQL_DATABASE=mysql
MYSQL_USER=root
MYSQL_PASSWORD=password

Before running the docker container, first of all list all of the available container by executing the following command :

sudo docker container ls -a

The execution of the above command exist in the following output :

user@hostname:/home/user/docker-compose/0-db# sudo docker container ls -a
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS                     PORTS                               NAMES
a86854907ea5        php:7.1-apache           "docker-php-entrypoi…"   3 hours ago         Exited (0) 3 hours ago                                         docker-php-71-mysql-apache_php_1
4b477836bff1        mysql/mysql-server:5.7   "/entrypoint.sh mysq…"   3 weeks ago         Exited (255) 3 weeks ago   33060/tcp, 0.0.0.0:3308->3306/tcp   determined_euclid
4cb95b4f1ea4        mysql/mysql-server:5.7   "/entrypoint.sh mysq…"   5 weeks ago         Exited (255) 5 weeks ago   3306/tcp, 33060/tcp                 laughing_noyce
167795afbce0        mysql/mysql-server:5.7   "/entrypoint.sh mysq…"   5 weeks ago         Exited (0) 5 weeks ago                                         epic_kilby
e4a9ee25671b        mysql/mysql-server:5.7   "/entrypoint.sh mysq…"   6 weeks ago         Exited (255) 5 weeks ago   33060/tcp, 0.0.0.0:3308->3306/tcp   hopeful_wescoff
10f7d4bdb1dd        mysql/mysql-server:5.7   "/entrypoint.sh mysq…"   7 weeks ago         Exited (255) 6 weeks ago   33060/tcp, 0.0.0.0:3308->3306/tcp   adoring_elion
6a5e88f9a264        mysql/mysql-server:5.7   "/entrypoint.sh mysq…"   7 weeks ago         Created                                                        vigiant_shtern
972d07bfe2a1        mysql/mysql-server:5.7   "/entrypoint.sh mysq…"   7 weeks ago         Exited (255) 7 weeks ago   33060/tcp, 0.0.0.0:3308->3306/tcp   vigilant_shtern
42c98cb79a53        mysql/mysql-server:5.7   "/entrypoint.sh mysq…"   7 weeks ago         Exited (0) 7 weeks ago                                         distracted_feynman
ecd2b654b683        mysql/mysql-server:5.7   "/entrypoint.sh mysq…"   7 weeks ago         Created                    33060/tcp, 0.0.0.0:3308->3306/tcp   eloquent_lovelace
267808afe834        mysql/mysql-server:5.7   "/entrypoint.sh mysq…"   7 weeks ago         Exited (255) 7 weeks ago   33060/tcp, 0.0.0.0:3308->3306/tcp   mysql_local
user@hostname:/home/user/docker-compose/0-db# 

4. After finishing the above step for creating the necessary files and also checking for the running docker container, execute the following command in the command line interface using sudo command as a normal user or just simply as root :

sudo docker-compose up -d

The output of the above command execution exist as follows :

user@hostname:~/docker-compose/0-db$ sudo docker-compose up -d
Pulling db (mysql:)...
latest: Pulling from library/mysql
619014d83c02: Pull complete
9ced578c3a5f: Pull complete
731f6e13d8ea: Pull complete
3c183de42679: Pull complete
6de69b5c2f3c: Pull complete
00f0a4086406: Pull complete
84d93aea836d: Pull complete
f18efbfd8d76: Pull complete
012b302865d1: Pull complete
fe16fd240f59: Pull complete
ca3e793e545e: Pull complete
51d0f2cb2610: Pull complete
Creating 0-db_db_1 ... 
Pulling db (mysql:)...
Creating 0-db_db_1 ... done
user@hostname:~/docker-compose/0-db$ 

Try to execute the previous command again to see whether there is a running container created after the execution of the above command as follows :

user@hostname:~/docker-compose/0-db$ sudo docker container ls -a
CONTAINER ID        IMAGE                    COMMAND                  CREATED              STATUS                     PORTS                               NAMES
6a051f794447        mysql                    "docker-entrypoint.s…"   About a minute ago   Up About a minute          3306/tcp, 33060/tcp                 0-db_db_1
4b477836bff1        mysql/mysql-server:5.7   "/entrypoint.sh mysq…"   3 weeks ago          Exited (255) 3 weeks ago   33060/tcp, 0.0.0.0:3308->3306/tcp   determined_euclid
4cb95b4f1ea4        mysql/mysql-server:5.7   "/entrypoint.sh mysq…"   5 weeks ago          Exited (255) 5 weeks ago   3306/tcp, 33060/tcp                 laughing_noyce
167795afbce0        mysql/mysql-server:5.7   "/entrypoint.sh mysq…"   5 weeks ago          Exited (0) 5 weeks ago                                         epic_kilby
e4a9ee25671b        mysql/mysql-server:5.7   "/entrypoint.sh mysq…"   6 weeks ago          Exited (255) 5 weeks ago   33060/tcp, 0.0.0.0:3308->3306/tcp   hopeful_wescoff
10f7d4bdb1dd        mysql/mysql-server:5.7   "/entrypoint.sh mysq…"   7 weeks ago          Exited (255) 6 weeks ago   33060/tcp, 0.0.0.0:3308->3306/tcp   adoring_elion
6a5e88f9a264        mysql/mysql-server:5.7   "/entrypoint.sh mysq…"   7 weeks ago          Created                                                        vigiant_shtern
972d07bfe2a1        mysql/mysql-server:5.7   "/entrypoint.sh mysq…"   7 weeks ago          Exited (255) 7 weeks ago   33060/tcp, 0.0.0.0:3308->3306/tcp   vigilant_shtern
42c98cb79a53        mysql/mysql-server:5.7   "/entrypoint.sh mysq…"   7 weeks ago          Exited (0) 7 weeks ago                                         distracted_feynman
ecd2b654b683        mysql/mysql-server:5.7   "/entrypoint.sh mysq…"   7 weeks ago          Created                    33060/tcp, 0.0.0.0:3308->3306/tcp   eloquent_lovelace
267808afe834        mysql/mysql-server:5.7   "/entrypoint.sh mysq…"   7 weeks ago          Exited (255) 7 weeks ago   33060/tcp, 0.0.0.0:3308->3306/tcp   mysql_local
user@hostname:~/docker-compose/0-db$ 

Testing Step

In the last part, it is just the part for testing the running MySQL Database Server exist in the docker container. According to the above output command execution in the previous part, there is a new container running with the name of ‘mysql’. So, in order to test the functionality of the running MySQL Datase Server, try to access it. Before trying to access it, execute the following following command to check the IP Address of the running container :

docker inspect container_id | grep IPAddresss

The following is the output of the above command execution using part of the container ID of ‘6a051f794447’ representing for the MySQL Database running container :

user@hostname:~/docker-compose/0-db$ sudo docker inspect 6a05 | grep IPAddress
            "SecondaryIPAddresses": null,
            "IPAddress": "",
                    "IPAddress": "172.20.0.2",
user@hostname:~/docker-compose/0-db$ 

The following is the execution of the command for accessing or connecting to MySQL Database Server :

root@hostname:/home/user/docker-compose/0-db# mysql -uroot -p -P3306 -h172.20.0.2
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.19 MySQL Community Server - GPL

Copyright (c) 2000, 2020, 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
owners.

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

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.14 sec)

mysql> create database mydb;
Query OK, 1 row affected (1.42 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mydb               |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> 

Apparently, as it exists in the above output, accessing or connecting to MySQL Database Server is a success.

Leave a Reply