[LINUX] Web system construction (super basic) ③: DB server construction and basic operation

Purpose

For the purpose of building a Web system, check the construction and operation of a super-basic DB server. (MySQL is used as the DB server.)

Environmental condition

--DB server - EC2:t2.micro - OS:Red Hat Enterprise Linux 8 (HVM), SSD Volume Type --Disk: General-purpose SSD (GP2) 10GB - MySQL:MySQL 8

The security group settings are nice.

Construction procedure

ec2-Login as user

Switch to root user
$ sudo su - 

Confirmation of existence of rpm
# yum info mysql*server

Check the version of installed MySQL
# mysqld --version
/usr/libexec/mysqld  Ver 8.0.17 for Linux on x86_64 (Source distribution)

Confirm that MySQL service is stopped
# service mysqld status
   Active: inactive (dead)Check the output of.

Start the MySQL service
# service mysqld start
Redirecting to /bin/systemctl start mysqld.service

Set password for MySQL root user
# mysql -uroot
> use mysql;
> ALTER USER 'root'@'localhost' identified BY 'password';
(The PASSWORD function seems to be abolished in MySQL 8.)

Confirm that you can log in with the set password
# mysql -uroot -ppassword

In addition, since it is not actual MySQL, mysql_secure_installation is not set.

Basic operation of DB server

Perform only the following simple operations.

  1. Create DB
  2. Create table
  3. Insert line
  4. Line update
  5. Delete line
  6. Search by joining multiple tables
  7. Truncate the table
  8. Delete table
  9. Delete DB

1. Create DB

Check existing DB
> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

Create a new DB
> create database test;

Confirm that a new DB has been created
> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+

2. Creating a table

Specify the DB to use
> use test;

Check existing table
> show tables;
Empty set

Create a table
> create table customer (
    -> id int,
    -> name varchar(255),
    -> age int,
    -> sex tinyint);

Check for the existence of the newly created table
> show tables;
+----------------+
| Tables_in_test |
+----------------+
| customer       |
+----------------+

3. Insert line

Check the rows that exist in the newly created table
> select * from customer;
Empty set

Add a row
> insert into customer values (1,"Yamada Taro",30,1);

Check the newly added line
> select * from customer;
+------+--------------+------+------+
| id   | name         | age  | sex  |
+------+--------------+------+------+
|    1 |Yamada Taro|   30 |    1 |
+------+--------------+------+------+

Add a few lines for subsequent operations
> insert into customer values (2,"Hanako Tanaka",25,2);
> insert into customer values (3,"Daisuke Suzuki",43,1);
> insert into customer values (4,"Akiko Yosano",99,2);
> select * from customer;
+------+-----------------+------+------+
| id   | name            | age  | sex  |
+------+-----------------+------+------+
|    1 |Yamada Taro|   30 |    1 |
|    2 |Hanako Tanaka|   25 |    2 |
|    3 |Daisuke Suzuki|   43 |    1 |
|    4 |Akiko Yosano|   99 |    2 |
+------+-----------------+------+------+

4. Line update

Check the information of Akiko Yosano to be updated
> select * from customer where id=4;
+------+-----------------+------+------+
| id   | name            | age  | sex  |
+------+-----------------+------+------+
|    4 |Akiko Yosano|   99 |    2 |
+------+-----------------+------+------+

Change the age of Akiko Yosano from 99 to 141
> update customer set age=141 where id=4;

Make sure your age has changed
> select * from customer where id=4;
+------+-----------------+------+------+
| id   | name            | age  | sex  |
+------+-----------------+------+------+
|    4 |Akiko Yosano|  141 |    2 |
+------+-----------------+------+------+

5. Delete line

Check the rows currently stored in the table
> select * from customer;
+------+-----------------+------+------+
| id   | name            | age  | sex  |
+------+-----------------+------+------+
|    1 |Yamada Taro|   30 |    1 |
|    2 |Hanako Tanaka|   25 |    2 |
|    3 |Daisuke Suzuki|   43 |    1 |
|    4 |Akiko Yosano|  141 |    2 |
+------+-----------------+------+------+

Delete the line of Akiko Yosano
> delete from customer where id=4;

Check the rows stored in the table after deletion
> select * from customer;
+------+--------------+------+------+
| id   | name         | age  | sex  |
+------+--------------+------+------+
|    1 |Yamada Taro|   30 |    1 |
|    2 |Hanako Tanaka|   25 |    2 |
|    3 |Daisuke Suzuki|   43 |    1 |
+------+--------------+------+------+

6. Search by joining multiple tables

Create another table and add rows
> create table sex(
    -> id tinyint,
    -> sex varchar(10));
> insert into sex values (1,"male");
> insert into sex values (2,"Female");
> select * from sex;
+------+--------+
| id   | sex    |
+------+--------+
|    1 |male|
|    2 |Female|
+------+--------+

Join the customer table and sex table with the sex and sex table id of the customer table,
Search for Japanese names by name and gender
> select customer.name, sex.sex from customer, sex where customer.sex=sex.id;
+--------------+--------+
| name         | sex    |
+--------------+--------+
|Yamada Taro|male|
|Hanako Tanaka|Female|
|Daisuke Suzuki|male|
+--------------+--------+

7. Truncate the table

Check for existing tables
> show tables;
+----------------+
| Tables_in_test |
+----------------+
| customer       |
| sex            |
+----------------+

Check the rows stored in the customer table
> select * from customer;
+------+--------------+------+------+
| id   | name         | age  | sex  |
+------+--------------+------+------+
|    1 |Yamada Taro|   30 |    1 |
|    2 |Hanako Tanaka|   25 |    2 |
|    3 |Daisuke Suzuki|   43 |    1 |
+------+--------------+------+------+

Truncate customer table
> truncate table customer;

Make sure the customer table is truncated
> select * from customer;
Empty set

8. Delete table

Check for existing tables
> show tables;
+----------------+
| Tables_in_test |
+----------------+
| customer       |
| sex            |
+----------------+

Delete sex table
> drop table sex;

Make sure the sex table has been deleted
> show tables;
+----------------+
| Tables_in_test |
+----------------+
| customer       |
+----------------+

9. Delete DB

Check for existing DB
> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+

Delete testDB
> drop database test;

Confirm that testDB has been deleted
> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

next time, Web system construction (super basic) ④: Web system construction is.

Recommended Posts

Web system construction (super basic) ③: DB server construction and basic operation
Web system construction (super basic) ②: AP server construction and basic operation
Web server construction commentary
One Liner Web Server
Ubuntu (18.04.3) Web server construction
Web system construction (super basic) ③: DB server construction and basic operation
Merry Christmas web server
Web system construction (super basic) ②: AP server construction and basic operation
Web server construction commentary
Linux Web server construction (Ubuntu & Apache)
Fastest and strongest web server architecture
Python basic operation 3rd: Object-oriented and class
Web server construction with Apache 2.4 (httpd 2.4.43) + PHP 7.4 on Linux ―― 4. Security (chown and firewalld)
Effective and simple Web server security measures "Linux"
[Python] Web application from 0! Hands-on (1) -Design, DB construction-
Launch a web server with Python and Flask
From 0 to Django development environment construction to basic operation
CentOS8 server construction (network opening and package update)
Basic operation of Python Pandas Series and Dataframe (1)