1

I'm having a hard time getting my website connected to the mySQL database on host. However, when I was running my website on the PC by Apache it was connecting smoothly. The snippet I'm using to connect is:

<?php
    $conn_error = 'Could not connect';

    $mysql_host = 'localhost';
    $mysql_user = 'root';
    $mysql_pass = '';

    $mysql_db = 'firstdatabase';

    if(!mysql_connect($mysql_host,$mysql_user,$mysql_pass)|| !@mysql_select_db($mysql_db)){
        die($conn_error);
    }else{
        //echo 'Connected';
    }
?>

And it gives me the following error when I try to connect to mysql on the web host:

Warning: mysql_connect() [function.mysql-connect]: Can't connect to local MySQL server
through socket 'MySQL' (2) in /home/umudo/public_html/connectserver.inc.php on line 10

I searched through the web for finding a solution but couldn't find any exact example of this.

4
  • Did you check whether the username/password is correct? Commented Feb 25, 2012 at 12:17
  • 1
    I strongly doubt the user is root with no password on your host.. Commented Feb 25, 2012 at 12:17
  • Yes, the user root has no password because of that i'm returning it as null. Commented Feb 25, 2012 at 12:21
  • 1
    @DamienPirsy The question is tagged with php. There's a strong chance that the user root has no password for localhost and production. Commented Feb 25, 2012 at 12:47

2 Answers 2

3

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

Says:

On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file.

So you have to

  • use "127.0.0.1" instead of "localhost" to use tcpip instead of sockets, or
  • enable sockets in mysql server config (socket connections are probably disabled and that causes that error on linux)

Second method is better - using sockets for local connections is better than TCP/IP (better performance), but you may have no privileges to mysql configuration on that server.

Sign up to request clarification or add additional context in comments.

Comments

0

Try installing phpmyadmin or php5-mysql as in apt-get install phpmyadmin.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.