Node Js mysql(和 mysql2) ECONNRESET

Wil*_*ker 5 javascript mysql node.js mysql2

mysql我目前正在尝试使用 Node.Js或NPM 依赖项连接到互联网上的 MySQL 服务器,mysql2以使用查询和其他相关内容。

代码很简单...

//i import my dependency
const mysql = require('mysql2') //either 'mysql' or 'mysql2'

//i create my pool to create connections as needed
var conn = mysql.createPool({
    host: 'some_database_i_have_access_to.mysql.uhserver.com',
    user: 'valid_user',
    password: 'valid_password',
    database: 'some_existing_database'
})

//i try to connect (this is the part where it fails)
conn.getConnection((err,conn) => {
    if (err) throw err //<- the error is thrown here

    //i do query stuff
    conn.query("SELECT * FROM atable",(err,res,firlds) => {
        if(err) throw err
        console.log(JSON.stringify(res))
    })
    //i close the connection
    conn.end()
})
Run Code Online (Sandbox Code Playgroud)

但我总是收到这样的错误:

Error: read ECONNRESET
    at TCP.onStreamRead (internal/stream_base_commons.js:111:27)
    --------------------
    at Protocol._enqueue (C:\Users\Aluno\Desktop\my-project\node_modules\mysql\lib\protocol\Protocol.js:144:48)
    at Protocol.handshake (C:\Users\Aluno\Desktop\my-project\node_modules\mysql\lib\protocol\Protocol.js:51:23)
    at Connection.connect (C:\Users\Aluno\Desktop\my-project\node_modules\mysql\lib\Connection.js:118:18)
    at Object.<anonymous> (C:\Users\Aluno\Desktop\my-project\private\dtp-mysql.js:13:6)
    at Module._compile (internal/modules/cjs/loader.js:707:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:718:10)
    at Module.load (internal/modules/cjs/loader.js:605:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:544:12)
    at Function.Module._load (internal/modules/cjs/loader.js:536:3)
    at Module.require (internal/modules/cjs/loader.js:643:17)
Run Code Online (Sandbox Code Playgroud)

我对这个错误的了解是,连接在一侧突然关闭,如这个问题(Node js ECONNRESET)中所述,但仅此而已,创建单一连接也不能为我解决这个问题。

有什么解决方法吗?

tfr*_*ois 0

您好,我知道这是不久前被问过的问题,但这可能是因为您正在使用:

conn.end()

由于您使用的是池连接,我认为您可以使用释放连接

conn.release()
Run Code Online (Sandbox Code Playgroud)

或者

conn.destroy()
Run Code Online (Sandbox Code Playgroud)