使用 Sequelize 连接到 MSSQL 服务器

2022-01-19 00:00:00 azure javascript sql-server sequelize.js

使用以下繁琐的代码,我可以成功连接到 Azure SQL Server.

Using the following tedious code, I can successfully connect to an Azure SQL Server.

const Connection = require('tedious').Connection;

const connection = new Connection({
    userName: '[USER]',
    password: '[PASSWORD]',
    server: '[HOSTNAME]',
    options: {encrypt: true}
});

connection.on('connect', (err) => {
    if (err) {
        console.log('error connecting', err);
    } else {
        console.log('connection successful');
    }
});

但是,使用应该是等效的 Sequelize 代码时,我收到了连接超时错误.

However, using what should be the equivalent Sequelize code, I get a connection timeout error.

const Sequelize = require('sequelize');

const sequelize = new Sequelize('[DBNAME]', '[USER]', '[PASSWORD]', {
    dialect: 'mssql',
    host: '[HOSTNAME]',
    dialectOptions: {
        encrypt: true
    }
});

sequelize.authenticate().then((err) => {
    console.log('Connection successful', err);
})
.catch((err) => {
    console.log('Unable to connect to database', err);
});

有什么想法吗?

使用:sequelize 3.29.0,繁琐的1.14.0,SQL Server v12

Using: sequelize 3.29.0, tedious 1.14.0, SQL Server v12

推荐答案

我遇到了错误

SequelizeConnectionError: 服务器需要加密,将 'encrypt' 配置选项设置为 true.

我在 Azure SQL 数据库中进行了尝试,以下方法对我有用.

I tried it out with Azure SQL Database and below way is working for me.

const sequelize = new Sequelize('DB Name', 'Username', 'Password', {
    host: 'Host',
    dialect: 'mssql',
    dialectOptions: {
        options: {
            encrypt: true,
        }
    }
  });

相关文章