Connecting to remote MySQL server using PHP


I am attempting to connect to a remote MySQL server from my local machine virtualhost using the following code:

$conn = mysql_connect("$dbhost", "$dbuser", "$dbpass") or die(mysql_error());
        mysql_select_db($dbname, $conn) or die(mysql_error());

My problem is that I am unable to connect locally, receiving the error:

Can't connect to MySQL server on '' (10060)

This is not the case when I upload the same PHP file to the server. I am able to query the database with no problems at all.

I am unable to connect via command line either, but I can access cPanel which rules out the chance of my IP being banned accidentally.

My local server is running PHP 5.2.9, the remote server 5.2.12

5/14/2011 5:25:35 PM

Accepted Answer

  • firewall of the server must be set-up to enable incomming connections on port 3306
  • you must have a user in MySQL who is allowed to connect from % (any host) (see manual for details)

The current problem is the first one, but right after you resolve it you will likely get the second one.

12/20/2009 9:03:56 AM

It is very easy to connect remote MySQL Server Using PHP, what you have to do is:

  1. Create a MySQL User in remote server.

  2. Give Full privilege to the User.

  3. Connect to the Server using PHP Code (Sample Given Below)

$link = mysql_connect('your_my_sql_servername or IP Address', 'new_user_which_u_created', 'password');
if (!$link) {
    die('Could not connect: ' . mysql_error());

echo 'Connected successfully';

mysql_select_db('sandsbtob',$link) or die ("could not open db".mysql_error());
// we connect to localhost at port 3306

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow