Express + MySQL Setup & MySQL Beginner Commands
This PDF contains a beginner-friendly setup guide for [Link] with MySQL and essential MySQL commands.
1. Express + MySQL Setup
Step Command / Example
Install Packages npm install express mysql2 dotenv
DB_HOST=localhost
DB_USER=root
DB_PASSWORD=123456
DB_NAME=testdb
Create .env DB_PORT=3306
Run Server node [Link]
[Link]
// [Link]
require('dotenv').config();
const mysql = require('mysql2');
const connection = [Link]({
host: [Link].DB_HOST,
user: [Link].DB_USER,
password: [Link].DB_PASSWORD,
database: [Link].DB_NAME,
port: [Link].DB_PORT
});
[Link]((err) => {
if (err) {
[Link]('Database connection failed');
} else {
[Link]('MySQL Connected');
}
});
[Link] = connection;
[Link]
// [Link]
const express = require('express');
const db = require('./db');
const app = express();
[Link]('/', (req, res) => {
[Link]('SELECT * FROM users', (err, result) => {
if (err) {
[Link](err);
} else {
[Link](result);
}
});
});
[Link](3000, () => {
[Link]('Server running on port 3000');
});
2. MySQL Beginner Commands
Show Databases
SHOW DATABASES;
Create Database
CREATE DATABASE school;
Use Database
USE school;
Show Tables
SHOW TABLES;
Create Table
CREATE TABLE students (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100), age INT);
Insert Data
INSERT INTO students (name, age) VALUES ('Parthiban', 21);
View Data
SELECT * FROM students;
Update Data
UPDATE students SET age = 23 WHERE id = 1;
Delete Data
DELETE FROM students WHERE id = 1;
Drop Table
DROP TABLE students;