1000 FAQs, 500 tutorials and explanatory videos. Here, there are only solutions!
Error: "Invalid query: MySQL server has gone away"
This type of error often originates from keeping an open MySQL connection without submitting queries for a period of time, beyond which the connection is closed: http://dev.mysql.com/doc/refman/5.7/en/gone-away.html
The wait_timeout and interactive_timeout variables that control this logout are at 30 seconds: http://dev.mysql.com/doc/refman/5.0/en/communication-errors.html
Run a script that regularly pings or queries, test if the connection still exists, and restart it if necessary.
If you have a Cloud Server, take a look at this solution: https://faq.infomaniak.com/1813