As a Database Engineer in Mydbops. We tend to solve multiple complex problems for our esteemed customers. To control the System resources and scale up /down based on needed we are evaluating Dockers and Kubernetes.
Docker is a set of platform as a service products that uses OS-level virtualization to deliver software in packages called Containers. Containers are isolated from one another and bundle their own software, libraries and configuration files; they can communicate with each other through well-defined channels.It’s more lightweight than standard Containers and boots up in seconds.
Docker also is easy to use when you need a simple, single instance. What is great about Docker though is that it allows configuring multiple versions of MySQL.
Docker Installation:
Docker can be installed with yum repository or apt-get repository based on your linux operating system distributions. (I’m using CentOS 7 operating system in the following examples).
To start the docker service, Run the following command:
How to pull the Docker MySQL Images?
There are two official MySQL Docker Repositories.
- Docker team (https://hub.docker.com/r/mysql/mysql-server/) maintains a branch and can be pulled by a simple
- MySQL team (https://hub.docker.com/_/mysql) is maintains a branchand can be pulled by a simple
I have used docker’s team images(latest), from Oracle MySQL’s team , but there are many custom designed docker images available in the Dockerhub too.
To download the MySQL Server image, run this command:
If :tag is omitted, the latest tag is used by default and the image for the latest GA version of MySQL Server is downloaded. For older versions use the tags available with above command.
Step 1 : Pull the Docker image for MySQL
To list all the docker images downloaded, run this command:
Start MySQL Server Instance
Start a new docker container for MySQL Server, run below command
The –name option, for supplying a custom name for your server container and it is optional. If no container name is supplied, a random one is generated.
- -m ( Memory )
- -c ( CPU’s )
- –port=3306.
Initialization for the container begins, and the container appears in the list of running containers when the docker ps command is executed.
Run this command to monitor the output from the container:
Once the initialisation is completed, the random password generated for the root user is filtered from the log output. And reset the password on the initial run.
Step 3: Connect the MySQL Server within the ContainerRun the following command to start a mysql client inside the docker container,
When asked, enter the generated root password.You must reset the server root password because the MYSQL_ONETIME_PASSWORD option is true by default.
Container Shell Access:To have shell access to your MySQL Server container, run the docker exec -it command to start a bash shell inside the container:
You can then run Linux commands inside the container. For example, to view contents in the MySQL server’s data directory inside the container, run this command:
Stopping and Deleting a MySQL Container
To stop the MySQL Server container you have created, run this command:
docker stop sends a SIGTERM signal to themysqld process, so that the server is shut down gracefully.
To start the MySQL Server container again, execute this command:
To restart the MySQL Server, run this command:
To delete the MySQL container, stop it first, and then run the docker rm command:
Storage management in docker ?
By default, Docker stores data in its internal volume. To validate the location of the volumes, use the command:
In that output,it shows
You can also change the location of the data directory and create one on the host for persistence. Having a volume outside the container allows other applications and tools to access the volumes when needed. It’s also a best practice followed for databases.
For example if you remove the docker instance unfortunately the data directory gets also removed, it is always better to have the data directory outside the docker with the DB log files.
Containers help us in automation and easy customisations. We have all needed DB tool and monitoring packages with a custom MySQL images which can scale the database operations.We will evaluate more about containers in upcoming days.