Troubleshooting MySQL error 1045 (Access denied)
Jul 02, 2025 pm 03:25 PMMySQL error 1045 (Access denied) is usually caused by incorrect username, password, or improper permission settings. 1. First, confirm whether the entered user name and password are correct, pay attention to case sensitivity, and check whether there are any spelling errors in the configuration file; 2. Make sure that the user permissions allow remote or local connections, you can view them through SELECT User, Host FROM mysql.user, and create a new user that allows remote connections and authorize them if necessary; 3. Check the bind-address settings in the MySQL configuration file to ensure that its binding address is consistent with the access requirements, such as 0.0.0.0 allows external connections; 4. Troubleshoot other factors, including the operating system user authentication method, whether MySQL service is running, whether the firewall or security group releases port 3306, and check it step by step in order to solve the problem.
MySQL error 1045 (Access denied) is one of the most common connection problems and usually occurs when you try to connect to the database. This problem may be caused by a variety of reasons, but in most cases it is related to username, password or permission settings.

Check whether the username and password are correct
This is the most common point of error. If the username or password you entered is incorrect, MySQL will return error 1045.

- Confirm that the username and password you are using is consistent with the account created in MySQL.
- Pay attention to case sensitivity: Passwords are case sensitive under some systems.
- If you are reading information from a configuration file, check for any typos or extra spaces.
- You can temporarily print out the account password in
my.cnf
or connection script for confirmation (available in the development environment, use it with caution in production).
For example:
mysql -u root -p
If you prompt for access denied after entering your password, you can first confirm whether you entered the wrong password or whether you have switched users.

Whether user permissions allow remote connections (or local connections)
Sometimes, although you have entered the correct account password, you still cannot connect, which may be a problem with permission restrictions.
- By default, users of MySQL may only allow logging in from localhost.
- If you are connecting from a remote machine, you need to make sure that the user has permissions similar to
'user'@'%'
or'user'@'指定IP'
. - Use the following command to view user permissions:
SELECT User, Host FROM mysql.user;
If you see 'root'@'localhost'
, then this account cannot be connected from other machines.
Solution:
- Create a user that allows remote connections:
CREATE USER 'newuser'@'%' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON *.* TO 'newuser'@'%' WITH GRANT OPTION; FLUSH PRIVILEGES;
Note: Opening
'user'@'%'
will bring security risks, and it is recommended to limit the IP range according to actual needs.
Check if MySQL is bound to the correct address and port
Sometimes, even if the account is fine, the connection may fail due to network configuration.
- Check the MySQL configuration file (usually
/etc/my.cnf
or/etc/mysql/my.cnf
) and find thebind-address
settings. - If the bound
127.0.0.1
, it can only be accessed locally. - Modify to
0.0.0.0
to accept external connections (provided that the firewall is also released). - At the same time, confirm whether the port is open, the default is 3306.
Inspection method:
netstat -tuln | grep 3306
If you do not see the listening status, it means that the service has not been launched or the configuration is incorrect.
Other common situations
Sometimes the problem may be more hidden, such as:
Operating system user permission interference : Some distributions (such as Ubuntu) use the
auth_socket
plug-in to log in, which means that you do not need a password when connecting with-u root
, but rely on system user authentication.The solution is to use a normal user instead, or modify the login method of the root user:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password'; FLUSH PRIVILEGES;
MySQL service is not running : You can use
systemctl status mysql
orservice mysql status
to check the service status.Firewall/Security Group Limitation : Pay special attention to whether the security group rules have been released on the 3306 port on the cloud server.
Basically, these common investigation directions. Although it is not complicated, it is easy to ignore a certain detail, such as the bind address is misaligned or the permissions are not refreshed. When you encounter problems, you can do it step by step, first confirm the account password, then check the permissions and network settings, and you can basically do it.
The above is the detailed content of Troubleshooting MySQL error 1045 (Access denied). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Title: Unknowncolumn'column_name'in'fieldlist'-How to solve MySQL error: Unknown column in field list, specific code examples are needed. When using the MySQL database for query or operation, sometimes you will encounter such error message: "Unknowncolumn' column_name'in'fieldlist'", that is, an unknown column error exists in the field list. This is usually

To resolve the MySQL database initialization failure issue, follow these steps: Check permissions and make sure you are using a user with appropriate permissions. If the database already exists, delete it or choose a different name. If the table already exists, delete it or choose a different name. Check the SQL statement for syntax errors. Confirm that the MySQL server is running and connectable. Verify that you are using the correct port number. Check the MySQL log file or Error Code Finder for details of other errors.

How to deal with MySQL connection error 1049? MySQL is a commonly used relational database management system. Many developers and system administrators use MySQL to store and manage data. However, when using MySQL, sometimes you encounter the problem of connection error 1049. This article will introduce the causes of connection error 1049 and give several methods to solve this problem. MySQL connection error 1049 is usually caused by the database not existing or the database name being wrong. When connecting to the MySQL service

This article will explain in detail the numerical encoding of the error message returned by PHP in the previous Mysql operation. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. . Using PHP to return MySQL error information Numeric Encoding Introduction When processing mysql queries, you may encounter errors. In order to handle these errors effectively, it is crucial to understand the numerical encoding of error messages. This article will guide you to use php to obtain the numerical encoding of Mysql error messages. Method of obtaining the numerical encoding of error information 1. mysqli_errno() The mysqli_errno() function returns the most recent error number of the current MySQL connection. The syntax is as follows: $erro

How to deal with MySQL connection error 1017? MySQL is an open source relational database management system that is widely used in website development and data storage. However, when using MySQL, you may encounter a variety of errors. One of the common errors is connection error 1017 (MySQL error code 1017). Connection error 1017 indicates a database connection failure, usually caused by an incorrect username or password. When MySQL fails to authenticate using the provided username and password

MySQL is a very commonly used open source relational database management system. It has the characteristics of stability, efficiency, flexibility, etc., and is widely used in various types of applications. When using MySQL, you will inevitably encounter database recovery operations. How to accurately check the progress of MySQL database recovery has become a more important issue. The recovery progress of the MySQL database can be obtained by viewing the MySQL error log. In the MySQL error log, all database operation records will be recorded.

During development, if you often deal with MySQL, you will encounter some common errors. Some errors will cause MySQL to not work properly, and some will affect the performance of the program. Here are some common MySQL errors and their solutions. MySQL server cannot connect. When connecting to the MySQL server, you may encounter the following error: Can'tconnecttoMySQLserveron'localhost'(10061)Ac

Solution: 1. Check the data types to ensure that the data types in the columns you create or modify the index are compatible; 2. Avoid using functions or expressions on the columns where you create the index; 3. Use explicit type conversion. If you must use functions or expressions on indexed columns, make sure you use explicit type conversion. 4. Check the MySQL version and documentation to learn more about indexes and data types. 5. Optimize queries , consider re-evaluating and optimizing your queries to ensure effective use of indexes; 6. Consider using other tools or methods, etc.
