Connecting Mysql database located in different server in php

Discussion in 'PHP' started by anujphp, Nov 5, 2008.

  1. #1
    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
     
    anujphp, Nov 5, 2008 IP
  2. dev_SeeInside

    dev_SeeInside Peon

    Messages:
    45
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    some webhosts don't allow remote mySQL connections. You should check with your web host to see if they allow it.
     
    dev_SeeInside, Nov 5, 2008 IP
  3. Bind

    Bind Peon

    Messages:
    70
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    most decent webhosts allow you to add a host to accept incomming connections for the mysql database on your account.

    if you have cPanel version 3, click on REMOTE DATABASES in the Databases section, then configure accordingly.

    if you have cPanel version 2, click on MySQL Databases >> Remote Databases and its somewhere in there (look around)

    if you dont have cpanel jsut have a look around the databases section or contact your host for more information.

    however, if you have a lowball host, they might have it turned off or disabled to save on resources.
     
    Bind, Nov 5, 2008 IP
  4. shineDarkly

    shineDarkly Banned

    Messages:
    241
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    the error is a common error indicating a problem connecting to the db host, it could be possible that you are connecting to a remote db and not on the local machine
     
    shineDarkly, Nov 5, 2008 IP
  5. keiths

    keiths Peon

    Messages:
    109
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Is this a server you have root access to or is it hosted?
     
    keiths, Nov 5, 2008 IP