Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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)
share|improve this question

migrated from Feb 7 '12 at 19:14

This question came from our site for professional and enthusiast programmers.

up vote 5 down vote accepted

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:

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.

share|improve this answer
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 '12 at 19:59
@Xint0 it works now after adding socket under [client]. Many thanks! – user173457 Feb 4 '12 at 0:01

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.