最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Need assistance with accessing mysql database using node.js - Stack Overflow

programmeradmin3浏览0评论

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)

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.

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)

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.

Share Improve this question asked Nov 18, 2012 at 3:00 anonymous123anonymous123 1,2856 gold badges20 silver badges45 bronze badges 8
  • 1 can you connect to mysql from console ? mysql -h 192.168.0.12 -u root -p app (enter password after request) – zb' Commented Nov 18, 2012 at 3:03
  • most probably you need set host to 'localhost' – zb' Commented Nov 18, 2012 at 3:04
  • nope I cannot, execute the command you gave me, but if I replace the ip addr with localhost it works from the command line but in the node script I get the following error-connect ECONNREFUSED. – anonymous123 Commented Nov 18, 2012 at 3:10
  • mysql host on the same server where node run ? – zb' Commented Nov 18, 2012 at 3:14
  • 3 login to mysql console and run GRANT ALL ON app.* to 'root'@'ubuntu.local' identified with 'password'; and retry your first approach – zb' Commented Nov 18, 2012 at 3:18
 |  Show 3 more comments

6 Answers 6

Reset to default 9

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:

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.

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)

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

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

Instructions

Connect to mysql via the console:

mysql -u root -p mysql

And Run

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

Source

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...

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

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.

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

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.

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

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

发布评论

评论列表(0)

  1. 暂无评论