mg nt
mg nt

Reputation: 191

Cannot connect to MySQL from NodeJS

I am getting this error, but none of the solutions worked for me

Error: ER_ACCESS_DENIED_ERROR: Access denied for user ''@'localhost' (using password: YES)

I tried the solutions posted online but they did not work. I can connect to my server in Python, but not in NodeJS despite having the MySQL module installed.

var con = mysql.createConnection({host: "localhost", port: 3306, username: "root", password: "password", database: "activity"})

For some reason, the error message does not show user 'root'@'localhost', but rather an empty user. I do not know why this isn't working, since I am able to connect to my database in python. I've tried flushing privileges and other solutions posted online, but none work. MySQL server does run on port 3306

Upvotes: 0

Views: 603

Answers (1)

Sumeet Kumar Yadav
Sumeet Kumar Yadav

Reputation: 12975

var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "password",
  database: "activity"
});

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
  var sql = "CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255))";
  con.query(sql, function (err, result) {
    if (err) throw err;
    console.log("Table created");
  });
});

Upvotes: 1

Related Questions