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

27

66

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

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?

Tuberculosis answered 7/8, 2014 at 4:31 Comment(0)
T
36

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.

Trailblazer answered 7/8, 2014 at 4:36 Comment(8)
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 acceptedTuberculosis
Your server says otherwise.Trailblazer
wait let me check again on how to connect to local host just to be sure il be back on youTuberculosis
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.Settlement
@Settlement 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...Reneareneau
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.Everard
Great, thanks Nacho, I saved those two hours.Hypothalamus
Thank you so so much @Settlement and Chiwda I was pulling my hairs till I reached your commentsTutt
S
16

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';
Settlement answered 16/7, 2017 at 10:23 Comment(3)
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.Ornithic
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.Cotten
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!!Manque
C
15

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

Cerebral answered 29/11, 2014 at 3:32 Comment(2)
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.Parados
Had to do this on my local Mac in OSX too. Didnt work with localhost. Had to enter 127.0.0.1 instead.Patrilineal
J
11

mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);

use DB_HOST instead of DB_SERVER

Jelene answered 21/9, 2014 at 19:39 Comment(2)
why? What difference does it make how one calls their vars or constants?Memorialize
In the question the user passed the wrong constant names.Allowedly
M
9

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);
Myrtismyrtle answered 17/12, 2015 at 12:39 Comment(0)
B
5

I have fixed a few things here, the "DB_HOST" defined here should be also DB_HOST down there, and the "DB_USER" is called "DB_USER" down there too, check the code always that those are the same.

<?php
    define("DB_HOST", "localhost");
    define("DB_USER", "root");
    define("DB_PASSWORD", "");
    define("DB_DATABASE", "");

    $db = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
?>
Bastille answered 5/4, 2019 at 9:16 Comment(0)
T
4

you defIne as DB_USER but use as DB_USERNAME. Also php says username@localhost cant access. Not root@localhost.

Change your define or connect paramater.

Trappings answered 7/8, 2014 at 4:50 Comment(0)
Q
2

This is your code:

<?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);

?>

The only error that causes this message is that:

  1. you're defining a DB_USER but you're calling after as DB_USERNAME.

Please be more careful next time.

It is better for an entry-level programmer that wants to start coding in PHP not to use what he or she does not know very well.

ONLY as advice, please try to use (for the first time) code more ubiquitous.

ex: do not use the define() statement, try to use variables declaration as $db_user = 'root';

Have a nice experience :)

Quantity answered 13/3, 2016 at 0:37 Comment(0)
R
2

The same issue faced me with XAMPP 7 and opencart Arabic 3. Replacing localhost by the actual machine name reslove the issue.

Republicanism answered 17/11, 2017 at 22:52 Comment(0)
B
2

Use this

After setup this you will not receive this issue again

SET PASSWORD FOR root@localhost = PASSWORD('your_root_password');

I was stuck up in this issue after used this query everything was solved

Bromo answered 23/9, 2019 at 13:11 Comment(0)
A
2

Try the above answers 90% of the time this will be you issue. For me my problem was that my database password had a $ in it.

$servername = '127.0.0.1';
$username = 'user_bob';
$password = 'sdfasdf$B';

$conn = mysqli_connect($servername, $username, $password, 'any_database');

//DO NOT USE DOUBLE QUOTES;
//$password = "sdfasdf$B"; 
Always answered 29/4, 2020 at 18:42 Comment(0)
H
1

Same problem occurd with me, with AWS RDS MySQL. Just now, answered similar question here. Looked various sources, but as of this thread, almost all lack of answer. While this thread helped me, tweak in mind to update hostnames at server. Access your SSH and follow the steps:

cd /etc/
sudo nano hosts

Now, Appending here your hostnames: For example:

127.0.0.1 localhost
127.0.0.1 subdomain.domain.com [if cname redirected to endpoints]
127.0.0.1 xxxxxxxx.xxxx.xxxxx.rds.amazonaws.com [Endpoints]

and now, configuring config/database.php as follows:

$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
    'dsn'   => '',
    'hostname' => '35.150.12.345',
    'username' => 'user-name-here',
    'password' => 'password-here',
    'database' => 'database-name-here',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => TRUE,
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

where 35.150.12.345 is your IPv4 Public IP, located at ec2-dashboard > Network Interfaces > Description : RDSNetworkInterface >> IPv4 Public IP('35.150.12.345') there.

Note: Please note that only IPV4 will work at 'hostname' option. hostname, cname, domains will output database connection error.

Huh answered 28/2, 2017 at 8:2 Comment(0)
G
1

