sequelize.sync()期间的ETIMEDOUT

时间:2014-10-31 00:36:34

标签: node.js sequelize.js

因此,当我启动服务器时,大约有60%的时间出现此错误。

[2014-10-31T00:55:30.763Z] ERROR: main/4588 on vagrant-centos65.vagrantup.com: undefined (err.errorno=ETIMEDOUT, err.code=ETIMEDOUT, err.syscall=connect, err.fatal=true)
Error: connect ETIMEDOUT
    at Connection._handleConnectTimeout (/vagrant/node_modules/mysql/lib/Connection.js:346:13)
    at Socket.g (events.js:180:16)
    at Socket.emit (events.js:92:17)
    at Socket._onTimeout (net.js:326:8)
    at Timer.unrefTimeout [as ontimeout] (timers.js:427:13)
    at Protocol._enqueue (/vagrant/node_modules/mysql/lib/protocol/Protocol.js:135:48)
    at Protocol.handshake (/vagrant/node_modules/mysql/lib/protocol/Protocol.js:52:41)
    at Connection.connect (/vagrant/node_modules/mysql/lib/Connection.js:109:18)
    at /vagrant/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:41:16
    at sequelizeResolver (/vagrant/node_modules/sequelize/lib/promise.js:32:12)
    at new Promise (/vagrant/node_modules/sequelize/node_modules/bluebird/js/main/promise.js:82:37)
    at new SequelizePromise (/vagrant/node_modules/sequelize/lib/promise.js:28:17)
    at ConnectionManager.connect (/vagrant/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:23:10)
From previous event:
    at new Promise (/vagrant/node_modules/sequelize/node_modules/bluebird/js/main/promise.js:82:37)
    at new SequelizePromise (/vagrant/node_modules/sequelize/lib/promise.js:28:17)
    at ConnectionManager.connect (/vagrant/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:23:10)
    at ConnectionManager.$connect (/vagrant/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:200:41)
    at Object.pool.Pooling.Pool.create (/vagrant/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:163:14)
    at createResource (/vagrant/node_modules/sequelize/node_modules/generic-pool/lib/generic-pool.js:261:13)
    at dispense (/vagrant/node_modules/sequelize/node_modules/generic-pool/lib/generic-pool.js:253:9)
    at Object.me.acquire (/vagrant/node_modules/sequelize/node_modules/generic-pool/lib/generic-pool.js:319:5)
From previous event:
    at new Promise (/vagrant/node_modules/sequelize/node_modules/bluebird/js/main/promise.js:82:37)
    at new SequelizePromise (/vagrant/node_modules/sequelize/lib/promise.js:28:17)
    at ConnectionManager.getConnection (/vagrant/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:183:10)
    at module.exports.Sequelize.query (/vagrant/node_modules/sequelize/lib/sequelize.js:590:85)
    at module.exports.QueryInterface.createTable (/vagrant/node_modules/sequelize/lib/query-interface.js:151:29)
    at /vagrant/node_modules/sequelize/lib/model.js:413:34
From previous event:
    at module.exports.Model.sync (/vagrant/node_modules/sequelize/lib/model.js:408:20)
    at /vagrant/node_modules/sequelize/lib/sequelize.js:730:20
Possibly unhandled Error: undefined
    at Promise._settlePromiseAt (/vagrant/node_modules/sequelize/lib/promise.js:66:18)
From previous event:
    at module.exports.Sequelize.sync (/vagrant/node_modules/sequelize/lib/sequelize.js:713:22)
    at Object.<anonymous> (/vagrant/server.js:111:3)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)

其他40%的时间没有问题。这只是在我切换到OS X时才开始发生,但它都在流浪汉中运行,所以它应该没关系。有什么想法吗?

修改 所以我已经缩小了问题所在。如果我忙于在代码中的任何地方等待~10秒(即使在开头!),我就能重现错误,例如。

'use strict';
for (var i = 0; i < 1e10; i+=2){}
var express = require(...

因此,调用sequelize.sync()花费太长时间会导致此错误。但为什么呢?

1 个答案:

答案 0 :(得分:2)

这看起来像Sequelize无法成功连接到您的数据库并且正在超时。

首先确保您的数据库实际上正在运行,并且您可以使用与sequ​​elize应用程序相同的连接参数从vagrant框连接。假设您正在运行MySQL,您可以通过以下方式执行此操作:

vagrant ssh

然后从流浪盒中

mysql -usteven -pstevenspassword -hhostameofserver.com

这至少会确认应用程序能够连接到db。

此外,如果您的数据库与您的应用程序在同一台计算机上运行,​​您可以尝试通过UNIX套接字而不是通过网络进行连接,如下所示:

var sequelize = new Sequelize('database', 'username', 'password', {

    host: '127.0.0.1',
    port: 3306,
    logging: false,
    dialect: 'mysql',
    dialectOptions: {
        socketPath: '/var/run/mysql.sock'
    },
    pool: {
        maxConnections: 5,
        maxIdleTime: 30
    }

});