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- First start your XAMPP Server (Both Apache and MySQL).
- Open Notepad or any text-editor and write the Node.js script
- In that script, first we have to load the mysql package using the below syntax var mysql_package = require('mysql');
- Create the connection using the server,username and password.
- 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.
- Now type the following command in your command prompt to run the script. node file_name.js
- You can verify whether the database is created or not in XAMPP SERver by using the URL in a browser: http://localhost/phpmyadmin/server_databases.php
var connection_data = mysql_package.createConnection({ host: "localhost", user: "root", password: "" });
connection_data.query("CREATE DATABASE database_name", function (err, result) { //Display message in our console. console.log("Database is created"); }); });
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 createdYou 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 createdYou 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.