【问题标题】:I cannot connect to SQL server using Node.JS server我无法使用 Node.JS 服务器连接到 SQL 服务器
【发布时间】:2020-03-11 15:26:26
【问题描述】:
module.exports = {
    "user": 'ThisUsername',
    "password": 'ThisPassword',
    "server": '169.254.4.58',
    "database": 'ThisDatabase',
    "pool": {
        "max": 10,
        "min": 0,
        "idleTimeoutMillis": 30000
    }
};

这是配置对象,存储在单独的文件中,因此值不会被硬编码

let sql = require('mssql');
var connStr = require('./connStr.js');

const connectionString = process.env.CONNECTION_STRING||'';
//console.log(process.env);
console.log(connectionString);

sql.on('error', err => {
  // ToDo: For testing only -- remove console.log later
  console.log(err);
});

// addapted from https://stackoverflow.com/questions/30356148/how-can-i-use-a-single-mssql-connection-pool-across-several-routes-in-an-express
const poolPromise = sql.connect(connStr)
  .then(pool => {
    console.log('SQL connection established.');
    return pool;
  })
  .catch(err => SqlClose(err));

module.exports = { sql, poolPromise };

function SqlClose(thisErr) {
    console.log(`SQL connection failed - \n${thisErr}`);
    sql.close();
};

这是连接到 SQL 服务器的代码

C:\ThisDirectory>npm run dev

> ThisProject@1.0.0 dev C:\ThisDirectory
> env-cmd -f ./config/dev.env node ./src/index.js


tedious deprecated The default value for `config.options.enableArithAbort` will change from `false` to `true` in the next major version of `tedious`. Set the value to `true` or `false` explicitly to silence this message. node_modules\mssql\lib\tedious\connection-pool.js:61:23
Server is up on port 3000
SQL connection failed -
ConnectionError: Failed to connect to 169.254.4.58:1433 in 15000ms

这是尝试连接时的错误

Server is up on port 3000 指的是 Node.JS 服务器 SQL连接失败指的是SQL服务器连接

看来这些文件之一是我无法连接的原因。由于工作网络的限制,我无法在 SQL Server 上启用 TCP/IP,SQL Server Browser 已经在运行,这是我可以在网上找到的唯一两个显然可以帮助解决这个问题的东西。我没有编写此代码,但开发人员还有另一个重要项目要处理。我很茫然,我已经超过了最后期限。请帮忙

【问题讨论】:

    标签: javascript sql node.js sql-server tedious


    【解决方案1】:

    你需要更新你的配置如下:

    module.exports = {
        "user": 'ThisUsername',
        "password": 'ThisPassword',
        "server": '169.254.4.58',
        "database": 'ThisDatabase',
        "pool": {
            "max": 10,
            "min": 0,
            "idleTimeoutMillis": 30000
        },
        "options": {
        "encrypt": true,
        "enableArithAbort": true
        }
    };
    

    enableArithAbort 必须在 options 属性中传递。

    【讨论】:

    • 没有修复,同样的问题依然存在
    • 尝试更改为false。
    • 不,还是无法连接
    猜你喜欢
    • 2011-09-10
    • 1970-01-01
    • 2014-06-28
    • 2019-09-29
    • 1970-01-01
    • 2018-12-15
    • 1970-01-01
    • 2019-10-21
    • 1970-01-01
    相关资源
    最近更新 更多