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.sock resides in the folder /var/lib/mysql folder.
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.

Method 2:  Edit /etc/my.cnf File

If there is no mysql.sock file at the above location, check your my.cnffile(usually located in /etc/mysql folder) to have a property with name socket.
The value of this property will indicate the location of .sock file. Example, socket=/var/lib/mysql/mysql.sock

Check if /etc/mysql/my.cnf file exists. Open it and add the below lines to this file.

[mysqld]
socket=/var/lib/mysql/mysql.sock

[client]
socket=/var/lib/mysql/mysql.sock

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 now might 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 start or service mysql start depending 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 s or find /var/ -name mysql.sock commands and then create a link to this file with the file at location /var/lib/mysql.sock using command

ln /var/lib/mysql/mysql.sock

If that doesn’t work either then create its link with tmp directory using command

ln /tmp/mysql.sock

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 mysql -version.
There are strong chances that your problem will be solved using the methods defined above.