Mysql ERROR 2002 (HY000): can't connect to localhost through socket

Dev
Dev
Sep 08 2021
.
1 min read
Sometime developer face Mysql ERROR 2002 (HY000), It's not normal error, It never occurs normally. Here I'm sharing my own experience and solution, how i came over it

I'm using hetzner.cloud, It's one of the top alternative for Digitalocean and cloud web hosting. Now a days, every cloud hosting provider provide snapshot feature to their user. Digitalocean snapshot feature is different from hetzner. Digitalocean limits their user to reinsate snaptshot to particular datacenter only. But in hetzner cloud you can reinsate your snaptshot with any available datacenter they have. that is really cool feature i found. Someday i have reinstated my snapshot. after accessing through terminal i faced this error.

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


Here bellow i'm sharing my terminal commands and output errors.

[email protected]:~# sudo mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
[email protected]:~# mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
[email protected]:~# mysql -u debian-sys-maint -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

[email protected]:~# sudo systemctl status mysql
mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Tue 2021-09-07 15:49:45 CEST; 24min ago
Process: 1020 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=1/FAILURE)
Sep 07 15:49:45 ubuntu systemd[1]: mysql.service: Service hold-off time over, scheduling restart.
Sep 07 15:49:45 ubuntu systemd[1]: mysql.service: Scheduled restart job, restart counter is at 5.
Sep 07 15:49:45 ubuntu systemd[1]: Stopped MySQL Community Server.
Sep 07 15:49:45 ubuntu systemd[1]: mysql.service: Start request repeated too quickly.
Sep 07 15:49:45 ubuntu systemd[1]: mysql.service: Failed with result 'exit-code'.
Sep 07 15:49:45 ubuntu systemd[1]: Failed to start MySQL Community Server.

[email protected]:~# sudo /etc/init.d/mysql stop
[ ok ] Stopping mysql (via systemctl): mysql.service.
[email protected]:~# sudo mysqld_safe
2021-09-07T14:16:38.238483Z mysqld_safe Logging to syslog.
2021-09-07T14:16:38.242249Z mysqld_safe Logging to '/var/log/mysql/error.log'.
2021-09-07T14:16:38.245129Z mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists.
[email protected]:~# mysql -u root
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
[email protected]:~# service mysql restart
Job for mysql.service failed because the control process exited with error code.
See "systemctl status mysql.service" and "journalctl -xe" for details.
[email protected]:~# mysql.server start
-bash: mysql.server: command not found

I have tried all best available possible methods to access my mysql through terminal and phpmyadmin. I'm also sharing list of solutions which i gone through but those did not worked for me. you can also try those if my method does not work for you.
  • https://www.digitalocean.com/community/questions/mysql-error-2002-cant-connect-to-localhost-through-socket
  • https://www.digitalocean.com/community/questions/mysql-can-t-connect-to-local-mysql-server-through-socket-var-run-mysqld-mysqld-sock-2
  • https://stackoverflow.com/questions/11657829/error-2002-hy000-cant-connect-to-local-mysql-server-through-socket-var-run

How to Solve - ERROR 2002 (HY000)


sudo apt-get update

I reinstalled every thing ( but before doing this make sure to take a backup your server and database )

sudo apt purge mysql-server mysql-server-5.7 mysql-server-core-5.7
sudo apt install mysql-server

When you use above command, system will ask you to Delete your existing database in YES or NO, If you say yes it will delete everything. If you say no then you can access your existing database after few mintues.


After Above Steps my problem is solved. No other step later.
but In case your system log shows an error like "mysqld: Can't read dir of '/etc/mysql/conf.d/'" create a symbolic link:

sudo ln -s /etc/mysql/mysql.conf.d /etc/mysql/conf.d
sudo service mysql start


Dev
Written By

Dev

The Dev

You may also like