简体   繁体   中英

How to connect mysql with express nodejs as i am getting error 1045, access denied for the user 'root'@'localhost' (using password: NO)

I am trying to connect mysql database with my node express application with the code: my db.js file

const mysql = require("mysql")
const dbconn = require('../config/db.config.js')
const connection = mysql.createConnection({
     host: dbconn.HOST,
     user: dbconn.USER,
     Password: dbconn.PASSWORD,
     database: dbconn.DB
});
connection.connect((error)=>{
    if(error) throw error;
    console.log("Successfully connected with Database");
});
module.exports = connection;

and my db.config.js file

module.exports = {
HOST: 'localhost',
USER: 'user1',
PASSWORD: 'user123',
DB: 'database1'

}

my server.js code is

    const express = require("express")
const app  = express()
const cors = require("cors");
const corOptions = {
    origin : "http://localhost:8081"
}
app.use(cors(corOptions));
app.use(express.json());
app.use(express.urlencoded({extended : true, }))
app.get("/", (req,res)=>{
res.json({message:"This is Homepage"})
});
const PORT = process.env.PORT || 8080;
require("./app/routes/tutorial.routes.js")(app);
app.listen(PORT, ()=>{
    console.log(`Server running at port: ${PORT}`)
})

Now when I run command node server.js I get the error code: 'ER_ACCESS_DENIED_ERROR', errno: 1045, sqlMessage: "Access denied for user 'user1'@'localhost' (using password: NO)", sqlState: '28000', fatal: true }

I have tried different methods but issue stil exists, help me in this case.

 host: dbconn.HOST,
 user: dbconn.USER,
 Password: dbconn.PASSWORD,
 database: dbconn.DB

Instead of P assword, write p assword

 host: dbconn.HOST,
 user: dbconn.USER,
 password: dbconn.PASSWORD,
 database: dbconn.DB

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM