Node JS
How to Connect Node.js with MySQL
Steps to setting up a connection between Node.js and MySQL, including the installation of the required packages and establishing a connection
G
Geekboots8/1/2023
0 views
mysql-nodejs-connection.jsJavaScript
const mysql = require('mysql');
const con = new mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'Qwerty@2022',
database : 'testing'
});
const connectDB = async () => {
con.connect(function (err) {
if(err) {
console.error('Error connecting to database: ', err.stack);
process.exit(1);
}
console.log('Connection established with ID: ', con.threadId);
return con;
});
}
const endConnection = async () => {
con.end((err) => {
if(err) {
console.error('Error while closing connection: ', err.stack);
process.exit(1);
}
console.log('Connection Closed Successfully!');
return con;
});
}
module.exports = {connectDB, endConnection};MySQLNodeJSDatabase Connection
Loading comments...







