How to Setup MySQL Database in Docker

Docker provides a lightweight, portable, and efficient containerization platform, while MySQL offers a robust relational database system. In this article, we will explore the steps about how to setup MySQL Database in a Docker container with docker-compose script.

Looking out to setup PostgreSQL in Docker container? This article will walk you through the step-by-step process to setup PostgreSQL in a Docker container.

Benefits of using MySQL in Docker

  1. Portability: Docker containers encapsulate the MySQL environment, ensuring consistent behavior across different systems.
  2. Ease of Deployment: Docker simplifies MySQL deployment, reducing setup time and ensuring reproducibility in various environments.
  3. Version Control: Docker lets you keep track of different versions of MySQL images, making it easy to go back to a previous state if needed.
  4. Infrastructure as Code (IaC): Include MySQL containers in your infrastructure code practices to improve automation.

How to Setup MySQL Database in Docker

Creating a MySQL database in Docker is a simple way for developers and system administrators to handle databases in a consistent and repeatable way. In this easy guide, we’ll walk you through the steps of setting up MySQL in Docker.

Create a Docker Compose Script

Start by making a Docker Compose script. In it, you’ll define the MySQL image and set up the credentials. The script fetches the latest image from the registry and creates a user profile with an initial database.

Generate a docker-compose.yml file with the following content. Customize the username, password, and database name as per your specific requirements.

This file helps create a MySQL service with specific settings, making sure data stays even if the container is restarted. You can change the environment variables to customize the MySQL setup as needed.

Build the Docker Container

Next, go ahead and create and start the Docker container. This involves getting the MySQL image, setting up the database, and making the first user linked to a specific database.

Execute the following command to initiate the construction and deployment of the container.

Docker Compose Build MySQL Database Image
Docker Compose Build MySQL Database Image

With the database now operational, let us examine the logs to verify its successful initiation.

Run the following command to get the container logs.

Docker MySQL Container Logs
Docker MySQL Container Logs

The MySQL database has been successfully configured and initiated within the Docker container.

Connect to MySQL Database

The last thing to do is connect to the MySQL database using the username, password, and database name you set in the Docker Compose script.

Run the following command to connect to the MySQL database.

Syntax is:

docker exec -it <container name> mysql -u <username> -p

Running the command will ask you to enter the password. Once you enter the password, it will connect to the database, and the following screen will appear.

Connect to MySQL in Docker Container
Connect to MySQL in the Docker Container

Run the following command in the MySQL environment to check the initial database.

It will show sbdb database is created by default. Set the sbdb as default database and create a table in it.

Docker MySQL Create Table and Select Data
Docker MySQL Create Table and Select Data

Conclusion

Here’s a summary of setting up a MySQL database in a Docker container:

  1. Make a Docker Compose script to get the latest MySQL image.
  2. Set up the initial username, password, and database configuration in Docker Compose Script.
  3. Build and start the container.
  4. Connect to the MySQL database using the specified credentials.

Are you using MongoDB in a Docker container? This article could assist you if you find yourself stuck and need help.

Leave a Reply

Your email address will not be published. Required fields are marked *