我想使用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)
我做错了什么,我不能连接到数据库?
发布于 2019-06-06 13:43:42
// You can do it with a string.
const Sequelize = require('sequelize');
const sequelize = new Sequelize("mssql://username:password@mydatabase.database.windows.net:1433",
{ pool: {
"max": 10,
"min": 0,
"idle": 25000,
"acquire": 25000,
"requestTimeout": 300000
},
dialectOptions: {
options: { encrypt: true }
}
});
// This uses the Raw Query to query for all dbs for example
sequelize.query(`
SELECT name, database_id, create_date
FROM sys.databases
GO `,
{ type: sequelize.QueryTypes.SELECT})
.then(async dbs => {
console.log("dbs", dbs);
return dbs;
});```
Example above: mssql db hosted on Azure.
Example below: mssql db on localhost.
You can do it with key value pairs or a string.
`
var Sequelize = require("sequelize");
var sequelize = new Sequelize("sequelize_db_name", user, password, {
host: "localhost",
port: 1433,
dialect: "mssql",
pool: {
max: 5,
min: 0,
idle: 10000
},
dialectOptions: {
options: { encrypt: true }
}
});`
https://stackoverflow.com/questions/49775451
复制相似问题