[英]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.