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

author

Dev

. 2 min read

Follow

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. 

root@ubuntu:~# sudo mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
root@ubuntu:~# mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
root@ubuntu:~# 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)

root@ubuntu:~# 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.

root@ubuntu:~# sudo /etc/init.d/mysql stop
[ ok ] Stopping mysql (via systemctl): mysql.service.
root@ubuntu:~# 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.
root@ubuntu:~# mysql -u root
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
root@ubuntu:~# 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.
root@ubuntu:~# 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. 

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