In-depth Analysis and Solutions for MySQL ERROR 1045 Access Denied

Nov 22, 2025 · Programming · 10 views · 7.8

Keywords: MySQL | ERROR 1045 | Password Reset | Privilege Management | Database Security

Abstract: This article provides a comprehensive analysis of MySQL ERROR 1045(28000) access denied issues, detailing the core solution of resetting root password through skip-grant-tables method, and extending to discuss supplementary approaches including user privilege verification and configuration file checks. Based on real-world cases, it offers complete operational procedures and code examples to help readers thoroughly resolve MySQL access permission problems.

Problem Background and Error Analysis

MySQL ERROR 1045(28000) is one of the most common access permission errors in database management. When users attempt to connect to MySQL database via command line or other clients, the system returns "Access denied for user 'root'@'localhost' (using password: YES)" error message. This error indicates authentication failure, but the specific causes may involve multiple levels.

From a technical architecture perspective, MySQL's permission verification system is based on encrypted password storage and privilege allocation mechanism in the mysql.user table. When a client initiates a connection request, the MySQL server verifies whether the combination of username, password, and host address matches system records. Any mismatch will trigger the 1045 error.

Core Solution: Password Reset Procedure

When confirmed that access denial is caused by forgotten or incorrect passwords, the most effective solution is to reset the root password by bypassing the permission tables. The core principle of this method is to temporarily bypass MySQL's permission verification system and directly modify user authentication information.

The complete operational procedure must strictly follow these steps:

  1. Stop MySQL Service: First, ensure the MySQL service is completely stopped to avoid data inconsistency or permission conflicts. On Linux systems, use the following command:
  2. systemctl stop mysql
    # Or using traditional service management
    service mysql stop
  3. Start MySQL in Skip-Grant-Tables Mode: This is the key step of the entire solution. Start the MySQL service using mysqld_safe command with --skip-grant-tables parameter:
  4. mysqld_safe --skip-grant-tables &

    This parameter instructs the MySQL server not to load permission tables during startup, allowing any user to connect without password authentication. The & symbol indicates running this process in the background.

  5. Establish Password-less Connection: In a new terminal window, connect to the MySQL server using mysql client. Since permission verification is skipped, no password is required at this stage:
  6. mysql -u root
  7. Execute Password Reset Operation: After successful connection, select the appropriate password update command based on MySQL version. This represents a key technical implementation difference:
  8. For MySQL 5.7 and earlier versions:

    UPDATE mysql.user SET Password=PASSWORD('new_password') WHERE User='root';

    For MySQL 5.7 and later versions:

    UPDATE mysql.user SET authentication_string=PASSWORD('new_password') WHERE User='root';

    Special attention should be paid to version differences. MySQL 5.7 introduced the authentication_string field to replace the traditional Password field, representing an important improvement in security architecture. The PASSWORD() function encrypts plaintext passwords to ensure secure storage in the database.

  9. Refresh Privilege Cache: After password modification, privilege refresh operation must be executed to make changes effective immediately:
  10. FLUSH PRIVILEGES;

    This command clears MySQL's privilege cache, forcing the server to reload permission tables. Without this step, password changes may not take effect immediately.

  11. Restart MySQL Service: Finally, restart the MySQL service normally to restore complete permission verification mechanism:
  12. systemctl restart mysql

Technical Principle Deep Analysis

The implementation mechanism of --skip-grant-tables parameter involves MySQL's permission management system architecture. Under normal circumstances, MySQL loads permission tables from mysql database (user, db, tables_priv, etc.) into memory during startup. When client connection requests arrive, the server queries these memory-based permission information for verification.

When started with --skip-grant-tables parameter, MySQL skips this loading process, and all connection requests are automatically authorized as root users with full privileges. This design was originally intended for emergency recovery scenarios but恰好 provides a technical approach for solving password遗忘 problems.

Regarding password encryption mechanism, the PASSWORD() function employs different hash algorithms in different MySQL versions. Early versions used SHA1 hashing, while newer versions may use more secure encryption methods. This evolution reflects continuous improvement in database security standards.

Extended Solutions and Best Practices

Beyond the core password reset method, other factors that may cause ERROR 1045 should be considered:

User Privilege Verification: Ensure the root user actually exists in mysql.user table and has appropriate privileges. Check using the following SQL statement:

SELECT User, Host, authentication_string FROM mysql.user WHERE User='root';

Connection Configuration Check: Verify relevant settings in MySQL configuration files (my.cnf or my.ini), particularly bind-address parameters and port settings. Incorrect network configurations may cause connection failures.

Complete Uninstallation and Reinstallation: As indicated by user feedback, incomplete uninstallation may leave residual configuration files affecting new installations. Complete uninstallation procedure should include:

# Stop service
systemctl stop mysql

# Remove software packages
yum remove mysql-server mysql-client

# Delete residual files and directories
rm -rf /var/lib/mysql
rm -rf /etc/my.cnf
rm -rf /etc/mysql

Security Considerations

When using --skip-grant-tables parameter, it's crucial to recognize that this poses serious security risks to the system. During this period, any user can access the database without password authentication. Therefore, it's recommended to:

Summary and Outlook

The solution for MySQL ERROR 1045 reflects the balanced design between security and availability in database management systems. By deeply understanding MySQL's permission verification mechanism, administrators can effectively handle various access permission issues. As MySQL technology continues to evolve, future versions may provide more secure password recovery mechanisms, but the current method based on --skip-grant-tables remains the standard solution for root password遗忘 problems.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.