Home  >  Article  >  Operation and Maintenance  >  How to use Docker to install and configure a MySQL database

How to use Docker to install and configure a MySQL database

PHPz
PHPzOriginal
2023-04-10 14:17:201067browse

Docker has become the most popular application containerization technology. It runs on any platform (Linux, Windows, Mac), thus greatly simplifying application deployment and management. Docker can containerize not only the application itself, but also dependent services, such as databases. In this article, we will discuss how to install and configure a MySQL database using Docker.

  1. Download the MySQL Docker image
    First, we need to download the MySQL Docker image from Docker Hub. Run the following command to download the latest version of MySQL from Docker Hub.
docker pull mysql

This will download the latest version of the MySQL Docker image.

  1. Create MySQL container
    Once the MySQL Docker image is downloaded, we can create the MySQL container. In Linux/Mac systems, you can use the following command:
docker run --name test-mysql -e MYSQL_ROOT_PASSWORD=<your_password> -p 3306:3306 -d mysql

The above command will create a new container from the MySQL Docker image, named test-mysql. The parameter "-e MYSQL_ROOT_PASSWORD=" specifies the password of the MySQL root user. The parameter "-p 3306:3306" maps the MySQL port in the container to port 3306 on the host.

If you are running Docker on a Windows system, you need to run the following command:

docker run --name test-mysql -e MYSQL_ROOT_PASSWORD=<your_password> -p 3306:3306 -d mysql --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

Unlike Linux/Mac, Windows does not support modifying the character set and sorting method inside the container before running the MySQL container. , so these options need to be specified when running the container.

  1. Check MySQL container status
    Run the following command to check the status of the container:
docker ps

You should see that the test-mysql container is included in the returned results information, indicating that the container has been successfully run.

  1. Login to the MySQL container
    On Linux/Mac systems, you can log in to the MySQL container with the following command:
docker exec -it test-mysql mysql -uroot -p<your_password>

On Windows, use the following command:

docker exec -it test-mysql mysql -uroot -p<your_password> --protocol=tcp

After running the above command, you will enter the MySQL command line interface. This means you have successfully installed the MySQL Docker container.

Summary
Docker makes it easier to install and manage MySQL. In this article, we explain how to download the MySQL Docker image from Docker Hub and create a MySQL container. We also provide more detailed installation steps for Windows users. With these steps, you should have successfully learned how to check Docker installation of MySQL.

The above is the detailed content of How to use Docker to install and configure a MySQL database. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn