Thesqld – cannot connect to socket after changing datadir location

MySQL

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
[mysqld]
datadir=/NEW_DIR/mysql
socket=/NEW_DIR/mysql/mysql.sock

# cat /etc/init.d/mysqld | grep datadir
get_mysql_option mysqld datadir "/NEW_DIR/mysql"
datadir="$result"
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)

Best Answer

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.

Related Question