如何用mssql配置sequelize?

时间:2018-04-11 12:40:55

标签: sql-server node.js sequelize.js

我想使用sequelize与Microsoft Sql连接。我找到了这个链接http://docs.sequelizejs.com/manual/installation/getting-started.html

我用nodejs编写了下面的代码:

require('dotenv').config();
var express = require('express');
var app = express();
const Sequelize = require('sequelize');

const sequelize = new Sequelize(process.env.DB_NAME,null,null, {
dialect: 'mssql',
host: process.env.DB_HOST + "\\" + process.env.DB_SERVER,
operatorsAliases: false,

pool: {
 max: 5,
 min: 0,
 acquire: 30000,
 idle: 10000
}

});

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

app.listen(process.env.PORT);
console.log('Starting Server on Port ', process.env.PORT);

但是当我运行代码时出现错误:

sequelize deprecated String based operators are now deprecated. Please use 
Symbol based operators for better security, read more at 
http://docs.sequelizejs.com/manual/tutorial/querying.html#operators 
node_modules\sequelize\lib\sequelize.js:242:13
Unable to connect to database { SequelizeHostNotFoundError: Failed to 
connect to USER-PC\SQLEXPRESS:1433-getaddrinfo ENOTFOUND USER-PC\SQLEXPRESS
at Connection.connection.on.err (C:\Users\User\Desktop\loginApp\node_modules
\sequelize\lib\dialects\mssql\connection-manager.js:98:22)

我做错了什么,我无法连接到数据库?

1 个答案:

答案 0 :(得分:0)

您需要为数据库提供mysql数据库名称,用户名和密码

var sequelize= new Sequelize('your Database name', 'username', 'password', {
host: 'localhost',
dialect: 'mssql', //postgres'|'sqlite'|''|'mysql',

pool: {
    max: 5,
    min: 0,
    acquire: 30000,
    idle: 10000
},

// SQLite only
//storage: 'path/to/database.sqlite'
});