问题描述:

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

Yes, this error is all too common, and I have spent much time researching it. However nothing solved the problem.

I get this error when attempting the first connect in one of my PHP scripts I'm developing. If it helps to know, I'm running Linux Mint with the LAMP configuration detailed at http://community.linuxmint.com/tutorial/view/486 installed.

The thing is, any other application works with connecting. Both logging into PhpMyAdmin as root or mysql client on the console as root work perfectly. The MySQL site itself says that it's likely a problem with my code, but I have yet to figure that out... I am kinda new to this stuff so please forgive me if it's a very nooby mistake.

I'm using an external function on a different page (imported with require_once). Is this a bad practice? Regardless, here it is:

global $sql_conn, $sql_addr, $sql_user, $sql_pass, $sql_datb;

$config=app_readconfig();

echo "<p>".$sql_addr." ".$sql_user." ".$sql_pass." ".$sql_datb."</p>";//DEBUG, shows values are read

if($config==FALSE)

{

echo "<p>Error: Missing values in darkace_config.txt!</p>";

return FALSE;

}

$sql_conn=mysql_connect($sql_addr,$sql_user,$sql_pass);

if(!$sql_conn)

{

echo "<p>MYSQL Error: ".mysql_error()."</p>";

return FALSE;

}

if(!mysql_select_db($sql_datb,$sql_conn))

{

echo "<p>MYSQL Error selecting database: ".mysql_error()."</p>";

return FALSE;

}

return TRUE;

That function is called, and the message I'm returned is:

MYSQL Error: Access denied for user 'root '@'localhost' (using password: YES)

网友答案:

You have 'root[SPACE]' as the user name. Try 'root' without the space?

网友答案:

Something is wrong with your credentials.

Verify the:

  • Username
  • Password
  • Host

Also, please stop using mysql_* functions as they are now deprecated. Use MySQLi or PDO instead.

网友答案:

Your connection should be like this

$link = mysql_connect('127.0.0.1:3307', 'mysql_user', 'mysql_password');

do not include http, add the port number to like in the example, check user name and password.

If will annoy you but each time you use a mysql_* function you'll get a PDO recommendation. I also recommend PDO, PHP also recomments PDO, you can check PHP Documentation for mysql_connect

相关阅读:
Top