#
Create a MySql database (schema)
This tutorial explains to you how to create a MySql database (schema) from a Node.js application. This article provides you with an example as well.
Using Node.js you can connect to a database and run different operation as creating a table, select data from tables, update rows in tables, insert data into tables or delete information from a database.
In order to make possible this thing, you have to install into Node.js the proper package.
For MySql database you have to install mysql module (package):
npm install mysql
After that you can connect to MySql Database and perform different operations.
Here it is a Node.js code which create a database (schema) in MySQL:
console.log("Application starts ...");
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "1234",
database: "world"
});
var databaseName = "myDatabase";
con.connect(function(err) {
console.log("[create database in MySql] - block BEGIN ");
if (err) throw err;
varId = 6;
con.query("create database "+databaseName, function (err, result, fields) {
if (err) throw err;
// console.log(result);
});
con.end();
console.log("[create database in MySql] - block END");
});
console.log("END of the application.");
When you run the code you will se the following into the console (you can see an ASYNC execution):
You can verify into MySQL Workbench to see if the new schema (database) is created:
Now, you can create tables in that schema.