저는 centos 8에서 nodejs와 함께 mariadb를 사용하고 있습니다. mysql.createConnection 함수에 process.env 변수를 추가할 때마다 오류가 발생 하지만 Access denied for user \'root\'@\'localhost\' (using password: NO)
코드를 그런 것으로 교체하기 host: localhost
전까지는 작동했습니다 host: process.env.DATABASE_HOST
.
.env 변수를 사용하기 전에 작동했던 코드:
const express = require('express');
const mysql = require("mysql");
const dotenv = require('dotenv');
dotenv.config({ path: './.env'});
const app = express();
const db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'DBPASSWORD',
database: 'nodejs-login'
});
db.connect( (error) => {
if(error) {
console.log(error)
} else {
console.log("MySQL Connected...")
}
})
app.get("/", (req, res) => {
res.send("<h1>Home page</h1>")
});
app.listen(3000, () => {
console.log("server started on Port 3000");
})
단말기:
server started on Port 3000
MySQL Connected...
.env 변수를 추가한 후:
const express = require('express');
const mysql = require("mysql");
const dotenv = require('dotenv');
dotenv.config({ path: './.env'});
const app = express();
const db = mysql.createConnection({
host: process.env.DATABASE_HOST,
user: process.env.DATABASE_USER,
password: process.env.DATABASE_PASSWORD,
database: process.env.DATABASE
});
db.connect( (error) => {
if(error) {
console.log(error)
} else {
console.log("MySQL Connected...")
}
})
app.get("/", (req, res) => {
res.send("<h1>Home page</h1>")
});
app.listen(3000, () => {
console.log("server started on Port 3000");
})
내 시스템 및 프로젝트 파일의 프로젝트 위치:
$ pwd /var/www/node
$ ls -a . .. app.js .env node_modules package.json package-lock.json
.env 파일 내용:
DATABASE = nodejs-login
DATABASE_HOST = localhost
DATABASE_USER = root
DATABASE_PASSSWORD = "DBPASSWORD"
단말기:
server started on Port 3000
{ Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'root'@'localhost' (using password: NO)
at Handshake.Sequence._packetToError (/var/www/node/node_modules/mysql/lib/protocol/sequences/Sequence.js:47:14)
at Handshake.ErrorPacket (/var/www/node/node_modules/mysql/lib/protocol/sequences/Handshake.js:123:18)
at Protocol._parsePacket (/var/www/node/node_modules/mysql/lib/protocol/Protocol.js:291:23)
at Parser._parsePacket (/var/www/node/node_modules/mysql/lib/protocol/Parser.js:433:10)
at Parser.write (/var/www/node/node_modules/mysql/lib/protocol/Parser.js:43:10)
at Protocol.write (/var/www/node/node_modules/mysql/lib/protocol/Protocol.js:38:16)
at Socket.<anonymous> (/var/www/node/node_modules/mysql/lib/Connection.js:88:28)
at Socket.<anonymous> (/var/www/node/node_modules/mysql/lib/Connection.js:526:10)
at Socket.emit (events.js:198:13)
at addChunk (_stream_readable.js:288:12)
--------------------
at Protocol._enqueue (/var/www/node/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Protocol.handshake (/var/www/node/node_modules/mysql/lib/protocol/Protocol.js:51:23)
at Connection.connect (/var/www/node/node_modules/mysql/lib/Connection.js:116:18)
at Object.<anonymous> (/var/www/node/app.js:16:4)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
code: 'ER_ACCESS_DENIED_ERROR',
errno: 1045,
sqlMessage:
'Access denied for user \'root\'@\'localhost\' (using password: NO)',
sqlState: '28000',
fatal: true }
답변1
.env 파일에 "S" 세 개로 DATABASE_PASSSWORD를 썼는데 이런 어리석은 실수를 해서 많은 시간을 낭비했습니다.