![](/img/trans.png)
[英]Error: read ECONNRESET when connected to a mysql server with Node.js
[英]Node.js mysql read ECONNRESET
在搜索了關於這個錯誤的很長時間之后,並沒有真正弄清楚如何在我的項目中處理這個錯誤。 我需要你的幫助:)。
我遇到了問題,我的屏幕和我的 Node-APP 會在幾個小時后給我一個錯誤(我不知道幾個小時后,但我的感覺告訴我它並不總是相同的時間)
我會在下面給你我的錯誤。 有誰知道如何解決這一問題? 我已經嘗試過間隔 select 從一個短表到重置與數據庫的連接,因此連接不會關閉,因為總會(每 30 分鍾)對數據庫進行一次短調用。
同樣在我收到錯誤並重新啟動應用程序后一切正常。 我看到它是一個空閑時間問題,但我沒有找到任何方法來解決這個問題。 這就是我創建與數據庫的連接的方式。
var con = mysql.createConnection({
host: process.env.DATABASE_HOST,
user: process.env.DATABASE_USER,
password: process.env.DATABASE_PASSWORD,
database: process.env.DATABASE });
con.connect(function(err) {
if (err) throw err;
console.log("Connected to Database!");
});
非常感謝您!
最誠摯的問候
錯誤:
node:events:371
throw er; // Unhandled 'error' event
^
Error: read ECONNRESET
at TCP.onStreamRead (node:internal/stream_base_commons:211:20)
Emitted 'error' event on Connection instance at:
at Connection._handleProtocolError (/var/www/xxx/xxx/node_modules/mysql/lib/Connection.js:423:8)
at Protocol.emit (node:events:394:28)
at Protocol._delegateError (/var/www/xxx/xxx/node_modules/mysql/lib/protocol/Protocol.js:398:10)
at Protocol.handleNetworkError (/var/www/xxx/xxx/node_modules/mysql/lib/protocol/Protocol.js:371:10)
at Connection._handleNetworkError (/var/www/xxx/xxx/node_modules/mysql/lib/Connection.js:418:18)
at Socket.emit (node:events:394:28)
at emitErrorNT (node:internal/streams/destroy:157:8)
at emitErrorCloseNT (node:internal/streams/destroy:122:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
errno: -104,
code: 'ECONNRESET',
syscall: 'read',
fatal: true
}
再次感謝您:!! :)
我認為這是因為多數據庫連接
使用createPool
代替createConnection
和 defile connectionLimit,acquireTimeout 毫秒,以便在此時間后自動關閉連接
var mysql = require('mysql');
var pool = mysql.createPool({
connectionLimit : 10,
acquireTimeout : 10000
host : 'example.org',
user : 'bob',
password : 'secret',
database : 'my_db'
});
pool.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
if (error) throw error;
console.log('The solution is: ', results[0].solution);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.