因此,当我启动服务器时,大约有 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() 时间太长会导致这个错误。但为什么?

最佳答案

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

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

vagrant ssh

然后从流浪盒子
mysql -usteven -pstevenspassword -hhostameofserver.com

这至少将确认应用程序能够连接到数据库。

此外,如果您的数据库与您的应用程序在同一台机器上运行,您可以尝试通过 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
    }

});

关于node.js - sequelize.sync() 期间的 ETIMEDOUT,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26665750/

10-16 13:35