Home >>Nodejs Tutorial >Node.Js Create Connection with MySQL

Node.Js Create Connection with MySQL

Node.js MySQL Create Database

The statement CREATE DATABASE is used for creating a MySQL database.

Example For creating a database named "Student".

Creating a javascript file called "connection.js" with the following data in the folder example1.


var db = require('db');  
var con = db.createConnection({  
host: "localhost",  
user: "root",  
password: ""  
});  
con.connect(function(err) {  
if (err) throw err;  
console.log("Connected!");  
con.query("CREATE DATABASE Student", function (err, result) {  
if (err) throw err;  
console.log("Database created");  
});  
});  

Now open the command terminal and execute the following command:

Node connection.js

The database is built for you to use.

Verification

To check whether or not the database is created use the command SHOW DATABASES. Before this, use mysql-p command to go to initial route.


No Sidebar ads