Node.js mySQL无法创建连接

时间:2017-04-10 09:31:09

标签: mysql node.js

我刚刚开始,所以我对上述两种情况都不太了解。 代码:

var mysql = require("mysql");
var express = require('express');
var app = express();


// First you need to create a connection to the db
var con = mysql.createConnection({
host: 'localhost',
user: 'root',
password: ''
});

con.connect(function(err){
 if(err){
  console.log('Error connecting to Db');
  console.log(err);
   return;
}
console.log('Connection established');
});

con.end(function(err) {
// The connection is terminated gracefully

尝试使用node.js运行它,但它说:

 Error connecting to Db
{ Error: connect ECONNREFUSED 127.0.0.1:3306
    at Object.exports._errnoException (util.js:1018:11)
    at exports._exceptionWithHostPort (util.js:1041:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1090:14)
--------------------
    at Protocol._enqueue (C:\Users\yeohc\Desktop\Newfolder\node_modules\mysql\lib\protocol\Protocol.js:141:48)
   at Protocol.handshake (C:\Users\yeohc\Desktop\Newfolder\node_modules\mysql\lib\protocol\Protocol.js:52:41)
    at Connection.connect     (C:\Users\yeohc\Desktop\Newfolder\node_modules\mysql\lib\Connection.js:130:18)
    at Object.<anonymous> (C:\Users\yeohc\Desktop\Newfolder\app.js:13:5)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
  code: 'ECONNREFUSED',
  errno: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 3306,
 fatal: true }

请帮帮我,请记住我刚刚开始编程。在此之前感谢您的帮助

1 个答案:

答案 0 :(得分:0)

我可以在我的机器上连接您的代码。

在您的方案中,请检查以下情况

  

1-确保您的a[i]

     

2-检查密码是否必要?