简体   繁体   English

MySQL 数据库连接的 Node.JS 错误:使用命令“NODE INDEX.JS”运行服务器时,终端给我两个错误

[英]Node.JS Error with MySQL Database Connection: Terminal is giving me two errors when running my server using the command "NODE INDEX.JS"

I am building a beginner's CRUD application, using JS, Express, CORS, and MYSQL.我正在构建初学者的 CRUD 应用程序,使用 JS、Express、CORS 和 MYSQL。

What does the application do?应用程序做什么? (skip this part if you want) (如果你想跳过这部分)

It handles the task of adding employees by their name, age, country, position, and wage and automatically having them inserted into a MySql database.它处理按姓名、年龄、国家、position 和工资添加员工的任务,并自动将他们插入到 MySql 数据库中。

The way you know if your employee has been added to the system is if you see a "success" statement logged on the console, after you click the "addEmployee" button.您知道您的员工是否已添加到系统的方式是,在您单击“添加员工”按钮后,您是否看到控制台上记录了“成功”语句。

However in my case, I do not see the "success" statement on the console, instead I am met with two errors on my terminal on VsCode:但是,就我而言,我没有在控制台上看到“成功”语句,而是在 VsCode 的终端上遇到了两个错误:

The errors I am experiencing.我遇到的错误。

ERROR #1错误#1

code: 'ER_NOT_SUPPORTED_AUTH_MODE', errno: 1251, sqlMessage: 'Client does not support authentication protocol requested by server; consider upgrading MySQL client', sqlState: '08004', fatal: true

ERROR #2错误#2

code: 'PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR', fatal: false

For Reference Here's my Code:供参考这是我的代码:

----SERVER ---- - - 服务器 - -

Index.js:索引.js:


const express = require("express");
const app = express();
const mysql = require("mysql");
const cors = require("cors");


app.use(cors());
app.use(express.json());

//Connecting to SQL
const db = mysql.createConnection({
    user: "root",
    host: "localhost",
    password:"password",
    database: "employeeSystem",
});


app.post("/create", (req, res) => {
    const name = req.body.name;
    const age = req.body.age;
    const country = req.body.country;
    const position = req.body.position;
    const wage = req.body.wage;
  

    db.query(
      "INSERT INTO employees (name, age, country, position, wage) VALUES (?,?,?,?,?)",
      [name, age, country, position, wage],
      (err, result) => {
          
    
        if (err) {
          console.log(err);
        } else {
          res.send("Values Inserted");
        }
      }
    );
  });


app.listen(3001, ()=> 
console.log("You're server is running on port 3001")
)

----CLIENT---- - - 客户 - -

App.js:应用程序.js:


import "./App.css";
import {useState} from "react"; 
import Axios from "axios";

function App() {
  const [name, setName] = useState("");
  const [age, setAge]= useState(0);
  const [country, setCountry]= useState("");
  const [position, setPosition]= useState("");
  const [wage, setWage] = useState(0);

  const addEmployee = () => {
    Axios.post("http://localhost:3001/create", {
      name: name,
      age: age,
      country: country,
      position: position,
      wage: wage,

  })
  .then(()=> {
    console.log(name, age,country, position, wage);
  })
}
  return (
    <div className="information">
      <div>
      <label>Name:</label> 
      <input 
        type="text"
        onChange={(event)=> {
          setName(event.target.value);
        }}
      />
      <label>Age:</label> 
      <input 
      type="number"
      onChange={(event)=> {
        setAge(event.target.value);
      }}
      ></input>
      <label>Country:</label> 
      <input 
      type="text"
      onChange={(event)=> {
          setCountry(event.target.value);
        }}
      />
      <label>Position:</label> 
      <input 
      type="text"
      onChange={(event)=> {
          setPosition(event.target.value);
        }}
      />
      <label>Wage:</label> 
      <input 
      type="number"
     onChange={(event)=> {
  setWage(event.target.value);
    }}
    />
    <button onClick={addEmployee}> Add Employees</button>
       </div>    
       </div>
       
  );
}

export default App;


Thank You!谢谢你!

Try creating a new user for your application on MySQL, instead of using the root user.尝试在 MySQL 上为您的应用程序创建一个新用户,而不是使用root用户。 In modern systems root is setup to automatically log you in if you use sudo mysql and without a password.在现代系统中,如果您使用sudo mysql并且没有密码, root设置为自动登录。 This is very helpful, for other cases where you just want your program to login with a password you get this unhelpful message.这非常有用,对于其他情况,您只是希望您的程序使用密码登录,您会收到此无用的消息。

The easiest solution is to just create more mysql users for these cases.最简单的解决方案是为这些情况创建更多 mysql 用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM