2016-04-28 89 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