introduction
In modern software development, MySQL is a popular relational database management system, which is widely popular for its reliability and ease of use. Docker enables quick and easy deployment and management of MySQL DB instances. This article will introduce two ways to deploy MySQL through Docker: using the Docker CLI command and using Docker Compose.
Part 1: Deploy MySQL through Docker CLI commands
1. Install Docker
Make sure Docker is installed on your system. If not installed, please refer toDocker official documentationProcess installation.
2. Pull MySQL Image
First, use the following command to pull a MySQL 8.0 image from Docker Hub:
docker pull mysql:8.0
3. Start the MySQL container
Next, start the MySQL container with the following command:
docker run -d \ --name mysql-8.0 \ -e MYSQL_ROOT_PASSWORD=123456 \ -e MYSQL_DATABASE=my_database \ -e MYSQL_USER=run \ -e MYSQL_PASSWORD=123456 \ -p 3306:3306 \ -v mysql-data:/var/lib/mysql \ mysql:8.0 \ --character-set-server=utf8mb4 \ --collation-server=utf8mb4_bin \ --lower_case_table_names=1
Parameter description:
-
-d
: Run containers in background mode. -
--name mysql-8.0
: Specify the container name asmysql-8.0
。 -
-e
: Set environment variables and configure MySQL's root password, default database name, user and its password. -
-p 3306:3306
: Map the host's port 3306 to the container's port 3306 for external access to the database. -
-v mysql-data:/var/lib/mysql
: Create a persistent volume, store the data in the container on the host, ensuring that the data is not lost. - Finally, character sets and collations are specified to optimize database support.
4. Verify the running status of MySQL
Run the following command to check if the container has been started successfully:
docker ps
If the container list appearsmysql-8.0
, it indicates that the deployment is successful.
5. Enter the MySQL container
You can connect to a MySQL database using the following command:
docker exec -it mysql-8.0 mysql -uroot -p123456
Part 2: Deploy MySQL through Docker Compose
Docker Compose allows users to define aFiles, conveniently manage and start multiple services. Here are the steps to deploy MySQL through Docker Compose.
1. Create a file
In your working directory, create a name calledfile and enter the following content:
version: '3.8' services: mysql: image: mysql:8.0 container_name: mysql-8.0 environment: MYSQL_ROOT_PASSWORD: 123456 MYSQL_DATABASE: my_database MYSQL_USER: run MYSQL_PASSWORD: 123456 ports: - "3306:3306" volumes: - mysql-data:/var/lib/mysql command: - mysqld - --character-set-server=utf8mb4 - --collation-server=utf8mb4_bin - --lower_case_table_names=1 networks: - mysql-network volumes: mysql-data: networks: mysql-network: driver: bridge
2. File analysis line by line
version: '3.8'
Specify the version of the Docker Compose file,3.8
Suitable for most users.services:
Defines the service running in Docker.mysql:
Specifies the name of the service, indicating the MySQL database service.image: mysql:8.0
Using the MySQL 8.0 image, if there is no local image, Docker will automatically download it from Docker Hub.container_name: mysql-8.0
Set the name of the container for easy management and identification.-
environment:
Set MySQL environment variables:-
MYSQL_ROOT_PASSWORD
: root user's password. -
MYSQL_DATABASE
: The name of the database created during initialization. -
MYSQL_USER
andMYSQL_PASSWORD
: Create a new user and its password.
-
ports:
Map the host's port 3306 to the container's port 3306 for external access to the database.volumes:
Put the host onmysql-data
The volume mounted to the container/var/lib/mysql
Directory to ensure the persistence of data.command:
Specify command parameters at MySQL startup, set character sets and collation rules to optimize database support.networks:
Define the network used by the container,mysql-network
Allows communication between containers.volumes:
Create a name calledmysql-data
volume for data persistence.networks:
Create a name calledmysql-network
The bridge network facilitates the interconnection between containers.
3. Start MySQL Service
existIn the directory where the file is located, execute the following command to start the service:
docker-compose up -d
4. Verify service status
Use the following command to view the running container:
docker-compose ps
make suremysql-8.0
The container is running.
5. Enter the MySQL container
Use the following command to access the MySQL database:
docker exec -it mysql-8.0 mysql -uroot -p123456
6. Stop and remove containers
To stop and delete the container, you can use the following command:
docker-compose down
Summarize
This article describes two ways to deploy MySQL databases in Docker: through Docker CLI commands and through Docker Compose files. Each approach has its own unique advantages, and you can choose the right approach based on project needs and personal preferences.
Attachment: Complete file content
version: '3.8' services: mysql: image: mysql:8.0 container_name: mysql-8.0 environment: MYSQL_ROOT_PASSWORD: 123456 MYSQL_DATABASE: my_database MYSQL_USER: run MYSQL_PASSWORD: 123456 ports: - "3306:3306" volumes: - mysql-data:/var/lib/mysql command: - mysqld - --character-set-server=utf8mb4 - --collation-server=utf8mb4_bin - --lower_case_table_names=1 networks: - mysql-network volumes: mysql-data: networks: mysql-network: driver: bridge
Through the above steps, you can quickly deploy MySQL databases in Docker, ensuring flexibility and consistency in your development environment.
This is the end of this article about two methods of deploying MySQL database using Docker. For more related content on Docker deployment MySQL database, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!