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 a MySQL connection open 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 disconnection are at 30 seconds: http://dev.mysql.com/doc/refman/5.0/en/communication-errors.htm
Run a script that regularly pings or, before making a query, test if the connection still exists, and restart it if necessary.
If you have a cloud server, it’s possible to increase the MySQL timeout limits (wait_timeout and interactive_timeout) from your cloud’s MySQL menu