Mysql – How to define custom path to MySQL socket file

configurationMySQL

I have following config

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql-socket/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
innodb_buffer_pool_size=64M
innodb_log_file_size=16M

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

So, there is a custom path to the socket file /var/lib/mysql-socket/mysql.sock.

MySQL server works but I cannot connect to it.

$ mysql -uroot
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

but needed file is exists.

$ ls /var/lib/mysql-socket/mysql.sock
/var/lib/mysql-socket/mysql.sock

When path to the socket file is /var/lib/mysql/mysql.sock it works fine.

Best Answer

Your error message says it's connecting to the wrong socket:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

You can either specify that in command line:

mysql -S /var/lib/mysql-socket/mysql.sock ...

Or in the my.cnf file under the client section (this can also be in users home directory ~/.my.cnf):

[client]
socket=/var/lib/mysql-socket/mysql.sock
Related Question