Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have changed my MySQL datadir and socket location in my /etc/my.cnf as follows:

#--default datadir
#--new datadir

#--default socket
#--new socket

# Disabling symbolic-links is recommended to prevent assorted security risks

# slow log


#--default socket
#--new socket

After writing my my.cnf as above, I restarted my mysqld with /etc/init.d/mysqld restart .

I am able to connect to my MySQL via my terminal using mysql -uroot -ppassword, and my data coorectly exists, so there is no problem there.

But when I try to connect to MySQL in a simple index.php as follows:

$link = mysql_connect('localhost', 'root', 'password', TRUE);
echo $link;

I get the error:

Warning: mysql_connect(): Can't connect to local MySQL server through socket
'/var/lib/mysql/mysql.sock' (2) in /var/ww/html/index.php on line 3

I can't figure out why my PHP is trying to look for /var/lib/mysql/mysql.sock , when I have obviously changed it to /data/lib/mysql/mysql.sock.

I am running a normal Apache v2.2.22 loading a normal php5_module .

I restarted my apache with both restart and graceful.

Does anyone know what's going on?

share|improve this question
Which php mysql extension are you using? mysql, mysqli or pdo_mysql? – Tonin Mar 15 '13 at 10:47
I am using the plain old mysql. And it turns out that I had to specify my socket either in mysql_connect() or in /etc/ . Happily solved, thanks though! – ashiina Mar 15 '13 at 10:51
up vote 1 down vote accepted

This is generally changed in your php.ini configuration file.

Depending on the php mysql extension you're using, the setting is a bit different. If you don't set those properties, your mysql extension will use the default that has been configured at compile time, which is more than probably /var/lib/mysql/mysql.sock

Once the property relative to the extension you're using is correctly set, there is no need to change any of your PHP code.


If you're using the pdo_mysql extension to connect to your MySQL server, the configuration to look for is:

pdo_mysql.default_socket = /data/lib/mysql/mysql.sock


If you're using the mysql extension, configure the following:

mysql.default_socket = /data/lib/mysql/mysql.sock


For the mysqliextension, look at:

mysqli.default_socket = /data/lib/mysql/mysql.sock
share|improve this answer

Been some time since I last messed with php but you can try

mysql_connect('localhost:/data/lib/mysql/mysql.sock', ........)

edit: check out

share|improve this answer
Thanks for the advice! This worked, but the real answer I wanted (which I just found out) was to set mysql.default_socket in /etc/php.ini . Your advice helped me find this problem, thank you! – ashiina Mar 15 '13 at 10:49
@ashiina You may also post your solution as an answer. Self-answer is encouraged, even rewarded, on SE. – jscott Mar 15 '13 at 10:54
@jscott was going to, but someone else has posted a clean answer so I marked his as the answer. Thanks for the tip! – ashiina Mar 15 '13 at 11:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.