05: iSCSI技術應用 資料庫服務基礎 管理表資料 總結和答疑
NSD ENGINEER DAY05
1 案例1:釋出iSCSI網路磁碟
1.1 問題
本例要求配置 server0 提供 iSCSI 服務,要求如下:
- 磁碟名為iqn.2016-02.com.example:server0
- 服務埠為 3260
- 使用 iscsi_store 作其後端卷,其大小為 3GiB
- 此磁碟服務只能被 desktop0.example.com 訪問
方案
首先利用parted或fdisk工具劃分分割槽
安裝targetcli服務端軟體,執行targetcli命令進行配置
- 建立後端儲存
- 建立iqn物件
- 將iqn與後端儲存繫結
- 授權客戶端的iqn
- 指定監聽地址及本機埠
1.2 步驟
實現此案例需要按照如下步驟進行。
步驟一:準備磁碟空間,劃分分割槽
1)利用parted命令劃分/dev/vdb
- [[email protected] /]# parted /dev/vdb
- (parted) mktable gpt //指定分割槽模式為gpt
- (parted) mkpart primary ext4 0 3GiB
- 警告: The resulting partition is not properly aligned for best performance.
- 忽略/Ignore/放棄/Cancel? Ignore
- (parted) print
- ……
- Number Start End Size File system Name 標誌
- 1 17.4kB 3221MB 3221MB primary
- (parted) quit
2)命令列檢視分割槽資訊
- [[email protected] /]# lsblk
- NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
- ……
- └─vdb1 252:17 0 3G 0 part ln -s
3)檢視分割槽裝置檔案
- [[email protected] /]# ls /dev/vdb1
- /dev/vdb1
步驟二:安裝與配置iSCSI磁碟
1)安裝軟體包targetcli
- [[email protected] ~]# yum -y install targetcli
2)命令列執行targetcli配置iSCSI磁碟
- [[email protected] /]# targetcli
- /> backstores/block create iscsi_store /dev/vdb1 //建立後端儲存
- /> iscsi/ create iqn.2016-02.com.example:server0 //建立iqn物件
- /> iscsi/iqn.2016-02.com.example:server0/tpg1/luns create /backstores/block/iscsi_store //將iqn物件與後端儲存繫結
- /> iscsi/iqn.2016-02.com.example:server0/tpg1/acls create iqn.2016-02.com.example:desktop0 //設定客戶端的iqn
- /> iscsi/iqn.2016-02.com.example:server0/tpg1/portals/ create 0.0.0.0 3260 //開啟本地監聽的IP地址與埠
- /> exit
3)啟動target服務,並設定為開機自啟
- [[email protected] /]# systemctl restart target
- [[email protected] /]# systemctl enable target
- 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 磁碟,要求如下:
- 目標磁碟組為 iqn.2016-02.com.example:server0
- 此 iSCSI 裝置在系統啟動的期間自動載入
- 此 iSCSI 塊裝置上包含一個大小為 2100MiB 的分割槽,並將其格式化為 ext4 檔案系統
- 此分割槽應該掛載到 /mnt/data 上,同時在系統啟動的期間自動掛載
方案
首先按住客戶端軟體iscsi-initiator-utils
- 設定客戶端iqn
- 重啟iscsid服務重新整理客戶端iqn
- 發現服務端iSCSI網路磁碟
- 使用iSCSI共享磁碟空間
2.2 步驟
實現此案例需要按照如下步驟進行。
步驟一:安裝客戶端軟體iscsi-initiator-utils
1)利用yum安裝軟體
- [[email protected] /]# yum repolist //生成yum倉庫資訊快取
- [[email protected] /]# yum -y install iscsi-initiator-utils.i686 //在生成yum倉庫資訊快取後,軟體包名可以補全(tab)
步驟二:設定客戶端iqn名稱
1)修改配置檔案
- [[email protected] /]# vim /etc/iscsi/initiatorname.iscsi
- InitiatorName=iqn.2016-02.com.example:desktop0
2)重啟iscsid服務重新整理iqn名稱
- [[email protected] /]# systemctl restart iscsid
- Warning: iscsid.service changed on disk. Run 'systemctl daemon-reload' to reload units.
- [[email protected] /]# systemctl daemon-reload
- [[email protected] /]# systemctl restart iscsid
- [[email protected] /]# systemctl enable iscsid
- Created symlink from /etc/systemd/system/multi-user.target.wants/iscsid.service to /usr/lib/systemd/system/iscsid.service.
步驟三:發現服務端iSCSI網路磁碟
1)執行發現iSCSI網路磁碟命令
- [[email protected] /]# iscsiadm --mode discoverydb --type sendtargets --portal 172.25.0.11 –discover //此命令較長可以通過man iscsiadm檢視幫助資訊
- 172.25.0.11:3260,1 iqn.2016-02.com.example:server0
2)重啟iscsi服務載入iSCSI網路磁碟
- [[email protected] /]# systemctl restart iscsi
- [[email protected] /]# lsblk
- NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
- sda 8:0 0 3G 0 disk
- [[email protected] /]# systemctl enable iscsi
步驟四:客戶端使用iSCSI網路磁碟
1)劃分分割槽
- [[email protected] /]# parted /dev/sda
- (parted) mktable gpt
- (parted) mkpart primary ext4 0 2100MiB
- 警告: The resulting partition is not properly aligned for best performance.
- 忽略/Ignore/放棄/Cancel? Ignore
- (parted) print
- (parted) exit
- [[email protected] /]# partprobe /dev/sda //重新整理分割槽表
- [[email protected] /]# lsblk
- NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
- sda 8:0 0 3G 0 disk
- └─sda1 8:1 0 2.1G 0 part
2)格式化使用分割槽,利用UUID實現開機自動掛載
- [[email protected] /]# mkfs.ext4 /dev/sda1
- [[email protected] /]# blkid /dev/sda1 #檢視分割槽UUID值
- /dev/sda1: UUID="402d1304-5781-4349-8c43-ba45eba8b427" TYPE="ext4" PARTLABEL="primary" PARTUUID="2e41f84d-5123-4511-aaca-7e779087579b"
- [[email protected] /]# mkdir /mnt/data
- [[email protected] /]# vim /etc/fstab
- UUID="402d1304-5781-4349-8c43-ba45eba8b427" /mnt/data ext4 defaults,_netdev 0 0
- [[email protected] /]# mount -a
- [[email protected] /]# df -h
- 檔案系統 容量 已用 可用 已用% 掛載點
- ……
- /dev/sda1 2.0G 6.2M 1.9G 1% /mnt/data
3 案例3:搭建mariadb資料庫系統
3.1 問題
本例要求在虛擬機器server0上安裝 MariaDB 資料庫系統:
- 安裝 mariadb-server、mariadb 軟體包
- 啟動 mariadb 服務,並確認監聽狀態
然後在客戶端訪問此資料庫服務:
- 使用 mysql 命令訪問本機的資料庫服務,使用者名稱為 root,密碼為空
- 執行 SHOW DATABASES; 指令列出有哪些庫
- 退出 mysql 互動介面
3.2 方案
資料庫表及相關軟體的基本知識:
- 資料(記錄):用來表示一個事物(實體)的一些資訊(屬性)的文字/圖片檔案等,例如字串“:tedu.cn”
- 資料表:存放很多條資料記錄的容器,例如學員聯絡資訊表、學員月考成績表
- 資料表的每一行:存放一條記錄
- 資料表的每一列/欄位:很多個事物的同一個屬性
- 資料庫:存放很多個相互關聯的表格的容器,例如NSD1609學員檔案庫
- 資料庫管理系統(DBMS):用來管理(建立庫/新增/查詢/刪除/授權等)資料庫資訊的軟體平臺
MariaDB服務端:軟體包mariadb-server、系統服務mariadb
MariaDB客戶端:軟體包mariadb、管理工具mysql
MariaDB服務端配置檔案:/etc/my.cnf
傳輸協議及埠:TCP 3306
mysql命令的簡單用法:
- mysql [-u使用者名稱] [-p[密碼]]
3.3 步驟
實現此案例需要按照如下步驟進行。
步驟一:搭建MariaDB資料庫伺服器
1)安裝軟體包mariadb-server、mariadb
- [[email protected] ~]# yum -y install mariadb-server mariadb
- .. ..
2)啟動系統服務mariadb,並設定開機自啟
- [[email protected] ~]# systemctl restart mariadb
- [[email protected] ~]# systemctl enable mariadb
- ln -s '/usr/lib/systemd/system/mariadb.service' '/etc/systemd/system/multi-user.target.wants/mariadb.service'
3)檢查監聽狀態
- [[email protected] ~]# netstat -antpu | grep :3306
- tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2922/mysqld
步驟二:訪問本機的MariaDB資料庫系統
1)以使用者root連線本機的mariadb(或mysqld)資料庫服務
- [[email protected] ~]# mysql -uroot
- Welcome to the MariaDB monitor. Commands end with ; or \g.
- Your MariaDB connection id is 3
- Server version: 5.5.35-MariaDB MariaDB Server
- Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
- Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
- MariaDB [(none)]>
2)檢視當前資料庫系統內有哪些庫
- MariaDB [(none)]> SHOW DATABASES;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | mysql |
- | performance_schema |
- | test |
- +--------------------+
- 4 rows in set (0.00 sec)
3)退出操作環境
- MariaDB [(none)]> QUIT
- Bye
- [[email protected] ~]#
4 案例4:配置一個數據庫
4.1 問題
本例要求在虛擬機器server0上部署 MariaDB 資料庫,具體要求如下:
- 此資料庫系統只能被 localhost 訪問
- 新建一個數據庫名為 Contacts,其中應該包含來自資料庫複製的內容,複製檔案的 URL 為:http://classroom/pub/materials/users.sql
- 除了 root 使用者,此資料庫只能被使用者 Raikon 查詢,此使用者的密碼為atenorth
- root使用者的密碼為 atenorth
4.2 方案
為資料庫賬號修改密碼:
- mysqladmin [-u使用者名稱] [-p[舊密碼]] password '新密碼'
匯入/恢復到資料庫:
- mysql [-u使用者名稱] [-p[密碼]] 資料庫名 < 備份檔案.sql
為資料庫使用者授權/撤銷許可權:
- grant 許可權1,許可權2... on 庫名.表名 to 使用者名稱@客戶機地址 identified by '密碼';
- revoke 許可權1,許可權2... on 庫名.表名 from 使用者名稱@客戶機地址;
4.3 步驟
實現此案例需要按照如下步驟進行。
步驟一:禁止mariadb服務提供網路監聽(只服務於本機)
1)修改配置檔案
- [[email protected] ~]# vim /etc/my.cnf
- [mysqld]
- skip-networking //跳過網路
2)重啟mariadb服務
- [[email protected] ~]# systemctl restart mariadb //重啟服務
3)確認結果
- [[email protected] ~]# netstat -anptu | grep :3306 //已經不提供埠監聽
- [[email protected] ~]# pgrep -l mysqld //但程序仍在
- 3127 mysqld_safe
- 3297 mysqld
步驟二:配置資料庫管理密碼
1)使用mysqladmin為使用者root設定密碼
原管理賬號root的密碼為空,因此無需驗證舊密碼:
- [[email protected] ~]# mysqladmin -u root password 'atenorth'
2)驗證新密碼是否可用
root使用空密碼從本機連線將會失敗:
- [[email protected] ~]# mysql -uroot
- ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
必須指定正確的新密碼才能連線成功:
- [[email protected] ~]# mysql -uroot -patenorth
- Welcome to the MariaDB monitor. Commands end with ; or \g.
- Your MariaDB connection id is 4
- Server version: 5.5.35-MariaDB MariaDB Server
- Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
- Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
- .. ..
步驟三:建Contacts庫並匯入備份資料
1)建立新庫Contacts,並退出操作環境
- MariaDB [(none)]> CREATE DATABASE Contacts;
- Query OK, 1 row affected (0.00 sec)
- MariaDB [(none)]> QUIT
- Bye
2)下載指定的資料庫備份
- [[email protected] ~]# wget http://classroom.example.com/pub/materials/users.sql
- --2016-11-26 19:00:37-- http://classroom.example.com/pub/materials/users.sql
- Resolving classroom.example.com (classroom.example.com)... 172.25.254.254
- Connecting to classroom.example.com (classroom.example.com)|172.25.254.254|:80... connected.
- HTTP request sent, awaiting response... 200 OK
- Length: 2634 (2.6K) [application/sql]
- Saving to: ‘users.sql’
- 100%[=====================>] 2,634 --.-K/s in 0s
- 2016-11-26 19:00:37 (269 MB/s) - ‘users.sql’ saved [2634/2634]
- [[email protected] ~]# ls -lh users.sql //確認下載的檔案
- -rw-r--r--. 1 root root 2.6K Mar 31 2016 users.sql
3)匯入資料庫
- [[email protected] ~]# mysql -uroot -patenorth Contacts < users.sql
4)重新連入操作環境,確認匯入結果
- [[email protected] ~]# mysql -uroot -patenorth
- .. ..
- MariaDB [(none)]> USE Contacts; //使用指定庫
- Database changed
- MariaDB [Contacts]> SHOW TABLES; //列出有哪些表
- +--------------------+
- | Tables_in_Contacts |
- +--------------------+
- | base |
- | location |
- +--------------------+
- 2 rows in set (0.00 sec)
步驟四:為Contacts庫授權
1)允許使用者Raikon從本機訪問,具有查詢許可權,密碼為atenorth
- MariaDB [Contacts]> GRANT select ON Contacts.* TO [email protected] IDENTIFIED BY 'atenorth';
- Query OK, 0 rows affected (0.00 sec)
2)退出操作環境
- MariaDB [Contacts]> QUIT
- Bye
- [[email protected] ~]#
5 案例5:使用資料庫查詢
5.1 問題
本例要求配置MariaDB資料庫,完成以下任務:
- 禁止空密碼root使用者訪問mariadb資料庫
- 在系統server0上使用資料庫Contacts,通過SQL查詢回答下列問題:密碼是solicitous的人的名字?有多少人的姓名是Barbara同時居住在 Sunnyvale?
5.2 方案
表記錄增刪改查:
- insert into [庫名.]表名 values(值1,值2,值3);
- delete from [庫名.]表名 where ...;
- update [庫名.]表名 set 欄位名=欄位值 where ....;
- select 欄位列表 from [庫名.]表名 where 欄位名1=值 and|or 欄位名2=值;
統計查詢結果的數量:
- select count(*) from [庫名.]表名 where .. ..;
5.3 步驟
實現此案例需要按照如下步驟進行。
步驟一:清理空密碼root使用者
1)確認空密碼root使用者記錄
MariaDB服務端預設的mysql庫user表儲存了使用者授權記錄。
使用DESC指令查看錶結構,以便了解相關欄位名:
- MariaDB [(none)]> DESC mysql.user;
- +------------------------+-----------------------------------+------+-----+---------+-------+
- | Field | Type | Null | Key | Default | Extra |
- +------------------------+-----------------------------------+------+-----+---------+-------+
- | Host | char(60) | NO | PRI | | |
- | User | char(16) | NO | PRI | | |
- | Password | char(41) | NO | | | |
列出user表中的Host、User、Password欄位,限定密碼為空的root使用者:
- MariaDB [(none)]> SELECT Host,User,Password FROM mysql.user WHERE User='root' AND Password='';
- +---------------------+------+----------+
- | Host | User | Password |
- +---------------------+------+----------+
- | server0.example.com | root | |
- | 127.0.0.1 | root | |
- | ::1 | root | |
- +---------------------+------+----------+
- 3 rows in set (0.00 sec)
- MariaDB [(none)]>
2)刪除空密碼root使用者記錄
使用DELETE指令刪除掉需要清除的授權記錄:
- MariaDB [(none)]> DELETE FROM mysql.user WHERE User='root' AND Password='';
- Query OK, 3 rows affected (0.00 sec)
再次查詢,確認刪除結果:
- MariaDB [(none)]> SELECT Host,User,Password FROM mysql.user WHERE User='root' AND Password='';
- Empty set (0.00 sec)
步驟二:按條件查詢表記錄
1)按單個條件查詢
找出密碼是solicitous的人的名字?
- MariaDB [(none)]> SELECT name FROM Contacts.base WHERE Password='solicitous';
- +-------+
- | name |
- +-------+
- | James |
- +-------+
- 1 row in set (0.00 sec)
2)按多個條件在關聯的兩張表中查詢
有多少人的姓名是Barbara同時居住在 Sunnyvale?
- MariaDB [(none)]> USE Contacts;
- .. ..
- Database changed
- MariaDB [Contacts]> SELECT COUNT(*) FROM base,location WHERE base.name='Barbara' AND location.city='Sunnyvale' AND base.id=location.id;
- +----------+
- | COUNT(*) |
- +----------+
- | 1 |
- +----------+
- 1 row in set (0.00 sec)