1. 程式人生 > >05: iSCSI技術應用 資料庫服務基礎 管理表資料 總結和答疑

05: iSCSI技術應用 資料庫服務基礎 管理表資料 總結和答疑

Top

NSD ENGINEER DAY05

  1. 案例1:釋出iSCSI網路磁碟
  2. 案例2:訪問iSCSI網路磁碟
  3. 案例3:搭建mariadb資料庫系統
  4. 案例4:配置一個數據庫
  5. 案例5:使用資料庫查詢

1 案例1:釋出iSCSI網路磁碟

1.1 問題

本例要求配置 server0 提供 iSCSI 服務,要求如下:

  1. 磁碟名為iqn.2016-02.com.example:server0
  2. 服務埠為 3260
  3. 使用 iscsi_store 作其後端卷,其大小為 3GiB
  4. 此磁碟服務只能被 desktop0.example.com 訪問

方案

首先利用parted或fdisk工具劃分分割槽

安裝targetcli服務端軟體,執行targetcli命令進行配置

  1. 建立後端儲存
  2. 建立iqn物件
  3. 將iqn與後端儲存繫結
  4. 授權客戶端的iqn
  5. 指定監聽地址及本機埠

1.2 步驟

實現此案例需要按照如下步驟進行。

步驟一:準備磁碟空間,劃分分割槽

1)利用parted命令劃分/dev/vdb

 
  1. [[email protected] /]# parted /dev/vdb
  2. (parted) mktable gpt //指定分割槽模式為gpt
  3. (parted) mkpart primary ext4 0 3GiB
  4. 警告: The resulting partition is not properly aligned for best performance.
  5. 忽略/Ignore/放棄/Cancel? Ignore
  6. (parted) print
  7. ……
  8. Number Start End Size File system Name 標誌
  9. 1 17.4kB 3221MB 3221MB primary
  10.  
  11. (parted) quit

2)命令列檢視分割槽資訊

 
  1. [[email protected] /]# lsblk
  2. NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
  3. ……
  4. └─vdb1 252:17 0 3G 0 part ln -s

3)檢視分割槽裝置檔案

 
  1. [[email protected] /]# ls /dev/vdb1
  2. /dev/vdb1

步驟二:安裝與配置iSCSI磁碟

1)安裝軟體包targetcli

 
  1. [[email protected] ~]# yum -y install targetcli

2)命令列執行targetcli配置iSCSI磁碟

 
  1. [[email protected] /]# targetcli
  2. /> backstores/block create iscsi_store /dev/vdb1 //建立後端儲存
  3. /> iscsi/ create iqn.2016-02.com.example:server0 //建立iqn物件
  4. /> iscsi/iqn.2016-02.com.example:server0/tpg1/luns create /backstores/block/iscsi_store //將iqn物件與後端儲存繫結
  5. /> iscsi/iqn.2016-02.com.example:server0/tpg1/acls create iqn.2016-02.com.example:desktop0 //設定客戶端的iqn
  6. /> iscsi/iqn.2016-02.com.example:server0/tpg1/portals/ create 0.0.0.0 3260 //開啟本地監聽的IP地址與埠
  7. /> exit

3)啟動target服務,並設定為開機自啟

 
  1. [[email protected] /]# systemctl restart target
  2. [[email protected] /]# systemctl enable target
  3. Created symlink from /etc/systemd/system/multi-user.target.wants/target.service to /usr/lib/systemd/system/target.service.

2 案例2:訪問iSCSI網路磁碟

2.1 問題

配置 desktop0 使用 iSCSI 磁碟,要求如下:

  1. 目標磁碟組為 iqn.2016-02.com.example:server0
  2. 此 iSCSI 裝置在系統啟動的期間自動載入
  3. 此 iSCSI 塊裝置上包含一個大小為 2100MiB 的分割槽,並將其格式化為 ext4 檔案系統
  4. 此分割槽應該掛載到 /mnt/data 上,同時在系統啟動的期間自動掛載

