mysql.sock missing: php connection works but cant connect from terminal When I try to run mysql from the command line I get the following:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)


interestingly the php scripts that connect to mysql in /var/www appear to be connecting just fine and when I stop MySQL these scripts can no longer connect as expected. However when I start mysql I continue to get the missing .sock error from the command line while my php scripts once again are connected to the database they are supposed to.

I have tried restarting mysql in various ways (restarting, starting then stopping, sudo pkill mysql) and still no luck. The only sock file on my system is /run/mysqld/mysqld.sock; but, without being sure that this is the cause of the command line error I dont want to haphazardly make symbolic links in my /tmp directory. 

How can I resolve the issue of connecting to mysql via the command line? some suggestions I have read recommend reinstalling MySQL which I don't want to do considering my php connection is working.
First you need to check your MySQL config file my.cnf and make sure it contains socket parameter

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

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


if those parameters not presented then you need to add them and start MySQL service.

after that you need to change MySQL folder permission sudo chmod -R 755 /var/lib/mysql/ and make sure it is owned by MySQL user.

finally start MySQL service.

Note: you need to make sure ports are opened for MySQL inside firewall