I have had a problem with my WordPress site, the connection with MySql has gone away, and to get it back I had to restart the server. This has been an annoying issue as I do not have that high traffic on the site.
I tried to check if MySQL is up and make the cronjob restart it but that does not work that well. I have been looking at the log, using.
tail /var/log/mysql/error.log
In the log, I looked for shutdown messages or errors. I identified that the MySQL connection crashed when the server run out of memory, so why did it run out of memory. I started to optimize Mysql resources by using the optimization tool from Percona tools.
I also checked when my WordPress is running smoothly or what is causing heavy traffic peaks to your DB. One way is to increase the size of the server, I did not have that much traffic, so I installed the New Relic tool on my server to monitor the server resources to find out which processes are consuming memory.
I found out that the server did not have enough memory and MySQL can’t allocate what it needs so it crashes, what I did was add swap space to help this.
here is the method to Add Swap Space
https://www.digitalocean.com/community/tutorials/how-to-add-swap-space-on-ubuntu-16-04
If that's not enough, you may need to consider upgrading to a larger server. Either way, you should take a closer look at your memory consumption to better understand the problem.