方案

首先按住客戶端軟體iscsi-initiator-utils

  1. 設定客戶端iqn
  2. 重啟iscsid服務重新整理客戶端iqn
  3. 發現服務端iSCSI網路磁碟
  4. 使用iSCSI共享磁碟空間

2.2 步驟

實現此案例需要按照如下步驟進行。

步驟一:安裝客戶端軟體iscsi-initiator-utils

1)利用yum安裝軟體

 
  1. [[email protected] /]# yum repolist //生成yum倉庫資訊快取
  2. [[email protected] /]# yum -y install iscsi-initiator-utils.i686 //在生成yum倉庫資訊快取後,軟體包名可以補全(tab)

步驟二:設定客戶端iqn名稱

1)修改配置檔案

 
  1. [[email protected] /]# vim /etc/iscsi/initiatorname.iscsi
  2. InitiatorName=iqn.2016-02.com.example:desktop0

2)重啟iscsid服務重新整理iqn名稱

 
  1. [[email protected] /]# systemctl restart iscsid
  2. Warning: iscsid.service changed on disk. Run 'systemctl daemon-reload' to reload units.
  3. [[email protected] /]# systemctl daemon-reload
  4. [[email protected] /]# systemctl restart iscsid
  5. [[email protected] /]# systemctl enable iscsid
  6. Created symlink from /etc/systemd/system/multi-user.target.wants/iscsid.service to /usr/lib/systemd/system/iscsid.service.

步驟三:發現服務端iSCSI網路磁碟

1)執行發現iSCSI網路磁碟命令

 
  1. [[email protected] /]# iscsiadm --mode discoverydb --type sendtargets --portal 172.25.0.11 –discover //此命令較長可以通過man iscsiadm檢視幫助資訊
  2.  
  3. 172.25.0.11:3260,1 iqn.2016-02.com.example:server0

2)重啟iscsi服務載入iSCSI網路磁碟

 
  1. [[email protected] /]# systemctl restart iscsi
  2. [[email protected] /]# lsblk
  3. NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
  4. sda 8:0 0 3G 0 disk
  5. [[email protected] /]# systemctl enable iscsi

步驟四:客戶端使用iSCSI網路磁碟

1)劃分分割槽

 
  1. [[email protected] /]# parted /dev/sda
  2. (parted) mktable gpt
  3. (parted) mkpart primary ext4 0 2100MiB
  4. 警告: The resulting partition is not properly aligned for best performance.
  5. 忽略/Ignore/放棄/Cancel? Ignore
  6. (parted) print
  7. (parted) exit
  8. [[email protected] /]# partprobe /dev/sda //重新整理分割槽表
  9. [[email protected] /]# lsblk
  10. NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
  11. sda 8:0 0 3G 0 disk
  12. └─sda1 8:1 0 2.1G 0 part

2)格式化使用分割槽,利用UUID實現開機自動掛載

 
  1. [[email protected] /]# mkfs.ext4 /dev/sda1
  2. [[email protected] /]# blkid /dev/sda1 #檢視分割槽UUID值
  3. /dev/sda1: UUID="402d1304-5781-4349-8c43-ba45eba8b427" TYPE="ext4" PARTLABEL="primary" PARTUUID="2e41f84d-5123-4511-aaca-7e779087579b"
  4. [[email protected] /]# mkdir /mnt/data
  5. [[email protected] /]# vim /etc/fstab
  6. UUID="402d1304-5781-4349-8c43-ba45eba8b427" /mnt/data ext4 defaults,_netdev 0 0
  7. [[email protected] /]# mount -a
  8. [[email protected] /]# df -h
  9. 檔案系統 容量 已用 可用 已用% 掛載點
  10. ……
  11. /dev/sda1 2.0G 6.2M 1.9G 1% /mnt/data

3 案例3:搭建mariadb資料庫系統

3.1 問題

