0

I see this question has been asked many times, but none of the answers match my situation:

Tech: node.js with mysql node_module

node code:

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

   var con = mysql.createConnection({
      host: "localhost",
      user: "node",
      password: "myPassword",
      database : "myDatabase"
   });
con.connect(function(err) {
  if (err) throw err;
  console.log('CONNECTED');
});

Error:

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:24: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)
[nodemon] app crashed - waiting for file changes before starting...

mysql works fine:

root@www:/var/node# mysql -u node -p myDatabase
Enter password:

Connects no problem at all with mysql from the command line; but does not work from node.

From package.json

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

Why why why... ? LOL

Tim
  • 39
  • 3
  • What does return the command `show grants;` in mysql console? – Fefux Aug 03 '18 at 08:52
  • mysql> show grants; +----------------------------------------------------------------------------------------------------------------------+ | Grants for node@localhost | | GRANT ALL PRIVILEGES ON *.* TO 'node'@'localhost' IDENTIFIED BY PASSWORD '*7F8BC63ED005891AB8272712C0FBB5A48CB5FFB9' | 1 row in set (0.00 sec) – Tim Aug 03 '18 at 09:57
  • It's fine.. show grants; LOL – Tim Aug 03 '18 at 09:58
  • Grants for node@localhost | +----------------------------------------------------------------------------------------------------------------------+ | GRANT ALL PRIVILEGES ON *.* TO 'node'@'localhost' IDENTIFIED BY PASSWORD '*7F8BC63ED005891AB8272712C0FBB5A48CB5FFB9' | – Tim Aug 03 '18 at 10:05
  • show grants; has star dot star in the output, but for some reason, the stack overflow editor just deleted these tokens at random when we cut and past in LOL – Tim Aug 03 '18 at 10:06
  • Can you confirm which port your mysql is running on? `ps ax | grep mysqld`. You should see something like `--port=xxx`. Add that your create connection `port: xxx`. If that does nothing check out https://stackoverflow.com/questions/21206801/node-js-mysql-error-econnrefused – Noel Kriegler Aug 03 '18 at 14:04
  • root@www:~# ps aux | grep mysqld mysql 20470 7.4 0.3 9318188 262964 ? Ssl Jul20 1472:38 /usr/sbin/mysqld root 23033 0.0 0.0 9408 892 pts/3 S+ 12:24 0:00 grep --color=auto mysqld root@www:~# cd /etc/mysql root@www:/etc/mysql# grep port my.cnf # One can use all long options that the program supports. # It has been reported that passwords should be enclosed with ticks/quotes port = 3306 port = 3306 root@www:/etc/mysql# – Tim Aug 03 '18 at 17:25
  • It runs on port 3306 by config and by examination: – Tim Aug 03 '18 at 17:26
  • root@www:/etc/mysql# netstat -an | grep 330 tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN – Tim Aug 03 '18 at 17:26

1 Answers1

0

The solution was:

port: '/var/run/mysqld/mysqld.sock'
Tim
  • 39
  • 3