Check mysql connection in sequelize
As of latest version of Sequelize (i.e. 3.3.2
), authenticate
can be used to check the connection:
var sequelize = new Sequelize("db", "user", "pass");
sequelize.authenticate().then(function(errors) { console.log(errors) });
authenticate
simply runs SELECT 1+1 AS result
query to check the db connection.
UPDATE:
Errors by the newest API need to be handled in catch
:
sequelize
.authenticate()
.then(() => {
console.log('Connection has been established successfully.');
})
.catch(err => {
console.error('Unable to connect to the database:', err);
});
UPDATE 2:
I haven't tested this, but its only logical that the same thing can be reached with async/await
:
try {
await sequelize.authenticate()
} catch (err) {
console.error('Unable to connect to the database:', err)
}
You won't see errors, like password authentication errors, in .then
.
From the sequelize documentation here:
You can use the .authenticate() function like this to test the connection.
sequelize
.authenticate()
.then(function(err) {
console.log('Connection has been established successfully.');
})
.catch(function (err) {
console.log('Unable to connect to the database:', err);
});