Home > Article > Backend Development > Solution to PHP Warning: mysqli_query(): (HY000/1045): Access denied for user
When using PHP to connect to the MySQL database, you often encounter the error message "PHP Warning: mysqli_query(): (HY000/1045): Access denied for user".
This error is usually caused by incorrect database username or password. If it is not handled in time, PHP will be unable to connect to the MySQL database and cannot perform database operations.
Here are some common ways to solve this problem:
Check that the username and password are correct in the php code The username and password are the same as in the MySQL database, including correct case. If it is a local connection, you can try to connect using the root account or other accounts with database access rights to check whether they can connect successfully.
If the MySQL server is not running, you will not be able to connect to it. Run the "service mysql status" or "systemctl status mysqld.service" command on the terminal, or use "mysqli_connect_errno()" in the PHP code to check whether there is an error in the connection. If the MySQL service does not start, you need to solve the problem by starting the MySQL server. question.
If the MySQL user does not obtain permission to use the database, database operations cannot be performed. Run the following command to authorize the mysql user to use the database: GRANT ALL ON . TO 'mysqluser'@'localhost' IDENTIFIED BY 'password';
Check whether your firewall settings allow communication with the MySQL server. Normally, the MySQL server uses port 3306 for connections by default, so you need to make sure that this port is allowed through the firewall.
The MySQL server log may provide detailed information about the connection failure. Find the error log file in the MySQL installation directory and try to find records about connection failures.
Finally, once the "PHP Warning: mysqli_query(): (HY000/1045): Access denied for user" error message appears, we need to investigate various possible causes one by one according to the specific situation, and after troubleshooting Retry connecting to the MySQL database to ensure that PHP can connect smoothly and perform database operations.
The above is the detailed content of Solution to PHP Warning: mysqli_query(): (HY000/1045): Access denied for user. For more information, please follow other related articles on the PHP Chinese website!