每次我的应用程序接口在24小时内崩溃一次两次。
我看到这个错误:
ECONNRESET
C:\Users\Admin\Desktop\node-express 1.0.1\server.js:50
if (err) throw err;
^
Error: Cannot enqueue Query after fatal error.
at Protocol._validateEnqueue (C:\Users\Admin\Desktop\node-express 1.0.1\node_modules\mysql\lib\protocol\Protocol.js:212:16)
at Protocol._enqueue (C:\Users\Admin\Desktop\node-express 1.0.1\node_modules\mysql\lib\protocol\Protocol.js:138:13)
at Connection.query (C:\Users\Admin\Desktop\node-express 1.0.1\node_modules\mysql\lib\Connection.js:201:25)
at C:\Users\Admin\Desktop\node-express 1.0.1\server.js:49:11
at Layer.handle [as handle_request] (C:\Users\Admin\Desktop\node-express 1.0.1\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\Admin\Desktop\node-express 1.0.1\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\Admin\Desktop\node-express 1.0.1\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\Admin\Desktop\node-express 1.0.1\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\Admin\Desktop\node-express 1.0.1\node_modules\express\lib\router\index.js:281:22
at Function.process_params (C:\Users\Admin\Desktop\node-express 1.0.1\node_modules\express\lib\router\index.js:335:12) {
code: 'PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR',
fatal: false
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! express-api@1.0.0 start: `node server.js`
npm ERR!
npm ERR! Failed at the express-api@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\Admin\AppData\Roaming\npm-cache\_logs\2019-12-31T06_50_16_476Z-debug.log
PS C:\Users\Admin\Desktop\node-express 1.0.1> npm start
> express-api@1.0.0 start C:\Users\Admin\Desktop\node-express 1.0.1
> node server.js
我不知道如果这个状态出现在第50行会发生什么
if (err) throw err;
aladinModel = result;
res.json({ aladinModel })
});
} catch (error) {
console.log("Error query database!!!");
}
我所有的代码
// Create express app
var express = require("express")
var app = express()
var mysql = require('mysql')
var express = require("express")
var cors = require('cors')
app.use(cors())
// Server port
var HTTP_PORT = 8000
var con = mysql.createConnection({
host: "192.168.0.1",
port: "1234",
user: "username",
password: "pass"
});
var aladinModel = '';
var aladinModelStations = '';
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;
return [year, month, day].join('-');
}
var dateNow = formatDate(Date());
app.route('/')
.get(function (req, res) {
// omitted
res.setHeader('Access-Control-Allow-Origin', '*', 'Cache-Control', 'private, no-cache, no-store, must-revalidate');
//const date = req.query.date;
const id = req.query.id;
const daysForward = req.query.daysForward;
const query = `CALL aladin_surfex.Get_mod_cell_values_meteogram_cell('${dateNow}', ${id}, ${daysForward})`;
con.query(query, function (err, result, fields) {
if (err)
return res.status(500).json({ error: "Internal server error"})
aladinModel = result;
res.json({ aladinModel })
});
});
app.route('/stations')
.get(function (req, res) {
// omitted
res.setHeader('Access-Control-Allow-Origin', '*', 'Cache-Control', 'private, no-cache, no-store, must-revalidate');
const id2 = req.query.id2;
const query2 = `SELECT Station,Ime FROM aladin_surfex.stations_cells WHERE Station=${id2}`;
con.query(query2, function (err, result2, fields) {
if (err)
return res.status(500).json({ error: "Internal server error"})
res.json({ aladinModelStations })
});
});
// Start server
app.listen(HTTP_PORT, () => {
console.log("Server running on port %PORT%".replace("%PORT%", HTTP_PORT))
});
con.on('error', function(err) {
console.log(err.code); // 'ER_BAD_DB_ERROR'
});
app.use(function (req, res) {
res.status(404);
});
当这个人从第一条评论开始写我的时候,我更新了代码。其他地方会有错误吗?我去掉了航线上的试捕区。
我想我这样做是为了没有可能突破这个网站
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
遇到 ECONNRESET
错误通常意味着你的应用程序在尝试与数据库建立连接或执行查询时遇到了网络问题,或者数据库服务端主动关闭了连接。从错误信息来看,你使用的是Node.js和MySQL模块,错误发生在尝试向已经发生致命错误的连接中再次添加查询操作。
针对你的情况,这里有几个建议来解决这个问题:
处理连接错误:确保你的代码能够妥善处理数据库连接失败的情况。可以在创建连接时增加重连逻辑,例如使用mysql.createPool
而不是createConnection
,因为连接池能更好地管理连接生命周期,包括自动重连。
var mysql = require('mysql');
var pool = mysql.createPool({
host: "192.168.0.1",
port: "1234", // 确认这个端口是正确的MySQL端口,默认是3306
user: "username",
password: "pass",
connectionLimit: 10, // 连接池大小
queueLimit: 0, // 可选,队列长度限制
});
pool.getConnection(function(err, connection) {
if (err) {
console.error("Error getting MySQL connection:", err);
return;
}
// 使用connection进行查询...
connection.query( /* ... */ , function(err, results, fields) {
// 当查询完成,确保释放连接回池中
connection.release();
if (err) throw err;
// 处理结果...
});
});
异常捕获和重试机制:在执行查询的地方增加异常捕获,并根据错误类型决定是否重试。对于某些暂时性错误(如网络波动导致的连接中断),可以设计一个简单的重试逻辑。
日志记录:详细记录错误信息,特别是当遇到fatal error
时,应该记录下当时的上下文信息,这有助于后续分析问题原因。
检查数据库配置:确认数据库地址、端口、用户名和密码是否正确,以及数据库服务器是否正常运行且监听在指定端口上。
资源限制:检查数据库服务器是否有连接数限制,或者当前系统是否存在资源不足的问题(如内存、CPU等)。
超时设置:考虑为数据库查询设置合理的超时时间,避免长时间阻塞导致其他问题。
通过上述方法优化后,如果问题仍然存在,可能需要进一步查看数据库的日志文件,以获取更详细的错误信息,从而定位问题所在。