How do I resolve the error "MySQL server has gone away" when connecting to my Amazon RDS MySQL DB instance?

3 minute read
1

I tried to query an Amazon Relational Database Service (Amazon RDS) DB instance that is running MySQL, and I received one of the following error messages: "MySQL server has gone away" or "Lost connection to server during query."

Short description

If the server timed out and closed the connection, you might receive one of the following errors:

  • CR_SERVER_GONE_ERROR - The client couldn't send a question to the server.
  • CR_SERVER_LOST - The client didn't get an error when writing to the server, but the client didn't get a full answer (or any answer) to the question.

For more information, see the MySQL documentation for MySQL server has gone away.

Resolution

See the following causes for and related resolutions for these errors:

  • If a connection is idle for too long, then the connection might be terminated incorrectly from the client. To resolve this issue, verify that application timeouts are shorter than the MySQL timeouts, and be sure that your applications close idle connections.
  • If a connection times out, then increase the timeouts for MySQL by increasing the wait_timeout and interactive_timeout parameters by using a custom parameter group. For more information, see Working with DB parameter groups.
  • If the query that is generating the error is retrieving a large dataset, then increase the max_allowed_packet size parameter by using a custom parameter group. For more information, see Modifying parameters in a DB parameter group.
    Note: For Amazon Aurora, you can modify the parameters and set them in a cluster parameter group to apply at the entire cluster level. Setting it in a DB parameter group applies the parameters only at the instance level. For Amazon Lightsail MySQL database, you must check if parameters are available for modification before modifying them using the command line interface (CLI). For more information, see Updating database parameters in Amazon Lightsail. For Lightsail instance or server local MySQL database or Amazon Elastic Compute Cloud (Amazon EC2) MySQL, you can set the parameter in the config file. You can also set global xxx, similar to how it is set in a normal on-premise MySQL environment.
  • If the error occurs only when returning large datasets, the client might be using a large MTU value of 9001. To resolve this issue, reduce the client TCP/IP MTU value. For information on changing your client MTU value, see Configuring the MTU of an Instance.
  • If any init_connect parameters are set to a value that doesn't allow the parameter to be processed, associated client connections can fail. Be sure that all init_connect parameters are processed correctly. Be sure that users have EXECUTE permissions for any procedures referenced as an init_connect parameter.
  • If all connections are dropping at the same time, confirm that other connections are still working when this issue occurs. To resolve this issue, verify the MySQL DB instance is not crashing or failing over by viewing Amazon RDS events and reviewing your MySQL error logs.

Related information

Connecting to a DB Instance running the MySQL database engine

Modifying an Amazon RDS DB instance