我在NodeJs中使用mssql模块连接sql server数据库报错。报错:
ConnectionError: Failed to connect to 111.222.333.44\SQLSERVER2014 in 15000ms
at E:\nodejs\node_modules\mssql\lib\tedious\connection-pool.js:71:17
at Connection.onConnect (E:\nodejs\node_modules\mssql\node_modules\tedious\lib\connection.js:1043:9)
at Object.onceWrapper (node:events:514:26)
at Connection.emit (node:events:394:28)
at Connection.emit (E:\nodejs\node_modules\mssql\node_modules\tedious\lib\connection.js:1071:18)
at Connection.connectTimeout (E:\nodejs\node_modules\mssql\node_modules\tedious\lib\connection.js:1530:10)
at Timeout._onTimeout (E:\nodejs\node_modules\mssql\node_modules\tedious\lib\connection.js:1475:12)
at listOnTimeout (node:internal/timers:557:17)
at processTimers (node:internal/timers:500:7) {
code: 'ETIMEOUT',
originalError: ConnectionError: Failed to connect to 111.222.333.44\SQLSERVER2014 in 15000ms
at ConnectionError (E:\nodejs\node_modules\mssql\node_modules\tedious\lib\errors.js:13:12)
at Connection.connectTimeout (E:\nodejs\node_modules\mssql\node_modules\tedious\lib\connection.js:1530:54)
at Timeout._onTimeout (E:\nodejs\node_modules\mssql\node_modules\tedious\lib\connection.js:1475:12)
at listOnTimeout (node:internal/timers:557:17)
at processTimers (node:internal/timers:500:7) {
code: 'ETIMEOUT'
路由JS代码如下:
var sql = require('mssql');
var dbConfig = {
user: 'sa',
password: 'sa@123,',
server: '111.222.333.44\\SQLSERVER2014',
database: 'PhotovoltaicDB',
port:5202,
pool: {
max: 10,
min: 0,
idleTimeoutMillis: 30000
}
};
// 查询所有的用户信息
function getAllUsers() {
var conn = new sql.ConnectionPool(dbConfig);
//console.log(conn);
var req = new sql.Request(conn);
conn.connect(function (err) { //就在这句报错,应该是不知道为什么连接失败
if (err) {
console.log(err);
return;
}
// 查询t_user表
req.query("SELECT * FROM t_user", function (err, recordset) {
if (err) {
console.log(err);
return;
}
else {
console.log(recordset);
}
conn.close();
});
});
}
// 查询所有的用户信息
getAllUsers();
sql server数据库地址没有问题,需要填写数据库实例名,而且密码带@和,两个特殊字符,所以不知道咋用连接串的形式进行拼接。NodeJs电脑使用sql server软件直接可以远程连接入数据库。试过好几种方法了,都是在连接的时候报错。求碰到过或者知道怎么解决的帮忙解答一下。对了,我sql server要连接的2014版本。用的mssql是直接npm上引入的,引入的时候也没有报错。而且也试了删除重新引入。