SoFunction
Updated on 2025-04-11

Detailed steps to deploy MySQL server through Docker under Ubuntu

introduction

Docker is an open source containerized platform that allows developers to package applications and their dependencies into a lightweight, portable container. It provides consistency with the software environment and greatly simplifies the development, testing and deployment of applications. MySQL is a popular open source relational database management system that supports the SQL language and is widely used in various projects. Deploying MySQL in Docker containers not only simplifies the installation process, but also facilitates version management and migration. Next, we will explain in detail how to deploy MySQL servers through Docker on Ubuntu systems.

Deployment steps

1. Install Docker

First, make sure Docker is installed on your system. Docker can be installed through the following command:

sudo apt update
sudo apt install 

After the installation is complete, start the Docker service and set it to power on and start:

sudo systemctl start docker
sudo systemctl enable docker

2. Pull MySQL Image

Next, we need to pull the MySQL image from the Docker Hub. Here we use the official MySQL image:

sudo docker pull mysql:latest

3. Create MySQL container

Now we can create a MySQL container. The following command creates a namedmy_mysqlMySQL container and set the root password toyour_password(Please change your password as needed):

sudo docker run --name my_mysql -e MYSQL_ROOT_PASSWORD=your_password -d mysql:latest
  • --name my_mysql: Specify the name of the container
  • -e MYSQL_ROOT_PASSWORD=your_password: Set the root user password of MySQL
  • -d: Let the container run in the background

4. Access MySQL container

You can enter the MySQL container and access the MySQL database through the following command:

sudo docker exec -it my_mysql mysql -u root -p

The system will prompt you to enter the password, and just enter the password you set when creating the container.

5. Data persistence

To ensure that data is not lost when the container stops, we need to configure the data volume. You can use the following command to create a namedmysql_datadata volume and mount it into the MySQL container:

sudo docker run --name my_mysql -e MYSQL_ROOT_PASSWORD=your_password -v mysql_data:/var/lib/mysql -d mysql:latest

In this way, even if the container is deleted, the data will be saved inmysql_dataIn the volume.

6. Common parameters description

  • MYSQL_ROOT_PASSWORD: Set the password of the MySQL root user.
  • -v: Used to mount volumes to ensure data persistence.

Summarize

Through the above steps, we successfully deployed the MySQL server through Docker on Ubuntu system. Whether it is a development environment or a production environment, this method allows you to quickly get started and manage your database. The flexibility of Docker and the power of MySQL combined together make developers' work more efficient and convenient. Come and try it!

This is the article about the detailed steps for deploying MySQL servers through Docker under Ubuntu. For more related content on Docker deployment MySQL under Ubuntu, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!