LoginSignup
2
4

More than 1 year has passed since last update.

Webシステム構築(超基礎)③:DBサーバ構築と基本動作

Last updated at Posted at 2019-12-26

目的

Webシステム構築を目的として、超基礎的なDBサーバの構築と動作を確認する。
(DBサーバとしてはMySQLを利用する。)

環境条件

  • DBサーバ
    • EC2:t2.micro
    • OS:Red Hat Enterprise Linux 8 (HVM), SSD Volume Type
    • Disk:汎用SSD(GP2) 10GB
    • MySQL:MySQL 8

セキュリティグループの設定等はいい感じに。

構築手順

ec2-userでログイン

rootユーザにスイッチ
$ sudo su - 

rpmの存在確認
# yum info mysql*server

インストール
# yum install mysql-server

インストールされたMySQLのバージョン確認
# mysqld --version
/usr/libexec/mysqld  Ver 8.0.17 for Linux on x86_64 (Source distribution)

MySQLサービスの停止を確認
# service mysqld status
   Active: inactive (dead)の出力を確認する。

MySQLサービスを起動する
# service mysqld start
Redirecting to /bin/systemctl start mysqld.service

MySQLのrootユーザのパスワード設定
# mysql -uroot
> use mysql;
> ALTER USER 'root'@'localhost' identified BY 'password';
(PASSWORD関数はMySQL8では廃止されている様。)

設定したパスワードでログインできることを確認
# mysql -uroot -ppassword

尚、実運用のMySQLではないので、mysql_secure_installationの設定などは実施しない。

DBサーバの基本動作

以下の簡単な操作だけ実施する。
1. DB作成
2. 表作成
3. 行の挿入
4. 行の更新
5. 行の削除
6. 複数表を結合した検索
7. 表のトランケート
8. 表の削除
9. DBの削除

1. DB作成

既存のDBの確認
> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

新しいDBの作成
> create database test;

新しいDBが作成されていることを確認
> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+

2. 表の作成

利用するDBを指定する
> use test;

既存の表を確認する
> show tables;
Empty set

表を作成する
> create table customer (
    -> id int,
    -> name varchar(255),
    -> age int,
    -> sex tinyint);

新しく作成した表の存在を確認する
> show tables;
+----------------+
| Tables_in_test |
+----------------+
| customer       |
+----------------+

3. 行の挿入

新しく作成した表に存在する行を確認する
> select * from customer;
Empty set

行を追加する
> insert into customer values (1,"山田太郎",30,1);

新しく追加した行を確認する
> select * from customer;
+------+--------------+------+------+
| id   | name         | age  | sex  |
+------+--------------+------+------+
|    1 | 山田太郎     |   30 |    1 |
+------+--------------+------+------+

この後のオペレーションのために数行追加
> insert into customer values (2,"田中花子",25,2);
> insert into customer values (3,"鈴木大輔",43,1);
> insert into customer values (4,"与謝野晶子",99,2);
> select * from customer;
+------+-----------------+------+------+
| id   | name            | age  | sex  |
+------+-----------------+------+------+
|    1 | 山田太郎        |   30 |    1 |
|    2 | 田中花子        |   25 |    2 |
|    3 | 鈴木大輔        |   43 |    1 |
|    4 | 与謝野晶子      |   99 |    2 |
+------+-----------------+------+------+

4. 行の更新

更新対象の与謝野晶子さんの情報を確認する
> select * from customer where id=4;
+------+-----------------+------+------+
| id   | name            | age  | sex  |
+------+-----------------+------+------+
|    4 | 与謝野晶子      |   99 |    2 |
+------+-----------------+------+------+

与謝野晶子さんの年齢を99から141に変更する
> update customer set age=141 where id=4;

年齢が変更されていることを確認する
> select * from customer where id=4;
+------+-----------------+------+------+
| id   | name            | age  | sex  |
+------+-----------------+------+------+
|    4 | 与謝野晶子      |  141 |    2 |
+------+-----------------+------+------+

5. 行の削除

現在表に格納されている行を確認する
> select * from customer;
+------+-----------------+------+------+
| id   | name            | age  | sex  |
+------+-----------------+------+------+
|    1 | 山田太郎        |   30 |    1 |
|    2 | 田中花子        |   25 |    2 |
|    3 | 鈴木大輔        |   43 |    1 |
|    4 | 与謝野晶子      |  141 |    2 |
+------+-----------------+------+------+

与謝野晶子さんの行を削除する
> delete from customer where id=4;

削除後の表に格納されている行を確認する
> select * from customer;
+------+--------------+------+------+
| id   | name         | age  | sex  |
+------+--------------+------+------+
|    1 | 山田太郎     |   30 |    1 |
|    2 | 田中花子     |   25 |    2 |
|    3 | 鈴木大輔     |   43 |    1 |
+------+--------------+------+------+

6. 複数表を結合した検索

もう一つ表を作成し、行を追加する
> create table sex(
    -> id tinyint,
    -> sex varchar(10));
> insert into sex values (1,"男性");
> insert into sex values (2,"女性");
> select * from sex;
+------+--------+
| id   | sex    |
+------+--------+
|    1 | 男性   |
|    2 | 女性   |
+------+--------+

customer表とsex表をcustomer表のsexとsex表のidで結合し、
名前と性別の日本語名称を検索する
> select customer.name, sex.sex from customer, sex where customer.sex=sex.id;
+--------------+--------+
| name         | sex    |
+--------------+--------+
| 山田太郎     | 男性   |
| 田中花子     | 女性   |
| 鈴木大輔     | 男性   |
+--------------+--------+

7. 表のトランケート

存在する表の確認
> show tables;
+----------------+
| Tables_in_test |
+----------------+
| customer       |
| sex            |
+----------------+

customer表に格納されている行の確認
> select * from customer;
+------+--------------+------+------+
| id   | name         | age  | sex  |
+------+--------------+------+------+
|    1 | 山田太郎     |   30 |    1 |
|    2 | 田中花子     |   25 |    2 |
|    3 | 鈴木大輔     |   43 |    1 |
+------+--------------+------+------+

customer表のトランケート
> truncate table customer;

customer表がトランケートされていることを確認
> select * from customer;
Empty set

8. 表の削除

存在する表の確認
> show tables;
+----------------+
| Tables_in_test |
+----------------+
| customer       |
| sex            |
+----------------+

sex表の削除
> drop table sex;

sex表が削除されていることを確認
> show tables;
+----------------+
| Tables_in_test |
+----------------+
| customer       |
+----------------+

9. DBの削除

存在するDBの確認
> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+

testDBの削除
> drop database test;

testDBが削除されていることを確認
> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

次回は、
Webシステム構築(超基礎)④:Webシステムの構築
です。

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4