“错误:接收到的数据包顺序错误."连接到无服务器 aurora 时 [英] "Error: Received packet in the wrong sequence." when connect to serverless aurora

查看:35
本文介绍了“错误:接收到的数据包顺序错误."连接到无服务器 aurora 时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现一个 Web 应用程序,它调用 lambda 函数从数据库中获取数据.
我选择了Serverless Aurora并写了一段代码,但是在查询方法中出现了异常Error: Received packet in the wrong sequence.".

I'm implementing a web application and it calls lambda function to get data from database.
I chose Serverless Aurora and wrote a code, but I get the exception "Error: Received packet in the wrong sequence." in query method.

我在谷歌上搜索了这个问题,但几乎所有问题都太旧了.
有文章说是bwisify的问题,但我没用.
我正在使用带有打字稿的无服务器框架.

I googled this issue but almost of all is too old.
An article said it is the problem of browisify but I don't use it.
I'm using serverless framework with typescript.

const mysql = require('serverless-mysql')({
  config: {
    host: process.env.DB_HOST,
    database: process.env.DB_NAME,
    user: process.env.DB_USER,
    password: process.env.DB_PASSWORD
  }
});

export async function query(sql: string, param?: Array<string>): Promise<any> {
  const results = await mysql.query(sql).catch(e => {
    console.log(e); // Error: Received packet in the wrong sequence
    throw new Error(e);
  });

  await mysql.end();
  return results;
}

以下也不起作用


export async function query(sql: string, param?: Array<string>): Promise<any> {
  const connQueryPromisified = util
    .promisify(connection.query)
    .bind(connection);
  const result = await connQueryPromisified(sql, param)
    .then(row => {
      console.log(row);
      return row;
    })
    .catch(err => {
      console.log(err); // getting Error: Received packet in the wrong sequence
      throw err;
    });
  return result;
}

我也尝试过使用 RDS DATA 服务,但在我所在的地区它不可用.

I also tried to use RDS DATA Service but in my region it is not available.

export async function query(sql: string, param?: Array<string>): Promise<any> {
 const params: aws.RDSDataService.Types.ExecuteSqlRequest = {
    awsSecretStoreArn: '***',
    dbClusterOrInstanceArn: '***',
    database: '***',
    schema: '***',
    sqlStatements: sql
  };
  console.log(params);
  try {
    const rdsService = new aws.RDSDataService({
      apiVersion: '2018-08-01',
      region: 'ap-northeast-1'
    });
    return rdsService
      .executeSql(params)
      .promise()
      .then(d => {
        return d;
      })
      .catch(e => {
        throw new Error(e);
      });
  } catch (err) {
    console.log(err); // nothing to say
    throw new Error(err);
  }
}

这是我的配置:
webpack.config.js

const path = require('path');
const slsw = require('serverless-webpack');

module.exports = {
  mode: slsw.lib.webpack.isLocal ? 'development' : 'production',
  entry: slsw.lib.entries,
  devtool: 'source-map',
  resolve: {
    extensions: ['.js', '.jsx', '.json', '.ts', '.tsx'],
  },
  output: {
    libraryTarget: 'commonjs',
    path: path.join(__dirname, '.webpack'),
    filename: '[name].js',
  },
  target: 'node',
  module: {
    rules: [
      // all files with a `.ts` or `.tsx` extension will be handled by `ts-loader`
      { test: /.tsx?$/, loader: 'ts-loader' },
    ],
  },
};

tsconfig.json

{
  "compilerOptions": {
    "lib": [
      "es2017"
    ],
    "moduleResolution": "node",
    "sourceMap": true,
    "target": "es2017",
    "outDir": "lib"
  },
  "exclude": [
    "node_modules"
  ]
}

我只想查询从 Serverless Aurora 获取记录.
有人可以帮我吗?

I want to query only getting records from Serverless Aurora.
Can anybody help me?

推荐答案

发生这种情况的原因是 Webpack(在生产模式下)将您的代码通过最小化器,而 mysql 模块serverless-mysql 正在使用与最小化不兼容.

The reason this is happening is because Webpack (in production mode) is putting your code through a minimiser, and the mysql module that serverless-mysql is using is not compatible with minimising.

您可以在此处查看问题:https://github.com/mysqljs/mysql/issues/1655.Node 模块依赖于函数名来构建代码,这是一个相当普遍的问题,因为丑化者/缩小者试图通过将函数名更改为单个字母来混淆/节省空间.

You can see the issue here: https://github.com/mysqljs/mysql/issues/1655. It's quite a common problem with Node modules which rely on function names to do code building, as uglyifiers/minifiers attempt to obfuscate/save space by changing the names of functions to single letters.

最简单的解决方法是通过添加以下内容来关闭 webpack 配置中的最小化:

The simplest fix would be to switch off minimising in your webpack config by adding:

  optimization: {
    minimize: false
  }

在链接的问题中有一些关于将各种其他最小化插件(如 terser)配置为 not 杂乱名称的讨论,这将使您在需要时获得最小化的一些好处.

There is some discussion in the linked issue on configuring various other minimising plugins (like terser) to not mangle names, which would allow you to get some of the benefit of minimising if you need it.

这篇关于“错误:接收到的数据包顺序错误."连接到无服务器 aurora 时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