Node.js - MySQL Database Creation in XAMPP Server

In this post, we will discuss how to create MYSQL database in XAMPP Server using Node.js Script.

It is important to install mysql package in node.js

Command to install the mysql package:

npm install mysql
				

Steps:

Now let's see steps
  1. First start your XAMPP Server (Both Apache and MySQL).
  2. Open Notepad or any text-editor and write the Node.js script
  3. In that script, first we have to load the mysql package using the below syntax
  4. var mysql_package = require('mysql');
  5. Create the connection using the server,username and password.
  6. var connection_data = mysql_package.createConnection({
      host: "localhost",
      user: "root",
      password: ""
    });
    
  7. Write the sql query to create a database in XAMPP. It will take two parameters. The first parameter is the SQL Query and the second parameter will handle the result.
  8. connection_data.query("CREATE DATABASE database_name", 
    function (err, result) {
    //Display message in our console.
        console.log("Database is created");
      });
    });
  9. Now type the following command in your command prompt to run the script.
  10. node file_name.js
  11. You can verify whether the database is created or not in XAMPP SERver by using the URL in a browser:
  12. http://localhost/phpmyadmin/server_databases.php

Example 1:-

Let's create database named - facility.
 // Load the mysql package
var mysql_package = require('mysql');
// Create the connection using the server,username and password.
//In my scenario - server is the localhost,
//username is root,
//password is empty.
var connection_data = mysql_package.createConnection({
  host: "localhost",
  user: "root",
  password: ""
});

connection_data.connect(function(err) {
  console.log("Connected to XAMPP Server!");
//sql query to create a database named  facility in XAMPP
  connection_data.query("CREATE DATABASE facility", function (err, result) {
//Display message in our console.
    console.log("Database-facility is created");
  });
});   
Output:
Connected to XAMPP Server!
Database-facility is created
You can see that database is created. Now let's check in XAMPP Server. - http://localhost/phpmyadmin/db_structure.php?server=1&db=facility

Example 2:-

Let's create database named - management.
// Load the mysql package
var mysql_package = require('mysql');
// Create the connection using the server,username and password.
//In my scenario - server is the localhost,
//username is root,
//password is empty.
var connection_data = mysql_package.createConnection({
  host: "localhost",
  user: "root",
  password: ""
});

connection_data.connect(function(err) {
  console.log("Connected to XAMPP Server!");
//sql query to create a database named  management in XAMPP
  connection_data.query("CREATE DATABASE management", function (err, result) {
//Display message in our console.
    console.log("Database-management is created");
  });
});
Output:
Connected to XAMPP Server!
Database-management is created
You can see that database is created. Now let's check in XAMPP Server. - http://localhost/phpmyadmin/db_structure.php?server=1&db=management

Summary

So we seen how to create a database in XAMPP Server through Node.js script with two examples. make sure that you have to install mysql package and xampp server.