Node.js and mysql - ER_ACCESS_DENIED_ERROR


 
Thread Tools Search this Thread
Top Forums Web Development Node.js and mysql - ER_ACCESS_DENIED_ERROR
# 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 or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Programming

Node-RED: Writing MQTT Messages to MySQL DB with UNIX timestamp

First, I want to thank Neo (LOL) for this post from 2018, Node.js and mysql - ER_ACCESS_DENIED_ERROR I could not get the Node-RED mysql module to work and searched Google until all my links were purple! I kept getting ER_ACCESS_DENIED_ERROR with the right credentials. Nothing on the web was... (0 Replies)
Discussion started by: Neo
0 Replies

2. HP-UX

Mount FIle systems from node-1 onto node-2

Hi, We have HP UX service guard cluster on OS 11.23. Recently 40+ LUNs presented to both nodes by SAN team but I was asked to mount them on only one node. I created required VGs/LVs, created VxFS and mounted all of them and they are working fine. Now client requested those FS on 2nd node as... (4 Replies)
Discussion started by: prvnrk
4 Replies

3. Homework & Coursework Questions

Accessing one UNIX node from another node of the same server

Hi Experts, I am in need of running a script from one node say node 1 via node 2. My scheduling tool dont have access to node2 , so i need to invoke the list file from node1 but the script needs to run from node2. because the server to which i am hitting, is having access only for the node... (5 Replies)
Discussion started by: arun1377
5 Replies

4. UNIX and Linux Applications

MySQL Daemon failed to start - no mysql.sock file

After doing a yum install mysql mysql-server on Fedora 14 I wasn't able to fully install the packages correctly. It installed MySQL 5.1. I was getting the following error when running the: mysql ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)... (3 Replies)
Discussion started by: jastanle84
3 Replies

5. Solaris

SVM metaset on 2 node Solaris cluster storage replicated to non-clustered Solaris node

Hi, Is it possible to have a Solaris cluster of 2 nodes at SITE-A using SVM and creating metaset using say 2 LUNs (on SAN). Then replicating these 2 LUNs to remote site SITE-B via storage based replication and then using these LUNs by importing them as a metaset on a server at SITE-B which is... (0 Replies)
Discussion started by: dn2011
0 Replies

6. UNIX for Advanced & Expert Users

MySQL problem >> missing mysql.sock

MySQL on my server is down.... I figured out that the mysqld process isn't running. When I try to run it, it says it can't find mysql.sock Any suggestions? Here's what I can't do: can't be root don't have physical access (do stuff via SSH) reinstall MySQL (need to keep the current MySQL... (8 Replies)
Discussion started by: _hp_
8 Replies
Login or Register to Ask a Question