Home  >  Article  >  Backend Development  >  Solution to PHP Warning: mysqli_query(): (HY000/1045): Access denied for user

Solution to PHP Warning: mysqli_query(): (HY000/1045): Access denied for user

WBOY
WBOYOriginal
2023-06-23 10:10:031705browse

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:

  1. Check that the database username and password are correct

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.

  1. Check if the MySQL server is running

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.

  1. Check whether the mysql user is authorized to use

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';

  1. Check the firewall settings

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.

  1. Check the MySQL server log

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!

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