The server had shut down yesterday because electricity had cut off. After that, I've tried connect to Mysql but only got the 2002 error related with mysql.sock. Anyway, I've fixed the issue with this solution:
Can't connect to local MySQL server through socket '/var/mysql/mysql.sock' (38)
sudo chmod -R 755 /var/lib/mysql/
But my question is: Why? Why a shutdown caused this error? How prevent this from happening again?
Check the '/var/log/mysqld.log' first to see messages on file/directory permission
If the MySQL server terminated unexpectedly, the MySQL socket was not reset after the reboot
Due to power shutdown, the existing mysql process got terminated and socket file is not removed properly as this is not a graceful shutdown. The MySQL server no longer attempts to verify the existence of a MySQL socket when the socket is not being used by any processes as the block checking for socket existence should only matter if there's a PID attached to the other end of the socket. On unclean reboot, this socket will persist and thus cause the failure.
Solution : I guess the simplest thing to do is remove the socket check. If there's no mysqld pids, then the existence of the socket may not be a useful check