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:
Copied
npm install mysql
Database creation 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
Copied
var connection_data = mysql_package.createConnection({
host: "localhost",
user: "root",
password: ""
});
Copied
connection_data.query("CREATE DATABASE database_name",
function (err, result) {
//Display message in our console.
console.log("Database is created");
});
});
Database creation Example 1:-
Copied
// 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:
Copied
Connected to XAMPP Server!
Database-facility is created
http://localhost/phpmyadmin/db_structure.php?server=1&db=facility
Database creation Example 2:-
Copied
// 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:
Copied
Connected to XAMPP Server!
Database-management is created
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.