本例要求在虛擬機器server0上安裝 MariaDB 資料庫系統:

  1. 安裝 mariadb-server、mariadb 軟體包
  2. 啟動 mariadb 服務,並確認監聽狀態

然後在客戶端訪問此資料庫服務:

  1. 使用 mysql 命令訪問本機的資料庫服務,使用者名稱為 root,密碼為空
  2. 執行 SHOW DATABASES; 指令列出有哪些庫
  3. 退出 mysql 互動介面

3.2 方案

資料庫表及相關軟體的基本知識:

  • 資料(記錄):用來表示一個事物(實體)的一些資訊(屬性)的文字/圖片檔案等,例如字串“:tedu.cn”
  • 資料表:存放很多條資料記錄的容器,例如學員聯絡資訊表、學員月考成績表
  • 資料表的每一行:存放一條記錄
  • 資料表的每一列/欄位:很多個事物的同一個屬性
  • 資料庫:存放很多個相互關聯的表格的容器,例如NSD1609學員檔案庫
  • 資料庫管理系統(DBMS):用來管理(建立庫/新增/查詢/刪除/授權等)資料庫資訊的軟體平臺

MariaDB服務端:軟體包mariadb-server、系統服務mariadb

MariaDB客戶端:軟體包mariadb、管理工具mysql

MariaDB服務端配置檔案:/etc/my.cnf

傳輸協議及埠:TCP 3306

mysql命令的簡單用法:

 
  1. mysql [-u使用者名稱] [-p[密碼]]

3.3 步驟

實現此案例需要按照如下步驟進行。

步驟一:搭建MariaDB資料庫伺服器

1)安裝軟體包mariadb-server、mariadb

 
  1. [[email protected] ~]# yum -y install mariadb-server mariadb
  2. .. ..

2)啟動系統服務mariadb,並設定開機自啟

 
  1. [[email protected] ~]# systemctl restart mariadb
  2. [[email protected] ~]# systemctl enable mariadb
  3. ln -s '/usr/lib/systemd/system/mariadb.service' '/etc/systemd/system/multi-user.target.wants/mariadb.service'

3)檢查監聽狀態

 
  1. [[email protected] ~]# netstat -antpu | grep :3306
  2. tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2922/mysqld

步驟二:訪問本機的MariaDB資料庫系統

1)以使用者root連線本機的mariadb(或mysqld)資料庫服務

 
  1. [[email protected] ~]# mysql -uroot
  2. Welcome to the MariaDB monitor. Commands end with ; or \g.
  3. Your MariaDB connection id is 3
  4. Server version: 5.5.35-MariaDB MariaDB Server
  5.  
  6. Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
  7.  
  8. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  9.  
  10. MariaDB [(none)]>

2)檢視當前資料庫系統內有哪些庫

 
  1. MariaDB [(none)]> SHOW DATABASES;
  2. +--------------------+
  3. | Database |
  4. +--------------------+
  5. | information_schema |
  6. | mysql |
  7. | performance_schema |
  8. | test |
  9. +--------------------+
  10. 4 rows in set (0.00 sec)

3)退出操作環境

 
  1. MariaDB [(none)]> QUIT
  2. Bye
  3. [[email protected] ~]#

4 案例4:配置一個數據庫

4.1 問題

本例要求在虛擬機器server0上部署 MariaDB 資料庫,具體要求如下:

  1. 此資料庫系統只能被 localhost 訪問
  2. 新建一個數據庫名為 Contacts,其中應該包含來自資料庫複製的內容,複製檔案的 URL 為:http://classroom/pub/materials/users.sql
  3. 除了 root 使用者,此資料庫只能被使用者 Raikon 查詢,此使用者的密碼為atenorth
  4. root使用者的密碼為 atenorth

4.2 方案

為資料庫賬號修改密碼:

 
  1. mysqladmin [-u使用者名稱] [-p[舊密碼]] password '新密碼'

