MySQL "read ECONNRESET" Error in Node.js After Idle Time
When connecting to MySQL via the node-mysql module, you may encounter the "read ECONNRESET" error after leaving the server idle for extended periods. This error indicates a connection reset, potentially due to MySQL's idle connection pruning mechanism.
Diagnosing the Issue:
-
Connection Disconnection: The error suggests a disconnection between Node's connection and the MySQL server. This could be caused by the MySQL variable "wait_timeout" setting, which defaults to 8 hours.
-
Connection Pool Issues: Node-mysql uses connection pools to handle disconnections and remove inactive connections. However, it may not be able to prune a disconnected connection until you make a query, which triggers the error.
-
External Factors: While the "read ECONNRESET" error is often associated with MySQL, consider checking for any potential external factors contributing to the connection loss.
Resolving the Error:
-
Increase Wait Timeout: Set the MySQL "wait_timeout" variable to a longer duration, such as 28,800 seconds (8 hours). This allows for longer periods of inactivity without disconnection.
-
Use Pool Pruning: Implement a heartbeat mechanism in your code to periodically send SELECT 1; queries to the database, keeping the connection alive.
-
Emphasize Idle Timeout: Use the node-pool module's idleTimeoutMillis option to automatically prune idle connections, preventing the issue from arising.
The above is the detailed content of Why am I getting 'read ECONNRESET' error in Node.js after MySQL connection is idle?. For more information, please follow other related articles on the PHP Chinese website!