Check if Node.js MySQL Server is Active or not - GeeksforGeeks

07 Oct, 2021
We will see how to check if the server where our MySQL Database is Hosted is Active or Not.
Syntax:
database_connection.ping(callback);
Modules:
NodeJS
ExpressJS
MySQL
Setting environment and Execution:Create Projectnpm initInstall Modulesnpm install express
npm install mysqlFile Structure:Create Serverindex.jsindex.jsconst express = require("express");const database = require('./sqlConnection');  const app = express();  app.listen(5000, () => {  console.log(`Server is up and running on 5000 ...`);});Create and export database connection objectsqlConnection.jssqlConnection.jsconst mysql = require("mysql");  let db_con  = mysql.createConnection({    host: "localhost",    user: "root",    password: ''});  db_con.connect((err) => {    if (err) {      console.log("Database Connection Failed !!!", err);    } else {      console.log("connected to Database");    }});  module.exports = db_con;Create Route to Check mysql server Active or Not.app.get("/getMysqlStatus", (req, res) => {    database.ping((err) => {    if(err) return res.status(500).send("MySQL Server is Down");          res.send("MySQL Server is Active");  })});Full index.js File:JavascriptJavascriptconst express = require("express");const database = require('./sqlConnection');  const app = express();  app.listen(5000, () => {  console.log(`Server is up and running on 5000 ...`);});  app.get("/getMysqlStatus", (req, res) => {          database.ping((err) => {        if(err) return res.status(500).send("MySQL Server is Down");                  res.send("MySQL Server is Active");    })});Run Servernode index.jsOutput: Put this link in your browser http://localhost:5000/getMysqlStatusIf server is Not Active you will see below output in your browser:MySQL Server is DownIf server is Active you will see below output in your browser:MySQL Server is Active
Setting environment and Execution:
Create Projectnpm init
Create Project
npm init
Install Modulesnpm install express
npm install mysqlFile Structure:
Install Modules
npm install express
npm install mysql
File Structure:
Create Serverindex.jsindex.jsconst express = require("express");const database = require('./sqlConnection');  const app = express();  app.listen(5000, () => {  console.log(`Server is up and running on 5000 ...`);});
Create Server
index.js
const express = require("express");const database = require('./sqlConnection');  const app = express();  app.listen(5000, () => {  console.log(`Server is up and running on 5000 ...`);});
Create and export database connection objectsqlConnection.jssqlConnection.jsconst mysql = require("mysql");  let db_con  = mysql.createConnection({    host: "localhost",    user: "root",    password: ''});  db_con.connect((err) => {    if (err) {      console.log("Database Connection Failed !!!", err);    } else {      console.log("connected to Database");    }});  module.exports = db_con;
Create and export database connection object
sqlConnection.js
const mysql = require("mysql");  let db_con  = mysql.createConnection({    host: "localhost",    user: "root",    password: ''});  db_con.connect((err) => {    if (err) {      console.log("Database Connection Failed !!!", err);    } else {      console.log("connected to Database");    }});  module.exports = db_con;
Create Route to Check mysql server Active or Not.app.get("/getMysqlStatus", (req, res) => {    database.ping((err) => {    if(err) return res.status(500).send("MySQL Server is Down");          res.send("MySQL Server is Active");  })});Full index.js File:JavascriptJavascriptconst express = require("express");const database = require('./sqlConnection');  const app = express();  app.listen(5000, () => {  console.log(`Server is up and running on 5000 ...`);});  app.get("/getMysqlStatus", (req, res) => {          database.ping((err) => {        if(err) return res.status(500).send("MySQL Server is Down");                  res.send("MySQL Server is Active");    })});
Create Route to Check mysql server Active or Not.
app.get("/getMysqlStatus", (req, res) => {    database.ping((err) => {    if(err) return res.status(500).send("MySQL Server is Down");          res.send("MySQL Server is Active");  })});
Full index.js File:
Javascript
const express = require("express");const database = require('./sqlConnection');  const app = express();  app.listen(5000, () => {  console.log(`Server is up and running on 5000 ...`);});  app.get("/getMysqlStatus", (req, res) => {          database.ping((err) => {        if(err) return res.status(500).send("MySQL Server is Down");                  res.send("MySQL Server is Active");    })});
Run Servernode index.js
Run Server
node index.js
Output: Put this link in your browser http://localhost:5000/getMysqlStatusIf server is Not Active you will see below output in your browser:MySQL Server is DownIf server is Active you will see below output in your browser:MySQL Server is Active
Output: Put this link in your browser http://localhost:5000/getMysqlStatus
If server is Not Active you will see below output in your browser:
MySQL Server is Down
If server is Active you will see below output in your browser:
MySQL Server is Active
NodeJS-MySQL
NodeJS-Questions
Technical Scripter 2020
Node.js
Technical Scripter
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Express.js express.Router() Function
Express.js req.params Property
JWT Authentication with Node.js
Mongoose Populate() Method
Difference between npm i and npm ci in Node.js
Roadmap to Become a Web Developer in 2022
How to fetch data from an API in ReactJS ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Convert a string to an integer in JavaScript
How to insert spaces/tabs in text using HTML/CSS?