【问题标题】:Nodejs with Forever script will not accessible after sometime带有 Forever 脚本的 Nodejs 将在一段时间后无法访问
【发布时间】:2018-01-02 23:55:41
【问题描述】:

我有一个 Nodejs 项目,它托管在 Digital Ocean 中,我使用 Forever 脚本保持它运行。但是我意识到一段时间后会出现一些错误导致网站关闭。它不仅发生过一次,而且已经发生了几次,下面是 Forever 日志:

Error: Connection lost: The server closed the connection.
    at Protocol.end (/var/www/menu/node_modules/mysql/lib/protocol/Protocol.js:109:13)
    at Socket.<anonymous> (/var/www/menu/node_modules/mysql/lib/Connection.js:109:28)
    at emitNone (events.js:72:20)
    at Socket.emit (events.js:166:7)
    at endReadableNT (_stream_readable.js:905:12)
    at nextTickCallbackWith2Args (node.js:441:9)
    at process._tickCallback (node.js:355:17)
error: Forever detected script exited with code: 1
error: Script restart attempt #178
Listening on xxx.xxx.xxx.xxx, server_port 80
events.js:141
      throw er; // Unhandled 'error' event

Error: connect ECONNREFUSED 127.0.0.1:3306
    at Object.exports._errnoException (util.js:870:11)
    at exports._exceptionWithHostPort (util.js:893:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1063:14)
    --------------------
    at Protocol._enqueue (/var/www/menu/node_modules/mysql/lib/protocol/Protocol.js:141:48)
    at Protocol.handshake (/var/www/menu/node_modules/mysql/lib/protocol/Protocol.js:52:41)
    at Connection.connect (/var/www/menu/node_modules/mysql/lib/Connection.js:130:18)
    at Connection._implyConnect (/var/www/menu/node_modules/mysql/lib/Connection.js:461:10)
    at Connection.query (/var/www/menu/node_modules/mysql/lib/Connection.js:206:8)
    at Object.<anonymous> (/var/www/menu/config/db.js:14:12)
    at Module._compile (module.js:410:26)
    at Object.Module._extensions..js (module.js:417:10)
    at Module.load (module.js:344:32)
    at Function.Module._load (module.js:301:12)
error: Forever detected script exited with code: 1

我不确定为什么会发生这种情况,因为日志没有说明哪一行导致错误,而且似乎 Forever 曾尝试重新启动脚本但它失败了。但是一旦我通过 SSH 连接到我的服务器并重新启动它,它就已经可以正常工作了。有谁能帮忙吗?谢谢。

【问题讨论】:

    标签: node.js ubuntu-16.04 node-modules forever


    【解决方案1】:
    Error: connect ECONNREFUSED 127.0.0.1:3306
    

    您收到此错误,表示该端口正在使用中。您可以使用以下命令终止在该端口上运行的进程。

    sudo kill $(sudo lsof -t -i:3306)
    

    之后,您可以像您一样使用永久重新部署您的服务器。

    【讨论】:

    • 但是这个错误是在Forever重启总共178次之后才出现的,所以这不是导致脚本无法重启的主要原因吧?
    • 永远无法重启的原因是因为你被mysql因为超时而断开连接,当你(永远)尝试重新连接时进程已经在运行
    • 表示服务器配置有问题。我建议您检查永久日志以找出错误。尝试“forever logs”列出日志文件,后跟“forever logs (number)”这里的数字是日志文件的编号。
    • 参考您的代码。您可以查看 config/db.js:14:12)
    【解决方案2】:

    发生在我身上一次 该问题似乎与mysql连接有关。

    请参考这个stackoverflow问题来解决您的问题

    nodejs mysql Error: Connection lost The server closed the connection

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-26
      • 1970-01-01
      • 1970-01-01
      • 2022-01-07
      • 2012-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多