NodeJS:MySQL有时会引发ETIMEDOUT错误

我目前正在使用NodeJS开发应用程序.

但是,经常服务器抛出此错误,我无法与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 }

有人有解决方案吗?

谢谢

最佳答案 从您的问题,我假设您可以完美地使用您的数据库,但这个错误经常发生在该连接的给定时间后…

假设您正在使用node-mysql
资料来源:https://github.com/felixge/node-mysql#error-handling
您的错误会终止与数据库的连接:

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() {});

从这里你可以检查连接状态并在需要时执行重新连接.

您还可以尝试手动连接到您的mysql服务并更改请求超时:

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/

点赞