使用节点js创建数据库时出错

时间:2018-02-21 04:40:40

标签: javascript mysql node.js database

我是节点js的新手,我正在尝试创建一个数据库。 我从w3school复制代码并试图在我的电脑上运行。 我收到以下错误:

C:\Users\HP\Desktop>node db.js
C:\Users\HP\Desktop\db.js:10
  if (err) throw err;
           ^

Error: connect ECONNREFUSED 127.0.0.1:3306
    at Object._errnoException (util.js:999:13)
    at _exceptionWithHostPort (util.js:1020:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1207:14)
    --------------------
    at Protocol._enqueue (C:\Users\HP\node_modules\mysql\lib\protocol\Protocol.j
s: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\Desktop\db.js:9:5)
    at Module._compile (module.js:660:30)
    at Object.Module._extensions..js (module.js:671:10)
    at Module.load (module.js:573:32)
    at tryModuleLoad (module.js:513:12)
    at Function.Module._load (module.js:505:3)
    at Function.Module.runMain (module.js:701:10)

C:\Users\HP\Desktop>

db.js:

var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  user: "myusername",
  password: "mypassword"
});

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
  /*Create a database named "mydb":*/
  con.query("CREATE DATABASE mydb", function (err, result) {
    if (err) throw err;
    console.log("Database created");
  });
});

我对节点js完全不熟悉。

0 个答案:

没有答案