When I try to log into the mysql shell with:

mysql -u root

then I get:

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

How do I log onto the mysql shell?

I have been googling this left and right and trying all those proposed solutions on stackoverflow and elsewhere but nothing seems to work.

I have tried adding -p at the end. Same error.

I have tried going by this: https://stackoverflow.com/questions/11657829/error-2002-hy000-cant-connect-to-local-mysql-server-through-socket-var-run The first answer, I already have mysql-server installed. The second answer doesn't work. The third answer, i cannot implement step no. 3:

mysql -u root

and in the second step the process just hangs with message:

mysqld_safe Logging to syslog.
2017-09-26T16:30:40.563159Z mysqld_safe Logging to '/var/log/mysql/error.log'.
2017-09-26T16:30:40.566955Z mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists.

So the question is, how do I log onto the mysql shell?

  • What does ps -ef | grep mysqld give you? Specifically, I'm looking for the value of the --socket parameter of the running mysqld process Sep 26, 2017 at 16:42
  • mysql 1039 1 0 17:29 ? 00:00:00 /usr/sbin/mysqld
    – john-jones
    Sep 26, 2017 at 17:31

1 Answer 1


Please, review my.cnf files (see oficial documentation for file locations https://dev.mysql.com/doc/refman/5.7/en/option-files.html).

Search under the [client] section if exist an entry called socket, i.e:


Replace it with the right path to mysql.sock and try again. To get the mysql.sock, use ps -FU mysql -H | more and get the --socket value

  • there was no socket entry in .my.cnf. I typed in the ps command you sent and got: mysql 1039 1 0 312739 154556 0 17:29 ? 00:00:02 /usr/sbin/mysqld. So I added line: socket=/usr/sbin/mysqld under mysql in .my.cnf. I still cannot access mysql, its almost the same error: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/usr/sbin/mysqld' (13)
    – john-jones
    Sep 26, 2017 at 18:07
  • Could you tell us the Linux Distro/Version? i.e cat /etc/centos-release for CentOS 7. MySQL|Maria|Percona version? i.e yum info mysql-server on CentOS/RHEL Where did you find the my.cnf that have not socket? If the file start with a dot "." (as you typed in the answer) please remove it renamming the file mv .my.cnf my.cnf Could you give us the ls -lh /var/run/ output please? How did you install mysql?
    – Cepxio
    Sep 26, 2017 at 18:59
  • Its a linux mint 17.3. fully upgraded with update upgrade. its an old .my.cnf file i've been using in other systems. the ls command gives a full screen of output that is difficult to display here, are you looking for anything specific? I was following this to install mysql: digitalocean.com/community/tutorials/….
    – john-jones
    Sep 26, 2017 at 19:16
  • Maybe I should just uninstall mysql and install it again. If so, how would be best for me to go about doing that?
    – john-jones
    Sep 26, 2017 at 19:40
  • Ok, which MySQL version are you running? Also, I need to know if the /var/run/mysqld directory exists and it content. If MySQL was intalled with apt-get, so the directory must exist.
    – Cepxio
    Sep 26, 2017 at 21:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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