Home >Operation and Maintenance >Docker >How to configure docker after installing mysql

How to configure docker after installing mysql

PHPz
PHPzOriginal
2023-04-10 14:14:121926browse

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

  1. Open the Terminal or Console and make sure Docker is correctly installed in the system.
  2. Run the following command to download the MySQL image from Docker Hub:
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

  1. Run the following command to 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.

  1. Check whether the container has been successfully created:
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

  1. Enter the MySQL container:
docker exec -it mysql-container-name bash

Among them, mysql-container-name is the name of the container.

  1. Connect to the MySQL server:
mysql -u root -p

Enter the root password you just set.

  1. Create a new user and grant permissions:
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.

  1. Modify the MySQL configuration file:
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.

  1. Restart MySQL:
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!

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