node.js 4.2.2中的握手问题,带有mysql实时数据

时间:2015-11-28 09:41:18

标签: mysql node.js express pug

    Error: Handshake inactivity timeout
    at Handshake.<anonymous> (C:\Users\PC02\Desktop\simple-blog\node_modules\mysql\lib\protocol\Protocol.js:154:17)
    at emitNone (events.js:67:13)
    at Handshake.emit (events.js:166:7)
    at Handshake._onTimeout (C:\Users\PC02\Desktop\simple-blog\node_modules\mysql\lib\protocol\sequences\Sequence.js:116:8)
    at Timer.listOnTimeout (timers.js:92:15)
    --------------------
    at Protocol._enqueue (C:\Users\PC02\Desktop\simple-blog\node_modules\mysql\l
ib\protocol\Protocol.js:135:48)
    at Protocol.handshake (C:\Users\PC02\Desktop\simple-blog\node_modules\mysql\
lib\protocol\Protocol.js:52:41)
    at PoolConnection.connect (C:\Users\PC02\Desktop\simple-blog\node_modules\my
sql\lib\Connection.js:123:18)
    at Pool.getConnection (C:\Users\PC02\Desktop\simple-blog\node_modules\mysql\
lib\Pool.js:45:23)
    at Object.exports.getConn (C:\Users\PC02\Desktop\simple-blog\libs\db.js:9:8)

    at Object.<anonymous> (C:\Users\PC02\Desktop\simple-blog\app.js:23:8)
    at Module._compile (module.js:435:26)
    at Object.Module._extensions..js (module.js:442:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:311:12)

C:\Users\PC02\Desktop\simple-blog>

我的app.js文件位于

之下
// local vars
var express = require('express')
    , app = express()
    , path = require('path')
    , flash = require('connect-flash')
    , colors = require('colors')
    , compress = require('compression')
    , favicon = require('serve-favicon')
    , morgan = require('morgan') // ex logger
    , bodyParser = require('body-parser')
    , methodOverride = require('method-override')
    , cookieParser = require('cookie-parser')
    , session = require('express-session')
    ;

// global vars
    dbConfig = require("./config/dbConfig")
  , dbPool = require('./libs/db')
  , check = require('./libs/check')
  , dateformat = require('dateformat')
  , app_env = process.env.NODE_ENV || 'development';

dbPool.getConn(function(dbConn) {
  app.set('port', 3737);

  // main global db connect
  db = dbConn;

  // helpers
  require('./libs/helpers')(app);


  // gzip all
  app.use(compress());

  // error handler
  app.use(require('errorhandler')({ dumpExceptions: true, showStack: true }));

  // etc settings
  app.set('port', process.env.SIMPLE_NODE_BLOG_PORT || 3737);
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(favicon(__dirname + '/public/favicon.ico'))
  app.use(morgan('short')); 
  app.use(bodyParser.urlencoded({
    extended: true
  }));
  app.use(bodyParser.json());
  app.use(methodOverride());

  // init session
  app.use(cookieParser('lalaleqwe123111keqwe123123ndfldsaknflsdfsdf'));

  // sessions mysql store
  var MysqlStore = require('connect-mysql')({session: session});
  app.use(session({
    secret: 'adhlbq erldhbq ;erb3241434134-~~~asd',
    resave: false,
    saveUninitialized: false,
    store: new MysqlStore({
      config: {
        user: dbConfig.user,
        password: dbConfig.password,
        database: dbConfig.database,
        table: 'sessions'
      }
    })
  }));

  //flash messages
  app.use(flash());

  // static
  app.use(express.static(path.join(__dirname, 'public')));

  // passport init & conf have to be here
  require('./config/pass-conf')(app);
  require('./config/pass-keys')(app);
  require('./config/pass-facebook-conf')(app);
  require('./config/pass-twitter-conf')(app);
  require('./config/pass-github-conf')(app);
  require('./config/pass-local-conf')(app);

  // load global app config
  var settings = require('./routes/admin/settings');

  // global config here
  appConfig = {};
  settings.getConf(function(err){
    if (err) throw err;
  }); // loaded config

  // auth module
  require('./routes/auth-routes')(app, require('./routes/auth'));

  // blog module
  require('./routes/blog-routes')(app, require('./routes/blog'));

  // admin BASE module (main routing check)
  require('./routes/admin/admin-routes')(app, require('./routes/admin/admin'));

  // admin users module
  require('./routes/admin/users-routes')(app, require('./routes/admin/users'));

  // admin settings module
  require('./routes/admin/settings-routes')(app, settings);

  // user zone
  require('./routes/my-routes')(app, require('./routes/my'));

  // sitemap
  require('./routes/sitemap-routes')(app, require('./routes/sitemap'));

  // rss
  require('./routes/rss-routes')(app, require('./routes/rss'));

  ////////////////////////////////////////
  // error 404 (just last available route)
  app.use(function(req, res, next){

    res.status(404).render('err404', { // error http code 404
      title: 'ouch.. 404 error',
      err_id: Math.floor((Math.random() * 12) + 1) // random 1 to 12
    });
  });

  if (app_env == 'development') {
    app.use(function(err, req, res, next) {

      res.status(500).render('err500', {
        title: 'ouch.. 500 [dev]',
        err: err
      });
    });
  }

  if (app_env == 'production') {
    app.use(function(err, req, res, next) {

      res.status(500).render('err500', {
        title: 'ouch.. 500',
        err: ''
      });
    });
  }

  // start server here
  app.listen(app.get('port'));
  console.log("-- simple node blog listening on port " + app.get('port') + " --");

});

