NODE.JS + MySQL CONNECTION

1️⃣ MySQL Server Check (XAMPP)

  • XAMPP Control Panel open karo

  • MySQL → Start (Running hona chahiye ✅)

phpMyAdmin open:

http://localhost/phpmyadmin

2️⃣ Database & Table Create

Database:

CREATE DATABASE node_shop;
USE node_shop;

Sample Table:

CREATE TABLE products (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
price DECIMAL(10,2),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

👉 Ye table CRUD ke liye use hogi.


3️⃣ DB Config Folder

Path confirm:

src/db/

Is folder ke andar file banao 👇

📄 src/db/db.js

const mysql = require('mysql2');

// Create connection pool
const db = mysql.createPool({
host: 'localhost',
user: 'root',
password: '', // XAMPP default blank
database: 'node_shop',
port: 3306
});

module.exports = db;

🧠 CI4 mapping:

\Config\Database::connect()

4️⃣ DB Connection Test (IMPORTANT)

📄 src/app.js me upar add karo:

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

// Test DB connection
db.query('SELECT 1', (err) => {
if (err) {
console.log('❌ MySQL Error:', err);
} else {
console.log('✅ MySQL Connected bhai 🔥');
}
});

⚠️ Path dhyaan se:

./db/db

5️⃣ Server Restart

Terminal me:

npm run dev

Expected Output:

Server running on http://localhost:3000
✅ MySQL Connected bhai 🔥

👉 Matlab:

  • Node server running ✅

  • MySQL connected ✅

Leave a Reply

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