PHP - Connection failed: Access denied for user 'username'@'localhost' (using password: YES)

49,908

Solution 1

Some thing wrong with username or password

If you are using windows by default username is "root" and password is "" (empty),

Solution 2

1 of 2 things is probably happening

  1. Your password is incorrect
  2. username is not authorized to access that database

What do you get when you execute execute following query

FLUSH PRIVILEGES; SHOW GRANTS;

You can probably find your problem here.

Update July 4,2016

I suspect that the user's password was stored incorrectly. Try resetting the password (you may need to be connected as the root user for this to work):

ALTER USER 'username'@'localhost'
    -> IDENTIFIED BY 'cleartext_password';

If that doesn't work you could try the older syntax:

SET PASSWORD FOR 'username'@'localhost' = PASSWORD('cleartext_password');

Replace cleartext_password with the password you wish to use. Then try to connect again.

Does it work?

still the same problem :'(

Ok, let's start with a fresh new user. Execute the 3 commands below as the root user.

CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'cleartext_password';
GRANT ALL PRIVILEGES ON * . * TO 'newuser'@'localhost';
FLUSH PRIVILEGES;

If all 3 commands executed properly, try next to connect to your database as newuser.

Did it work? If not, what error do you see?

Share:
49,908
SumIt Sahu
Author by

SumIt Sahu

Updated on July 09, 2022

Comments

  • SumIt Sahu
    SumIt Sahu almost 2 years

    I am facing this error. tried all solutions given in other threads...creating a new user@localhost and grant privileges to it. But nothing works for me...

    still getting this error..

    Connection failed: Access denied for user 'username'@'localhost' (using password: YES)

    here is my code..

    <?php
    $servername = "localhost";
    $username = "username";
    $password = "password";
    $dbname = "databasename";
    
    // Create connection
    $conn = mysqli_connect($servername, $username, $password, $dbname);
    // Check connection
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }
    else {
        echo "Connection Successful";
    }
    mysqli_close($conn);
    ?>