Home >Backend Development >PHP Tutorial >PHP Warning: mysqli_connect(): (HY000/2002): Solution to Connection refused
If you encounter the following error message when using PHP to connect to a MySQL database:
PHP Warning: mysqli_connect(): (HY000/2002): Connection refused
Then you can try the following steps to resolve this issue.
First you should check whether the MySQL service is running normally. If the service is not running or fails to start, it may cause a connection refused error. . You can check the status of the MySQL service by using the following command:
$ systemctl status mysql.service
If the MySQL service is not running, you can use the following command to start the service:
$ systemctl start mysql.service
If the MySQL service is already running, you need to check whether the MySQL service is listening on the correct port. By default, the MySQL service listening port is 3306. You can check the port bound by the MySQL service through the following command:
$ netstat -an | grep 3306
If the MySQL service is not bound to the correct port, you need to configure it in the MySQL configuration file ( my.cnf), add the following lines:
[mysqld]
port=3306
After modifying the configuration file, you need to restart the MySQL service for the modification to take effect:
$ systemctl restart mysql.service
If the MySQL service is running correctly and listening on the correct port, you need to check the user Are the name and password correct? When connecting to MySQL, you need to use the correct username and password to log in to the MySQL server. If you are not sure whether the username and password are correct, you can try to log in using this username and password in the MySQL command line interface:
$ mysql -u username -p
In this command, You need to replace "username" with the username you want to use, and then enter this user's password to log in to the MySQL server. If the login is successful, it means that the username and password you entered are correct.
The last step is to confirm whether the user connecting to MySQL has sufficient permissions to access the database that needs to be used. When connecting to the MySQL server, you need to specify the name of the database to connect to. If the specified database does not exist or you do not have permission to access the database, the connection will be refused.
You can check the permissions of the MySQL user through the following command:
$ mysql -u username -p -e "SHOW GRANTS FOR username;"
In this In the command, you need to replace "username" with the MySQL user whose permissions you want to query. After executing this command, you can see the permissions this user has.
If the database you want to access does not exist or does not have access permissions, you need to use the following command in the MySQL command line interface to create or grant access permissions:
CREATE DATABASE dbname;
GRANT ALL PRIVILEGES ON dbname.* TO 'username'@'localhost';
In this command, you need to replace "dbname" with the name of the database you want to create, and "username" with the database name you want to authorize MySQL username.
Summary
Through the above steps, you should be able to solve the PHP Warning: mysqli_connect(): (HY000/2002): Connection refused error. First, you need to check whether the MySQL service has been started and is listening on the correct port; second, you need to confirm whether the user name and password entered are correct; finally, you need to check whether the MySQL user has permission to access the database.
The above is the detailed content of PHP Warning: mysqli_connect(): (HY000/2002): Solution to Connection refused. For more information, please follow other related articles on the PHP Chinese website!