Linking of MySql Database in Node.js Backend Side
We can link our MySQL Database in Node.js Backend Side with the mysql module. This module helps in connecting our database with the backend server for storing the data.
- Properly installed node in your system.
- Properly installed npm (node package manager) in your system.
- MySQL server installed in your system.
Module installation: To download and install the mysql module, open the Command Terminal, and execute the following command:
npm install mysql
Create Connection & Database: Start by creating a connection to the database by creating a database name as gfg. Use the username and password from your MySQL database gfg.
Run the db.js file using the following command:
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.
Now let’s create a new file named table.js by creating the table name as ‘geeksforgeeks’.
Run the table.js file using the following command:
We will create the table, insert the records, and do the query as per requirement. In this way, we have created the database, table, and done the query after insertion of the records.