我有这个问题,我已经研究过,我无法解决它,我想它与数据库权限有关,但我无法修复它:
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;
答案 0 :(得分:1)
您没有提供数据库用户:
Error: ER_ACCESS_DENIED_ERROR: Access denied for user ''@'localhost' (using password: NO)
答案 1 :(得分:1)
您必须专门创建一个MySQL用户才能访问该应用。创建用户并设置密码(不要将密码保留为null)。不要忘记授予此用户权限,以便能够访问数据库和应用程序表。我希望我有所帮助。
答案 2 :(得分:1)
我遇到了类似的问题,我的所有连接都在工作,但是我收到错误“ER_ACCESS_DENIED_ERROR:用户 ''@'localhost' 访问被拒绝(使用密码:NO)'我的电话是在 app.js 级别的中间件内部制作,因此 .env 值尚无法访问,对于这种情况(它可以帮助那些和我有同样问题的人)我不得不补充:
require('dotenv').config();
在这样的连接文件中:
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
})
对我来说就是分辨率。
答案 3 :(得分:0)
您必须为新用户添加的IP地址必须是允许的主机部分的IP地址,而不是“ localhost”
RENAME USER 'myuser'@'localhost' TO 'myuser'@'127.0.0.1';
答案 4 :(得分:0)
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '';
然后在执行该命令后立即通过执行以下命令清除特权:
flush privileges;
答案 5 :(得分:0)
使用 %
代替 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';
答案 6 :(得分:-2)
您可以执行以下操作: mysql -u root -p 输入密码:
在此处创建用户和数据库,之后在代码中使用这些详细信息。 即以root用户身份登录。