Node.js MySQL Create Database

If your administrator provide you priviledge to create database, then you can create it yourself. To create a new Database in MySQL, CREATE DATABASE statement is used. Here is the sample code to create a new database in MySQL.

Create a file name 'createdb.js' and copy & paste this code.

var mysql = require("mysql");var con = mysql.createConnection({
   host: "hostname",
   user: "username",
   password: "password"
});

con.connect(function(error){
    if(error) {
        console.log('Error establishing connection to db');
        return;
    }
    console.log('connection established');
    con.query("CREATE DATABASE company", function(error){
        if(error){
            console.log("Error in creating new database");
            return;
        }
        console.log("Created New Database");
    });
});

Open cmd and run the file as -

node createdb.js


Read more articles


General Knowledge



Learn Popular Language