Umut
Umut

Reputation: 509

"Can't connect to local MySQL server through socket" on linux server

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.

Upvotes: 1

Views: 3114

Answers (2)

Zuko
Zuko

Reputation: 2914

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

Upvotes: 0

Kamil
Kamil

Reputation: 13931

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.

Upvotes: 3

Related Questions