Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS: MySQL sometimes raises ETIMEDOUT error

I'm currently developing an application using NodeJS.

However, often the server throws this error, and I can't interact with mysql.

 [Error: read ETIMEDOUT]
  code: 'ETIMEDOUT',
  errno: 'ETIMEDOUT',
  syscall: 'read',
  fatal: true }
{ [Error: Cannot enqueue Query after fatal error.] code: 'PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR', fatal: false }

Does someone have a solution to fix that?

Thanks

like image 625
Napsters Desmars Avatar asked Nov 09 '22 00:11

Napsters Desmars


1 Answers

From your question, I assume that you can work with your database perfectly but this error happens often after a given time of that connection being up...

Supposing you are using node-mysql
Source : https://github.com/felixge/node-mysql#error-handling
Your error terminates your connection to the database :

err.fatal: Boolean, indicating if this error is terminal to the connection object.
If the error is not from a MySQL protocol operation, this property will not be defined.

Note: 'error' events are special in node. If they occur without an attached listener, a stack trace is printed and your process is killed.

tl;dr: This module does not want you to deal with silent failures. You should always provide callbacks to your method calls. If you want to ignore this advice and suppress unhandled errors, you can do this:

    // I am Chuck Norris:
connection.on('error', function() {});

From this you could check connection status and perform a reconnect if needed.


You could also try to connect manually to your mysql service and change request timeout :

wait_timeout” : the amount of seconds during inactivity that MySQL will wait before it will close a connection on a non-interactive connection in seconds.

http://www.serveridol.com/2012/04/13/mysql-interactive_timeout-vs-wait_timeout/
https://support.rackspace.com/how-to/how-to-change-the-mysql-timeout-on-a-server/

like image 134
EMX Avatar answered Nov 14 '22 21:11

EMX