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:
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:
mysql -u node -p myDatabase
After hours and hours of this, it always give error:
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.
package.json
...
"dependencies": {
"express": "^4.16.3",
"mysql": "^2.16.0",
"nodemon": "^1.18.3"
},
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>