匯入/恢復到資料庫:

 
  1. mysql [-u使用者名稱] [-p[密碼]] 資料庫名 < 備份檔案.sql

為資料庫使用者授權/撤銷許可權:

 
  1. grant 許可權1,許可權2... on 庫名.表名 to 使用者名稱@客戶機地址 identified by '密碼';
  2. revoke 許可權1,許可權2... on 庫名.表名 from 使用者名稱@客戶機地址;

4.3 步驟

實現此案例需要按照如下步驟進行。

步驟一:禁止mariadb服務提供網路監聽(只服務於本機)

1)修改配置檔案

 
  1. [[email protected] ~]# vim /etc/my.cnf
  2. [mysqld]
  3. skip-networking                                         //跳過網路

2)重啟mariadb服務

 
  1. [[email protected] ~]# systemctl restart mariadb             //重啟服務

3)確認結果

 
  1. [[email protected] ~]# netstat -anptu | grep :3306         //已經不提供埠監聽
  2. [[email protected] ~]# pgrep -l mysqld                     //但程序仍在
  3. 3127 mysqld_safe
  4. 3297 mysqld

步驟二:配置資料庫管理密碼

1)使用mysqladmin為使用者root設定密碼

原管理賬號root的密碼為空,因此無需驗證舊密碼:

 
  1. [[email protected] ~]# mysqladmin -u root password 'atenorth'

2)驗證新密碼是否可用

root使用空密碼從本機連線將會失敗:

 
  1. [[email protected] ~]# mysql -uroot
  2. ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

必須指定正確的新密碼才能連線成功:

 
  1. [[email protected] ~]# mysql -uroot -patenorth
  2. Welcome to the MariaDB monitor. Commands end with ; or \g.
  3. Your MariaDB connection id is 4
  4. Server version: 5.5.35-MariaDB MariaDB Server
  5.  
  6. Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
  7.  
  8. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  9.  
  10. .. ..

步驟三:建Contacts庫並匯入備份資料

1)建立新庫Contacts,並退出操作環境

 
  1. MariaDB [(none)]> CREATE DATABASE Contacts;
  2. Query OK, 1 row affected (0.00 sec)
  3.  
  4. MariaDB [(none)]> QUIT
  5. Bye

2)下載指定的資料庫備份

 
  1. [[email protected] ~]# wget http://classroom.example.com/pub/materials/users.sql
  2. --2016-11-26 19:00:37-- http://classroom.example.com/pub/materials/users.sql
  3. Resolving classroom.example.com (classroom.example.com)... 172.25.254.254
  4. Connecting to classroom.example.com (classroom.example.com)|172.25.254.254|:80... connected.
  5. HTTP request sent, awaiting response... 200 OK
  6. Length: 2634 (2.6K) [application/sql]
  7. Saving to: ‘users.sql’
  8.  
  9. 100%[=====================>] 2,634 --.-K/s in 0s
  10.  
  11. 2016-11-26 19:00:37 (269 MB/s) - ‘users.sql’ saved [2634/2634]
  12.  
  13. [[email protected] ~]# ls -lh users.sql                     //確認下載的檔案
  14. -rw-r--r--. 1 root root 2.6K Mar 31 2016 users.sql

3)匯入資料庫

 
  1. [[email protected] ~]# mysql -uroot -patenorth Contacts < users.sql

4)重新連入操作環境,確認匯入結果

 
  1. [[email protected] ~]# mysql -uroot -patenorth
  2. .. ..
  3. MariaDB [(none)]> USE Contacts;                     //使用指定庫
  4. Database changed
  5.  
  6. MariaDB [Contacts]> SHOW TABLES;                 //列出有哪些表
  7. +--------------------+
  8. | Tables_in_Contacts |
  9. +--------------------+
  10. | base |
  11. | location |
  12. +--------------------+
  13. 2 rows in set (0.00 sec)

步驟四:為Contacts庫授權

