在 Linux/Ubuntu 上使用 Nodejs + MSSQL

2021-09-12 00:00:00 ubuntu node.js sql-server unixodbc freetds

我的 nodejs 应用程序连接到 MSSQL 服务器,在我的 Windows 笔记本电脑上运行良好,唱 mssql@npm

同一个应用程序无法在我的 Ubuntu 笔记本电脑中看到数据库.

我在 Ubuntu 中定义了 mssql 连接如下,我是否遗漏了什么?

  1. 将我的 ~/.profile 更新为:

<块引用>

 ~$ export ODBCINI=/etc/odbc.ini~$ 导出 ODBCSYSINI=/etc~$ export FREETDSCONF=/etc/freetds/freetds.conf

  1. 注销笔记本电脑,以激活上述内容,并刷新配置文件.

  2. 安装了所需的连接包.

<块引用>

 ~$ sudo apt-get install unixodbc unixodbc-dev freetds-dev sqsh tdsodbc -y

  1. 配置的 FreeTDS

<块引用>

 ~$ sudo gedit/etc/freetds/freetds.conf[智慧服务器]主机 = 192.168.0.10端口 = 1433tds 版本 = 7.0

  1. 使用 sqsh 测试了 FreeTDS 连接,它工作正常:

<块引用>

 ~$ sqsh -S ACUMENSERVER -U mssql-username -P mssql-password

  1. 配置的 ODBC - odbcinst.ini:

<块引用>

 ~$ sudo gedit/etc/odbcinst.ini[免费TDS]说明 = TDS 驱动程序 (Sybase/MS SQL)驱动程序 =/usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so设置 =/usr/lib/x86_64-linux-gnu/odbc/libtdsS.soCP超时=CP重用=文件使用率 = 1

  1. 配置的 ODBC - odbc.ini:

<块引用>

~$ sudo gedit/etc/odbc.ini[智慧服务器]驱动程序 = FreeTDS说明 = 通过 FreeTDS 的 ODBC 连接跟踪 = 否服务器名称 = ACUMENSERVER数据库 = myDataBase

  1. 用 isql 测试了 ODBC 连接,它工作正常:

<块引用>

isql -v ACUMENSERVER mssql-username mssql-passward

当我运行我的 nodejs 应用程序(在 Windows 中运行良好)时,我在 Ubuntu 中遇到以下错误,考虑到上述所有内容都已完成,并检查:

{ name: 'ConnectionError',消息:'无法连接到 ACUMENSERVER:1433 - getaddrinfo ENOTFOUND',代码:'ESOCKET' }

这里可能有什么错误/遗漏,我可以使用另一个 npm 包进行 mssql 连接.

解决方案

我发现如果使用服务器的 IP 地址,这可以工作,服务器名称不起作用!

以下对我有用:

1 安装 mssql:

npm 安装 mssql

2 index.js 文件:

