Solution to the error "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'"

When something fails on the mysql server, there is usually evidence of what happened, how, and at what time. Recently in this very blog, we had a 20-minute outage due to this error, which we promptly resolved. The issue triggered a 500 error that was hiding something deeper, essentially the following mysql error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)
Origin
The socket was unresponsive, making a connection via terminal impossible. By investigating the cause of this incident a bit more thoroughly, we were able to observe the cause. For this, all it took was to view the mysql log report with the following command on Linux systems.
tail -8 /var/log/mysql/error.log
This command shows the output of the latest errors that occurred on the MySQL server. In this particular case, it was the following:
2020-10... 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.19) starting as process 32002
2020-10... 0 [ERROR] [MY-012681] [InnoDB] mmap(137363456 bytes) failed; errno 12
2020-10... 1 [ERROR] [MY-012956] [InnoDB] Cannot allocate memory for the buffer pool
2020-10... 1 [ERROR] [MY-012930] [InnoDB] Plugin initialization aborted with error Generic error.
2020-10... 1 [ERROR] [MY-010334] [Server] Failed to initialize DD Storage Engine
2020-10... 0 [ERROR] [MY-010020] [Server] Data Dictionary initialization failed.
2020-10... 0 [ERROR] [MY-010119] [Server] Aborting
2020-10... 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.19) MySQL Community Server - GPL.
These two lines make it more than clear where the error originated from: Cannot allocate memory for the buffer pool, mmap(137363456 bytes) failed; errno 12. It turns out that the server ran out of memory to process requests to MySQL. This usually happens when the database has gradually increased with respect to a previous time or when there is a spike in requests on the server.
Solution
The immediate solution to this problem is quite simple. Just restart the MySQL server. On Debian-based systems, you can execute the following command:
sudo service mysql start
Or use the service manager.
systemctl start mysql
And all of this to restart the MySQL server? Yes, it is important to know what caused the errors that occurred. Keep in mind that if it happened once, it will probably happen again. That's why it's important to verify the traffic and memory management of the server. A good starting point might be the htop tool.

See you soon!.