1)允許使用者Raikon從本機訪問,具有查詢許可權,密碼為atenorth

 
  1. MariaDB [Contacts]> GRANT select ON Contacts.* TO [email protected] IDENTIFIED BY 'atenorth';
  2. Query OK, 0 rows affected (0.00 sec)

2)退出操作環境

 
  1. MariaDB [Contacts]> QUIT
  2. Bye
  3. [[email protected] ~]#

5 案例5:使用資料庫查詢

5.1 問題

本例要求配置MariaDB資料庫,完成以下任務:

  1. 禁止空密碼root使用者訪問mariadb資料庫
  2. 在系統server0上使用資料庫Contacts,通過SQL查詢回答下列問題:密碼是solicitous的人的名字?有多少人的姓名是Barbara同時居住在 Sunnyvale?

5.2 方案

表記錄增刪改查:

 
  1. insert into [庫名.]表名 values(值1,值2,值3);
  2. delete from [庫名.]表名 where ...;
  3. update [庫名.]表名 set 欄位名=欄位值 where ....;
  4. select 欄位列表 from [庫名.]表名 where 欄位名1=值 and|or 欄位名2=值;

統計查詢結果的數量:

 
  1. select count(*) from [庫名.]表名 where .. ..;

5.3 步驟

實現此案例需要按照如下步驟進行。

步驟一:清理空密碼root使用者

1)確認空密碼root使用者記錄

MariaDB服務端預設的mysql庫user表儲存了使用者授權記錄。

使用DESC指令查看錶結構,以便了解相關欄位名:

 
  1. MariaDB [(none)]> DESC mysql.user;
  2. +------------------------+-----------------------------------+------+-----+---------+-------+
  3. | Field | Type | Null | Key | Default | Extra |
  4. +------------------------+-----------------------------------+------+-----+---------+-------+
  5. | Host | char(60) | NO | PRI | | |
  6. | User | char(16) | NO | PRI | | |
  7. | Password | char(41) | NO | | | |

列出user表中的Host、User、Password欄位,限定密碼為空的root使用者:

 
  1. MariaDB [(none)]> SELECT Host,User,Password FROM mysql.user WHERE User='root' AND Password='';
  2. +---------------------+------+----------+
  3. | Host | User | Password |
  4. +---------------------+------+----------+
  5. | server0.example.com | root | |
  6. | 127.0.0.1 | root | |
  7. | ::1 | root | |
  8. +---------------------+------+----------+
  9. 3 rows in set (0.00 sec)
  10.  
  11. MariaDB [(none)]>

2)刪除空密碼root使用者記錄

使用DELETE指令刪除掉需要清除的授權記錄:

 
  1. MariaDB [(none)]> DELETE FROM mysql.user WHERE User='root' AND Password='';
  2. Query OK, 3 rows affected (0.00 sec)

再次查詢,確認刪除結果:

 
  1. MariaDB [(none)]> SELECT Host,User,Password FROM mysql.user WHERE User='root' AND Password='';
  2. Empty set (0.00 sec)

步驟二:按條件查詢表記錄

1)按單個條件查詢

找出密碼是solicitous的人的名字?

 
  1. MariaDB [(none)]> SELECT name FROM Contacts.base WHERE Password='solicitous';
  2. +-------+
  3. | name |
  4. +-------+
  5. | James |
  6. +-------+
  7. 1 row in set (0.00 sec)

2)按多個條件在關聯的兩張表中查詢

有多少人的姓名是Barbara同時居住在 Sunnyvale?

 
  1. MariaDB [(none)]> USE Contacts;
  2. .. ..
  3. Database changed
  4. MariaDB [Contacts]> SELECT COUNT(*) FROM base,location WHERE base.name='Barbara' AND location.city='Sunnyvale' AND base.id=location.id;
  5. +----------+
  6. | COUNT(*) |
  7. +----------+
  8. | 1 |
  9. +----------+
  10. 1 row in set (0.00 sec)