Devlog/Web

[Express+MySQL] DB ์—ฐ๊ฒฐ ์‹œ ๋ฐœ์ƒํ•˜๋Š” access error ํ•ด๊ฒฐ๋ฐฉ๋ฒ•

FATKITTY 2021. 11. 23. 18:09
๋ฐ˜์‘ํ˜•

๐Ÿ’ข Access denied for user ''@'localhost' (using password: NO)

 

โ–ช๋ฌธ์ œ์ƒํ™ฉ : 

mysql ์—ฐ๊ฒฐ ๊ฐ์ฒด ๋ชจ๋“ˆํ™”, dotenv ์„ค์น˜ ํ›„ ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์„ค์ •๊นŒ์ง€ ๋ชจ๋‘ ์™„๋ฃŒํ–ˆ์ง€๋งŒ,

์—ฐ๊ฒฐ ํ™•์ธ์„ ํ•ด๋ณด๋ ค๊ณ  ํ•˜๋ฉด ์ €๋Ÿฐ ์—๋Ÿฌ๊ฐ€ ๋–ด๋‹ค.

Error: ER_ACCESS_DENIED_ERROR: Access denied for user ''@'localhost' (using password: NO)
...
code: 'ER_ACCESS_DENIED_ERROR',
errno: 1045,
sqlMessage: "Access denied for user ''@'localhost' (using password: NO)",
sqlState: '28000',
fatal: true

root๊ฐ€ ๋“ค์–ด๊ฐ€์•ผํ•  ์ž๋ฆฌ๊ฐ€ ๋น„์–ด์žˆ๊ณ , using password: NO๋ผ๊ณ  ๋œจ๋Š”๊ฑธ ๋ณด๋‹ˆ ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๋Œ€๋กœ ๋ชป ์ฝ์–ด์˜ค๋Š” ๊ฒƒ์ด ๋ถ„๋ช…ํ–ˆ๋‹ค.

 

๐Ÿ”ฝ ๋‹น์‹œ์˜ ์ฝ”๋“œ

const mysql = require('mysql');
require('dotenv').config();

const mysqlConnection = {
    init: function() {
        return mysql.createConnection({
            host : process.env.DB_HOST,
            user : process.env.DB_USER,
            password : process.env.DB_PASSWORD,
            database : process.env.DB_DATABASE
        });
    },
    open: function(con) {
        con.connect(err => {
            if (err) {
                console.log('MySQL connection error : ', err);
            } else {
                console.log('MySQL connected!');
            }
        });
    },
    close: function(con) {
        con.end(err => {
            if (err) {
                console.log("MySQL termination error : ", err);
            } else {
                console.log("MySQL terminated...");
            }
        });
    }
}

module.exports = mysqlConnection;

 

โ–ช์›์ธ : dotenv์˜ config()๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ถ€๋ถ„์—์„œ .env ํŒŒ์ผ ์œ„์น˜๊ฐ€ ์ž˜๋ชป๋จ

์ƒ๊ฐ๋ณด๋‹ค ๋„ˆ๋ฌด ๋‹จ์ˆœํ–ˆ๋˜ ๋ฌธ์ œ์ธ๋ฐ,, ์‚ฝ์งˆ์„ ๋„ˆ๋ฌด ๋งŽ์ด ํ–ˆ๋‹ค ใ…œใ…œ

 

โ–ชํ•ด๊ฒฐ๋ฐฉ๋ฒ•

config()๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ํŒŒ์ผ๊ณผ ๊ฐ™์€ ๋””๋ ‰ํ† ๋ฆฌ์— ์œ„์น˜ํ•˜๋„๋ก .env์˜ ๊ฒฝ๋กœ๋ฅผ ์ˆ˜์ •ํ•ด์ค€๋‹ค. (๊ถŒ์žฅ๋ฐฉ๋ฒ•)

or

.env์˜ ์ƒ๋Œ€์ฃผ์†Œ๋ฅผ ์ •์˜ํ•ด์ค€๋‹ค. (์ฐธ๊ณ )

require('dotenv').config({ path: '../.env' });

 

 

 

์ฐธ๊ณ ์ž๋ฃŒ

https://velog.io/@serious_yeon/SQL-28000-Access-denied-for-user-localhost

๋ฐ˜์‘ํ˜•