Node.js with MySQL database.

Connecting node.js project with MySQL database is easy, follow the following steps to connect to the the database

By reading this article I believe you have installed nodee Js on your system and familiar with it.

Step1

Open your code editor.

Step…


This content originally appeared on DEV Community and was authored by popoola Temitope

Connecting node.js project with MySQL database is easy, follow the following steps to connect to the the database

By reading this article I believe you have installed nodee Js on your system and familiar with it.

Step1

Open your code editor.

Step2

Create a new project
Open your command prompt or terminal and enter

 mkdir project_name
 cd project_name

Step3

Create project file.
enter

 npm init -y

into the command prompt or terminal.

Step4

To be able to connect to mysql database you need to install MySQL module into your project.

To do this is very simple, open your CMD or terminal and enter

 npm install mysql

this command will install the module into your project.

Step5

Now we can open connection From our project code to mysql database
add the code below to your app.js code to open a connection.


const mysql = require('mysql');

// Creating a mysql connection
const conn = mysql.createConnection({
   host: "localhost",
   user: "db_username",
   password: "db_password",
   database: "db_name"
});

Step6

Let create a simple database and table

conn.connect((err)=>{
   if (err) throw err;
      console.log("Database connected successful");
   var sql = "CREATE TABLE books (book_name VARCHAR(255), book_description VARCHAR(255))";
   con.query(sql,(err, result)=> {
      if (err) throw err;
         console.log("Table created");
   });
});

You can leave a comment...
Thanks❤️


This content originally appeared on DEV Community and was authored by popoola Temitope


Print Share Comment Cite Upload Translate Updates
APA

popoola Temitope | Sciencx (2021-08-09T21:13:20+00:00) Node.js with MySQL database.. Retrieved from https://www.scien.cx/2021/08/09/node-js-with-mysql-database/

MLA
" » Node.js with MySQL database.." popoola Temitope | Sciencx - Monday August 9, 2021, https://www.scien.cx/2021/08/09/node-js-with-mysql-database/
HARVARD
popoola Temitope | Sciencx Monday August 9, 2021 » Node.js with MySQL database.., viewed ,<https://www.scien.cx/2021/08/09/node-js-with-mysql-database/>
VANCOUVER
popoola Temitope | Sciencx - » Node.js with MySQL database.. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/09/node-js-with-mysql-database/
CHICAGO
" » Node.js with MySQL database.." popoola Temitope | Sciencx - Accessed . https://www.scien.cx/2021/08/09/node-js-with-mysql-database/
IEEE
" » Node.js with MySQL database.." popoola Temitope | Sciencx [Online]. Available: https://www.scien.cx/2021/08/09/node-js-with-mysql-database/. [Accessed: ]
rf:citation
» Node.js with MySQL database. | popoola Temitope | Sciencx | https://www.scien.cx/2021/08/09/node-js-with-mysql-database/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.