在 sequelize 中检查 mysql 连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19429152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 19:09:12  来源:igfitidea点击:

Check mysql connection in sequelize

mysqlnode.jssequelize.js

提问by vistur

I have a very simple program in where I create a Sequelize instance and then I perform a raw query on a mysql database. The case is that when MySql is up and running there is no problem, I can perform the query with no problems. But when MySql is not running then the query doesn't emmit any error until the query timeout has reached and then it emmits a ETIMEOUT error. But that's not really what's happening. I expect the query to emit ENOTFOUND error or something like that if mysql is not running so I can manage the error and perform different actions if Mysql has gone down or Mysql is very busy and has a very large response time. What shoul'd I do to check if Mysql is up and running without having to wait the timeout exception.

我有一个非常简单的程序,我在其中创建了一个 Sequelize 实例,然后在 mysql 数据库上执行原始查询。情况是,当 MySql 启动并运行时没有问题,我可以毫无问题地执行查询。但是,当 MySql 未运行时,查询不会发出任何错误,直到查询超时达到,然后它才会发出 ETIMEOUT 错误。但事实并非如此。如果 mysql 没有运行,我希望查询会发出 ENOTFOUND 错误或类似的错误,因此如果 Mysql 出现故障或 Mysql 非常繁忙并且响应时间非常长,我可以管理错误并执行不同的操作。我应该做什么来检查 Mysql 是否已启动并正在运行,而不必等待超时异常。

sequelize = new Sequelize(db_name, db_user, db_pass, opts);

sequelize.query('SELECT * FROM some_table').success(function(result) {
  console.log(result);
}).error(function(err) {
  console.log(err);
});

回答by Yan Foto

As of latest version of Sequelize(i.e. 3.3.2), authenticatecan be used to check the connection:

从最新版本的Sequelize(ie 3.3.2) 开始,authenticate可用于检查连接:

var sequelize = new Sequelize("db", "user", "pass");

sequelize.authenticate().then(function(errors) { console.log(errors) });

authenticatesimply runs SELECT 1+1 AS resultquery to check the db connection.

authenticate只需运行SELECT 1+1 AS result查询来检查数据库连接。

UPDATE:

更新

Errors by the newest APIneed to be handled in catch:

需要处理最新 API 的错误catch

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

回答by Nate

You won't see errors, like password authentication errors, in .then.

您不会在.then.

From the sequelize documentation here:

这里的续集文档:

You can use the .authenticate() function like this to test the connection.

您可以像这样使用 .authenticate() 函数来测试连接。

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

回答by Franck Freiburger

I use the following code to wait for the db engine to start:

我使用以下代码等待数据库引擎启动:

function sleep(ms) {

    return new Promise(function(resolve) {

        setTimeout(resolve, ms);
    });
}

for (;;) {

    try {

        await db.authenticate();
        break;
    } catch(ex) {

        await sleep(1000);
    }
}

回答by avisheks

Please check "wait_timeout" system variable if its been reset to some trivial value

请检查“wait_timeout”系统变量是否已重置为某个微不足道的值