繁体   English   中英

Node.js Mysql连接终止

[英]Node.js Mysql Connection Terminated

我正在尝试向数据库中插入基本表单以了解节点的工作原理,但我不断收到一个有趣的小错误。

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();

app.listen(80...应该解决此问题之前。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM