5

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.

2
  • please provide result of ps -ef | grep mysql Oct 19 '16 at 14:34
  • I assure you, it works. :) mysql 15 1 0 14:54 ? 00:00:00 /usr/sbin/mysqld
    – Kirby
    Oct 19 '16 at 14:56
11

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
1
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

[mysqld]
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /mysql/data/mysql/mysql.sock
#datadir                = /var/lib/mysql
datadir         = /mysql/data/mysql
log-error       = /var/log/mysql/error.log
# By default we only accept connections from localhost
bind-address    = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
explicit_defaults_for_timestamp = 1
[client]
port=3306
socket=/mysql/data/mysql/mysql.sock

Note:

  1. Do not forget to add a [client] section, else you will get an error:

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

  2. Or you could pass a flag to the mysql command as below:

    mysql -u root -p --socket=/absolute/path/of/your/mysql.sock
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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