ER_ACCESS_DENIED_ERROR:拒绝访问用户‘’@‘LOCALHOST’(使用密码:no)

w8rqjzmb  于 2022-09-21  发布在  Node.js
关注(0)|答案(12)|浏览(321)

我有这个问题,我已经研究过了,我无法解决它,我想它与数据库权限有关,但我无法解决它:

if (error) throw error;
       ^

Error: ER_ACCESS_DENIED_ERROR: Access denied for user ''@'localhost' (using password: NO)
at Handshake.Sequence._packetToError (/home/carlos/www/express-cc/node_modules/mysql/lib/protocol/sequences/Sequence.js:52:14)
at Handshake.ErrorPacket (/home/carlos/www/express-cc/node_modules/mysql/lib/protocol/sequences/Handshake.js:103:18)
at Protocol._parsePacket (/home/carlos/www/express-cc/node_modules/mysql/lib/protocol/Protocol.js:279:23)
at Parser.write (/home/carlos/www/express-cc/node_modules/mysql/lib/protocol/Parser.js:76:12)
at Protocol.write (/home/carlos/www/express-cc/node_modules/mysql/lib/protocol/Protocol.js:39:16)
at Socket.<anonymous> (/home/carlos/www/express-cc/node_modules/mysql/lib/Connection.js:103:28)
at emitOne (events.js:115:13)
at Socket.emit (events.js:210:7)
at addChunk (_stream_readable.js:266:12)
at readableAddChunk (_stream_readable.js:253:11)
--------------------
at Protocol._enqueue (/home/carlos/www/express-cc/node_modules/mysql/lib/protocol/Protocol.js:145:48)
at Protocol.handshake (/home/carlos/www/express-cc/node_modules/mysql/lib/protocol/Protocol.js:52:23)
at Connection.connect (/home/carlos/www/express-
cc/node_modules/mysql/lib/Connection.js:130:18)
at Object.<anonymous> (/home/carlos/www/express-cc/db.js:10:12)
at Module._compile (module.js:573:30)
at Object.Module._extensions..js (module.js:584:10)
at Module.load (module.js:507:32)
at tryModuleLoad (module.js:470:12)
at Function.Module._load (module.js:462:3)
at Module.require (module.js:517:17)

-编辑-

我的env.Default文件:

NODE_ENV=DEVELOPMENT

DB_HOST=localhost
DB_USER=user
DB_PASSWORD=userpass
DB_NAME=teste

所以这是我的db.js文件:

var mysql = require('mysql')
  var connection = mysql.createConnection({
  host: process.env.DB_HOST,
  user: process.env.DB_USER,
  password: process.env.DB_PASSWORD,
  database : process.env.DB_NAME,
  sockertPath: '/var/run/mysqld/mysqld.sock'
})

 connection.connect()

 connection.query('SELECT 1 + 1 AS solution',
 function (error, results, fields) {
if (error) throw error;
console.log('The solution is: ', results[0].
  solution)
});

 module.exports = connection;
eit6fx6z

eit6fx6z1#

您可以执行以下操作:mysql-u根用户-p输入密码:

在这里创建用户,在这里创建数据库,然后在代码中使用这些详细信息。即通过以超级用户身份登录来创建用户。

2nc8po8w

2nc8po8w2#

我也遇到了类似的问题,我的所有连接都工作正常,但是我收到错误‘ER_ACCESS_DENIED_ERROR:用户’‘@’LOCALHOST‘的访问被拒绝(使用密码:no)’我的调用是在app.js级别的中间件内部进行的,所以.env值还无法访问,对于这种情况(它可以帮助那些和我有相同问题的人),我必须添加:

require('dotenv').config();

在Connecexion文件中,如下所示:

const mysql = require('mysql')
require('dotenv').config();
const connectionLog = mysql.createPool({
    connectionLimit : 10,
    host: process.env.DB_HOST_LOG,
    user: process.env.DB_USER,
    password: process.env.DB_PASSWORD,
    database: process.env.DB_DATABASE,
    port:process.env.DB_PORT
})

对我来说,这就是决心。

j13ufse2

j13ufse23#

您必须专门创建一个MySQL用户才能访问该应用程序。创建用户并设置密码(不要将密码保留为空)。别忘了给这个用户权限,才能访问APP的数据库和表格。我希望我能帮上忙。

vcirk6k6

vcirk6k64#

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '';

然后,在执行该命令后,通过执行以下命令刷新权限:

flush privileges;

dauxcl2d

dauxcl2d5#

您没有提供数据库用户:

Error: ER_ACCESS_DENIED_ERROR: Access denied for user ''@'localhost' (using password: NO)
hc2pp10m

hc2pp10m6#

您必须使用允许的主机部分的IP添加新用户,而不是使用名称“Localhost”

RENAME USER 'myuser'@'localhost' TO 'myuser'@'127.0.0.1';
csbfibhn

csbfibhn7#

通过使用%代替localhost解决了此问题:

CREATE USER 'user'@'%' IDENTIFIED WITH mysql_native_password BY 'password';
GRANT ALL PRIVILEGES ON db.* TO 'user'@'%';

而不是

CREATE USER 'user'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
GRANT ALL PRIVILEGES ON db.* TO 'user'@'localhost';
hec6srdp

hec6srdp8#

当您将变量移动到.env文件时,有时会遇到此错误。如果是那样的话

1.使用NPM或Yarn添加dotenv安装dotenv
1.使用Require(‘dotenv’).config()导入.env;

在此之后,您将能够读取‘USER’环境变量

juzqafwq

juzqafwq9#

导入db.js文件之前检查一次是否已导入environment variables文件**。

例.-

不正确:

require("db.js");

const dotenv = require('dotenv');  
dotenv.config({ path: 'env.default' });

更正:

const dotenv = require('dotenv');  
dotenv.config({ path: 'env.default' });

require("db.js");

因此,首先导入env文件,然后导入db.js文件。

rm5edbpk

rm5edbpk10#

在我的例子中,添加端口号可以解决问题->

host:'localhost',
port:3406,
database:'demo_db',
user:'u_name',
password:'u_pass'
00jrzges

00jrzges11#

如果您正在使用环境变量和dotenv库,请确保添加了以下要求(‘dotenv’).config();.....先检查一下就行了。

Const{createPool}=Required(‘MySQL’)Required(‘dotenv’).config();

const pool = createPool({
    port: process.env.DB_PORT,
    host: process.env.DB_HOST,
    user: process.env.DB_USERNAME,
    password: process.env.DB_PASSWORD,
    database: process.env.DB_DATABASE,
    waitForConnections: true,
    connectionLimit: 10,
    queueLimit: 0,
}).on("error", (err) => {
    console.log("Failed to connect to Database - ", err);
});

module.exports = pool;
chhqkbe1

chhqkbe112#

first you need to install env module and then create a root file .env mentioning the details of environment details:-

DB_NAME=yourdb_name DB_HOST=localhost DB_USER=root DB_PASSWORD= PORT=port_no in which your code is executing

相关问题