ENGINEER 03: iscsi网络磁盘的操作步骤,小白款mariadb数据库系统的初级配置步骤

案例1:发布iSCSI网络磁盘
案例2:搭建mariadb数据库系统
案例3:配置一个数据库
案例4:使用数据库查询
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
[root@server0 /]# 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)命令行查看分区信息
[root@server0 /]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
……
└─vdb1 252:17 0 3G 0 part ln -s
3)查看分区设备文件
[root@server0 /]# ls /dev/vdb1
/dev/vdb1
步骤二:安装与配置iSCSI磁盘
1)安装软件包targetcli
[root@server0 ~]# yum -y install targetcli
2)命令行运行targetcli配置iSCSI磁盘
[root@server0 /]# 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服务,并设置为开机自启
[root@server0 /]# systemctl restart target
[root@server0 /]# systemctl enable target
Created symlink from /etc/systemd/system/multi-user.target.wants/target.service to /usr/lib/systemd/system/target.service.
2 案例2:搭建mariadb数据库系统
2.1 问题
本例要求在虚拟机server0上安装 MariaDB 数据库系统:
安装 mariadb-server、mariadb 软件包
启动 mariadb 服务,并确认监听状态
然后在客户端访问此数据库服务:
使用 mysql 命令访问本机的数据库服务,用户名为 root,密码为空
执行 SHOW DATABASES; 指令列出有哪些库
退出 mysql 交互界面
2.2 方案
数据库表及相关软件的基本知识:
数据(记录):用来表示一个事物(实体)的一些信息(属性)的文字/图片文件等,例如字符串“:tedu.cn”
数据表:存放很多条数据记录的容器,例如学员联系信息表、学员月考成绩表
数据表的每一行:存放一条记录
数据表的每一列/字段:很多个事物的同一个属性
数据库:存放很多个相互关联的表格的容器,例如NSD1609学员档案库
数据库管理系统(DBMS):用来管理(创建库/添加/查询/删除/授权等)数据库信息的软件平台
MariaDB服务端:软件包mariadb-server、系统服务mariadb
MariaDB客户端:软件包mariadb、管理工具mysql
MariaDB服务端配置文件:/etc/my.cnf
传输协议及端口:TCP 3306
mysql命令的简单用法:
mysql [-u用户名] [-p[密码]]
2.3 步骤
实现此案例需要按照如下步骤进行。
步骤一:搭建MariaDB数据库服务器
1)安装软件包mariadb-server、mariadb
[root@server0 ~]# yum -y install mariadb-server mariadb
… …
2)启动系统服务mariadb,并设置开机自启
[root@server0 ~]# systemctl restart mariadb
[root@server0 ~]# systemctl enable mariadb
ln -s ‘/usr/lib/systemd/system/mariadb.service’ ‘/etc/systemd/system/multi-user.target.wants/mariadb.service’
3)检查监听状态
[root@server0 ~]# netstat -antpu | grep :3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2922/mysqld
步骤二:访问本机的MariaDB数据库系统
1)以用户root连接本机的mariadb(或mysqld)数据库服务
[root@server0 ~]# 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 © 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
[root@server0 ~]#
3 案例3:配置一个数据库
3.1 问题
本例要求在虚拟机server0上部署 MariaDB 数据库,具体要求如下:
此数据库系统只能被 localhost 访问
新建一个数据库名为 Contacts,其中应该包含来自数据库复制的内容,复制文件的 URL 为:http://classroom/pub/materials/users.sql
除了 root 用户,此数据库只能被用户 Raikon 查询,此用户的密码为atenorth
root用户的密码为 atenorth
3.2 方案
为数据库账号修改密码:
mysqladmin [-u用户名] [-p[旧密码]] password ‘新密码’
导入/恢复到数据库:
mysql [-u用户名] [-p[密码]] 数据库名 < 备份文件.sql
为数据库用户授权/撤销权限:
grant 权限1,权限2… on 库名.表名 to 用户名@客户机地址 identified by ‘密码’;
revoke 权限1,权限2… on 库名.表名 from 用户名@客户机地址;
3.3 步骤
实现此案例需要按照如下步骤进行。
步骤一:禁止mariadb服务提供网络监听(只服务于本机)
1)修改配置文件
[root@server0 ~]# vim /etc/my.cnf
[mysqld]
skip-networking //跳过网络
2)重启mariadb服务
[root@server0 ~]# systemctl restart mariadb //重启服务
3)确认结果
[root@server0 ~]# netstat -anptu | grep :3306 //已经不提供端口监听
[root@server0 ~]# pgrep -l mysqld //但进程仍在
3127 mysqld_safe
3297 mysqld
步骤二:配置数据库管理密码
1)使用mysqladmin为用户root设置密码
原管理账号root的密码为空,因此无需验证旧密码:
[root@server0 ~]# mysqladmin -u root password ‘atenorth’
2)验证新密码是否可用
root使用空密码从本机连接将会失败:
[root@server0 ~]# mysql -uroot
ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password: NO)
必须指定正确的新密码才能连接成功:
[root@server0 ~]# 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 © 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)下载指定的数据库备份
[root@server0 ~]# 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]
[root@server0 ~]# ls -lh users.sql //确认下载的文件
-rw-r–r--. 1 root root 2.6K Mar 31 2016 users.sql
3)导入数据库
[root@server0 ~]# mysql -uroot -patenorth Contacts < users.sql
4)重新连入操作环境,确认导入结果
[root@server0 ~]# 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 Raikon@localhost IDENTIFIED BY ‘atenorth’;
Query OK, 0 rows affected (0.00 sec)
2)退出操作环境
MariaDB [Contacts]> QUIT
Bye
[root@server0 ~]#
4 案例4:使用数据库查询
4.1 问题
本例要求配置MariaDB数据库,完成以下任务:
禁止空密码root用户访问mariadb数据库
在系统server0上使用数据库Contacts,通过SQL查询回答下列问题:密码是solicitous的人的名字?有多少人的姓名是Barbara同时居住在 Sunnyvale?
4.2 方案
表记录增删改查:
insert into [库名.]表名 values(值1,值2,值3);
delete from [库名.]表名 where …;
update [库名.]表名 set 字段名=字段值 where …;
select 字段列表 from [库名.]表名 where 字段名1=值 and|or 字段名2=值;
统计查询结果的数量:
select count() from [库名.]表名 where … …;
4.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)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

彭淦淦

是你的双手成就了我的生活!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值