javascript 需要帮助使用 node.js 访问 mysql 数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13437066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 18:46:32  来源:igfitidea点击:

Need assistance with accessing mysql database using node.js

javascriptmysqlnode.js

提问by anonymous123

I am trying to run a simple node sample for access a mysql db and am getting the following error-Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'root'@'ubuntu.local' (using password: YES)

我正在尝试运行一个简单的节点示例来访问 mysql 数据库,但出现以下错误-错误:ER_ACCESS_DENIED_ERROR: Access denied for user 'root'@'ubuntu.local'(使用密码:YES)

var mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : '192.168.0.12',
  user     : 'root',
  password : 'password',
  database : 'app'
});

connection.connect();

connection.query('SELECT * from users', function(err, rows, fields) {
  if (err) throw err;

  console.log('The solution is: ', rows[0]);
});

connection.end();

The above is the code for accessing the mysql using node.js.Any suggestions as to what I may be doing wrong, thanks in advance.

以上是使用 node.js 访问 mysql 的代码。关于我可能做错了什么的任何建议,提前致谢。

回答by irm518

I had a similar problem where I was getting the same error.

我有一个类似的问题,我遇到了同样的错误。

The answer for me ended up being that I was using a different port. By default, mysql expects a port of 3306, but I was using MAMP, where the MySQL server port was specified as 8889. As soon as I added a port definition to my database connection, it was fine:

我的答案最终是我使用了不同的端口。默认情况下,mysql 期望端口为 3306,但我使用的是 MAMP,其中 MySQL 服务器端口被指定为 8889。一旦我将端口定义添加到我的数据库连接中,就可以了:

var connection = mysql.createConnection({
    port: 8889,
    user: 'root',
    password : 'password',
    database : 'my_database'
});

NOTE: This is for Mac using MAMP, haven't tried it on ubuntu or Windows.

注意:这适用于使用 MAMP 的 Mac,尚未在 ubuntu 或 Windows 上尝试过。

回答by NullPointer

I was getting the exact same issue. I was not able to connect via the mysql module in nodejs, but was able to connect via the mysql client in the terminal (mysql -u root -p1111 -h localhost mydb)

我遇到了完全相同的问题。我无法通过 nodejs 中的 mysql 模块进行连接,但能够通过终端中的 mysql 客户端进行连接(mysql -u root -p1111 -h localhost mydb)

It turns out, the mysql module converts "localhost" to 127.0.0.1. This would not work via the (mysql -u root -p1111 -h 127.0.0.1 mydb). See Issue 734

事实证明,mysql 模块将“localhost”转换为 127.0.0.1。这不能通过 (mysql -u root -p1111 -h 127.0.0.1 mydb) 工作。见第 734 期

So I had to set a password for 127.0.0.1 in MySQL and it solved the problem.

所以我不得不在 MySQL 中为 127.0.0.1 设置一个密码,它解决了问题。

Instructions

指示

Connect to mysql via the console:

通过控制台连接到mysql:

mysql -u root -p mysql

And Run

并运行

SET PASSWORD FOR 'root'@'127.0.0.1' = PASSWORD('Add-Your_password-here');

Source

来源

回答by PsyKoPeeR

I've been on this problem for a long time now and among all forums I did, one simple solution resolve my issue. My password was with some special character. I change it to a simpler one like "onlyletters" and it works perfectly. Probably a encoding problem in the nodejs mysql module, I can... Hope it would help some of you guys...

我已经解决这个问题很长时间了,在我做过的所有论坛中,一个简单的解决方案解决了我的问题。我的密码带有一些特殊字符。我把它改成更简单的,比如“onlyletters”,它工作得很好。可能是nodejs mysql模块中的编码问题,我可以......希望它会帮助你们中的一些人......

回答by engcmreng

I had a similar problem,my nodejs project.I was using knex.js for querybuilder and it's configuration.

我有一个类似的问题,我的 nodejs 项目。我将 knex.js 用于 querybuilder 及其配置。

This problem occurs when host value is 127.0.0.1,when i changed to 127.0.0.1 to localhost,the problem is gone away.

当主机值为127.0.0.1时会出现此问题,当我将 127.0.0.1 更改为 localhost 时,问题就消失了。

var knex = require('knex')({
    client: 'mysql',
    connection: {
        **host: 'localhost',**
        user: 'userNew',
        password: 'password',
        database: 'appDb'
    }
});

回答by Balibrera

The user might not have any administrative role assigned so he is not able to access the database. The user will need at least the SELECT privilege to connect.

用户可能没有分配任何管理角色,因此他无法访问数据库。用户至少需要 SELECT 权限才能连接。

More info here: https://dev.mysql.com/doc/workbench/en/wb-mysql-connections-navigator-management-users-and-privileges.html

更多信息:https: //dev.mysql.com/doc/workbench/en/wb-mysql-connections-navigator-management-users-and-privileges.html

回答by user5960462

Actually the solution is easier than you think. Change the host to 127.0.0.1 for the database connection.

实际上,解决方案比您想象的要容易。将数据库连接的主机更改为 127.0.0.1。