bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/Node.js/Database Integration
Node.js•Database Integration

Node.js MySQL

Node.js can be used in database applications.

One of the most popular databases is MySQL.

MySQL Database

To be able to experiment with the code examples, you should have MySQL installed on your computer.

You can download a free MySQL database at [https://www.mysql.com/downloads/ .

Install 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:

C:\Users\
Your Name
>npm install mysql

Now you have downloaded and installed a mysql database driver.

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

let mysql = require('mysql');

Create Connection

Start by creating a connection to the database.

Use the username and password from your MySQL database.

demo_db_connection.js

let mysql = require('mysql');
let con = mysql.createConnection({
 host: "localhost", user: " yourusername ", password: " yourpassword "
});
con.connect(function(err) {
 if (err) throw err;
 console.log("Connected!");
});

Save the code above in a file called "demo_db_connection.js" and run the file:

Run "demo_db_connection.js"

C:\Users\
Your Name
>node demo_db_connection.js

Which will give you this result

Connected!

Now you can start querying the database using SQL statements.

Query a Database

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

The connection object created in the example above, has a method for querying the database:

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);
 });
});

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

Learn how to read, write, delete, and update a database in the next chapters.

Read more about SQL statements in our SQL Tutorial .

Chapter

Back to Database Integration

Next

Node.js MySQL Create Database

This chapter

Overview
24

Lessons

70m

Read time

1. Node.js MySQL2. Node.js MySQL Create Database3. Node.js MySQL Create Table4. Node.js MySQL Insert Into5. Node.js MySQL Select From6. Node.js MySQL Where7. Node.js MySQL Order By8. Node.js MySQL Delete9. Node.js MySQL Drop Table10. Node.js MySQL Update11. Node.js MySQL Limit12. Node.js MySQL Join13. Node.js MongoDB14. Node.js MongoDB Create Database15. Node.js MongoDB Create Collection16. Node.js MongoDB Insert17. Node.js MongoDB Find18. Node.js MongoDB Query19. Node.js MongoDB Sort20. Node.js MongoDB Delete21. Node.js MongoDB Drop22. Node.js MongoDB Update23. Node.js MongoDB Limit24. Node.js MongoDB Join

On this page

MySQL DatabaseInstall MySQL DriverCreate ConnectionQuery a Database