首页 > 解决方案 > 如何在建立连接时使用 knex npm 包获取 MySQL connectionId?

问题描述

let data = require('knex')({
    client: 'mysql',
    connection: {
        host: 'localhost',
        user: 'root',
        password: 'root',
        database: 'aaaaaa',
        charset: 'utf8'
    },
    pool: {
      min: 1,
      max: 20,
      destroyed: true
    },
})

这是我的连接请求。我如何找到这个的连接ID?

标签: node.js

解决方案


I have tried different ways.it's working fine.

  let data = require("knex")({
    client: "mysql",
    connection: {
      host: "localhost",
      user: "root",
      password: "root",
      database: "aaa",
      charset: "utf8",
    },
    pool: { min: 1, max: 20 },
  });
  let result = await data.raw("SELECT CONNECTION_ID() as connectionId");
  console.log("HERE IS THE PRIMARY CONNECTION ID", result[0][0]["connectionId"]);

if anyone having a different way please share. Without doing any query operation.

推荐阅读