The question has been answered above, Just to make you feel comfortable using all four string at the same time in a connection

<?php
  define("DB_HOST", "localhost");
  define("DB_USER", "root");
  define("DB_PASSWORD", "");
  define("DB_DATABASE", "databasename");

  $db = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
  // you could test connection eventually using a if and else conditional statement, 
  // feel free to take out the code below once you see Connected!
  if ($db) {
    echo "Connected!";
  } else {
    echo "Connection Failed";
  }
?>
Gnarly answered 16/7, 2017 at 6:27 Comment(0)
S
1

I had this error trying to connect with a different user than root I'd set up to limit access.

In mysql I'd set up user MofX with host % and it wouldn't connect.

When I changed the user's host to 127.0.0.1, as in 'MofX'@'127.0.0.1', it worked.

Sting answered 23/9, 2017 at 1:50 Comment(1)
I was using MAMP on MacOs and replacing 'localhost' by '127.0.0.1' resolved the issue.Robbierobbin
T
1

For somebody use XAMPP 8. Actually XAMPP install MySQL in VM, so connect it via IP address show in XAMPP. There is no MySQL server on localhost. enter image description here

Tamikatamiko answered 9/8, 2021 at 2:44 Comment(0)
T
0

In the privileges section of the database in phpmyadmin, make sure to use the privileged user and password. My default user is "root" and no password. I got this problem since i was searching in w3schools and copy pasted the whole script in and find out that's the problem

Tutorial answered 24/9, 2021 at 12:42 Comment(1)
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.Ultimatum
C
0

Here is what solved my problem:

Open config.inc.php

/* Authentication type and info */
$cfg['Servers'][$i]['auth_type'] = 'config';
$cfg['Servers'][$i]['user'] = 'root';
$cfg['Servers'][$i]['password'] = 'something';
$cfg['Servers'][$i]['extension'] = 'mysqli';
$cfg['Servers'][$i]['AllowNoPassword'] = true;
$cfg['Lang'] = '';

This is what part of the file looks like. I removed the password and then was able to log in to localhost/myphpadmin (the place that gave me this error). You might have the issue from somewhere else like trying to go to adminer.

I got this issue because I reinstalled and recopied files so things got messed up. I went an removed the password, accessed myphpadmin and set the password to the database from there and readded the files like above.

Catadromous answered 11/3, 2022 at 18:23 Comment(0)
S
0

Unfortunately above all answers won't work if you are working around MAMP setup with Wordpress

latest update

try this if you are using Default MAMP config: define( 'DB_HOST', 'localhost:8889' )

Spikenard answered 4/7 at 14:40 Comment(0)
K
-1
$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);

Put " DB_HOST " instead of " DB_SERVER " also 
put " DB_USER " Instead of " DB_USERNAME " 

than it working fine !

Kindergartner answered 7/8, 2014 at 4:31 Comment(0)
M
-1

Do you have two connections at the same time? If so, close every time you don't need it.

Marguritemargy answered 11/2, 2015 at 11:50 Comment(0)
S
-1

There is a typo error in define arguments, change DB_HOST into DB_SERVER and DB_USER into DB_USERNAME:

<?php

    define("DB_SERVER", "localhost");
    define("DB_USERNAME", "root");
    define("DB_PASSWORD", "");
    define("DB_DATABASE", "databasename");
    $db = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);

?>
Splenomegaly answered 29/11, 2016 at 10:27 Comment(0)
N
-1

In my case the problem was:

define ('DB_PASSWORD', "MyPassw0rd!'");

(the odd single ' before the double ")

Nildanile answered 26/9, 2018 at 13:54 Comment(0)
K
-1

Not that anyone would ever have CAPS on when entering a password...but it can give this error.

Kanarese answered 15/5, 2020 at 22:12 Comment(0)
R
-1

This can happen for a number of reasons. In my case, I just needed to change the port. You can do so by specifying it after a colon when defining DB_HOST.

define( 'DB_HOST', localhost:3308 )

Rodroda answered 1/9, 2021 at 15:50 Comment(0)
R
-1

For windows user open task manager, go to Details tab, find all 'mysqld.exe' file, right click and end task, then restart wamp server

enter image description here

Reek answered 23/4 at 4:38 Comment(0)
L
-2

try

define("DB_PASSWORD", null);

and those are warnings try

$db = @mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);

But i will recommend you to set a root password

Lowtension answered 7/8, 2014 at 4:51 Comment(0)
F
-2

Try changing:

define("DB_SERVER", "localhost");

to this:

define("DB_SERVER", "127.0.0.1");
Fulltime answered 7/3, 2016 at 6:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.