使用节点js连接到My Sql会引发错误“连接ECONNREFUSED 127.0.0.1:3306”

时间:2019-03-23 07:26:15

标签: mysql node.js

这是什么问题?这是我过去两天卡住的代码,我几乎阅读了所有文章,却找不到解决方案。

    const express = require("express");
    const bodyParser = require("body-parser");
    const passport = require("passport");
    const mysql = require("mysql");
    //const users = require("./routes/user");

    const app = express();
    //app.use(passport.initialize());
    //require("./passport")(passport);

    const db = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "",
    database: "users"
    });

db.connect(function(err) {
  if (err) {
    enter code herereturn console.error("error: " + err.message);
  }

  console.log("Connected to the MySQL server.");
});
global.db = db;

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

//app.use("/api/users", users);

app.get("/", function(req, res) {
  res.send("hello");
});

const PORT = process.env.PORT || 5000;

app.listen(PORT, () => {
  console.log(`Server is running on PORT ${PORT}`);
});

1 个答案:

答案 0 :(得分:0)

您需要在sql控制台中运行它:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY ''

或者如果您有密码,也可以这样:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password'

选中此link以获得更多信息。