I have this weird problem after changing the MySQL datadir location from default "/var/lib/mysql" It was working yesterday, but today I get "Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'," a typical message if the daemon is not running and it's looking for the socket file in the old datadir location.

I changed the my.cnf file and mysqld file to point to the new location, NEW_DIR. The daemon runs, but I cannot open mysql.

Thanks in advance.

# cat /etc/my.cnf

# cat /etc/init.d/mysqld | grep datadir
get_mysql_option mysqld datadir "/NEW_DIR/mysql"
get_mysql_option mysqld socket "$datadir/mysql.sock"

# /etc/init.d/mysqld start
Starting MySQL:                                            [  OK  ]

# tail mysqld.log 
120203 11:12:20  mysqld started
120203 11:12:20  InnoDB: Started; log sequence number 0 43655
120203 11:12:20 [Note] /usr/libexec/mysqld: ready for connections.
Version: '5.0.77'  socket: '/NEW_DIR/mysql/mysql.sock'  port: 3306  Source distribution

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

1 Answer 1


The problem is the mysql client program configuration. You can specify the --socket=/NEW_DIR/mysql/mysql.sock command-line option or add it under the [mysql] section of your my.cnf file.

Please look at the documentation for specifying options through option files: http://dev.mysql.com/doc/refman/5.0/en/option-files.html

Since changing the socket path affects all the client programs, then it will make sense to update the global my.cnf and set the socket option under the [client] section.

  • Ok. Adding --socket=/NEW_DIR/mysql/mysql.sock does work, but I have added it under [mysql] section in my.cnf as shown in my original post (socket=/NEW_DIR/mysql/mysql.sock), but it still doesn't work.
    – user173457
    Feb 3, 2012 at 19:59
  • @Xint0 it works now after adding socket under [client]. Many thanks!
    – user173457
    Feb 4, 2012 at 0:01
  • [mysql] socket=/newdir/lib/mysql/mysql.sock worked just fine for me (thanks) we could previously start the server no issue but not the client (5.6.35), I dont have a [client] section
    – zzapper
    Feb 2, 2017 at 18:13

Your Answer

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