node.js - 如何修复“ConnectionError:无法连接到:在 15000 毫秒内”错误?
问题描述
第一次为 Azure SQL Server 数据库创建 Node/Express api。看起来这只是延长超时限制的问题,但它也只是与数据库的连接,而不是实际的查询。这对于连接到 Azure 是否正常,延长超时实际上是一个问题,还是其他问题?
index.js
var express = require('express');
var router = express.Router();
const sql = require("../dboperation")
/* GET root route */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
// Test db connection
router.get('/testconnect', function(req, res, next) {
sql.getdata();
res.render('index', { title: 'Express' });
});
module.exports = router;
数据库操作.js
var config = require("./dbconfig")
const sql = require("mssql")
async function getdata(){
try {
let pool = await sql.connect(config)
console.log("SQL Server connnected...")
} catch(error) {
console.log("error: " + error)
}
}
module.exports = {
getdata: getdata,
}
dbconfig.js
const config = {
user : "username",
password : "password",
server : "server-name.database.windows.net",
database : "database-name",
options: {
trustedConnection: true,
enableArithAort: true,
encrypt: true
},
port: 49678
}
module.exports = config;
SQL Server 配置管理器:
解决方案
您可以尝试延长超时一次。
假设你已经设置了合适的环境变量,你可以如下构造一个配置对象:
const sql = require('mssql')
const sqlConfig = {
user: process.env.DB_USER,
password: process.env.DB_PWD,
database: process.env.DB_NAME,
server: 'localhost',
pool: {
max: 10,
min: 0,
idleTimeoutMillis: 30000
},
options: {
encrypt: true, // for azure
trustServerCertificate: false // change to true for local dev / self-signed certs
}
}
async () => {
try {
// make sure that any items are correctly URL encoded in the connection string
await sql.connect(sqlConfig)
const result = await sql.query`select * from mytable where id = ${value}`
console.dir(result)
} catch (err) {
// ... error checks
}
}
推荐阅读
- python - python如何获取Coordinated Universal Time 通用坐标时间?
- lua - Lua相当于dictionary.get()方法?
- c# - dllimport 从 c++ 到 c# 类型不匹配
- css - gotty 如何使用本地图像作为背景?
- javascript - FlatList scrollToOffset 触发多个滚动事件
- c - 将数组转换为可变数量的参数
- c# - 如何在属性类型也是类的类中实例化属性?
- reactjs - Material Table React - 自定义排序 - 定义不同的升序和降序算法
- html - 如何在独立移动项目时增加它所在的 div 的宽度或高度?
- azure - 使用 PowerShell 配置 Function App 私钥绑定