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.

| improve this question | | | | |
  • Either change the path of socket file in my.cnf file or you may use --host option while connecting. We can connect MySQL using with UDP which is socket or TCP which involves providing either --host=localhost or IP or you can also use --protocol=TCP option. – Nawaz Sohail Feb 7 '19 at 3:11
  • 1
    UDP -> Unix Socket. You could create a ~/.my.cnf with a [client]\nsocket=/run/mysqld/mysqld.sock – danblack Feb 7 '19 at 3:29
  • 1
    Or you can list all the current listening sockets (network and FS) by ss -l | grep mysql on the most linux systems. – Kondybas Feb 7 '19 at 7:16

First you need to check your MySQL config file my.cnf and make sure it contains socket parameter



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

| improve this answer | | | | |
  • to be precise I used danblack's comment from above but I really appreciate the help! – nak3c Feb 8 '19 at 0:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.