Determine cause of "MySQL server has gone away"
I had a similar issue to this, and it turned out to be a connection time out. My program sets up a connection pool, and threads grab a connection from that pool when they need to make queries. Every morning when I would come in and try to run my program I would get the MySQL Server has gone away error. A lot of solutions I found said to increase the wait_timeout field in my.cnf, but I found in the C API you can enable automatic re-connection like this: 

// set up your connection

// enable automatic reconnect
bool reconnect = 1;
mysql_options(&amp;connection, MYSQL_OPT_RECONNECT, &amp;reconnect);


From the docs: 


  The MySQL client library can perform an automatic reconnection to the server if it finds that the connection is down when you attempt to send a statement to the server to be executed. If auto-reconnect is enabled, the library tries once to reconnect to the server and send the statement again. 


This gave me a hard time for a while as it was pretty hard to troubleshoot, so hopefully this is helpful to somebody else!

Source: C API Automatic Reconnection Control