Report this

What is the reason for this report?

Showing Error SQLSTATE[HY000] [1045] Access denied

Posted on June 10, 2021

I have gone through the tutorial and done the exact but my when I try to open in website it is showing Error!: SQLSTATE[HY000] [1045] Access denied for user ‘user’@‘localhost’ (using password: YES)



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi there @riyastp04,

Are you using the correct password for your connection string in your website configuration file?

Did you get any errors when you ran the CREATE USER query:

CREATE USER 'example_user'@'%' IDENTIFIED WITH mysql_native_password BY 'password';

Also, did you run the grant privileges query too:

GRANT ALL ON example_database.* TO 'example_user'@'%';

Regards, Bobby

Hey,

The error SQLSTATE[HY000] [1045] Access denied for user 'user'@'localhost' (using password: YES) indicates that the MySQL server is rejecting the credentials provided by your application. Here’s how to troubleshoot and resolve the issue:


Step 1: Verify Database Credentials

Check the database credentials used in your application configuration:

  1. Locate the configuration file for your application (e.g., .env for Laravel or config.php for many PHP applications).
  2. Ensure the following parameters are correct:
    • DB_HOST: Should typically be localhost or 127.0.0.1.
    • DB_PORT: Default is 3306 unless explicitly changed.
    • DB_DATABASE: The name of your database.
    • DB_USERNAME: The MySQL user.
    • DB_PASSWORD: The password for the MySQL user.

Example .env configuration for Laravel:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_username
DB_PASSWORD=your_password

Step 2: Test MySQL Credentials

Manually test the credentials from the terminal:

mysql -u your_username -p -h 127.0.0.1 your_database_name
  • Replace your_username, your_password, and your_database_name with your actual values.
  • If prompted for a password, enter it and check if you can log in.

Step 3: Grant Correct Privileges

If the credentials fail, grant the necessary privileges to the user in MySQL:

  1. Log in to MySQL as root or an admin user:
sudo mysql -u root -p
  1. Grant privileges to the user for the database:
GRANT ALL PRIVILEGES ON your_database_name.* TO 'your_username'@'localhost' IDENTIFIED BY 'your_password';
FLUSH PRIVILEGES;

Step 4: Restart Services

Ensure that MySQL and Apache services are running:

sudo systemctl restart mysql
sudo systemctl restart apache2

Step 5: Check MySQL Authentication Plugin

Sometimes, the MySQL user is configured to use an authentication plugin like auth_socket, which may prevent password-based login. Check the authentication method:

  1. Log in as root:
sudo mysql -u root -p
  1. Check the authentication plugin for the user:
SELECT user, host, plugin FROM mysql.user WHERE user = 'your_username';
  1. If the plugin is auth_socket, change it to mysql_native_password:
ALTER USER 'your_username'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password';
FLUSH PRIVILEGES;

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.