tags:

views:

46

answers:

2

Hi,

I need to connect to a remote MySQL database and have created the following connection code:

<?php
/* Set Variables */
$host="myipaddress";
$db="mydbname"; 
$username="dbuser";
$pass="mypass";

/* 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();

}
?>

For security reasons, I've not provided the actual variable values. When I run this on my development system, I receive

Connect Error (2003) Can't connect to MySQL server on 'myipaddress' (10061)

My PHP is a bit rusty, can someone identify where my code is faulty? Note that dbuser has select, insert and update privileges on the database name set as the variable.

Thanks, Sid

Edit I made changes to my.cnf and restarted mysql. I now receive access denied for user 'dbuser'@'mycurrenthostname' (using password YES). When I use mysql -u dbuser -p from command line, I can login. I granted insert, update and select to dbuser with host '%' so that dbuser could connect from anywhere.

I've read the MySQL Reference guide about this error, but am still stuck. Is there a problem with my code, now that my.cnf has been fixed?

+1  A: 

According to the MySql documentation , The error (2003) Can't connect to MySQL server on 'server' (10061) indicates that the network connection has been refused. You should check that there is a MySQL server running, that it has network connections enabled, and that the network port you specified is the one configured on the server.

I'm not familiar with php , but the problem might not be in your code.

amal
This error also can appears on Windows if you don't have privileges to create or access to the innoDb data files.
Dubas
+1  A: 

Check that your firewall is allowing connections through on port 3306.

Check the MySQL configuration parameter bind-address in my.cnf to ensure that it is allowing remote connections.

There's information and troubleshooting tips here:

http://dev.mysql.com/doc/refman/5.1/en/can-not-connect-to-server.html

Mike
@Mike - Thanks for the link. mysqld is running, but cannot execute the first few mysqladmin commands in that article. Both mysqladmin variables and mysqladmin version return Access denied for user root@localhost using password NO. Where would the my.cnf file be located and how should I modify bind-address ? Thanks:)
SidC