Last update: 2021/03/08 03:49 

SQL 実習環境の整備

LPIC-1 で出題されるSQLを試す環境の整備。

MySQLの導入

CentOSでは標準でMariaDB(MySQL)とPostgreSQLが使用できます。ここではMySQLを使った実習環境整備を解説します。
  1. パッケージのインストール
    MySQL サーバのパッケージをインストールします。
    # yum install  mariadb-server
    
  2. 初期設定
    mysql_install_db を使い、MySQL サーバの初期設定を行います。
    MySQL内で使用するユーザは mysql とします。
    # mysql_install_db --user=mysql
    # systemctl start mariadb
    

実習環境の作成

続いて MySQL 上に実習のデータベース作成し、サンプルのテーブル2つを作ります。
  1. DBの作成
    作業領域であるデータベースLAを作成します。
    # mysqladmin -u root password 'mysql'
    # mysqladmin -u root -pmysql create LA
    # mysqlshow -pmysql
    +--------------------+
    |     Databases      |
    +--------------------+
    | information_schema |
    | LA                 |
    | mysql              |
    | performance_schema |
    | test               |
    +--------------------+
    
  2. ユーザの作成
    データベースを操作するためのデータベースユーザを作成します。
    # mysql -u root -pmysql
    Welcome to the MariaDB monitor.  Commands end with ; or \g.
    Your MariaDB connection id is 5
    Server version: 5.5.68-MariaDB MariaDB Server
    
    Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    MariaDB [(none)]> grant all privileges on LA.* to mysql@localhost identified by 'mysql';
    Query OK, 0 rows affected (0.00 sec)
    
    MariaDB [(none)]> quit
    Bye
    
    
  3. クライアント起動
    クライアント mysql が起動できることを確認します
    # mysql -u mysql -pmysql
    Welcome to the MariaDB monitor.  Commands end with ; or \g.
    Your MariaDB connection id is 7
    Server version: 5.5.68-MariaDB MariaDB Server
    
    Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    MariaDB [(none)]> quit
    Bye
    
  4. テーブルの作成
    サンプルとしてテーブル staff と books を作成し、データを挿入します。
    mysql(1)を起動し、その中で行います。
    $ mysql –u mysql -pmysql LA
    mysql> CREATE TABLE staff (
     ->	id	int(3),
     ->	name text,
     ->	branch	text,
     ->	age	int(3),
     ->	PRIMARY KEY (id)
     -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT 'LPIC-1 Sample';
     -> INSERT INTO staff (id,name,branch,age) VALUES
     -> ('1','Aoki','Tokyo','26'),
     -> ('2','Baba','Yokohama','31'),
     -> ('3','Chihara','Nagoya','20'),
     -> ('4','Deguchi','Yokohama','33'),
     -> ('5','Endo','Osaka','25'),
     -> ('6','Fujikawa','Tokyo','38'),
     -> ('10','Hato','Tokyo','19'),
     -> ('11','Abe','Yokohama','29');
    
    mysql> CREATE TABLE IF NOT EXISTS books (
     ->	id		int(3),
     ->	category	text,
     ->	title		text,
     ->	auther		text,
     ->	PRIMARY KEY (id)
     -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT 'LPIC-1 14-1';
     -> INSERT INTO books (id,category,title,auther) VALUES
     -> ('1','Linux','Shell Script Handbook','Ichiro Kato'),
     -> ('2','CCNA','Cisco Unifid Communication','Taro Yamada'),
     -> ('3','Linux','The NFS administration','Ken Tompson'),
     -> ('4','Programming','PHP basics','John Hollbe'),
     -> ('5','Programming','Introduction Ruby','Ichiyo Takano'),
     -> ('6','CCNA','The Layer 2','Kenichi Matsuda');
    
    mysql> quit;
    
これらはスクリプトとして提供しています。
http://ycos2.sakura.ne.jp/LA/LPIC/sqlsetup.sh
リンク先を保存し、実行できます。
# bash sqlsetup.sh
 (上記の作業全てを一括して実行)
# mysql -u mysql -pmysql LA
(SQL の演習)

Copyright 2008-2021 ycos Systems[/LA/05.LINC1/Sec14-setup.html]
[ Back ]