sql node.js错误10061

时间:2017-11-23 19:01:23

标签: mysql sql node.js

我正在尝试使用node.js创建我的第一个数据库但是我无法连接到服务器。它给了我错误1061.我是创建数据库的新手,我不知道我的错误在哪里。可以有人帮帮我? 谢谢!
这是我的代码:

var mysql = require('mysql');

var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "mydb"
});

 con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
con.query("SELECT * FROM customers", function (err, result, fields) {
    if (err) throw err;
    console.log(result);
  });
  });


const http = require('http');
var qs = require('querystring');
var url = require('url');
const server = http.createServer();
server.on('request', (request, response) => {
 var url_parts = url.parse(request.url, true);
 var query = url_parts.query;
 console.log(query);
 var body = '';
  response.setHeader("Access-Control-Allow-Origin", "*");
   request.on('data', (chunk) => {
   body += chunk;
  }).on('end', () => {
    var post = qs.parse(body);
      console.log(post);
      if(query.command == "register") {
          con.query("INSERT INTO customers (name, email, password) 
 VALUES(?,?,?)", [post.name, post.email, post.password], function (err, result, fields) {
            if (err) throw err;
            console.log(result);
          });
  }
  response.statusCode = 200;
  response.end();
  });  
  });
server.listen(8080);

以下是控制台输出:

    C:\Users\HP>node server.js
    C:\Users\HP\server.js:11
    if (err) throw err;
         ^

   Error: connect ECONNREFUSED 127.0.0.1:3306
at Object._errnoException (util.js:1031:13)
at _exceptionWithHostPort (util.js:1052:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1195:14)
--------------------
at Protocol._enqueue (C:\Users\HP\node_modules\mysql\lib\protocol\Protocol.js:145:48)
at Protocol.handshake (C:\Users\HP\node_modules\mysql\lib\protocol\Protocol.js:52:23)
at Connection.connect (C:\Users\HP\node_modules\mysql\lib\Connection.js:130:18)
at Object.<anonymous> (C:\Users\HP\server.js:10:5)
at Module._compile (module.js:641:30)
at Object.Module._extensions..js (module.js:652:10)
at Module.load (module.js:560:32)
at tryModuleLoad (module.js:503:12)
at Function.Module._load (module.js:495:3)
at Function.Module.runMain (module.js:682:10)

1 个答案:

答案 0 :(得分:0)

此错误表示node.js无法连接到您的MySQL服务器。 很可能你的MySQL服务器根本没有启动。