1

我正在尝试运行此 js 代码来访问 MySQL/MariaDB 服务器(此时,我都尝试了)。

const knex = require('../connection')
const mysql = require('mysql2');
 

function getDifficulty(){
    /*
    return knex.select('*').from('tb_dificuldades').limit(9)
        .then((results) => {
            console.log("GOT RESULTS!")
            return results
        })
    */
    // create the connection to database
    const connection = mysql.createConnection({
        host: 'localhost',
        user: 'root',
        password: '',
        database: ''
    })
    const results = connection.query(
        'SELECT * FROM tb_dificuldades;',
        function(err, results, fields) {
          console.log(results); // results contains rows returned by server
          console.log(fields); // fields contains extra meta data about results, if available
          return results
        }
    )
    return results
 
}

getDifficulty()

<knexfile.js>

require('dotenv').config()
const path = require('path')

module.exports = {
    development: {
        client: 'mysql2',
        version: '5.7',
        connection: {
            host: process.env.DB_HOST || 'localhost',
            user: process.env.DB_USER || 'root',
            password: process.env.DB_PASSWORD || '',
            database: process.env.DB_NAME || '',
        },
        migrations: {
            tableName: 'knex_migrations',
            directory: `${path.resolve(__dirname, 'src', 'database', 'migrations')}`
        },
        seeds: {
            directory: `${path.resolve(__dirname, 'src', 'database', 'seeds')}`
        }
    }
}

<连接.js>

require('dotenv').config()

const knexfile = require('../../knexfile')
const knex = require('knex')(knexfile[process.env.ENV || 'development'])

module.exports = knex

结果和字段被打印出来,但连接没有关闭,所以代码停止在回调函数上执行。

环境:

OS:Debian GNU/Linux 10 (buster)
D:Docker version 19.03.13
DC:docker-compose version 1.21.0
DB:MariaDB & MySQL (docker)
IP:localhost(docker-proxy); db(docker-dns)
CLIENT: mysql & mysql2 (nodejs); knexjs

我已经尝试过从 mysql 更改为 mariadb;
本来我是用带mysql的knex,然后用mysql2,然后用mysql2不带knex;
我正在通过 localhost (docker-proxy) 使用 DBeaver,并且相同的查询运行良好;
Knex 迁移和种子也在起作用;

预期的:

[...]
 columnType: 3,
    flags: 4097,
    decimals: 0
  }
]

[Done] exited with code=0 in 0.305 seconds

得到:

[...]
 columnType: 3,
    flags: 4097,
    decimals: 0
  }
]

[Done] exited with code=null in 18.594 seconds

OBS: exited after aborting process, also this query shouldn't take 18s
and it isn't taking that long.
It does return what I expect, but the client holds the connection.
So, my program freezes after running the query. (Not happening in DBeaver)

EDIT1:使用 knex.destroy() 程序可以正常工作,但这是预期的用法吗?

return knex.select('*').from('tb_dificuldades').limit(9)
        .then((results) => {
            console.log("GOT RESULTS!")
            console.log(results)
            knex.destroy()
            return results
        })
4

1 回答 1

1

每次查询后您不需要关闭连接,只需在应用程序的其他部分重用它即可。如果您确定需要关闭连接,只需手动关闭它。

例如,使用 Knex,您可以这样做

knex.destroy();

例子

文件

于 2020-10-08T10:21:21.303 回答