【发布时间】:2014-04-05 14:32:10
【问题描述】:
我正在尝试将一个基本表单插入到我的数据库中以了解节点的工作原理,但我不断收到一个有趣的小错误。
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:"fake@email.com"
};
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()被调用了吗?您在控制台中看到Re-connecting lost connection:了吗? -
它没有触发,因为错误是致命的,而你的答案只是导致了重复的握手错误
-
将我的问题更新为代码
node-mysql建议但仍然是同样的问题 -
error when connecting to db:你确定 MySQL 服务器没问题吗?你的db_config是正确的吗?你的 mysql 服务器上有防火墙吗? -
原来我的
mysql用户名是 root 而不是我设置的。我没有正确sudo:9
标签: javascript mysql node.js express