Warning: mysqli_connect(): (HY000/1045): Access denied for user 'username'@'localhost' (using password: YES)

690,172

Solution 1

That combination of username, host, and password is not allowed to connect to the server. Verify the permission tables (reloading grants if required) on the server and that you're connecting to the correct server.

Solution 2

If youre running wamp update

define("DB_HOST", "localhost");

To your machines ip address (mine is 192.168.0.25);

define("DB_HOST", "192.168.0.25");

You can find it on window by typing ipconfig in your console or ifconfig on mac/linux

Solution 3

Make sure that your password doesn't have special characters and just keep a plain password (for ex: 12345), it will work. This is the strangest thing that I have ever seen. I spent about 2 hours to figure this out.

Note: 12345 mentioned below is your plain password

GRANT ALL PRIVILEGES ON dbname.* TO 'yourusername'@'%' IDENTIFIED BY '12345';

Solution 4

mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);

use DB_HOST instead of DB_SERVER

Solution 5

Look at your code You defined DB_HOST and querying DB_SERVER. DB_USER and DB_USERNAME Use this code

define("DB_SERVER", "localhost");
define("DB_USER", "root");
define("DB_PASSWORD", "");
define("DB_DATABASE", "databasename");

$connect = mysqli_connect(DB_SERVER , DB_USER, DB_PASSWORD, DB_DATABASE);
Share:
690,172
Admin
Author by

Admin

Updated on July 09, 2022

Comments

  • Admin
    Admin almost 2 years

    Warning: mysqli_connect(): (HY000/1045): Access denied for user 'username'@'localhost' (using password: YES) in C:\Users\xampp\htdocs\PHP_Login_Script\config.php on line 6

    Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, boolean given in C:\Users\xampp\htdocs\PHP_Login_Script\login.php on line 10

    Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, boolean given in C:\Users\xampp\htdocs\PHP_Login_Script\login.php on line 11

    Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in C:\Users\xampp\htdocs\PHP_Login_Script\login.php on line 15

    Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:\Users\xampp\htdocs\PHP_Login_Script\login.php on line 16

    Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in C:\Users\xampp\htdocs\PHP_Login_Script\login.php on line 19

    I'm getting this error above on localhost even if my config file is like this:

    <?php
    
        define("DB_HOST", "localhost");
        define("DB_USER", "root");
        define("DB_PASSWORD", "");
        define("DB_DATABASE", "databasename");
    
        $db = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
    
    ?>
    

    This used to work, but now it doesn't anymore. Is there any problem with this code or is it not working now? This is the tutorial I am working on right now.

  • Admin
    Admin over 9 years
    sir im only using local host and i have root as username and a blank password..i dont see anything wrong with it because before it is working is it obsolete?that way of coding that is why it is not working?using localhost is like that and root and blank password is always accepted
  • Ignacio Vazquez-Abrams
    Ignacio Vazquez-Abrams over 9 years
    Your server says otherwise.
  • Admin
    Admin over 9 years
    wait let me check again on how to connect to local host just to be sure il be back on you
  • vlex
    vlex over 8 years
    why? What difference does it make how one calls their vars or constants?
  • Jack
    Jack over 8 years
    In the question the user passed the wrong constant names.
  • Sathya
    Sathya almost 7 years
    Make sure that your password doesn't have special characters and just keep a plain password (for ex: 12345), it will work. This is the strangest thing that I have ever seen. I spent about 2 hours to figure this out.
  • Chiwda
    Chiwda about 6 years
    @Sathya is right. Don't use cpanel's method of generating a password - apparently some special characters don't work. It took me the same 2 hours - then I found the above comment...
  • Nosajimiki
    Nosajimiki about 6 years
    While this answer is the most correct, specifically do no use 12345 since it is one of the top 10 most used passwords in the world, but do keep your password alphanumeric. By adding just a few extra characters something like 'smJ4bJ39kHab29PpfjKq' can still provide equal or better entropy as those cPanel randomly generated passwords, while still excluding special characters.
  • Rakibul Haq
    Rakibul Haq over 5 years
    don't know why this happened for one server, similar password worked in the other server, good catch by the way!! it solved the problem.
  • thisisjaymehta
    thisisjaymehta over 3 years
    On WAMP, one also need to make sure that PHP is connecting to mySQL only. By default it will connect to MariaDB. It can be changed from WAMP tray icon by changing default SQL server to mySQL.
  • RyJ
    RyJ about 3 years
    I CANNOT believe that this worked, but it did and ended the madness I've been going through for the past 3+ hours! Alphanumeric passwords FTW!!
  • techmagister
    techmagister almost 3 years
    I was using MAMP on MacOs and replacing 'localhost' by '127.0.0.1' resolved the issue.
  • Admin
    Admin over 2 years
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
  • AndyNope
    AndyNope over 2 years
    In my case, I've selected "locally access only" on the server. Sp I did not have permission. Just set it to any Host and voila.
  • KarTo
    KarTo over 2 years
    Great, thanks Nacho, I saved those two hours.
  • moiamserru
    moiamserru over 2 years
    Had to do this on my local Mac in OSX too. Didnt work with localhost. Had to enter 127.0.0.1 instead.
  • Muhammad Asif Raza
    Muhammad Asif Raza almost 2 years
    Thank you so so much @Sathya and Chiwda I was pulling my hairs till I reached your comments