Learn how to unlock MySQL root account when access denied for user root localhost account is locked error appears. Simple commands and solutions inside. Our MySQL Support Team is always here to help you.
Unlock MySQL Root Account When Access Denied for User Root Localhost Account is Locked
Seeing access denied for user root localhost account is locked can throw anyone off guard, especially when applications suddenly stop connecting to the database. This error usually means that the root account in MySQL has been marked as locked. Fortunately, you can bring it back without losing your data. Let’s go through the direct commands and methods that will help you unlock the account and get things working again.
An Overview
Why This Error Appears
This error occurs when the root account is locked due to strict security policies, repeated failed login attempts, or even after upgrades. When the account is locked, applications fail to connect and throw the following error:
[3118] Access denied for user 'username'@'localhost'. Account is locked.
Confirming Account Status
To be certain that the account is locked, log in with an alternative admin user and run:
mysql> SELECT user, host, account_locked FROM mysql.user where user = 'username';
+------------------+--------------------------+----------------+
| user | host | account_locked |
+------------------+--------------------------+----------------+
| username | localhost | Y |
+------------------+--------------------------+----------------+
4 rows in set (0.00 sec)
If the account_locked column shows Y, the account is indeed locked.
Unlocking the Account
Once confirmed, you can unlock the account with:
ALTER USER 'username'@'hostname' ACCOUNT UNLOCK;
- ‘username’: MySQL user’s name.
- ‘hostname’: Host or IP.
- ACCOUNT UNLOCK: Removes the lock.
For example, if the lock is only for localhost:
ALTER USER username@localhost ACCOUNT UNLOCK;
Now, since accounts can be locked on multiple hosts, it’s better to recheck with:
mysql> SELECT user, host, account_locked FROM mysql.user WHERE user = 'username';
+-------+---------------------------+----------------+
| user | host | account_locked |
+-------+---------------------------+----------------+
| username | 10.0.0.2 | Y |
| username | localhost | N |
| username | cloudlinux7.11-90-0-6.tld | Y |
+-------+---------------------------+----------------+
4 rows in set (0.00 sec)
Run the ALTER USER … ACCOUNT UNLOCK; command again for each locked host.
Password Reset Considerations
Sometimes unlocking is not enough, especially when the default authentication plugin causes trouble. MySQL 8+ uses caching_sha2_password by default, but many prefer mysql_native_password for local setups. You may need to reset the root password after changing the plugin.
Also, pay attention to password policy variables. You can check them with:
mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+--------+
| Variable_name | Value |
+--------------------------------------+--------+
| validate_password_dictionary_file | |
| validate_password_length | 8 |
| validate_password_mixed_case_count | 1 |
| validate_password_number_count | 1 |
| validate_password_policy | MEDIUM |
| validate_password_special_char_count | 1 |
+--------------------------------------+--------+
6 rows in set (0.01 sec)
By lowering the policy, for instance:
SET GLOBAL validate_password.policy=LOW;
you can reset passwords with fewer restrictions. Restart MySQL after making changes, then use mysqladmin to reset the root password.
For MacBook Pro and MAMP Users
If you are on a MacBook Pro using MAMP, take a backup of your htdocs folder, uninstall MAMP, and reinstall it. This often resolves cases where the root account remains locked even after attempting changes.
[If needed, Our team is available 24/7 for additional assistance.]
Conclusion
The error access denied for user root localhost account is locked is not the end of the road. By checking the account status, unlocking it for all hosts, adjusting password policies if needed, and resetting the password, you can restore access quickly. For Mac users, a clean reinstall of MAMP may be the final step. With these methods, your database access will be back without unnecessary downtime.
0 Comments