How to Connect MySQL with Express

How to connect MySQL with Express?

  • MySQL is a widely used and open-source Relational Database Management System(RDBMS).
  • It is based on Structured Query Language, which is a popular language for accessing and managing data in databases.
  • Download MySQL using the link.
  • Install MySQL and once MySQL is up and running on your computer, you can access it using Express and Node.js.
  • To Download and install the MySQL module, open the command Terminal and execute the following:
npm install mysql

Create Connection

  • Use the username and password from your MySQL database.
  • In db.js
var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  user: "yourusername",
  password: "yourpassword"

con.connect(function(err) {
  if (err) throw err;

Save the code and run the file:

node db.js