RequestError:连接丢失-读取ECONNRESET

时间:2019-10-28 16:06:55

标签: node.js sql-server azure-sql-database knex.js objection.js

我刚将mssql部署到我的开发环境中时遇到错误,但是在本地,它仅对特定查询有效,无法正常工作

这是我的代码

 return Order.query()
.joinEager('[shipments.shipmentQuantities.lineItem, lineItems.shipmentQuantities]')
.findById(id);

使用带有版本10和反对版本1.4.0的nodejs

这是我的连接文件

database: {
  client: 'mssql',
  connection: {
    host: process.env.DB_HOST,
    user: process.env.DB_USER,
    password: process.env.DB_PASSWORD,
    database: process.env.DB_NAME,
    charset: 'utf8',
    stream: true,
       options: process.env.ENABLE_DB_SSL ? {
       port: 1433,
       database: process.env.DB_NAME,
       encrypt: true,
    } : undefined,
   },
    debug: false,
  },

错误详细信息

RequestError: Connection lost - read ECONNRESET 
at handleError (/src/node_modules/mssql/lib/tedious.js:519:15) 
at Connection.emit (events.js:203:15) 
at Connection.socketError (/src/node_modules/tedious/lib/connection.js:1028:14) 
at Socket.<anonymous> (/src/node_modules/tedious/lib/connection.js:878:18) 
at Socket.emit (events.js:203:15) 
at emitErrorNT (internal/streams/destroy.js:91:8) 
at emitErrorAndCloseNT (internal/streams/destroy.js:59:3) 
at process._tickCallback (internal/process/next_tick.js:63:19) 

对于初始数据库设置,我正在使用knex 这是初始代码

import body = require('koa-body');
import compress = require('koa-compress');
import Objection = require('objection');
import errorHandler = require('koa-json-logger-next');
import responseTime from 'koa-response-time-next/lib';
import config from './configuration';
import * as Koa from 'koa';
const Model = Objection.Model;

export default class Config {

   constructor(private app: Koa) {
   this.app = app;
 }

 public build() {
   // db setup
  const knex = require('knex')(config.database);
  Model.knex(knex);
  this.app.use((ctx: Koa.Context, next: () => Promise<any>) => {
  ctx.knex = knex;
  return next();
  });
  // return response time in X-Response-Time header
  this.app.use(responseTime());

  // HTTP compression
   this.app.use(compress({}));

  // parse request body into ctx.request.body
  this.app.use(body());

  // Global Error handling
  this.app.use(errorHandler({
  // As this is behind the api gateway, surface errors
  surfaceErrors: true,
  }));

  return config;
}

}

1 个答案:

答案 0 :(得分:0)

如果数据库实例也在azure上,您可以尝试以下配置吗?

knex({
  client : 'mssql',
  connection: {
    database: 'mydatabase',
    server: 'myserver.database.windows.net',
    user: 'myuser',
    password: 'mypass',
    port: 1433,
    connectionTimeout: 30000,
    options: {
      encrypt: true
    }
  }
});

如果数据库实例处于天蓝色,则必须将encrpt属性设置为true。

来源:https://github.com/knex/knex/wiki/Recipes