If you must use createConnection, you’ll need to handle error events and reconnect on PROTOCOL_CONNECTION_LOST, but pooling is simpler and safer.
you should use connection pooling with mysql2.create.Pool() instead of a single connection.
That way, if one connection closes, the pool opens a new one automatically.