Node JS - Node js mysql - Node - Node JS tutorial - webnode
What is Node.js MySQL ?
- Node.js can be used in database applications.
- One of the most popular databases is MySQL.
- It provides all most all connection/query from MySQL.
- Node-mysql is probably one of the best modules used for working with MySQL database and the module is actively maintained.
Learn Node js - node js Tutorial - node.js mysql - node - Node js Examples
Why use node.js with DB?
- Multiple data updates to a database can create chaos in normal synchronized processes.
- node.js manages these simultaneous connections via message queues.
- This approach is generally useful when there are concurrent batch uploads to database, that are collected for use in later stage.
- Eg. User Log are collected and uploaded to database which are used later for report preparations. Such data uploads don’t require immediate acknowledgements and hence they are handled by node.js event driven queues.
- To be able 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:
- Now you have downloaded and installed a mysql database driver.
- Node.js can use this module to manipulate the MySQL database:
- Start by creating a connection to the database.
- Use the username and password from your MySQL database.
- Save the code above in a file called "demo_db_connection.js" and run the Node JS Tutorial File:
- 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: