使用Nodejs的mysql模块连接MySql数据库出现以下错误

Posted xiandedanteng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Nodejs的mysql模块连接MySql数据库出现以下错误相关的知识,希望对你有一定的参考价值。

C:\Users\horn1\Desktop\node.js\39>node express.js
{ Error: Handshake inactivity timeout
    at Handshake.<anonymous> (C:\Users\horn1\Desktop\node.js\39\node_modules\mysql\lib\protocol\Protocol.js:164:17)
    at emitNone (events.js:105:13)
    at Handshake.emit (events.js:207:7)
    at Handshake._onTimeout (C:\Users\horn1\Desktop\node.js\39\node_modules\mysql\lib\protocol\sequences\Sequence.js:129:8)
    at ontimeout (timers.js:469:11)
    at tryOnTimeout (timers.js:304:5)
    at Timer.listOnTimeout (timers.js:264:5)
    --------------------
    at Protocol._enqueue (C:\Users\horn1\Desktop\node.js\39\node_modules\mysql\lib\protocol\Protocol.js:145:48)
    at Protocol.handshake (C:\Users\horn1\Desktop\node.js\39\node_modules\mysql\lib\protocol\Protocol.js:52:23)
    at PoolConnection.connect (C:\Users\horn1\Desktop\node.js\39\node_modules\mysql\lib\Connection.js:130:18)
    at Pool.getConnection (C:\Users\horn1\Desktop\node.js\39\node_modules\mysql\lib\Pool.js:48:16)
    at IncomingMessage.<anonymous> (C:\Users\horn1\Desktop\node.js\39\express.js:29:8)
    at emitOne (events.js:115:13)
    at IncomingMessage.emit (events.js:210:7)
    at IncomingMessage.Readable.read (_stream_readable.js:478:10)
    at flow (_stream_readable.js:849:34)
    at resume_ (_stream_readable.js:831:3)
  code: PROTOCOL_SEQUENCE_TIMEOUT,
  fatal: true,
  timeout: 10000 }

代码如下:

‘use strict‘;

var express=require(‘express‘);
var fs=require(‘fs‘);
var querystring=require(‘querystring‘);
var http=require(‘http‘);
var app=express();

var mysql=require(‘mysql‘);
var pool=mysql.createPool({
    host:‘192.168.80.129‘,// 数据库在本地虚拟机上,能用mysql-front连通
    port:‘3306‘,
    database:‘test‘,
    user:‘root‘,
    password:‘12345678‘,
        timeout:1000000000,
        connectTimeout:1000000000,
});

app.get(‘/index.html‘,function(req,res){
    res.writeHead(200,{‘Content-type‘:‘text/html‘});
    var file=fs.createReadStream(‘index.html‘);
    file.pipe(res);
});

app.post(‘/index.html‘,function(req,res){
    req.on(‘data‘,function(data){
        var obj=querystring.parse(data.toString());
        pool.getConnection(function(err,connection){
            if(err){
                res.send(‘Can not connect to MySql DB‘);
                console.log(err);
            }else{
                var str;
                connection.query(‘insert into emp set ?‘,{name:obj.name},function(err,result){
                    if(err){
                        str=‘Insert failed‘;
                    }else{
                        str="Insert succeed";
                    }

                    connection.release();
                    res.send(str);
                })
            }
        });
    });
});


app.listen(3333,"127.0.0.1");

 

以上是关于使用Nodejs的mysql模块连接MySql数据库出现以下错误的主要内容,如果未能解决你的问题,请参考以下文章

物联网服务NodeJs-5天学习第四天存储篇② ——NodeJs连接操作mysql 8.0

nodejs操作MySQL数据库

nodejs简单访问及操作mysql数据库的方法示例

如何在nodejs的单个文件中提供mysql数据库连接

nodejs连接池连接mysql

nodejs上使用sql