2016-04-28 163 views
-2

我想要的Node.js和MySQL,但即時收到此錯誤:服務器關閉了連接

:Error: Connection lost: The server closed the connection. my code here.

var mysql = require('mysql'); 
var config = { 
    connectionLimit: 100, 
    waitForConnections: true, 
    queueLimit: 0, 
    debug: true, 
    wait_timeout: 28800, 
    connect_timeout: 10, 
    host: "localhost", 
    user: " root ", 
    password: " root ", 
    database: "mydb" 
}; 
var table = 'mytable'; 
var connection; 

function handleConnection() { 
    connection = mysql.createConnection(config); 
    connection.connect(function(err) { 
     console.log("connected"); 
    }); 
    connection.query('select * from' + table, 
     function(err, result, fields) { 
      if (!err) { 
       console.log("result :" + result); 
      } else { 
       console.log("error1 :" + err); 
      } 
     }); 
} 
+0

請縮進代碼。 –

+0

什麼是wait_timeout ..? 以及爲什麼你沒有寫connectTimeout而不是connect_timeout ..? – vkstack

回答

0

在你的代碼行

connection.query('select * from' + table,

將解析如下:

select * frommytable

由於沒有spac e在'from'關鍵字之後。嘗試在'from'之後添加空格字符。

而且在配置,你之前和之後的用戶名和密碼的值增加一個空格字符:

user : " root ", password : " root ",

+0

將該行更改爲:connection.query('select * from mytable',..仍然即時接收該錯誤 – Gayathri

+0

您的用戶名和密碼字段在前後都有空格,是否正確?更新我的答案以反映此情況。 –

+0

最後我發現它..我的XAMPP沒有正確安裝。 – Gayathri

相關問題