Home Man
Search
Today's Posts
Register

Javascript, Javascript libs and toolkits, LAMP, Apache, MySQL, HTML, and other web coding posts.

Node.js and mysql - ER_ACCESS_DENIED_ERROR

Tags
er_access_denied_error, express, javascript, mysql, node.js, web

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 08-03-2018
Node.js and mysql - ER_ACCESS_DENIED_ERROR

This problem has been killing me all day, and I cannot solve it.

Basically, I am using node.js with the mysql module and it will not connect to the database.

Here is the JS code snippet in node.js:

Code:
app.get("/test", function(req, res) {
  var mysql = require("mysql");

  var con = mysql.createConnection({
    host: "127.0.0.1",
    user: "node",
    password: "myPassword",
    database: "myDatabase",
    port: 3306
  });

  console.log("Connection Object Created");
  con.connect(function(error1) {
    if (error1) {
      console.log(error1);
      return;
    } else {
      console.log("Connected1");
    }
  });
});

I have checked the grants and permissions and all is just fine in MySQL and I can easily connect to the DB manually from the command line using:

Code:
mysql -u node -p  myDatabase

After hours and hours of this, it always give error:

Code:
Connection Object Created
{ Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'node'@'127.0.0.1' (using password: YES)
    at Handshake.Sequence._packetToError (/var/node/node_modules/mysql/lib/protocol/sequences/Sequence.js:47:14)
    at Handshake.ErrorPacket (/var/node/node_modules/mysql/lib/protocol/sequences/Handshake.js:124:18)
    at Protocol._parsePacket (/var/node/node_modules/mysql/lib/protocol/Protocol.js:278:23)
    at Parser.write (/var/node/node_modules/mysql/lib/protocol/Parser.js:76:12)
    at Protocol.write (/var/node/node_modules/mysql/lib/protocol/Protocol.js:38:16)
    at Socket.<anonymous> (/var/node/node_modules/mysql/lib/Connection.js:91:28)
    at Socket.<anonymous> (/var/node/node_modules/mysql/lib/Connection.js:502:10)
    at Socket.emit (events.js:182:13)
    at addChunk (_stream_readable.js:283:12)
    at readableAddChunk (_stream_readable.js:264:11)
    --------------------
    at Protocol._enqueue (/var/node/node_modules/mysql/lib/protocol/Protocol.js:144:48)
    at Protocol.handshake (/var/node/node_modules/mysql/lib/protocol/Protocol.js:51:23)
    at Connection.connect (/var/node/node_modules/mysql/lib/Connection.js:118:18)
    at /var/node/index.js:27:5
    at Layer.handle [as handle_request] (/var/node/node_modules/express/lib/router/layer.js:95:5)
    at next (/var/node/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/var/node/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/var/node/node_modules/express/lib/router/layer.js:95:5)
    at /var/node/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/var/node/node_modules/express/lib/router/index.js:335:12)
  code: 'ER_ACCESS_DENIED_ERROR',
  errno: 1045,
  sqlMessage:
   'Access denied for user \'node\'@\'127.0.0.1\' (using password: YES)',
  sqlState: '28000',
  fatal: true }

I have Googled and read at least 30 tutorials, watched at least 5 YT videos on node.js and MySQL, and read the docs many time.

I cannot get past this error.

Darn!

Does anyone here use node.js and MySQL ??? If so, please help.

Code:
  package.json 

   ...

  "dependencies": {
    "express": "^4.16.3",
    "mysql": "^2.16.0",
    "nodemon": "^1.18.3"
  },



Code:
me@myServer:~# mysql -u node -p myDatabase
Enter password: 
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.

....


mysql> show grants;
+----------------------------------------------------------------------------------------------------------------------+
| Grants for node@localhost                                                                                            |
+----------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'node'@'localhost' IDENTIFIED BY PASSWORD '*7F8BC63ED005891Axxxxxxxx0FBB5A48CB5FFB9' |
+----------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql>

# 2  
Old 08-03-2018
Are localhost and 127.0.0.1 refering to the same? (/etc/hosts)
# 3  
Old 08-03-2018
Quote:
Originally Posted by MadeInGermany
Are localhost and 127.0.0.1 refering to the same? (/etc/hosts)
Yes, I checked that a number of times using both localhost and 127.0.0.1

Also

Code:
cat /etc/hosts
127.0.0.1	localhost
...

# 4  
Old 08-03-2018
Yay!

The solution turns out to be:


Code:
port: '/var/run/mysqld/mysqld.sock'

Seems in this config, MySQL listens on a Unix socket not on a TCP socket.
# 5  
Old 08-04-2018
Here is the final node.js test script (index.js):

Code:
var express = require("express");
var fs = require("fs");
var https = require("https");
var app = express();
require("dotenv").config();
const privateKey = fs.readFileSync(process.env.SSL_PRIVKEY, "utf8");
const certificate = fs.readFileSync(process.env.SSL_CERT, "utf8");
const ca = fs.readFileSync(process.env.SSL_CHAIN, "utf8");
const user = process.env.DATABASE_USER;
const password = process.env.DATABASE_PASSWORD;
const database = process.env.DATABASE_NAME;
const port = process.env.DATABASE_SOCKET;

app.get("/", function(req, res) {
  res.send("hello world");
});

app.get("/test", function(req, res) {
  var mysql = require("mysql");
  var con = mysql.createConnection({
    host: "127.0.0.1",
    user: user,
    password: password,
    database: database,
    port: port
  });

  con.connect(function(error) {
    if (error) {
      console.log(error);
      return;
    } else {
      con.query("SELECT username FROM user WHERE userid < 10", function(
        err,
        rows,
        fields
      ) {
        if (err) {
          console.log(err);
          return;
        } else {
          rows.forEach(function(result) {
            console.log(result.username);
          });
        }
      });
      con.end();
      res.send("MySQL Query Complete");
    }
  });
});

https
  .createServer(
    {
      key: privateKey,
      cert: certificate,
      ca: ca
    },
    app
  )
  .listen(8080, function(error) {
    if (error) {
      console.log(error);
    } else console.log("Example app listening on port 8080");
  });

Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Mount FIle systems from node-1 onto node-2 prvnrk HP-UX 4 07-04-2014 10:52 AM
Accessing one UNIX node from another node of the same server arun1377 Homework & Coursework Questions 5 02-05-2014 03:56 PM
MySQL Daemon failed to start - no mysql.sock file jastanle84 UNIX and Linux Applications 3 10-12-2011 02:56 PM
SVM metaset on 2 node Solaris cluster storage replicated to non-clustered Solaris node dn2011 Solaris 0 04-14-2011 08:34 AM
mysql would not start: missing mysql.sock xnightcrawl UNIX for Advanced & Expert Users 2 05-26-2006 10:06 AM
MySQL problem >> missing mysql.sock _hp_ UNIX for Advanced & Expert Users 8 11-03-2002 01:44 PM


All times are GMT -4. The time now is 08:06 AM.

Unix & Linux Forums Content Copyrightę1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password