2016-04-28 90 views
0

我收到提示: 错误:错误:握手闲置超时到的NodeJS MySQL连接错误

+0

您可以发布在上下文中的代码的NodeJS? – 2016-04-28 06:20:46

+0

看到这个帖子:http://stackoverflow.com/questions/20210522/nodejs-mysql-error-connection-lost-the-server-closed-the-connection – Subburaj

+0

变化'主持人: “127.0.0.1”''到主机:“localhost”的' –

回答

0

使用连接池:

var mysql = require('mysql'); 
var pool = mysql.createPool({ 
    host  : 'example.org', 
    user  : 'bob', 
    password : 'secret' 
}); 

pool.getConnection(function(err, connection) { 
    // Use the connection 
    connection.query('SELECT something FROM sometable', function(err, rows) { 
    // And done with the connection. 
    connection.release(); 

    // Don't use the connection here, it has been returned to the pool. 
    }); 
}); 
+0

@ SubburajThanks..it works。 – Gayathri

+0

@Gayathri很高兴为您提供帮助..有一个快乐的编码.. – Subburaj

+0

但我不知道为什么我得到这个错误:握手不活动超时 – Gayathri

0

您可以使用node-mysql,它非常容易使用。我已经使用过一次,这里是例子: -

var mysql  = require('mysql'); 
var connection = mysql.createConnection({ 
    host  : 'localhost', 
    user  : 'me', 
    password : 'secret', 
    database : 'my_db' 
}); 

connection.connect(); 

connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields) { 
    if (err) throw err; 
    console.log('The solution is: ', rows[0].solution); 
}); 

connection.end(); 
+0

我已经安装了node-mysql模块。 – Gayathri

+0

粘贴你的连接代码,这样我们就可以看到.. – Bik

+0

我只用这个。谢谢你的回复。 – Gayathri