在执行queires之前,Node.Js的Mysql2是否需要在mysql.CreateConnection上运行connect()

在执行queires之前,Node.Js的Mysql2是否需要在mysql.CreateConnection上运行connect(),mysql,node.js,node-mysql2,Mysql,Node.js,Node Mysql2,我最近将node.js项目从mysqljs迁移到mysql2(),因为它有一些我需要的附加功能。对于我的数据库调用,我正在导出一个连接,该连接是通过运行mysql.createConnection({CONFIG})创建的,然后由其他文件导入,然后使用execute命令执行查询 在转换之前,在我设置数据库连接的文件中,我有以下内容: const mysql = require("mysql2"); const dbConfig = require("../config/db.config");

我最近将node.js项目从mysqljs迁移到mysql2(),因为它有一些我需要的附加功能。对于我的数据库调用,我正在导出一个连接,该连接是通过运行
mysql.createConnection({CONFIG})
创建的,然后由其他文件导入,然后使用execute命令执行查询

在转换之前,在我设置数据库连接的文件中,我有以下内容:

const mysql = require("mysql2");
const dbConfig = require("../config/db.config");

const connection = mysql.createConnection({
    host: dbConfig.HOST,
    user: dbConfig.USER,
    password: dbConfig.PASSWORD,
    database: dbConfig.DB,
    port: dbConfig.PORT
});

connection.connect(error => {
    if (error) throw error;
    console.log("DB connect")
});

module.exports = connection;

没有connection.connect函数,我的代码运行起来很有趣。删除它是否对性能有任何影响,或者mysql2是否保持开放连接,或者连接的创建是否可以忽略不计?在导出之前保留该行还是删除该行更好?

如果查看源代码,您将看到调用
createConnection
已经与DB进行了握手,并且
connect
方法仅用于传递在尝试创建连接时可能发生的任何错误。
createConnection
方法不会抛出或接受错误回调。因此,我建议在建立连接时继续呼叫它更安全