Home >Common Problem >How to solve the problem that mysql cannot connect to localhost
The solution to the problem that mysql cannot connect to localhost is: 1. Check whether MySQL is running; 2. Confirm the port used by MySQL; 3. Check whether MySQL accepts connections from the local host; 4. Reinstall and configure MySQL.
The operating system of this tutorial: Linux5.18.14 system, MySQL8. version, Dell G3 computer.
MySQL cannot connect to localhost (localhost), there may be several problems:
MySQL server is not running.
MySQL server is listening for requests from a different port.
MySQL server has refused the connection.
Here are some solutions you can try:
1. Check if MySQL is running
sudo service mysql status
2. Check which port MySQL is using
sudo lsof -i -P -n | grep mysqld
3. Check whether MySQL accepts connections from local host
In mysql During startup, perform the following two steps.
(1) Enter mysql
mysql -u root -p
(2) Modify the binding IP to be empty, that is, any IP can access the mysql instance
use mysql; update user set host='%' where user='root'; flush privileges;
4. If you still cannot connect , you may need to reinstall MySQL.
The above is the detailed content of How to solve the problem that mysql cannot connect to localhost. For more information, please follow other related articles on the PHP Chinese website!