To create MySQL database with node.js we need first to create a connection to MySQL server then use the “CREATE DATABASE” statement. The following examples will show you exactly how to create a database using the two types of connection: Single Connection and Connection Pool.
Related Articles:
- Automating Your MySQL Database with Triggers.
- How to use ON DELETE CASCADE in MySQL.
- MySQL + Event Scheduler: How to delete data from MYSQL table using Event Scheduler.
- Using MySQL Event Scheduler: A Complete Guide.
- How to interact with MySQL database using async/await promises in node.js ?
- How to use Sequelize async/await to interact with MySQL database in Node.js.
- MANY-TO-MANY Association in MYSQL Database using Sequelize async/await with Node.js.
- ONE-TO-ONE Association in MYSQL Database using Sequelize async/await with Node.js
- ONE-TO-ONE Association in MYSQL Database using Sequelize async/await with Node.js.
- Complete JWT Authentication and Authorization System for MySQL/Node.js API.
- How to Build a Complete API for User Login and Authentication using MySQL and Node.js.
- How to store Session in MySQL Database using express-mysql-session.
- Complete JWT Authentication and Authorization System for MySQL/Node.js API
Single connection
Single database connection means that you are using only one shared connection to execute all the queries triggered by all your application users. So, we use single connection only when the number of an application users at the same time is limited and theirs queries are simple.
To know more about single connection and how it works click here: What is Single Connection?.
//createDB.js
const connection = mysql.createConnection({
password: “yourpassword”,
host: localhost,
user: “username”,
port: 3306
});
connection.connect(function(err) {
if (err) throw err;
console.log("Connected to Mysql");
connection.query("CREATE DATABASE ExampleDB", function (err, result) {
if (err) throw err;
console.log("ExampleDB database created");
});
});
Connection Pooling
Connection pools are used to improve the performance of the queries’s execution on a database without exception, prevent opening and closing connections frequently and reduce the number of new connections.
To know more about Connection Pooling and how it works click here: What is Connection Pooling and How it works?.
//createDB.js
const mysql = require('mysql');
const pool = mysql.createPool({
password: process.env.DB_PASS,
host: process.env.DB_HOST,
user: process.env.DB_USER,
port: process.env.DB_PORT
});
pool.query("CREATE DATABASE ExampleDB", function (err, result) {
if (err) throw err;
console.log("ExampleDB database created");
})
Now run “createDB.js”
node createDB.js
ou might also like:
Automating Your MySQL Database with Triggers.
How to use ON DELETE CASCADE in MySQL.
MySQL + Event Scheduler: How to delete data from MYSQL table using Event Scheduler.
Using MySQL Event Scheduler: A Complete Guide.
Node.js + MySQL : Add Forgot/Reset Password to Login-Authentication System.
How to Build a Complete API for User Login and Authentication using MySQL and Node.js.
How to store Session in MySQL Database using express-mysql-session.
How to interact with MySQL database using async/await promises in node.js ?
How to use Sequelize async/await to interact with MySQL database in Node.js.
MANY-TO-MANY Association in MYSQL Database using Sequelize async/await with Node.js.
ONE-TO-ONE Association in MYSQL Database using Sequelize async/await with Node.js
ONE-TO-ONE Association in MYSQL Database using Sequelize async/await with Node.js.
How to add Routes to insert data into MySQL database-related tables in Node.js API?
Example How to use initialize() Function in Node.js/Express API .
Why is Connection Pooling better than Single Connection?.
How to create MySQL database using node.js.
Nodemailer + Gmail: How to Send Emails from Node.js API using Gmail.
Node.js + Nodemailer : How to send Emails via SMTP with Nodemailer
How to get Request Origin in Express.js?
How to get the Request Host in express.js?