添加 .env 变量后,为什么我收到“用户 \'root\'@\'localhost\' 的访问被拒绝(使用密码:NO)”?

添加 .env 变量后,为什么我收到“用户 \'root\'@\'localhost\' 的访问被拒绝(使用密码:NO)”?

我在 centos 8 上将 mariadb 与 nodejs 一起使用。每当我将 process.env 变量添加到 mysql.createConnection 函数时,我都会收到错误,Access denied for user \'root\'@\'localhost\' (using password: NO)但在我替换类似host: localhostwith 的代码之前它就工作了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,因为我犯了这个愚蠢的错误而浪费了很多时间。

相关内容