1
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)

8
  • If you're developing a new application, I'd suggest using PDO or mysqli, mysql_* is out of date. Although that's probably not what the problem is. Are you sure the password is right. Commented Jun 20, 2012 at 3:36
  • Very sure, I'm using the same password as I am to connect via console or PhpMyAdmin. I'm new to PHP, Apache, and Linux, but I have worked a tiny bit with SQL before. However I wouldn't count it as experience. Commented Jun 20, 2012 at 3:37
  • well mysql is very suer your not using the right password. Commented Jun 20, 2012 at 3:39
  • Again, using the same password on PhpMyAdmin or terminal I can connect and work just well. Commented Jun 20, 2012 at 3:41
  • 1
    mysql is going to be right,here not you, so reset the password, to something simple then try it again. Commented Jun 20, 2012 at 3:44

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

I tried this back in the era of the question being asked. I tried removing a space, there was no space to remove in the config file. Back then, I gave up and decided to postpone the config file reading until I finished the app. I just now found out, there was a CRLF or something floating around. trim() fixed it, so I suppose this was the most accurate answer. Thank you.
1

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.

2 Comments

I've verified the credentials plenty of times...however I'll switch up the mysql_* functions and see how it works.
Use the credentials exactly as PHP has them. Don't type them in from memory, yourself.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.