In this tutorial you will learn about the Nodejs MySql Create Database and its application with practical example.
Node.js MySql Create Database
MySQL Database can simply be created using “CREATE DATABASE” statement.
Example :- Let’s create MySQL database named as “nodemysql”
Step 1:- Let’s, create a node_mysql_create_db.js file and put the following code in it –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
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 nodemysql", function (err, result) { if (err) throw err; console.log("W3Adda | MySQL Database Created"); }); }); |
Step 2:- Save the code and open the terminal again, and type the following command in order to run the file.
1 |
$ node node_mysql_create_db.js |
you will see following output on terminal –