简体   繁体   English

如何使用 JavaScript 从 Azure Functions 连接到 Microsoft SQL Server?

[英]How to connect to Microsoft SQL Server from Azure Functions using JavaScript?

We´re developing a function app in JavaScript with the Serverless Framework and deploying it to Azure.我们正在使用无服务器框架在 JavaScript 中开发一个函数应用程序并将其部署到 Azure。

We´re pulling data from a Microsoft SQL Server 2014 database, everything goes well when testing the functions in a local environment with the Serverless Offline plugin and a VPN.我们从 Microsoft SQL Server 2014 数据库中提取数据,在本地环境中使用无服务器离线插件和 VPN 测试功能时一切顺利。 This is the code to establish the connection:这是建立连接的代码:

const mssql = require("mssql");

const sqlConfig = {
  user: process.env["DB_USER"],
  password: process.env["DB_PWD"],
  database: process.env["DB_NAME"],
  server: process.env["DB_HOST"],
  pool: {
    max: 10,
    min: 0,
    idleTimeoutMillis: 30000,
  },
  options: {
    encrypt: true, // for azure
    trustServerCertificate: false, // change to true for local dev / self-signed certs
  },
};

// pool-manager.js
const pools = new Map();

module.exports = {
  get: (name) => {
    if (!pools.has(name)) {
      const pool = new mssql.ConnectionPool(sqlConfig);
      // automatically remove the pool from the cache if `pool.close()` is called
      const close = pool.close.bind(pool);
      pool.close = (...args) => {
        pools.delete(name);
        return close(...args);
      };
      pools.set(name, pool.connect());
    }
    return pools.get(name);
  },

  closeAll: () =>
    Promise.all(
      Array.from(pools.values()).map((connect) => {
        return connect.then((pool) => pool.close());
      })
    ),
};

We´re also using Key Vault's references, these are working normally.我们也在使用 Key Vault 的参考资料,这些资料工作正常。

The problem is when the functions are deployed, the connections to the database is never established.问题是在部署功能时,从未建立与数据库的连接。 This is the error I get:这是我得到的错误:

{
  "code": "ESOCKET",
  "originalError": {
    "code": "ESOCKET"
  },
  "name": "ConnectionError"
}

So, my questions is: What has to be done in Azure or what has to be changed in our code to allow this connection?所以,我的问题是:在 Azure 中必须做什么,或者我们的代码需要改变什么才能允许这种连接?

Thanks谢谢

Is your MS SQL 2014 server hosted on-prem/on a VM behind a VNET?您的 MS SQL 2014 服务器是否托管在本地/在 VNET 后面的 VM 上?

If so, you will need to integrate your Function App with that virtual network.如果是这样,您需要将 Function App 与该虚拟网络集成。 See Microsoft guide .请参阅Microsoft 指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 重用 SQL 服务器数据库连接与 Azure 函数使用 Javascript - Reusing SQL server Database Connections With Azure Functions Using Javascript 从Microsoft SQL Server数据库向JavaScript函数发送数据的最佳方法 - The best way to send data to JavaScript functions from a Microsoft SQL Server Database 如何在浏览器中从JavaScript连接到SQL Server数据库? - How to connect to SQL Server database from JavaScript in the browser? 无法使用私有 IP 从 Cloud Functions 连接到 Cloud SQL SQL Server - Cannot connect to Cloud SQL SQL Server from Cloud Functions using private IP 如何使用JavaScript在Azure SQL Server上执行操作? - How to perform operations on Azure SQL Server using javascript? 使用Azure函数和Azure SQL服务器制作登录和注册系统 - Make a login and registration system using Azure functions and Azure SQL server 使用JavaScript将HTML页面与SQL Server连接 - Connect HTML page with SQL server using javascript 如何将JavaScript连接到SQL Server? - How can i connect JavaScript to a SQL server? 如何在浏览器中使用来自 JavaScript 的 SOCKS5 协议连接到服务器? - How to connect to server using SOCKS5 protocol from JavaScript in browser? 使用 Vue Axios 或使用 Express Server 连接 Microsoft Azure Machine Learning Studio API? - Connect Microsoft Azure Machine Learning Studio API using Vue Axios or using Express Server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM