Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

[mysqld]
#--default datadir
#datadir=/var/lib/mysql
#--new datadir
datadir=/data/lib/mysql

#--default socket
#socket=/var/lib/mysql/mysql.sock
#--new socket
socket=/data/lib/mysql/mysql.sock

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

# slow log
log-slow-queries=/var/log/mysqld-slow.log
long-query-time=1


[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

[client]
#--default socket
#socket=/var/lib/mysql/mysql.sock
#--new socket
socket=/data/lib/mysql/mysql.sock

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/php.in . Happily solved, thanks though! –  ashiina Mar 15 '13 at 10:51

2 Answers 2

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.

pdo_mysql

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

mysql

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

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

mysqli

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 http://php.net/manual/en/function.mysql-connect.php

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

 
discard

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.