Docker container is a lightweight virtualization technology that packages applications and their dependencies in a complete file system at the operating system level for easy porting and deployment. MySQL is a popular open source database. Using Docker to deploy MySQL can simplify the installation and management of the database environment. However, Docker containers are different from traditional virtual machines, and the installation and configuration process may require some additional steps. In this article, we'll cover how to install and configure MySQL in a Docker container.
1. Install the MySQL image
docker pull mysql
This will download the latest version of the MySQL image from Docker Hub. You can view the list of installed images through the docker images command.
2. Create a MySQL container
docker run --name=mysql-container-name -e MYSQL_ROOT_PASSWORD=your_password -d mysql:latest
Among them, mysql-container-name can be the name of the container , your_password is the password of the MySQL root user. This command will create a MySQL container and set it to run in the background.
docker ps
This command will list all running containers. Make sure the MySQL container you just created is running properly.
3. Configure the MySQL container
docker exec -it mysql-container-name bash
Among them, mysql-container-name is the name of the container.
mysql -u root -p
Enter the root password you just set.
CREATE USER 'your_username'@'%' IDENTIFIED BY 'your_password'; GRANT ALL PRIVILEGES ON *.* TO 'your_username'@'%' WITH GRANT OPTION; FLUSH PRIVILEGES;
Where, your_username is the name of the new user to be created, and your_password is the password of the user.
vim /etc/mysql/my.cnf
Add the following content under the [mysqld] tag of the MySQL configuration file:
[mysqld] character-set-server=utf8mb4 collation-server=utf8mb4_unicode_ci
This will set the MySQL Character sets and collations.
service mysql restart
Now that the MySQL container is installed and properly configured, you can connect to the MySQL server through the corresponding client tools.
Conclusion
Using Docker containers to deploy MySQL can simplify the deployment and management of database environments. However, some additional steps may be required during installation and configuration. In this article, we introduce how to install and configure MySQL in a Docker container, providing you with a simple and effective way to deploy a MySQL database.
The above is the detailed content of How to configure docker after installing mysql. For more information, please follow other related articles on the PHP Chinese website!