Home  >  Article  >  Operation and Maintenance  >  What should I do if docker cannot connect to mysql?

What should I do if docker cannot connect to mysql?

PHPz
PHPzOriginal
2023-04-18 09:48:125841browse

When using Docker to build a MySQL database, sometimes you will encounter the problem of being unable to connect to MySQL. This may be due to network issues, configuration issues, or other issues. In this article, we will discuss how to solve this problem.

1. Check the network connection

First, we need to ensure that the network connection between the Docker container and the local host is normal, which can be detected through the ping command. Open a terminal on the local host and execute the following command:

$ ping <docker容器IP>

Among them, the can be obtained using the docker inspect command.

If you can ping successfully, it means the network connection is normal; if you cannot ping successfully, you need to troubleshoot the network fault.

2. Check the configuration of the MySQL container

When the network connection is normal, we need to check the configuration file of the MySQL container. Execute the following command:

$ docker exec -it <container ID> /bin/bash
$ cd /etc/mysql
$ cat my.cnf

Among them, is the ID of the MySQL container.

Check whether the configuration in the my.cnf file is correct, especially the configuration for network settings.

3. Create a MySQL user and authorize it

If the configuration of the MySQL container is normal, you need to check whether the appropriate MySQL user has been created and authorized.

In the MySQL container, execute the following command:

$ mysql -u root -p

Enter the root password to enter the MySQL console.

Execute the following sql statement to create a user:

CREATE USER 'username'@'%' IDENTIFIED BY 'password';

Among them, 'username' and 'password' are the username and password you want to create respectively.

Then, authorize the user:

GRANT ALL ON *.* TO 'username'@'%';

Where, 'username' is the username you just created.

Finally, execute the following command to save the changes and exit the MySQL console:

FLUSH PRIVILEGES;
EXIT;

4. Check the firewall settings

If you still cannot connect to MySQL, it may be due to a problem with the firewall settings. . We need to confirm whether there is a firewall blocking MySQL connections.

Execute the following command on the local host to confirm whether the port is occupied by the firewall:

$ sudo netstat -tunlp | grep <MySQL容器端口>

Among them, is the port number exposed by the MySQL service.

If the port is already occupied, you need to enable the port. Taking the Ubuntu system as an example, execute the following command to open the port:

$ sudo ufw allow <MySQL容器端口>/tcp

Among them, is the port number exposed by the MySQL service.

If it is another operating system, you need to open the port according to the corresponding operating system settings.

5. Restart the MySQL container

Finally, if the above four steps do not solve the problem, then we can try to restart the MySQL container.

You can use the following command to restart the MySQL container:

$ docker restart <container ID>

Among them, is the ID of the MySQL container.

Summary

The problem of being unable to connect to Docker MySQL may be caused by many reasons, and we need to eliminate them one by one. First confirm whether the network connection is normal, then check the configuration of the MySQL container, create a MySQL user and authorize it, check the firewall settings, and finally try to restart the MySQL container. If none of the above steps solve the problem, you can consider changing to another MySQL image or looking for other solutions.

The above is the detailed content of What should I do if docker cannot connect to 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