๐ข 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
'Devlog > Web' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[SQL] Basic SQL Statements (0) | 2022.03.02 |
---|---|
Netlify ๋ฐฐํฌ - ๋น๋ ์๋ฌ ํด๊ฒฐ๋ฐฉ๋ฒ (0) | 2022.02.28 |
[React] hover effect ์ด๋ฒคํธํจ์๊ฐ child element์ ์ ๋๋ก ์ ์ฉ์ด ์ ๋ ๋ ํด๊ฒฐ๋ฐฉ๋ฒ (2) | 2021.10.11 |
[React: CSS] CSS๋ ํจ์จ์ ์ผ๋ก ๊น๋ํ๊ฒ ์ ์ฉํ์โจ (0) | 2021.09.29 |
[React] useAsync ์กฐ๊ฑด๋ฌธ ์ฌ์ฉํ๊ธฐ (if๋ฌธ โ) (0) | 2021.08.27 |