I am getting the following error when I try to connect to mysql:
Can\'t connect to local MySQL server through socket \'/var/lib/mysql/mysql.sock\' (2)
<
Just edit /etc/my.cnf
Add following lines to my.cnf
[mysqld]
socket=/var/lib/mysql/mysql.sock
[client]
socket=/var/lib/mysql/mysql.sock
Restart mysql and connect again
mysql -u user -p password database -h host;
Check if your mysqld service is running or not, if not run, start the service.
If your problem isn't solved, look for /etc/my.cnf
and modify as following, where you see a line starting with socket
. Take a backup of that file before doing this update.
socket=/var/lib/mysql/mysql.sock
Change to
socket=/opt/lampp/var/mysql/mysql.sock -u root
One way to reproduce this error: If you meant to connect to a foreign server but instead connect to the non existent local one:
eric@dev ~ $ mysql -u dev -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through
socket '/var/lib/mysql/mysql.sock' (2)
eric@dev ~ $
So you have to specify the host like this:
eric@dev ~ $ mysql --host=yourdb.yourserver.com -u dev -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 235
Server version: 5.6.19 MySQL Community Server (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+-------------------------+
| Database |
+-------------------------+
| information_schema |
| mysql |
| performance_schema |
+-------------------------+
3 rows in set (0.00 sec)
mysql> exit
Bye
eric@dev ~ $
Are you connecting to "localhost" or "127.0.0.1" ? I noticed that when you connect to "localhost" the socket connector is used, but when you connect to "127.0.0.1" the TCP/IP connector is used. You could try using "127.0.0.1" if the socket connector is not enabled/working.
Make sure you started the server:
mysql.server start
Then connect with root user:
mysql -uroot
If your mysql was previously working and has stopped suddenly just "reboot" the server.
Was facing this issue on my CentOS VPS.->
Was constantly getting
Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'(2)
Tried all techniques, finally restarting the server fixed the issues ->
shutdown -r now
Hope this helps !!