DEV Community

FOLASAYO SAMUEL OLAYEMI
FOLASAYO SAMUEL OLAYEMI

Posted on

How to create a database in MYSQL using Nodejs

Node.js can be used in database applications. And one of the most popular databases is MySQL.

To enable the code examples that will be explained in this tutorial, you must have MYSQL installed on your computer. Click here to download MYSQL database for free.

Installing MYSQL Driver

Once you have MySQL up and running on your computer, you can access it by using Node.js. To access a MySQL database with Node.js, you need a MySQL driver. This tutorial will use the "mysql" module, downloaded from NPM.

To download and install the "mysql" module, open the Command Terminal and execute the following code: npm install mysql

MYSQL database driver has been downloaded and installed on your computer.

Node.js can use this module to manipulate the MySQL database

Code Sample:

var mysql = require('mysql');

Creating Connection in Nodejs with MYSQL database

Now, let's start by creating a connection to the database

Note: Always use the username and password from your MySQL database, when connecting to a MYSQL database.

Code Sample:

const mysql = require('mysql');

const con = mysql.createConnection({
  host: "localhost",
  user: "yourusername",
  password: "yourpassword"
});

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
});
Enter fullscreen mode Exit fullscreen mode

Now, save the code above in a file called "demo_db_connection.js" and run the file with this command in your terminal:
node demo_db_connection.js

The code sample above will result to:

Connected!
Enter fullscreen mode Exit fullscreen mode

After that, you can now start querying the database using SQL statements.

How to Query a MYSQL Database in Nodejs

Use SQL statements to read from (or write to) a MySQL database. This is also called "to query" the database.

Note that, the connection object created in the example above, has a method for querying the database.

Code Sample:

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
  con.query(sql, function (err, result) {
    if (err) throw err;
    console.log("Result: " + result);
  });
});
Enter fullscreen mode Exit fullscreen mode

The query method takes a sql statements as a parameter and returns the result.

How to create a database in MYSQL with Nodejs

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

Code Sample:

const mysql = require('mysql');

const con = mysql.createConnection({
  host: "localhost",
  user: "yourusername",
  password: "yourpassword"
});

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");
  });
});
Enter fullscreen mode Exit fullscreen mode

Now, save the code above in a file called "demo_create_db.js" and run the file in your terminal with the command:
node demo_create_db.js

This will display the result below:

Connected!
Database created
Enter fullscreen mode Exit fullscreen mode

Thanks for reading...
I believe you gain some knowledge.

Happy Coding!

Top comments (0)