mysql_connect() always denies access

kingkong picture kingkong · Apr 11, 2012 · Viewed 10.8k times · Source

I have phpMyAdmin running with MAMP, and I am finding it impossible to use mysql_connect().

$_db_connect = mysql_connect("root", "localhost");

Produces an error in php_error.log: mysql_connect() [<a href='function.mysql-connect'>function.mysql-connect</a>]: Access denied for user 'root'@'localhost' (using password: NO) in /....../common.lib.php on line 19

I've checked out the privileges:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*81F5E21E35407D884A6CD4A731AEBFB6AF209E1B' WITH GRANT OPTION

GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION

and I am simply stumped. I've created new users with passwords just to get the same result. Strangely it always says (using password: NO), even when I attempt to connect as a user witha password:

$_db_connect = mysql_connect(MYSQL_HOST, MYSQL_USERNAME, MYSQL_PASSWORD);

Any advice is welcomed. Thanks!

Answer

hakre picture hakre · Apr 11, 2012

That error message for dummies:

Access denied for user 'root'@'localhost' (using password: NO)

It has three parts, each of it contains important information. Let's see:

  1. Access denied - In plain words, this means: Get lost! Don't try to connect to me.
  2. for user 'root'@'localhost' - That says who is denied access, that user. The name: root and at (@) the following server: localhost.
  3. using password: NO - Just some nice additional information that when trying to access and giving the user-name, no password was used.

So now on with the privileges you have:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost'
 IDENTIFIED BY PASSWORD '*81F5E21E35407D884A6CD4A731AEBFB6AF209E1B'
 WITH GRANT OPTION
GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION

It basically says, that the user 'root'@'localhost' needs to use a password to get access. As the error message tells you, you are not using a password. So that is likely to be the cause of the error, the password is missing.

Add the password and try again.