Install MariaDB on Docker
- August 10, 2023
- 5 min read
Intro
In this article, I will walk you through the process of installing MariaDB on a new Docker container. This process is useful if you’re using Docker containers to host different applications and orchestrate them all together in a highly available way.
What is docker?
Docker is an open platform for developing, shipping and running applications in a distributed environment. It provides developers with tools to create lightweight, portable and reusable containers from any application.
What is MariaDB?
MariaDB is an open-source database server. Originally developed as a MySQL replacement, it is proven to be fast, reliable, and scalable. In addition, MariaDB has maintained compatibility with MySQL features and handles data storage elegantly. It’s used by thousands of organizations around the world because of its high availability and security features.
Installation
Now, we have a better idea about docker and MariaDB. Let's get into the real deal. To continue this part of the blog you have to install docker on your computer or server first. Once you've done that, set up MariaDB on that same environment for further installation.
Step 1 - Pull docker image: This command will pull the MariaDB docker image from the docker hub
Step 2 - Setup the environment: You can store all the environment variables in Docker Secrets using Swarm Service
In Docker secrets, you use a secret name to store credentials and values that are shared across different Docker hosts. This includes all types of secrets, such as database access credentials or API keys that are used by your application. When you set a secret in the registry, it will be stored as a host-level secret.
Initialize Swarm service
Add secrets
Root password: Store the root password as a secret to pass when running the image.
Create network
In this article, I'll explain how to create a new overlay network for docker swarm and MariaDB. As I'm using swarm service and running MariaDB as a service, I decided to use an overlay network adapter. To learn more about overlay docker network adapter refer to Docker network basics
Step 3 - Run docker image
I run the MariaDB image as a docker service to use with the swarm service. If you are not using swarm service you can run the image with the docker run command as below
But, here we use the service method.
These are the values I passed in the above command:
- --name: Name of the service
- --restart: Restart when the condition is met ("none"|"on-failure"|"any") (default "any")
- --network: Network attachment. Here, we can use the network created in the previous step.
- --publish: Publish a port as a node port
- --mount: Mount a file-system
- --secret: Store or retrieve an arbitrary secret in the key/value store in docker secrets. (default " none")
- -e --env: Environment variables. Here I pass the root password which I stored in secrets.
Step 4 - Setup MariaDB
So first of all, let's log in to the MariaDB container so that we can set up the database. If you haven't passed the root password in environment variables you can set it by running mysql_secure_installation
.
i. Log in to container
ii. Setup MariaDB root password
iii. If you have passed the MariaDB root password in environment variables, you can immediately log in to the MySQL command line with the following command
iv. Once you log in to the MySQL command line using your credentials, you can create your new database and start setting up your MySQL configuration. Be aware that this creates the database structure, user accounts, and grants
- Create Database :
Use the following command to check whether your database has been created or not.
- Create user :
use the following command to check your created user,
- To grant permissions to a user for a particular database, you can use the GRANT command. Most likely, you have already granted the user default permissions for tables in that particular database. By using the GRANT command, you can add more rights to the existing users, give permissions to new users and change or delete user privileges.
(YOUR_DB.
Once you have executed the MySQL commands above, you can check the access with the below command,
Summary
In this article, MariaDB installation is discussed. It helps in ensuring that MariaDB can be reliably used together with other applications in the Docker environment. Also, the Docker overlay network description is discussed here. Please let me know your thought and ideas in the comment section. Also, I would like to know if you have any questions regarding Docker and MariaDB, I'm happy to help you.
Docker
Maria DB
Containerization