提问者:小点点

连接到My Sql with node js抛出错误"connect ECONNREFUSED127.0.0.1:3306"


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

    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个答案

匿名用户

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

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

或者如果您有密码:

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

查看此链接以获取有关它的更多信息。