我目前正在尝试使用 Node.Js 连接到互联网上的 MySQL 服务器mysql
or the mysql2
使用查询和其他相关内容的 NPM 依赖项。
代码很简单...
//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()
})
但我总是收到这样的错误:
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)
我对这个错误的了解是,连接在一侧突然关闭,如这个问题所述(Node.js 经济重置 https://stackoverflow.com/questions/17245881/node-js-econnreset),但仅此而已,创建单一连接也不能为我解决这个问题。
有什么解决方法吗?
您也可以参考以下网址。使用node.js向mysql插入大卷数据时出错(错误代码:'ECONNRESET') https://stackoverflow.com/questions/53586818/error-while-inserting-large-volume-data-in-mysql-by-using-node-js-error-code
我已经解决了这个问题。这是由默认定义max_allowed_packet引起的。在 my.ini (C:\ProgramData\MySQL\MySQL Server 5.7) 中找到 max_allowed_packet。更新为“max_allowed_packet=64M”。重新启动 mysql。完毕。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)