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)
<
Most likely mysql.sock
does not exist in /var/lib/mysql/
.
If you find the same file in another location then symlink it:
For ex: I have it in /data/mysql_datadir/mysql.sock
Switch user to mysql and execute as mentioned below:
su mysql
ln -s /data/mysql_datadir/mysql.sock /var/lib/mysql/mysql.sock
That solved my problem
In my case I have moved socket file to another location inside /etc/my.cnf
from /var/lib/mysql/mysql.sock
to /tmp/mysql.sock
Even after restarting the mysqld service, I still see the error message when I try to connect.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
The problem is with the way that the client is configured. Running diagnostics will actually show the correct socket path. eg ps aux | grep mysqld
Works:
mysql -uroot -p -h127.0.0.1
mysql -uroot -p --socket=/tmp/mysql.sock
Does not Work:
mysql -uroot -p
mysql -uroot -p -hlocalhost
You can fix this problem by adding the same socket line under [client]
section inside mysql config.
Make sure you have enough space left in /var
. If Mysql demon is not able to write additional info to the drive the mysql server won't start and it leads to the error Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
Consider using
expire_logs_days = 10
max_binlog_size = 100M
This will help you keep disk usage down.
This might be a stupid suggestion but make 100% sure your DB is still hosted at localhost. For example, if a Network Admin chose (or changed to) Amazon DB hosting, you will need that hostname instead!
My problem was that I installed mysql successfully and it worked fine.
But one day, the same error occurred.
Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
And no mysql.sock file existed.
This sollution solved my problem and mysql was up and running again:
Log in as root:
sudo su -
Run:
systemctl stop mysqld.service
systemctl start mysqld.service
systemctl enable mysqld.service
Test as root:
mysql -u root -p
mysql should now be up and running.
I hope this can help someone else as well.
Here's what worked for me:
ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
service mysqld restart