我正在尝试服务器的实时服务器数据库配置

// db connection prop
module.exports = {
  user: 'a8246231_blog',
  password: 'pass123',
  host: 'mysql12.000webhost.com',
  database: 'a8246231_blog'
};

我无法与我的实时服务器数据库建立连接。握手不活动超时错误抛出

var mysql = require('mysql')
  , pool = mysql.createPool(dbConfig)

// for bunch queries like select + found_rows
exports.getConn = function(next){
  pool.getConnection(function(err, conn){
    var dbObj = {
      // standard query
      q: function(sql, callee, next){
        if (typeof(callee) != 'object' || !(callee instanceof Array)) {
          calleeSend = [];
        } else {
          calleeSend = callee;
        }

        if (err) {
          throw err;
        }

        conn.query(sql, calleeSend, function(err, qres){
          if (err){
            if (typeof(callee) == 'function') {
              return callee(err);
            } else if (typeof(next) == 'function') {
              return next(err);
            } else {
              console.log('Uncaught DB error: ', err);
              console.log('Uncaught error in query: ', sql);
            }
          }

          try {
            if (typeof(callee) == 'function') {
              callee(err, qres);
            } else if (typeof(next) == 'function') {
              next(err, qres);
            }
          } catch(e) {
            next(e);
          }

        });
      },

      // get row
      getRow: function(sql, callee, next){
        if (typeof(callee) != 'object' || !(callee instanceof Array)) {
          calleeSend = [];
        } else {
          calleeSend = callee;
        }

        conn.query(sql, calleeSend, function(err, qres){
          if (err){
            if (typeof(callee) == 'function') {
              return callee(err);
            } else if (typeof(next) == 'function') {
              return next(err);
            } else {
              console.log('Uncaught DB error: ', err);
              console.log('Uncaught error in query: ', sql);
            }
          }

          row = (qres[0]) ? qres[0]: false ;

          try {
            if (typeof(callee) == 'function') {
              callee(err, row);
            } else if (typeof(next) == 'function') {
              next(err, row);
            }
          } catch(e) {
            next(e);
          }

        });

      },

      // last insert id
      lastId: function(next){
          conn.query("SELECT LAST_INSERT_ID() as id", function sres(err, qres){
          var id = (qres[0].id) ? qres[0].id: false ;

          try {
            next(err, id);
          } catch (e) {
            return next(e);
          }

        });
      },

      // calc found rows
      foundRows: function(next){
        conn.query("select found_rows() as cnt", function(err, qres){
          var cnt = (qres[0].cnt) ? qres[0].cnt : false ;

          try {
            next(err, cnt);
          } catch (e) {
            return next(e);
          }
        });
      },

      // release connection
      release: function(){
        conn.release();
      }

    };

    next(dbObj);
  });
};

上面的代码用于我的数据库函数

1 个答案:

答案 0 :(得分:0)

这是一个已知问题:https://github.com/felixge/node-mysql/issues/1239

尝试降级到节点4.2.0或稳定的5.1.0