Example create database in mysql with nodejs

Creating a Database

To create a database in MySQL, use the "CREATE DATABASE" statement

Example: Create a database named "mydb"

 

var mysql = require('mysql');

var con = mysql.createConnection({

  host: "localhost",

  user: "root",

  password: ""

});

con.connect(function(err) {

  if (err) throw err;

  console.log("Connected!");

  con.query("CREATE DATABASE mydb", function (err, result) {

    if (err) throw err;

    console.log("Database created");

  });

});

 

Run and result:

 

C:\Nodejs>node mysqlCreateDatabase.js

Connected!

Database created