Connecting Mysql database located in different server in php

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
anujphp
Forum Commoner
Posts: 25
Joined: Wed Nov 05, 2008 10:12 am

Connecting Mysql database located in different server in php

Post by anujphp »

Hello
I have a website where i have used php and mysql,till now both php and mysql were located in same server and my site was working fine but recently we have shifted Mysql to a different server.
In the php code we have changed the server name with the new server name.but when we try to open the page we are getting error

THE ERROR IS

Warning: mysql_connect() [function.mysql-connect]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /home/web/XXXXX/STONEDIVER_PHP_LIBRARY/source/shared/database.php on line 38
Could not connect: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)


The code before migration of Mysql was

$server = "localhost";
$dbname = "database";
$user = "username"; //Allowed for the ip
$password = "password";
$link = mysql_connect($server,$user,$password) or die(mysql_error());
mysql_select_db($dbname);


The code after migrating Mysql is

$server = "serverip/servername";
$dbname = "database";
$user = "username"; //Allowed for the ip
$password = "password";
$link = mysql_connect($server,$user,$password) or die(mysql_error());
mysql_select_db($dbname);

I have tried using both server IP and server name...

Please help me out in solving this problem...
THANKS IN ADVANCE
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Connecting Mysql database located in different server in php

Post by onion2k »

http://dev.mysql.com/doc/refman/5.0/en/ ... erver.html

Check the right port (3306) is open on the firewall if there is one.
anujphp
Forum Commoner
Posts: 25
Joined: Wed Nov 05, 2008 10:12 am

Re: Connecting Mysql database located in different server in php

Post by anujphp »

Thank you onion2k
port is open and there is no problem with firewall.
oddsmojo
Forum Newbie
Posts: 6
Joined: Thu Nov 06, 2008 9:23 am

Re: Connecting Mysql database located in different server in php

Post by oddsmojo »

Warning: mysql_connect() [function.mysql-connect]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /home/web/XXXXX/STONEDIVER_PHP_LIBRARY/source/shared/database.php on line 38
Could not connect: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
After you updated the $server with your new server, are you still getting that exact error? I'm curious why it is trying to connect to a local server if you have updated the $server to your new host.
anujphp
Forum Commoner
Posts: 25
Joined: Wed Nov 05, 2008 10:12 am

Re: Connecting Mysql database located in different server in php

Post by anujphp »

Thank you guys the link from onion2k was of great help i am able to solve my issue..thanks once again
Post Reply