PM2半夜莫名重启,然后报下面的错是什么意思?是mysql的原因?
Error: Connection lost: The server closed the connection.
at Protocol.end (/xxxx/xxx/xxx/node_modules/mysql/lib/protocol/Protocol.js:113:13)
at Socket.<anonymous> (/xxxx/xxx/xxx/node_modules/mysql/lib/Connection.js:109:28)
at emitNone (events.js:111:20)
at Socket.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1056:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickDomainCallback (internal/process/next_tick.js:218:9)
Node.js的版本是v8.9.0. PM2的版本是2.9.1. mysql库的版本是2.15.0。
当过几个小时没有访问的MySQL的时候,MySQL自动断开连接,这个问题的原因是MySQL有一个wait_time 当超过这个时间的时候连接会丢失,当你再去请求MySQL的时候会连接不上MySQL服务。
第一个方法就是监听 error 事件, 当段开始重连 。
第二个方法就是使用连接池
mysql.createPool
, 具体实现方案可以参考 解决nodejs mysql Error: Connection lost The server closed the connection的两种方法 。假设作者或其他小伙伴有自己的方法, 还望不吝赐教