skip to Main Content

I am having trouble connecting node js with my database in phpmyadmin, I do not understand where this problem is coming from:

the code:

var mysql      = require('mysql');
var connection = mysql.createConnection({
    host     : 'localhost',
    database : 'db_client3',
    user     : 'root',
    password : '',
});

connection.connect(function(err) {
    if (err) {
        console.error('Error connecting: ' + err.stack);
        return;
    } 
    console.log('Connected as id ' + connection.threadId);
});

connection.end();

However when I execute the code with the terminal, I get this error.

Command line:

node app.js

Error connecting: Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'root'@'localhost' (using password: NO)

package.json

{
  "name": "nodejs",
  "version": "1.0.0",
  "description": "node js test ",
  "main": "app.js",
  "scripts": {
    "test": "echo "Error: no test specified" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

Thank you for the help.

2

Answers


  1. Try adding port like this,

    var connection = mysql.createConnection({
        host     : 'localhost',
        database : 'db_client3',
        user     : 'root',
        password : '',
        port : '3000'
    });
    

    Note : Make sure that you are not running anything on the given port

    If it doesn’t work change your mysql version to latest in package.json.

    Login or Signup to reply.
  2. var mysql = require("mysql")
    
    var con = mysql.createConnection({
        host:"localhost",
        user:"root",
        password:"",
        database:"db_client3",
        port : '3000'
    
    })
    
        con.connect((error)=>{
            if(!error){
                console.log("connected with database...")
            }
            else
            {
                console.log("Error While Connecting With Database...",error)
            }
        })
    

    try something like this

    if issue not solve with above solution then
    please check you database password and make it null and if issue is still not solve then i suggest you to Create new user instead of using root

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search