var sql = require('mssql');变量配置 = {用户:'sa',密码:'sql@123',//server: 'myServername', -->不工作//服务器:'ACUMENSERVER', -->不工作服务器:'6192.168.0.10',//有效数据库:'myDB'}sql.connect(config).then(function() {//询问new sql.Request().query('select top 1 itemcode from OITM').then(function(recordset) {控制台目录(记录集);}).catch(function(err) {console.log(err);/* ... 查询错误检查 ... */});//存储过程新的 sql.Request().input('input_parameter', sql.Int, value).output('output_parameter', sql.VarChar(50)).execute('procedure_name').then(function(recordset) {控制台目录(记录集);}).catch(function(err) {console.log(err);/* ... 执行错误检查 ... */});}).catch(function(err) {console.log(err);/* ... 连接错误检查 ... */});

更新,作者:Michael J. Ryan

您可能需要 myServer 的 DNS 解析器 .. 在 Windows 之外,您将无法获得 netbios 名称解析...无论您将其作为服务器名称,如果您无法 ping myserver命令提示符,您将无法通过节点连接

那么:

要么使用上面提到的 IP(这意味着根本不需要安装 ODBCunix 或 FreeTDS),要么使用本地网络的内部 DNS 并使用完整的 ADS 名称...myserver.domain.应该解析的名称(假设 ADS 用作本地解析的 dns 主机).

I've my nodejs app connected to MSSQL server, worked perfectly in my Windows laptop, sing mssql@npm

The same app is not able to see the database in my Ubuntu laptop.

I defined the mssql connectivity in Ubuntu as below, am I missing any thing?

  1. Updated my ~/.profile, as:

  ~$ export ODBCINI=/etc/odbc.ini
  ~$ export ODBCSYSINI=/etc
  ~$ export FREETDSCONF=/etc/freetds/freetds.conf

  1. Logged-out the laptop, to get the above active, and the profile refreshed.

  2. Installed the required connectivity packages.

  ~$ sudo apt-get install unixodbc unixodbc-dev freetds-dev sqsh tdsodbc -y

  1. Configured FreeTDS

  ~$ sudo gedit /etc/freetds/freetds.conf

   [ACUMENSERVER]
   host = 192.168.0.10
   port = 1433
   tds version = 7.0

  1. Tested the FreeTDS connection with sqsh, and it works fine:

  ~$ sqsh -S ACUMENSERVER -U mssql-username -P mssql-password

  1. Configured ODBC - odbcinst.ini:

  ~$ sudo gedit /etc/odbcinst.ini

     [FreeTDS]
     Description     = TDS driver (Sybase/MS SQL)
     Driver          = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
     Setup           = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
     CPTimeout       =
     CPReuse         =
     FileUsage       = 1

  1. Configured ODBC - odbc.ini:

~$ sudo gedit /etc/odbc.ini

   [ACUMENSERVER]
      Driver          = FreeTDS
      Description     = ODBC connection via FreeTDS
      Trace           = No
      Servername      = ACUMENSERVER
      Database        = myDataBase

  1. Tested the ODBC connection with isql, and it works fine:

isql -v ACUMENSERVER mssql-username mssql-passward

When I run my nodejs app (that is working fine in Windows), I got th below error in Ubuntu, considering all the above got done, and checked:

{ name: 'ConnectionError',
  message: 'Failed to connect to ACUMENSERVER:1433 - getaddrinfo ENOTFOUND',
  code: 'ESOCKET' }

What could be the wrong/missing thing here, I'm opened to use another npm package for mssql connectivity.

解决方案

I found this can work if the IP address of the server is used, server name is not working!

below what worked with me:

1 Instal mssql:

npm install mssql

2 The index.js file:

var sql = require('mssql');

var config = {
    user: 'sa',
    password: 'sql@123',
//  server: 'myServername',  --> Not Working
//  server: 'ACUMENSERVER',  --> Not Working
    server: '6192.168.0.10', // WORKED
    database: 'myDB'
}

sql.connect(config).then(function() { 
// Query 

new sql.Request().query('select top 1 itemcode from OITM').then(function(recordset) {
    console.dir(recordset);
    }).catch(function(err) {console.log(err); /* ... query error checks ... */ });


// Stored Procedure 

new sql.Request()
    .input('input_parameter', sql.Int, value)
    .output('output_parameter', sql.VarChar(50))
    .execute('procedure_name').then(function(recordset) {
         console.dir(recordset);
     }).catch(function(err) {console.log(err); /* ... execute error checks ... */ });



}).catch(function(err) {console.log(err); /* ... connect error checks ... */ });

UPDATE by Michael J. Ryan

You probably need a DNS resolver for myServer .. Outside of windows, you won't get netbios name resolution... whatever you put as the servername, if you can't ping myserver from the command prompt, you won't be able to connect via node

Then:

Either use the IP as noted above (which means no need at all to install neither ODBCunix nor FreeTDS), or have an internal DNS for the local network and use the full ADS name... myserver.domain.name which should resolve (assuming ADS is used as dns host for local resolution).

相关文章