本文介绍了使用 Sequelize 连接到 MSSQL 服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用以下繁琐的代码,我可以成功连接到 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,
}
}
});
这篇关于使用 Sequelize 连接到 MSSQL 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!