views:

66

answers:

2

Good Morning,

I wrote the code block below on my local Windows 7 PC and tried to run it. Unfortunately, I received:

Connect Error (1045) Access denied for user 'dbuser'@'myhost(using password: YES)

I have granted dbuser Insert, Select, Update, and Execute using both localhost and % for this database schema. I am able to mysql -u dbuser -p from command line on server as well.

Here's the code block:

<?php
/* Set Variables */
$host="serveripaddress";
$db="dbname"; 
$username="dbuser";
$pass="pass";

/* Attempt to connect */
$mysqli=new mysqli($host,$username,$pass,$db);
if (mysqli_connect_error()){
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
echo 'Success... ' . $mysqli->host_info . "\n";

$mysqli->close();

}
?>

I'm having difficulty understanding whether the above code block is causing my error, or whether there's something required to be done on the server. Can anyone suggest some areas of investigation?

Thanks, Sid

+1  A: 

Make sure that if you're using a hostname for the GRANT in MySQL, that MySQL can properly resolve that hostname to the IP you're connecting from.

For instance, if you do

GRANT blah ON *.* to user@somehost

you have to remember that MySQL won't see 'somehost', it'll see an IP address. It'll have to do a reverse lookup to get a hostname, and if the IP either doesn't have a reverse mapping, or maps to something completely different, MySQL won't give access.

Unless you can guarantee that the reverse mapping is stable, it's best to use IP addresses for remote access accounts in MySQL.

Marc B
@Marc: My grant statement was to 'dbuser'@'%' My intent was that dbuser would be a service account used by the web application to conduct SQL commands. As such, I didn't want to limit myself to any specific host names. Is this the correct approach?
SidC
That would leave the account open to ANY host that can reach the server, which could lead to a bruteforce compromise of the password. It's best to NEVER leave a database account, no matter how limited in privileges it is, so wide open.
Marc B
Thanks for your help. Turns out that we had a DNS issue with the server:( We should be golden in a few hours.
SidC
+1  A: 
<?php
/* Set Variables */
$host="127.0.0.1:3306";
$db="dbname"; 
$username="dbuser";
$pass="pass";

/* Attempt to connect */
$mysqli=new mysqli($host,$username,$pass,$db);
if (mysqli_connect_error()){
    die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());    
}
else
{
echo 'Success... ' . $mysqli->host_info . "\n";
$mysqli->close();
}

?>

First of all, add these braces in for your if/else statement. Second, try hardcoding the IP. I just ran this with an IP set to a variable (didnt work) and then I hardcoded it, worked just fine.

Elxx
@Elxx: Thanks for bringing the if/else braces to my attention. I made the change, and then tried using "my server ip address" instead of $host in the mysqli statement, and received the same error :(
SidC
maybe try to connect to the host without passing the $db parameter. Also check the database name. I know on my server, the names have a prefix attached to them. So it would be something like '44536_mydatabase' instead of just 'mydatabase'
Elxx