Node.js应用程序中的SequelizeConnectionError

时间:2019-03-13 04:18:42

标签: javascript mysql node.js docker sequelize.js

我有一个奇怪的问题,不知道问题出在什么地方。我将不胜感激。

我有 Node.js 应用程序,该应用程序在本地Windows 10计算机上运行良好。我在CentOS服务器中的 Docker 中成功运行了该应用程序。该应用程序可用于远程MySQL和PostgreSQL数据库。几天正常工作,但今天我注意到我有错误。应用程序无法再连接到远程MySQL数据库。同时,如果我在本地计算机上运行应用程序或通过DBeaver / dbForge工具进行连接,则可以毫无问题地连接到该远程MySQL数据库。

MySQL.js

const Sequelize = require('sequelize');

const sequelize = new Sequelize('database_name', 'username', 'password', {
    host: 'host',
    dialect: 'mysql'
});

sequelize.authenticate().then(() => {
    console.log('Connection to database has been established successfully.');
}).catch(err => {
    console.error('Unable to connect to database:', err);
});

module.exports = sequelize;

routes.js

const express = require('express');

const router = express.Router();

const sequelize = require('../configurations/MySQL');
const Sequelize = require('sequelize');

const passport = require('passport');
require('../configurations/password')(passport);

router.post('/search_by_name', passport.authenticate('jwt', {session: false}, null), function(req, res) {
    const token = getToken(req.headers);
    if (token) {
        sequelize.query("LONG SQL QUERY", {
            replacements: {
                name: req.body.name,
            },
            type: Sequelize.QueryTypes.SELECT
        }).then((locations) => {
            res.status(200).send(locations)
        }).catch((error) => {
            res.status(400).send(error);
        });
    } else {
        return res.status(401).send({
            status: false,
            description: "Unauthorized"
        });
    }
});

如您所见,我使用sequelize库将应用程序连接到远程MySQL数据库。我用于连接到远程PostgreSQL数据库的库相同。正如我之前所说,只有在尝试连接到Docker中的远程MySQL数据库时,错误才会发生。 Docker中的PostgreSQL连接没有错误。 Docker /网络内部是否可能出现问题?

依赖项

"sequelize": "^4.42.0"
"mysql2": "^1.6.4"

我还认为问题的原因可能是因为大量的池/连接,而sequelize库不会自动关闭它们。因此,我几次重新启动dockerсontainer希望确认理论。不幸的是,错误并没有消失。

您如何看待?

错误

Unable to connect to the database: { SequelizeConnectionError: connect ETIMEDOUT
    at Utils.Promise.tap.then.catch.err (/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:149:19)
    at tryCatcher (/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/node_modules/bluebird/js/release/promise.js:512:31)
    at Promise._settlePromise (/node_modules/bluebird/js/release/promise.js:569:18)
    at Promise._settlePromise0 (/node_modules/bluebird/js/release/promise.js:614:10)
    at Promise._settlePromises (/node_modules/bluebird/js/release/promise.js:690:18)
    at _drainQueueStep (/node_modules/bluebird/js/release/async.js:138:12)
    at _drainQueue (/node_modules/bluebird/js/release/async.js:131:9)
    at Async._drainQueues (/node_modules/bluebird/js/release/async.js:147:5)
    at Immediate.Async.drainQueues [as _onImmediate] (/node_modules/bluebird/js/release/async.js:17:14)
    at processImmediate (timers.js:632:19)
  name: 'SequelizeConnectionError',
  parent:
   { Error: connect ETIMEDOUT
       at Connection._handleTimeoutError (/node_modules/mysql2/lib/connection.js:173:17)
       at listOnTimeout (timers.js:324:15)
       at processTimers (timers.js:268:5)
     errorno: 'ETIMEDOUT',
     code: 'ETIMEDOUT',
     syscall: 'connect',
     fatal: true },
  original:
   { Error: connect ETIMEDOUT
       at Connection._handleTimeoutError (/node_modules/mysql2/lib/connection.js:173:17)
       at listOnTimeout (timers.js:324:15)
       at processTimers (timers.js:268:5)
     errorno: 'ETIMEDOUT',
     code: 'ETIMEDOUT',
     syscall: 'connect',
     fatal: true }}

1 个答案:

答案 0 :(得分:-1)

简而言之,我想说的是,当您输入某些配置错误时,就会出现此类错误。这些是:

  1. 数据库名称
  2. 用户名
  3. 密码

所以请交叉验证您的 congif.json 文件中的这些内容