7

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.

asked Oct 19, 2016 at 13:47
2
  • please provide result of ps -ef | grep mysql Commented Oct 19, 2016 at 14:34
  • I assure you, it works. :) mysql 15 1 0 14:54 ? 00:00:00 /usr/sbin/mysqld Commented Oct 19, 2016 at 14:56

2 Answers 2

13

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
answered Oct 19, 2016 at 15:21
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
    
answered Jul 23, 2019 at 6:58

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.