The PHP MySQL mysql_connect() can't connect to local MySQL server through socket error

PHP MySQL socket error FAQ: Help, I'm getting a MySQL error that says, "Warning: mysql_connect(): Can't connect to local MySQL server through socket '/home/lib/mysql/mysql.sock'". I get this error message when trying to connect to my MySQL database from a PHP application. What can I do to fix this?

Back to top

My problem

I just ran into this problem on a new server I'm using with A2 Hosting, where the exact error I got in my PHP MySQL program is this:

Warning: mysql_connect(): Can't connect to local MySQL server through socket '/home/lib/mysql/mysql.sock' (2) in /home/al/apps/app1.php on line 162
Could not connect: Can't connect to local MySQL server through socket '/home/lib/mysql/mysql.sock'
Back to top

My PHP MySQL database properties

I was trying to connect to my database using the host string 'localhost', just like I do with the MySQL command line tool, but I still got this socket error. My PHP MySQL database connection code initially looked like this:

$username = 'myusername';
$password = 'mypassword';
$host     = 'localhost';
$database = 'mydatabase';

# connect to the database or die
$link = mysql_connect($host, $username, $password);
if (!$link) {
  die('Could not connect: ' . mysql_error());
}
mysql_select_db ($database);
Back to top

MySQL socket error - Solution #1

The short story is that there are at least two possible solutions to this MySQL socket error problem. First, you can change your host string to "127.0.0.1", like this:

$host = '127.0.0.1';

By using an IP address like this instead of the string "localhost", MySQL automatically won't try to access a socket file.

If you're not familiar with this approach, the term "localhost" and the IP address 127.0.0.1 are synonymous. Just ping localhost or this IP address from the command line, and you'll see that they are the same.

Back to top

MySQL mysql_connect socket error - Solution #2

The second possible solution is to change your host to specify the MySQL socket file location, like this:

$host = 'localhost:/tmp/mysql.sock';

I just tested both of these MySQL socket error solutions, and they both work on my Linux system.

This second solution raises the question, "What is my MySQL socket file location?"

Back to top

Finding your MySQL socket file location

The way I determined my MySQL socket file location was to log into my MySQL database server from the mysql command prompt, and then issue the show variables command, like this:

$prompt> mysql -u myusername -p
(enter the password here)

mysql> show variables like '%socket%';

+---------------+-----------------+
| Variable_name | Value           |
+---------------+-----------------+
| socket        | /tmp/mysql.sock |
+---------------+-----------------+
1 row in set (0.00 sec)

Once you know this socket location, you can use the second solution shown above.

If you're not familiar with the MySQL show command, I've written about it before in these MySQL tutorials:

Back to top

PHP MySQL socket error - Summary

I hope this PHP MySQL socket error information has been helpful.

Back to top

Comments

Permalink

Thank you very much for the tips. I've just started working with Laravel and this helped fix my issue. Again thank you!