0% found this document useful (0 votes)
2 views3 pages

Express MySQL Guide

This document provides a beginner-friendly guide for setting up Express.js with MySQL, including installation commands and configuration details. It also includes essential MySQL commands for database management, such as creating databases, tables, and performing CRUD operations. The setup involves creating a .env file for database credentials and two JavaScript files for database connection and server setup.

Uploaded by

ramchin2000
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Express MySQL Guide

This document provides a beginner-friendly guide for setting up Express.js with MySQL, including installation commands and configuration details. It also includes essential MySQL commands for database management, such as creating databases, tables, and performing CRUD operations. The setup involves creating a .env file for database credentials and two JavaScript files for database connection and server setup.

Uploaded by

ramchin2000
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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;

You might also like