0

I am trying to connect to my db using php following is my connection string.

$con = mysqli_connect("localhost","almawakeb_khawaneej","******");
mysqli_select_db($con, "moodle_amsk_2018");
if (mysqli_connect_errno())
{
die('Could not connect: ' . mysqli_connect_error(). mysqli_connect_errno().'');
}
else {
die('Connection success');
}

But what I am getting is

Could not connect: Access denied for user 'almawakeb_khawaneej'@'localhost' (using password: YES)1045

When I checked privileges for that user

mysql> SHOW GRANTS FOR 'almawakeb_khawaneej'@'localhost';
+-----------------------------------------------------------------------------------+
| Grants for almawakeb_khawaneej@localhost                                          |
+-----------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'almawakeb_khawaneej'@'localhost'                  |
| GRANT ALL PRIVILEGES ON `moodle_amsk_2018`.* TO 'almawakeb_khawaneej'@'localhost' |
+-----------------------------------------------------------------------------------+

It has all necessary privileges. Password is also correct because it is working for yii2 framework config file, that is in the same server.

mysql> select version();
+-------------------------+
| version()               |
+-------------------------+
| 5.7.23-0ubuntu0.16.04.1 |
+-------------------------+

What should be the issue ?


EDIT

Resolved ! Actual problem was $ in the password, you can't use string password in the connection string if it contains $. Declare password as a variable.

$password = '***$***'; 
Ebin Manuval
  • 1,045
  • 13
  • 29
  • Why doesn't the output show `IDENTIFIED BY PASSWORD '...'`? – Barmar Sep 18 '18 at 08:22
  • Can you please check this link https://stackoverflow.com/questions/8484722/access-denied-for-user-rootlocalhost-while-attempting-to-grant-privileges – Praveen Kumar Sep 18 '18 at 08:23
  • @Barmar all other users in the system don't have that `IDENTIFIED BY PASSWORD '...'` section even if those users don't have this problem – Ebin Manuval Sep 18 '18 at 08:33
  • Looks like they've taken that out of the output in 5.7. I'm using an older version here. – Barmar Sep 18 '18 at 08:36

1 Answers1

1

Sometimes... You need to use 127.0.0.1 instead of localhost as host.

MrG
  • 168
  • 1
  • 11
  • The error message says that the user is @localhost, so those grants should match it. – Barmar Sep 18 '18 at 08:31
  • Still the same issue – Ebin Manuval Sep 18 '18 at 08:35
  • Ah, yes... Please create the grants for 127.0.0.1 too... If my memory serves me right... 127.0.0.1 forces to use TCP, as localhost will use sockets to connect to MySQL. This is probably causing your issues – MrG Sep 18 '18 at 08:41