mysql - 重现 MySQL 错误 : The server closed the connection (node. js)

标签 mysql node.js node-mysql

我正在尝试使用 node mysql library 重现我在 EC2 上的 node.js 应用程序中看到的 MySQL 错误。 :

Connection lost: The server closed the connection.

我无法在本地重现错误 - 我的代码可以很好地处理终止数据库 - 它只是每隔几秒钟重新检查一次,并在重新启动后重新连接到数据库。在 EC2 上,它发生在太平洋时间凌晨 4 点左右,但数据库仍然正常运行。

我愿意

  1. 用我的本地 mysql 重现崩溃
  2. 在我的 mysql 帮助模块中添加我需要的任何逻辑来处理这个

这是我的 node.js 应用程序中的错误:

2012-10-22T08:45:40.518Z - error: uncaughtException date=Mon Oct 22 2012 08:45:40 GMT+0000 (UTC), pid=14184, uid=0, gid=0, cwd=/home/ec2-user/my-app, execPath=/usr/bin/nodejs, version=v0.6.18, argv=[/usr/local/bin/node, /home/ec2-user/my-app/app.js, --my-app], rss=15310848, heapTotal=6311392, heapUsed=5123292, loadavg=[0.0029296875, 0.0146484375, 0.04541015625], uptime=3238343.511107486, trace=[column=13, file=/home/ec2-user/my-app/node_modules/mysql/lib/protocol/Protocol.js, function=Protocol.end, line=63, method=end, native=false, column=10, file=stream.js, function=Socket.onend, line=80, method=onend, native=false, column=20, file=events.js, function=Socket.emit, line=88, method=emit, native=false, column=51, file=net.js, function=TCP.onread, line=388, method=onread, native=false], stack=[Error: Connection lost: The server closed the connection.,
at Protocol.end (/home/ec2-user/my-app/node_modules/mysql/lib/protocol/Protocol.js:63:13), at Socket.onend (stream.js:80:10), at Socket.emit (events.js:88:20), at TCP.onread (net.js:388:51)]

这是我的代码(mysql 辅助模块):

module.exports = function (conf,logger) {
  var mysql = require('mysql');

  var connectionState = false;
  var connection = mysql.createConnection({
    host: conf.db.hostname,
    user: conf.db.user,
    password: conf.db.pass,
    database: conf.db.schema,
    insecureAuth: true
  });

  function attemptConnection(connection) {
    if(!connectionState){
      connection = mysql.createConnection(connection.config);
      connection.connect(function (err) {
        // connected! (unless `err` is set)
        if (err) {
          logger.error('mysql db unable to connect: ' + err);
          connectionState = false;
        } else {
          logger.info('mysql connect!');
          connectionState = true;
        }
      });
      connection.on('close', function (err) {
        logger.error('mysqldb conn close');
        connectionState = false;
      });
      connection.on('error', function (err) {
        logger.error('mysqldb error: ' + err);
        connectionState = false;

        /*
        if (!err.fatal) {
          return;
        }
        if (err.code !== 'PROTOCOL_CONNECTION_LOST') {
          throw err;
        }
        */
      });
    }
  }
  attemptConnection(connection);

  var dbConnChecker = setInterval(function(){
    if(!connectionState){
      logger.info('not connected, attempting reconnect');
      attemptConnection(connection);
    }
  }, conf.db.checkInterval);

  return connection;
};

最佳答案

查看 node-mysql 中的 mysql 池功能

var mysql = require('mysql');
var pool  = mysql.createPool({
  host     : 'example.org',
  user     : 'bob',
  password : 'secret'
});

pool.getConnection(function(err, connection) {
  // connected! (unless `err` is set)
  connection.end();
});

关于mysql - 重现 MySQL 错误 : The server closed the connection (node. js),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13018227/

相关文章:

javascript - 如何编译 Node.js 和 Angular

mysql - Node.Js - mysql 连接没有输出

javascript - 为什么我尝试将 SQL 查询中的行获取到脚本中的全局变量中却只得到一个空数组?

Mysql 过程语法更改为 Firebird 过程语法

node.js - 服务器和客户端的 Package.json

mysql - SQL从三个相关表中选择数据

node.js - 在 emacs 中启动 Node REPL

javascript - Angular electron mysql create connection 不是一个函数

php - Laravel 5.1 中的 mysql 外键错误

mysql - 根据两个字段查找重复记录