Node.js Tutorial
Estimated reading: 3 minutes 25 views

๐Ÿ“‚ Node.js โ€“ MySQL: Introduction & Setup โ€“ Connect and Manage Databases with Ease

๐Ÿงฒ Introduction โ€“ Why Use MySQL with Node.js?

When building data-driven applications, integrating a relational database like MySQL with Node.js is a powerful choice. MySQL offers structured data storage and advanced querying capabilities, while Node.js provides fast, asynchronous execution for backend APIs.

๐ŸŽฏ In this guide, youโ€™ll learn:

  • How to install MySQL and set it up for use with Node.js
  • How to install and use the mysql Node.js driver
  • How to connect Node.js to a MySQL server
  • How to create a database and tables from Node.js

๐Ÿ“˜ Topics Covered

๐Ÿ”น Topic๐Ÿ“– Description
โš™๏ธ Node.js โ€“ MySQL Get StartedInstall MySQL and Node.js driver; test database connection
๐Ÿ› ๏ธ Node.js โ€“ MySQL Create Database / TablesProgrammatically create database and tables using SQL queries

โš™๏ธ Node.js โ€“ MySQL Get Started

๐Ÿ”น Prerequisites

โœ… Make sure you have:

  • Node.js installed (node -v)
  • MySQL server running locally or remotely
  • MySQL root/user credentials

๐Ÿ”น Step 1: Install MySQL Driver

npm install mysql

โœ… The mysql package allows Node.js to query and manipulate MySQL databases.


๐Ÿ”น Step 2: Create db.js for Connection

const mysql = require('mysql');

// Create connection
const db = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: '', // change if needed
});

// Connect to MySQL
db.connect((err) => {
  if (err) throw err;
  console.log('Connected to MySQL!');
});

module.exports = db;

๐Ÿงช Output:

Connected to MySQL!

โœ… Always wrap connect() in error handling for production setups.


๐Ÿ› ๏ธ Node.js โ€“ MySQL Create Database / Tables

๐Ÿ”น Step 3: Create Database

const db = require('./db');

db.query("CREATE DATABASE companyDB", (err, result) => {
  if (err) throw err;
  console.log("Database created!");
});

๐Ÿงช Output:

Database created!

๐Ÿ”น Step 4: Connect to the New Database

Update db.js:

const db = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: '',
  database: 'companyDB'
});

๐Ÿ”น Step 5: Create Table โ€“ Employees

const db = require('./db');

const sql = `
  CREATE TABLE employees (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100),
    position VARCHAR(100),
    salary DECIMAL(10,2)
  )
`;

db.query(sql, (err, result) => {
  if (err) throw err;
  console.log("Employees table created!");
});

๐Ÿงช Output:

Employees table created!

โœ… You can run similar queries to define other tables like departments, tasks, etc.


๐Ÿ“Œ Summary โ€“ Recap & Next Steps

With just a few lines of code, Node.js can connect to a MySQL server and execute queries to create databases and tables. This forms the foundation of full-stack apps that store and retrieve structured data.

๐Ÿ” Key Takeaways:

  • Use the mysql package to connect Node.js to MySQL
  • Always check connections with proper error handling
  • Create databases and tables programmatically using SQL from Node

โš™๏ธ Real-World Uses:

  • Backend APIs that interact with MySQL for authentication and CRUD
  • Inventory, finance, and reporting systems
  • Admin dashboards with real-time MySQL data

โ“ Frequently Asked Questions

โ“ Do I need MySQL installed locally for this to work?
โœ… You can use either a local or remote MySQL serverโ€”just update the host and port.


โ“ Whatโ€™s the difference between mysql and mysql2 packages?
โœ… mysql2 supports Promises and prepared statements. mysql is callback-based.


โ“ How do I secure my database credentials?
โœ… Use environment variables (dotenv package) and avoid hardcoding credentials in production.


โ“ Can I create multiple tables in one file?
โœ… Yes, just chain db.query() calls or use a batch SQL string.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿ“‚ Node.js โ€“ MySQL: Introduction & Setup

Or Copy Link

CONTENTS
Scroll to Top