Fix "Access denied" error in MySQL
What is the "Access denied" error?
The "Access denied" error in MySQL occurs when you try to connect to the database or execute a command, but the user doesn't have the necessary permissions or the credentials are incorrect. This is one of the most common errors when working with MySQL.
Common causes of the error
- Incorrect password
- User doesn't exist or doesn't have permissions
- Connection attempt from a non-allowed host
- Insufficient privileges for the operation
Step-by-step solutions
1. Verify credentials
- Confirm that the username and password are correct.
- Check that there are no extra spaces or incorrectly typed special characters.
- Try connecting from the terminal to verify:
mysql -u user -p
2. Verify user exists
If you have root or administrator access, verify that the user exists:
mysql -u root -p
SELECT user, host FROM mysql.user WHERE user='username';
If the user doesn't exist, you'll need to create it or use a different user.
3. Verify user permissions
Check the user's permissions for the specific database:
SHOW GRANTS FOR 'user'@'localhost';
Or to see all permissions:
SELECT * FROM mysql.user WHERE user='username';
4. Grant necessary permissions
If the user exists but doesn't have permissions, grant them:
GRANT ALL PRIVILEGES ON database_name.* TO 'user'@'localhost';
FLUSH PRIVILEGES;
For specific permissions:
GRANT SELECT, INSERT, UPDATE, DELETE ON database_name.* TO 'user'@'localhost';
FLUSH PRIVILEGES;
5. Verify allowed host
MySQL may be configured to allow connections only from certain hosts. Check:
SELECT user, host FROM mysql.user WHERE user='username';
If you need to allow connections from any host (not recommended for production):
GRANT ALL PRIVILEGES ON database_name.* TO 'user'@'%';
FLUSH PRIVILEGES;
6. Reset root password
If you forgot the root password, you can reset it:
- Stop MySQL:
sudo systemctl stop mysql
- Start MySQL in safe mode:
sudo mysqld_safe --skip-grant-tables &
- Connect without password and change the password:
mysql -u root
USE mysql;
UPDATE user SET authentication_string=PASSWORD('new_password') WHERE User='root';
FLUSH PRIVILEGES;
EXIT;
- Restart MySQL normally.
7. Verify phpMyAdmin configuration
If the error occurs in phpMyAdmin:
- Verify that the
config.inc.phpfile has the correct credentials. - Check that the user has permissions to access phpMyAdmin.
- Clear browser cache and try again.
Specific common problems
Error: "Access denied for user 'root'@'localhost'"
- Try with
sudo mysqlon Linux. - Or reset the password as explained above.
Error: "Access denied for user '@'localhost'"
- The username is empty. Check the connection configuration.
Error connecting from web application
- Verify that the user has permissions to connect from the application's host.
- Check the connection configuration in your application.
Security tips
- Never use the root user for web applications.
- Create specific users with limited permissions.
- Use secure passwords and change them periodically.
- Regularly review user permissions.
Need help?
If you can't resolve the "Access denied" error, open a ticket from the billing.baires.host panel or contact us for support.
You can also reach us through our social media:
- Instagram: @baires_host
- Discord (active support): https://discord.gg/dzjauatAFN
- Linktree: https://linktr.ee/baires.host