0
我试图插入一个基本形式到我的数据库,以了解如何节点的作品,但我不断收到一个有趣的小错误返回..Node.js的MySQL连接终止
error when connecting to db: { [Error: Connection lost: The server closed the connection.] fatal: true, code: 'PROTOCOL_CONNECTION_LOST' }
我有一个包装断开连接,但这似乎也没有帮助。
SELECT * FROM player
也不会在mysql
中返回任何内容,因此不会插入任何内容。
这里是我的server.js文件:(我省略,当然连接凭证)
var connection;
function handleDisconnect() {
connection = mysql.createConnection(db_config);
connection.connect(function(err) {
if(err) {
console.log('error when connecting to db:', err);
setTimeout(handleDisconnect, 2000);
}
});
connection.on('error', function(err) {
console.log('db error', err);
if(err.code === 'PROTOCOL_CONNECTION_LOST') {
handleDisconnect();
} else {
throw err;
}
});
}
handleDisconnect();
app.use(express.static(__dirname));
app.use(express.json());
app.use(express.urlencoded());
app.post("/post",function(req,res) {
console.log(JSON.stringify(req.body));
res.send("Username is: "+req.body.user+".");
var post = {
name:req.body.user,
password:"testing",
email:"[email protected]"
};
connection.query("INSERT INTO player VALUES ?",post,function(err,result) {
console.log(err);
console.log(result);
});
});
app.listen(80, function() {
console.log("Server running on port 80");
});
为什么我的连接被切断?
看起来它根本没有连接。你确定con.connect()被调用吗?你看到'重新连接丢失的连接:'在控制台中? – Hereblur
它不会触发,因为错误是致命的,你的回答jsut导致了一个重复的握手错误 –
更新我的问题到代码'node-mysql'建议但仍然是相同的问题 –