Develope Me!

[Node.js] Node.js + MySql 연동 중 발생한 에러 해결 (Client does not support authentication protocol requested by server; consider upgrading MySQL client) 본문

JavaScript/Node.js

[Node.js] Node.js + MySql 연동 중 발생한 에러 해결 (Client does not support authentication protocol requested by server; consider upgrading MySQL client)

코잘알지망생 2024. 3. 20. 19:15

문제 상황

 

server.js

// express 모듈 호출
const express = require("express");
const api = require("./routes/index");
const app = express();
const port = 3001;

// api 처리는 './routes/index'에서 일괄처리
app.use(api);

// server port 3001 할당
// 클라이언트와 다른 번호로 충돌나지 않도록
app.listen(port, () => {
  console.log(`Server run : http://localhost:${port}/`);
});

 

index.js

const express = require("express");
const router = express.Router();
var cors = require("cors");

router.use(cors());

const mysql = require("mysql");

const conn = mysql.createConnection({
  host: "localhost",
  port: "4000",
  user: "root",
  password: "비밀번호",
  database: "db명",
});

conn.connect();

router.get("/", (req, res) => {
  conn.query("SELECT * FROM gazi.user", function (err, results, fields) {
    if (err) throw err;
    res.send(results);
  });
});

module.exports = router;

 

Node.js 서버와 MySql 연동중 서버를 실행하자 에러가 발생했다.

 

에러 메시지

Client does not support authentication protocol requested by server; consider upgrading MySQL client

 

구글링을 해보니 node.js, mysql플러그인이 caching_sha2_password을 사용하지 못해서 생기는 오류로 판단되어진다.

그럴 경우에는 mysql2 플러그인을 사용하는 방법이 있다고 하는데 찾아보니 mysql-installer에서 설정을 바꿔주고 해결되는 경우도 있다고 한다!

 

 

문제 해결

 

mysql installer > server 옆에 reconfigure > Authentication Method > Use Legacy Authentication Method를 체크하고 설정을 바꿔주면 

db가 연동되고 정상적으로 select 데이터가 출력되는 것을 확인할 수 있다.

Comments