Module 9 - Assignment

Problem Statement: You work for xyz organization. Your job work is to manage Linux-based servers.

You have been asked to:

  1. Install MySQL on CentOS
  2. Create a database with two tables - table1 & table2
  3. Create a shell script which can be used to insert 4 rows of data into table1 & table2

Update packages:

sudo yum update -y

Install mysql:

sudo yum install mysql-server -y

Start the service and confirm it is up

sudo systemctl start mysqld
sudo systemctl status mysqld

Run mysql_secure_installation for security-related operations like configuring a password

sudo mysql_secure_installation

Log in to the MySQL shell as the root user: mysql -u root -p using previously configured password

Step 1: Create a Database

CREATE DATABASE mydatabase;

Step 2: Create Tables

USE mydatabase;  

CREATE TABLE table1 (
  id INT PRIMARY KEY AUTO_INCREMENT,
  name VARCHAR(255),   
  age INT 
);  
  
CREATE TABLE table2 (
  id INT PRIMARY KEY AUTO_INCREMENT,
  product VARCHAR(255),   
  price FLOAT
);

3. Create a Shell Script for Inserting Data:

Created script named mysql_script.sh with the following content using vi:

#!/bin/bash
 
# Log in to the MySQL server and insert data into table1 and table2
mysql -u root -p -D 'mydatabase' <<EOF
 
INSERT INTO table1 (name, age) VALUES ('Alice', 30);
INSERT INTO table1 (name, age) VALUES ('Bob', 40);
INSERT INTO table1 (name, age) VALUES ('Charlie', 50);
INSERT INTO table1 (name, age) VALUES ('Dave', 60);
 
INSERT INTO table2 (product, price) VALUES ('Laptop', 1000);
INSERT INTO table2 (product, price) VALUES ('Phone', 500);
INSERT INTO table2 (product, price) VALUES ('TV', 1500);
INSERT INTO table2 (product, price) VALUES ('Camera', 800);
 
EOF

Giving script execution permissions:

chmod +x insert_data.sh

Executing the script:

./insert_data.sh

Prompts for password

Confirming input:

  1. Logging to MySQL shell as the root user:
mysql -u root -p

prompted for password

  1. Switch to database:
USE mydatabase;
  1. Checking data in table1:
SELECT * FROM table1;
  1. Checking data in table2:
SELECT * FROM table2;