'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.
Solution 1:[1]
host: dbconn.HOST,
user: dbconn.USER,
Password: dbconn.PASSWORD,
database: dbconn.DB
Instead of Password, write password
host: dbconn.HOST,
user: dbconn.USER,
password: dbconn.PASSWORD,
database: dbconn.DB
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |