0% found this document useful (0 votes)
21 views

Introduction To MySQL

Uploaded by

b.nomundari
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)
21 views

Introduction To MySQL

Uploaded by

b.nomundari
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
You are on page 1/ 5

Step-by-Step Guide: Connecting to a Remote

MySQL Database Using HeidiSQL and Node.js


Integration

1 Step-by-Step Guide on Connecting to a Re-


mote DB Server using HeidiSQL and Creating
a Basic MySQL Database
1.1 1. Install HeidiSQL
Download and install HeidiSQL from the official website: https://www.heidisql.com/download.php.

1.2 2. Connecting to a Remote MySQL Database in Hei-


diSQL
1. Open HeidiSQL.

2. Create a New Session:


• Click on the New button on the session manager.
• In the ”Session name” field, give your session a recognizable name
(e.g., Remote MySQL Lab).
• Set Connection Parameters:
– Network Type: Select MySQL (TCP/IP).
– Hostname / IP: Enter the IP address or hostname of the re-
mote server. (203.91.116.122)
– User: Enter your MySQL username: ”teams”
– Password: Enter the password for your MySQL user : ”{superSecretPassword!123}”
– Port: Use the default MySQL port 22136, unless your server
uses a different port.
• Test Connection: Click the Open button to test the connection.

1
1.3 3. Creating a Basic MySQL Database
1. Create a Database:
• Right-click on the server node and select Create New → Database.
• Enter the name for the database (e.g., yourname lab db).
• Select the character set (utf8mb4) and collation (utf8mb4 unicode ci),
then click OK.
2. Create a Table:
• Right-click on the newly created database and select Create New
→ Table.
• Set the table name (e.g., students).
• Add fields such as:
– id (INT, Primary Key, Auto Increment)
– name (VARCHAR(100))
– email (VARCHAR(100), unique)
– created at (TIMESTAMP, Default = CURRENT TIMESTAMP)

1.4 4. Understanding Different Types of MySQL Queries


• Create Table:
1 CREATE TABLE students (
2 id INT A UTO_INCR EMENT PRIMARY KEY ,
3 name VARCHAR (100) ,
4 email VARCHAR (100) UNIQUE ,
5 created_at TIMESTAMP DEFAULT C U R R E N T _ T I M E S T A M P
6 );
7

• Insert Data:
1 INSERT INTO students ( name , email ) VALUES ( ’ John Doe ’ , ’
john@example . com ’) ;
2

• Select Data:
1 SELECT * FROM students ;
2

• Update Data:
1 UPDATE students SET email = ’ j oh n do e@ ex a mp le . com ’ WHERE id =
1;
2

• Delete Data:
1 DELETE FROM students WHERE id = 1;
2

2
2 Using the Database in a Node.js, Express.js,
and EJS Project
2.1 1. Install Required Packages
In the root of your Node.js project, run:
1 npm install mysql2 express ejs

2.2 2. Set Up MySQL Connection in Node.js


Create a file db.js for the database connection:
1 const mysql = require ( ’ mysql2 ’) ;
2
3 const connection = mysql . c r e a t e Co n n e c t i o n ({
4 host : ’ your - remote - host ’ ,
5 user : ’ your - username ’ ,
6 password : ’ your - password ’ ,
7 database : ’ yourname_lab_db ’
8 }) ;
9
10 connection . connect (( err ) = > {
11 if ( err ) throw err ;
12 console . log ( ’ Connected to MySQL ’) ;
13 }) ;
14
15 module . exports = connection ;

2.3 3. Create Express Routes for Database Queries


In app.js (your main Express app), require db.js:
1 const express = require ( ’ express ’) ;
2 const db = require ( ’./ db ’) ;
3 const app = express () ;
4
5 app . set ( ’ view engine ’ , ’ejs ’) ;
6
7 app . get ( ’/ ’ , ( req , res ) = > {
8 db . query ( ’ SELECT * FROM students ’ , ( err , results ) = > {
9 if ( err ) throw err ;
10 res . render ( ’ index ’ , { students : results }) ;
11 }) ;
12 }) ;
13
14 app . post ( ’/ add ’ , ( req , res ) = > {
15 const { name , email } = req . body ;
16 db . query ( ’ INSERT INTO students ( name , email ) VALUES (? , ?) ’, [
name , email ] , ( err , results ) = > {
17 if ( err ) throw err ;
18 res . redirect ( ’/ ’) ;
19 }) ;
20 }) ;

3
21
22 app . post ( ’/ delete /: id ’ , ( req , res ) = > {
23 const { id } = req . params ;
24 db . query ( ’ DELETE FROM students WHERE id = ? ’ , [ id ] , ( err ,
results ) = > {
25 if ( err ) throw err ;
26 res . redirect ( ’/ ’) ;
27 }) ;
28 }) ;
29
30 app . listen (3000 , () = > {
31 console . log ( ’ Server running on port 3000 ’) ;
32 }) ;

2.4 4. Create EJS Views


Create a file views/index.ejs:
1 < h1 > Student List </ h1 >
2 < ul >
3 <% students . forEach ( student = > { % >
4 < li > < %= student . name % > ( < %= student . email % >)
5 < form action = " / delete / <%= student . id % > " method = " POST "
style = " display : inline ; " >
6 < button type = " submit " > Delete </ button >
7 </ form >
8 </ li >
9 <% }) % >
10 </ ul >
11
12 < h2 > Add Student </ h2 >
13 < form action = " / add " method = " POST " >
14 < input type = " text " name = " name " placeholder = " Name " required >
15 < input type = " email " name = " email " placeholder = " Email " required >
16 < button type = " submit " > Add </ button >
17 </ form >

2.5 5. Run Your Project


Start the server:
1 node app . js

Open your browser and navigate to http://localhost:3000. You should see


the list of students and have the ability to add and delete records using buttons.

3 Key Takeaways
• HeidiSQL is used to interact with MySQL databases graphically.
• Basic Queries: INSERT, SELECT, UPDATE, DELETE.
• Integration with Node.js: You can connect and interact with MySQL
databases using the mysql2 package.

4
• Express.js and EJS: Create routes and dynamic HTML views for handling
and displaying database records.

You might also like