Whey trying to connect to MySQL server on linux Operating system you might encounter an error Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2). There are many ways to resolve this error and this post will cover those. Before moving with any of the below resolutions first check if the MySQL service is running. If it is not then run it with command
service mysqld start.
Method 1: Giving permissions to socket folder
Default socket file for MySQL server
mysql.sockresides in the folder
/var/lib/mysqlfolder. If this folder has no read permissions, then above error will arise. Give all permissions to this folder using the below command
sudo chmod -R 777 /var/lib/mysql/
and chances are that this error will be resolved. Start you mysql service and it will restart successfully.
mysql.sockfile at the above location, check your
my.cnffile(usually located in
/etc/mysqlfolder) to have a property with name socket. The value of this property will indicate the location of
Method 2: Edit /etc/my.cnf File
/etc/mysql/my.cnffile exists. Open it and add the below lines to this file.
Restart MySQL service and try connecting again or check MySQL server’s version using
mysql -version command.
Method 3: Check disk space
MySQL writes some information to the disk and if there is no enough space in the /var folder, it will not be able to write leading to Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2) error.
Free up space and this error should resolve.
Method 4: Restarting MySQL service or Host machine
Sometimes it may happen that a process is blocking the access of MySQL server to some files. Restarting the system using command
shutdown -r nowmight release the lock on those files and MySQL starts working again.
Another common resolution is restarting the MySQL service only. Execute the command
service mysqld startor
service mysql startdepending on your installation to start the service. Restart the system only when restarting service doesn’t work.
Method 5: Linking to .sock file
Probably you have some other .sock file which MySQL server is referring to. Find such file by using command
sudo find /-type sor
find /var/ -name mysql.sockcommands and then create a link to this file with the file at location
If that doesn’t work either then create its link with tmp directory using command
Method 6: Terminating mysql process
Find if a mysql process is running using command
ps -A | grep mysql. If it shows a running process then terminate it using command
sudo kill mysql
Similarly search for
mysqld process using command
ps -A | grep mysqld and kill it using command
sudo kill mysqld
Start the service using service
mysql start and confirm it is running by using command
There are strong chances that your problem will be solved using the methods defined above. Thanks for reading !!!