我的linux学习之路(第十八节)

只谈情不闲聊 提交于 2020-03-22 22:34:54

3 月,跳不动了?>>>

(一)使用iSCSI服务部署网络存储

当前的硬盘接口类型主要有IDE、SCSI和SATA三种:

  • IDE是一种成熟稳定、价格便宜的并行传输接口。
  • SATA是一种传输速度更快、数据校验更完整的串行传输接口。
  • SCSI是一种用于计算机和硬盘、光驱等设备之间系统级接口的通用标准,具有系统资源占用率低、转速高、传输速度快等优点。

IBM公司研发的基于TCP/IP协议和SCSI接口协议的新型存储技术互联网小型计算机系统接口(Internet Small Computer System Interface,iSCSI)是一种将SCSI接口与以太网技术相结合的新型存储技术,可以用来在网络中传输SCSI接口的命令和数据。

运行Linux系统的服务器会基于iSCSI协议把硬盘设备命令与数据打包成标准的TCP/IP数据包,然后通过以太网传输到目标存储设备,而当目标存储设备接收到这些数据包后,还需要基于iSCSI协议把TCP/IP数据包解压成硬盘设备命令与数据。

1、创建RAID磁盘阵列

//在虚拟机中添加4块新硬盘,使用mdadm命令创建RAID5磁盘阵列并配置备份盘。其中,-Cv参数为创建阵列并显示过程;/dev/md0为生成的阵列组名称;-n 3参数为创建RAID 5磁盘阵列所需的硬盘个数;-l 5参数为RAID磁盘阵列的级别;-x 1参数为磁盘阵列的备份盘个数;在命令后面逐一写上创建raid5使用的硬盘名称(包含备份磁盘即热备盘)。

[root@linuxprobe ~]# mdadm -Cv /dev/md0 -n 3 -l 5 -x 1 /dev/sdb /dev/sdc /dev/sdd /dev/sde

//使用mdadm -D命令来查看设备的详细信息

[root@linuxprobe ~]# mdadm -D /dev/md0

 

2、配置iSCSI服务端

iSCSI技术在工作形式上分为服务端(target)与客户端(initiator):iSCSI服务端即用于存放硬盘存储资源的服务器,它作为前面创建的RAID磁盘阵列的存储端,能够为用户提供可用的存储资源;iSCSI客户端则是用户使用的软件,用于访问远程服务端的存储资源。

1)安装iSCSI服务端程序以及配置命令工具

[root@linuxprobe ~]# yum -y install targetd targetcli

//安装完成后启动iSCSI的服务端程序targetd,并加入到开机启动项中,以便下次服务器重启后依然能够为用户提供iSCSI共享存储资源服务。

[root@linuxprobe ~]# systemctl restart targetd

[root@linuxprobe ~]# systemctl enable targetd

ln -s '/usr/lib/systemd/system/targetd.service' '/etc/systemd/system/multi-user.target.wants/targetd.service'

2)通过targetcli配置iSCSI服务端共享资源

targetcli是用于管理iSCSI服务端存储资源的专用配置命令,提供类似于fdisk命令的交互式配置功能,将iSCSI共享资源的配置内容抽象成“目录”的形式,只需将各类配置信息填入到相应的“目录”中即可。

//把刚刚创建的RAID 5磁盘阵列md0文件加入到配置共享设备的“资源池”/backstores/block(iSCSI服务端配置共享设备的位置)中,并将该文件重新命名为disk0。用户不知道是由服务器中的哪块硬盘来提供共享存储资源,而只会看到一个名为disk0的存储设备。

[root@linuxprobe ~]# targetcli

Warning: Could not load preferences file /root/.targetcli/prefs.bin.

targetcli shell version 2.1.fb34

Copyright 2011-2013 by Datera, Inc and others.

For help on commands, type 'help'.

/> ls

o- / ..................................................................... [...]

o- backstores .......................................................... [...]

| o- block .............................................. [Storage Objects: 0]

| o- fileio ............................................. [Storage Objects: 0]

| o- pscsi .............................................. [Storage Objects: 0]

| o- ramdisk ............................................ [Storage Objects: 0]

o- iscsi ........................................................ [Targets: 0]

o- loopback ..................................................... [Targets: 0

/> cd /backstoresock/block

/backstoresock/block> create disk0 /dev/md0

Created block storage object disk0 using /dev/md0.

/backstoresock> cd /

/> ls

o- / ..................................................................... [...]

  o- backstores .......................................................... [...]

  | o- block .............................................. [Storage Objects: 1]

  | | o- disk0 ..................... [/dev/md0 (40.0GiB) write-thru deactivated]

  | o- fileio ............................................. [Storage Objects: 0]

  | o- pscsi .............................................. [Storage Objects: 0]

  | o- ramdisk ............................................ [Storage Objects: 0]

  o- iscsi ........................................................ [Targets: 0]

  o- loopback ..................................................... [Targets: 0]

3)创建iSCSI target名称及配置共享资源

iSCSI target名称是由系统自动生成的,是一串用于描述共享资源的唯一字符串。用户在扫描iSCSI服务端时即可看到这个字符串。通过create命令生成target名称后,会在/iscsi参数目录中创建一个与其字符串同名的新“目录”用来存放共享资源。我们需要把前面加入到iSCSI共享资源池中的硬盘设备/backstores/block/disk0通过create /backstores/block/disk0命令添加到这个新目录中,这样用户在登录iSCSI服务端后,即可默认使用这硬盘设备提供的共享存储资源了。

/> cd iscsi

/iscsi> create

Created target iqn.2003-01.org.linux-iscsi.linuxprobe.x8664:sn.d497c356ad80.

Created TPG 1.

/iscsi> cd iqn.2003-01.org.linux-iscsi.linuxprobe.x8664:sn.d497c356ad80/

/iscsi/iqn.20....d497c356ad80> ls

o- iqn.2003-01.org.linux-iscsi.linuxprobe.x8664:sn.d497c356ad80 ...... [TPGs: 1]

  o- tpg1 ............................................... [no-gen-acls, no-auth]

    o- acls .......................................................... [ACLs: 0]

    o- luns .......................................................... [LUNs: 0]

    o- portals .................................................... [Portals: 0]

/iscsi/iqn.20....d497c356ad80> cd tpg1/luns

/iscsi/iqn.20...d80/tpg1/luns> create /backstores/block/disk0

Created LUN 0.

4)设置访问控制列表(ACL)

iSCSI协议是通过客户端名称进行验证的,用户在访问存储共享资源时不需要输入密码,只要iSCSI客户端的名称与服务端中设置的访问控制列表中某一条目名称一致即可,因此需要在iSCSI服务端的配置文件中写入一串能够验证用户信息的名称。acls参数目录用于存放能够访问iSCSI服务端共享存储资源的客户端名称。推荐在系统生成的iSCSI target后面追加上类似于:client名称的参数来标识客户端,这样既能保证客户端的名称具有唯一性,又非常便于管理和阅读。

/iscsi/iqn.20...c356ad80/tpg1> cd acls

/iscsi/iqn.20...d80/tpg1/acls> create iqn.2003-01.org.linux-iscsi.linuxprobe.x8664:sn.d497c356ad80:client        //可创建多个客户端名称,被不同的客户端连接,但同一时间只能被一个客户端挂载。

Created Node ACL for iqn.2003-01.org.linux-iscsi.linuxprobe.x8664:sn.d497c356ad80:client

Created mapped LUN 0.

5)设置iSCSI服务端的监听IP地址和端口号

/iscsi/iqn.20...c356ad80/tpg1> cd portals

/iscsi/iqn.20.../tpg1/portals> create 192.168.10.10

Using default IP port 3260

Created network portal 192.168.10.10:3260.

说明:如果需要修改默认端口3260为32600,可以使用create 192.168.10.10 32600(新端口)命令。

6)检查配置,设置防火墙规则允许3260端口流量

上述参数文件配置完成后,通过ls可以浏览配置信息,确保信息正确无误后,输入exit命令来退出配置。重启iSCSI服务端程序,设置firewalld防火墙策略,放行3260/tcp端口的流量。

/iscsi/iqn.20.../tpg1/portals> ls /

o- / ........................... [...]

  o- backstores................. [...]               //cd命令切换到该目录下

  | o- block ................... [Storage Objects: 1]

  | | o- disk0 ................. [/dev/md0 (40.0GiB) write-thru activated]

  | o- fileio .................. [Storage Objects: 0]

  | o- pscsi ................... [Storage Objects: 0]

  | o- ramdisk ................. [Storage Objects: 0]

  o- iscsi ..................... [Targets: 1]

  | o- iqn.2003-01.org.linux-iscsi.linuxprobe.x8664:sn.d497c356ad80 .... [TPGs: 1]

  |   o- tpg1 .................. [no-gen-acls, no-auth]      //cd命令切换到该目录下(下同)

  |     o- acls ........................................................ [ACLs: 1]

  |     | o- iqn.2003-01.org.linux-iscsi.linuxprobe.x8664:sn.d497c356ad80:client [Mapped LUNs: 1]

  |     |   o- mapped_lun0 ............................................. [lun0 block/disk0 (rw)] 

        o- luns .................... [LUNs: 1]        //cd命令切换到该目录下

  |     | o- lun0 .............. [block/disk0 (/dev/md0)]

  |     o- portals ............. [Portals: 1]         //cd命令切换到该目录下

  |       o- 192.168.10.10:3260  [OK]

  o- loopback .................. [Targets: 0]

/> exit

[root@linuxprobe ~]# systemctl restart targetd

[root@linuxprobe ~]# firewall-cmd --permanent --add-port=3260/tcp

success

[root@linuxprobe ~]# firewall-cmd --reload

Success

或通过以下命令清空iptables防火墙并保存配置,确认重启后防火墙规则依然为空。

[root@linuxprobe ~]#iptables -F

[root@linuxprobe ~]#service iptables save

3、配置Linux客户端

1)安装Linux客户端

[root@linuxprobe ~]# yum install iscsi-initiator-utils

说明:在RHEL7系统中默认已安装iSCSI客户端服务程序initiator,服务名为iscsi。

2)编辑iSCSI客户端名称

iSCSI协议是通过客户端的名称来进行验证,该名称是iSCSI客户端的唯一标识,必须与服务端配置文件访问控制列表中的信息一致,否则客户端在尝试访问存储共享设备时,系统会弹出验证失败的保存信息。编辑iSCSI客户端中的initiator名称文件,输入服务端的访问控制列表名称,重启客户端iscsid服务程序并将其加入到开机启动项中。

[root@linuxprobe ~]# vim /etc/iscsi/initiatorname.iscsi

InitiatorName=iqn.2003-01.org.linux-iscsi.linuxprobe.x8664:sn.d497c356ad80:client

[root@linuxprobe ~]# systemctl restart iscsid

[root@linuxprobe ~]# systemctl enable iscsid

 ln -s '/usr/lib/systemd/system/iscsid.service' '/etc/systemd/system/multi-user.target.wants/iscsid.service'

3)iSCSI客户端访问并使用共享存储资源时“先发现,再登录,最后挂载并使用”。

说明:iscsiadm是用于管理、查询、插入、更新或删除iSCSI数据库配置文件的命令行工具,用户需要先使用这个工具扫描发现远程iSCSI服务端,然后查看找到的服务端上有哪些可用的共享存储资源。-m discovery参数的目的是扫描并发现可用的存储资源;-t st参数为执行扫描操作的类型;-p 192.168.10.10参数为iSCSI服务端的IP地址。

[root@linuxprobe ~]# iscsiadm -m discovery -t st -p 192.168.10.10

192.168.10.10:3260,1 iqn.2003-01.org.linux-iscsi.linuxprobe.x8664:sn.d497c356ad80

4)客户端通过iscsiadm命令登录iSCSI服务端

说明:-m node参数为将客户端所在主机作为一台节点服务器,-T  iqn.2003-01. org.linux-iscsi.linuxprobe.x8664:sn.d497c356ad80参数为要使用的存储资源(大家可以直接复制前面命令中扫描发现的结果,以免录入错误),-p 192.168.10.10参数依然为对方iSCSI服务端的IP地址。最后使用--login或-l参数进行登录验证。

[root@linuxprobe ~]# iscsiadm -m node -T iqn.2003-01.org.linux-iscsi.linuxprobe.x8664:sn.d497c356ad80 -p 192.168.10.10 –login      //重启后永久生效

Logging in to [iface: default, target: iqn.2003-01.org.linux-iscsi.linuxprobe.x8664:sn.d497c356ad80, portal: 192.168.10.10,3260] (multiple)

Login to [iface: default, target: iqn.2003-01.org.linux-iscsi.linuxprobe.x8664:sn.d497c356ad80, portal: 192.168.10.10,3260] successful.

5)格式化、挂载使用iSCSI磁盘

在iSCSI客户端成功登录之后,会在客户端主机上多出一块名为/dev/sdb的设备文件。可以像使用本地主机上的硬盘那样来操作这个设备文件(过程略)。

格式化分区:mkfs.xfs /dev/sdb

创建挂载点:mkdir /iscsi

挂载:mount /dev/sdb /iscsi

查看挂载情况:df -h

6)编辑/etc/fstab文件,实现开机自动挂载

由于udev服务是按照系统识别硬盘设备的顺序来命名硬盘设备的,当客户端主机同时使用多个远程存储资源时,如果下一次识别远程设备的顺序发生了变化,则客户端挂载目录中的文件也将随之混乱。为了防止发生这样的问题,我们应该在/etc/fstab配置文件中使用设备的UUID唯一标识符进行挂载,这样,不论远程设备资源的识别顺序再怎么变化,系统也能正确找到设备所对应的目录。

通过blkid命令查看设备的名称、文件系统及UUID。

[root@linuxprobe ~]# blkid | grep /dev/sdb

/dev/sdb: UUID="eb9cbf2f-fce8-413a-b770-8b0f243e8ad6" TYPE="xfs"

由于/dev/sdb是一块网络存储设备,而iSCSI协议是基于TCP/IP网络传输数据的,因此必须在/etc/fstab配置文件中添加上_netdev参数,表示当系统联网后再进行挂载操作,以免系统开机时间过长或开机失败。

[root@linuxprobe ~]# vim /etc/fstab

UUID=eb9cbf2f-fce8-413a-b770-8b0f243e8ad6 /iscsi xfs defaults,_netdev 0 0

7)iSCSI共享设备资源可用iscsiadm命令的-u参数将其设备卸载

[root@linuxprobe ~]# iscsiadm -m node -T iqn.2003-01.org.linux-iscsi.linuxprobe.x8664:sn.d497c356ad80 -u      //重启后永久生效

Logging out of session [sid: 7, target : iqn.2003-01.org.linux-iscsi.linuxprobe.x8664:sn.d497c356ad80, portal: 192.168.10.10,3260]

Logout of [sid: 7, target: iqn.2003-01.org.linux-iscsi.linuxprobe.x8664:sn.d497c356ad80,portal:192.168.10.10,3260] successful.

4、配置Windows客户端

进入到“管理工具”à“iSCSI发起程序”。在第一次运行iSCSI发起程序时,系统会提示“Microsoft iSCSI服务端未运行”,单击“是”按钮即可自动启动并运行iSCSI发起程序,按照提示完成配置即可。注意修改客户端名称与服务端配置文件访问控制列表中的信息一致,否则客户端在尝试访问存储共享设备时,系统会弹出验证失败的保存信息。

 

(二)使用MariaDB数据库管理系统

数据库是指按照某些特定结构来存储数据资料的数据仓库。数据库管理系统是一种能够对数据库中存放的数据进行建立、修改、删除、查找、维护等操作的软件程序。通过把计算机中具体的物理数据转换成适合用户理解的抽象逻辑数据,有效地降低数据库管理的技术门槛。

Oracle公司在2009年收购了MySQL的母公司Sun。MySQL数据库项目也随之纳入Oracle麾下,逐步演变为保持着开源软件的身份,但又申请了多项商业专利的软件系统。MySQL项目创始者重新研发了一款名为MariaDB的全新数据库管理系统。该软件当前由开源社区进行维护,是MySQL的分支产品,而且几乎完全兼容MySQL。

RHEL7CentOS7及最新的Fedora系统将MariaDB作为默认的数据库管理系统,且红帽公司首次将数据库知识加入到了RHCE认证的考试内容中。

1、安装、初始化MariaDB服务

[root@linuxprobe Desktop]# yum install mariadb mariadb-server

//重启mariadb服务并添加开机自动启动

[root@linuxprobe Desktop]# systemctl restart mariadb

[root@linuxprobe Desktop]# systemctl enable mariadb

ln -s '/usr/lib/systemd/system/mariadb.service' '/etc/systemd/system/multi-user.target.wants/mariadb.service'

说明:为了确保数据库的安全性和正常运转,需要先对数据库程序进行初始化操作。初始化操作涉及下面5个配置:

设置root管理员在数据库中的密码值(注意,该密码并非root管理员在系统中的密码,这里的密码值默认应该为空,可直接按回车键)。

设置root管理员在数据库中的专有密码。

随后删除匿名账户,并使用root管理员从远程登录数据库,以确保数据库上运行的业务的安全性。

删除默认的测试数据库,取消测试数据库的一系列访问权限。

刷新授权列表,让初始化的设定立即生效。

[root@linuxprobe Desktop]# mysql_secure_installation

/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found

 

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB

      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

 

In order to log into MariaDB to secure it, we'll need the current

password for the root user.  If you've just installed MariaDB, and

you haven't set the root password yet, the password will be blank,

so you should just press enter here.

 

Enter current password for root (enter for none):   //当前数据库密码为空,直接按回车键

OK, successfully used password, moving on...

 

Setting the root password ensures that nobody can log into the MariaDB

root user without the proper authorisation.

 

Set root password? [Y/n] y    //设置数据库root管理员账号的密码,密码为passmord

New password:

Re-enter new password:

Password updated successfully!

Reloading privilege tables..

 ... Success!

 

By default, a MariaDB installation has an anonymous user, allowing anyone

to log into MariaDB without having to have a user account created for

them.  This is intended only for testing, and to make the installation

go a bit smoother.  You should remove them before moving into a

production environment.

 

Remove anonymous users? [Y/n] y    //删除匿名账号

 ... Success!

 

Normally, root should only be allowed to connect from 'localhost'.  This

ensures that someone cannot guess at the root password from the network.

 

Disallow root login remotely? [Y/n] y  //禁止数据库root账号从远程登录

 

 ... Success!

 

By default, MariaDB comes with a database named 'test' that anyone can

access.  This is also intended only for testing, and should be removed

before moving into a production environment.

 

Remove test database and access to it? [Y/n] y  //删除test数据库并取消对它的访问权限

 - Dropping test database...

 ... Success!

 - Removing privileges on test database...

 ... Success!

 

Reloading the privilege tables will ensure that all changes made so far

will take effect immediately.

 

Reload privilege tables now? [Y/n] y      //刷新授权表,让初始化后的设定立即生效

 ... Success!

 

Cleaning up...

 

All done!  If you've completed all of the above steps, your MariaDB

installation should now be secure.

 

Thanks for using MariaDB!

[root@linuxprobe Desktop]#

说明:数据库服务程序默认使用3306端口,在防火墙策略中服务名称统一叫作mysql。

[root@linuxprobe Desktop]# firewall-cmd --permanent --add-service=mysql

success

[root@linuxprobe Desktop]# firewall-cmd --reload

Success

2、登录MariaDB数据库

说明:-u参数用来指定以root管理员的身份登录,而-p参数用来验证该用户在数据库中的密码值。在登录MariaDB数据库后执行数据库命令时,需要在命令后面用分号(;)结尾,这也是与Linux命令最显著的区别。命令执行不区分大小写。

[root@linuxprobe Desktop]# mysql -u root -p

Enter password:    //此处输入root管理员在数据库中的密码passmord

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 10

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)]> set password = password('linuxprobe');    //将root管理员账号在数据库管理系统中的密码值修改为linuxprobe

Query OK, 0 rows affected (0.00 sec)

3、管理用户以及授权

为了保障数据库系统的安全性,让其他用户协同管理数据库,可以在MariaDB数据库管理系统中创建多个专用的数据库管理账户,然后再分配合理的权限,以满足他们的工作需求。

使用root管理员登录数据库管理系统,按照“CREATE USER 用户名@主机名 IDENTIFIED BY '密码';”的格式创建数据库管理账户。

MariaDB [(none)]> create user doudou@localhost identified by 'linuxprobe';

Query OK, 0 rows affected (0.00 sec)

//创建的账户信息可以使用select命令语句来查询。

MariaDB [(none)]> use mysql

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

MariaDB [mysql]> select host,user,password from user where user='doudou';

+-----------+--------+-------------------------------------------+

| host      | user   | password                                  |

+-----------+--------+-------------------------------------------+

| localhost | doudou | *55D9962586BE75F4B7D421E6655973DB07D6869F |

+-----------+--------+-------------------------------------------+

1 row in set (0.01 sec)

MariaDB [mysql]>

说明:此时创建的账户仅是一个普通账户,没有数据库的任何操作权限。

//切换到doudou账户查询数据库管理系统中当前都有哪些数据库,发现该账户甚至没法查看完整的数据库列表(刚才使用root账户时可以查看到3个数据库列表)。

[root@linuxprobe Desktop]# mysql -u doudou -p

Enter password:      //输入doudou用户的密码linuxprobe

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 14

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)]> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

+--------------------+

1 row in set (0.00 sec)

MariaDB [(none)]>

// 使用grant命令为doudou账户进行授权。

说明:账户的授权工作需要数据库管理员root来执行。使用grant命令时需要写上要赋予的权限、数据库、表单名称及对应的账户及主机信息。

GRANT命令的常见格式及解释:

命令

作用

GRANT 权限 ON 数据库.表单名称 TO 用户名@主机名

对某个特定数据库中的特定表单给予授权

GRANT 权限 ON 数据库.* TO 用户名@主机名

对某个特定数据库中的所有表单给予授权

GRANT 权限 ON *.* TO 用户名@主机名

对所有数据库及所有表单给予授权

GRANT 权限1,权限2 ON 数据库.* TO 用户名@主机名

对某个数据库中的所有表单给予多个授权

GRANT ALL PRIVILEGES ON *.* TO 用户名@主机名

对所有数据库及所有表单给予全部授权(需谨慎操作)

以root管理员身份登录到数据库管理系统中,针对mysql数据库中的user表单向账户doudou授予查询、更新、删除以及插入等权限。

[root@linuxprobe Desktop]# mysql -u root -p

Enter password:     //输入数据库账号root的密码

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 16

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)]> use mysql;    //指定使用的数据库

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

MariaDB [mysql]> grant select,update,delete,insert on mysql.user to doudou@localhost;

//授予doudou用户对user表单的查询、更新、删除及插入权限

Query OK, 0 rows affected (0.00 sec)

MariaDB [mysql]> show grants for doudou@localhost;   //查看doudou用户的授权是否成功。

+---------------------------------------------------------------------------------------------------------------+

| Grants for doudou@localhost                                                                                   |

+---------------------------------------------------------------------------------------------------------------+

| GRANT USAGE ON *.* TO 'doudou'@'localhost' IDENTIFIED BY PASSWORD '*55D9962586BE75F4B7D421E6655973DB07D6869F' |

| GRANT SELECT, INSERT, UPDATE, DELETE ON `mysql`.`user` TO 'doudou'@'localhost'                                |

+---------------------------------------------------------------------------------------------------------------+

2 rows in set (0.00 sec)

MariaDB [mysql]>exit

Bye

 

切换到账户doudou就能够看到mysql数据库了,而且还能看到表单user(只授权了user表单,其余表单会因无权限而被继续隐藏)。

[root@linuxprobe Desktop]# mysql -u doudou -p

Enter password:

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 17

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)]> use mysql;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

MariaDB [mysql]> show tables;

+-----------------+

| Tables_in_mysql |

+-----------------+

| user            |

+-----------------+

1 row in set (0.00 sec)

MariaDB [mysql]>

在数据库root账号下通过revoke命令移除grant授权的权限。

[root@linuxprobe Desktop]# mysql -u root -p

Enter password:

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 18

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)]> use mysql;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

MariaDB [mysql]> revoke select,update,delete,insert on mysql.user from doudou@localhost;

Query OK, 0 rows affected (0.00 sec)

查看doudou权限确认已删除

MariaDB [mysql]> show grants for doudou@localhost;

+---------------------------------------------------------------------------------------------------------------+

| Grants for doudou@localhost                                                                                   |

+---------------------------------------------------------------------------------------------------------------+

| GRANT USAGE ON *.* TO 'doudou'@'localhost' IDENTIFIED BY PASSWORD '*55D9962586BE75F4B7D421E6655973DB07D6869F' |

+---------------------------------------------------------------------------------------------------------------+

1 row in set (0.00 sec)

MariaDB [mysql]>

4、数据库与表单操作配置

在MariaDB数据库管理系统中,一个数据库可以存放多个数据表单,数据表单是数据库中最重要最核心的内容。我们可以根据自己的需求自定义数据库表结构,然后在其中合理地存放数据,以便后期轻松地维护和修改。

创建数据库、数据库表单的命令及作用(下表按照数据库日常操作顺序整理):

用法

作用

CREATE database 数据库名称。

创建新的数据库

SHOW databases;

显示当前已有的数据库

DROP DATABASE 数据库名称;

彻底删除数据库,数据库中的数据表单也将被删除。

USE 数据库名称;

指定使用的数据库

CREATE TABLE表单名称 (字段名称1 类型,字段名称2 类型);

创建数据库表单

SHOW tables;

显示当前数据库中的表单

DESCRIBE 表单名称;

描述表单

INSERT INTO表单名称(字段名称1,字段名称2) VALUES(‘字段名称1的值’, ‘字段名称2的值’);

向数据库表单中插入数据

SELECT * FROM 表单名称;

从表单中选中某个记录值,查看数据库表单中的数据。

UPDATE 表单名称 SET attribute=新值 WHERE attribute > 原始值;

更新表单中的数据

DELETE FROM 表单名 WHERE attribute=值;

从表单中删除某个记录值

//通过数据库管理员root账号创建新的数据库并查看是否成功

MariaDB [mysql]> create database linuxprobe;

Query OK, 1 row affected (0.00 sec)

MariaDB [mysql]> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| linuxprobe         |

| mysql              |

| performance_schema |

+--------------------+

4 rows in set (0.00 sec)

MariaDB [mysql]>

//在新创建的数据库中创建数据库表单、显示当前数据库表单并查看表单结构

MariaDB [mysql]> use linuxprobe;

Database changed

MariaDB [linuxprobe]> create table mybook (name char(15),price int,pages int);

Query OK, 0 rows affected (0.01 sec)

MariaDB [linuxprobe]> show tables;

+----------------------+

| Tables_in_linuxprobe |

+----------------------+

| mybook               |

+----------------------+

1 row in set (0.00 sec)

MariaDB [linuxprobe]> describe mybook;

+-------+----------+------+-----+---------+-------+

| Field | Type     | Null | Key | Default | Extra |

+-------+----------+------+-----+---------+-------+

| name  | char(15) | YES  |     | NULL    |       |

| price | int(11)  | YES  |     | NULL    |       |

| pages | int(11)  | YES  |     | NULL    |       |

+-------+----------+------+-----+---------+-------+

3 rows in set (0.00 sec)

MariaDB [linuxprobe]>

//向mybook数据表单中插一条图书信息(insert增加)并确认(select查询)是否成功

MariaDB [linuxprobe]> insert into mybook(name,price,pages) value('linuxprobe','55','550');

Query OK, 1 row affected (0.00 sec)

MariaDB [linuxprobe]> select * from mybook;

+------------+-------+-------+

| name       | price | pages |

+------------+-------+-------+

| linuxprobe |    55 |   550 |

+------------+-------+-------+

1 row in set (0.00 sec)

MariaDB [linuxprobe]>

//使用update命令将上面插入的linuxprobe图书信息的价格修改为60元,然后再使用select命令查看该图书的名称和定价信息是否修改成功。

MariaDB [linuxprobe]> update mybook set price=60;

Query OK, 1 row affected (0.00 sec)

Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [linuxprobe]> select * from mybook;

+------------+-------+-------+

| name       | price | pages |

+------------+-------+-------+

| linuxprobe |    60 |   550 |

+------------+-------+-------+

1 row in set (0.00 sec)

MariaDB [linuxprobe]> select name,price from mybook;

+------------+-------+

| name       | price |

+------------+-------+

| linuxprobe |    60 |

+------------+-------+

1 row in set (0.00 sec)

MariaDB [linuxprobe]>

//使用delete命令删除某个数据表单中的内容

MariaDB [linuxprobe]> delete from mybook;

Query OK, 1 row affected (0.00 sec)

MariaDB [linuxprobe]> select * from mybook;

Empty set (0.00 sec)

MariaDB [linuxprobe]> select name,price from mybook;

Empty set (0.00 sec)

MariaDB [linuxprobe]>

//使用select与where命令查询数据库中的数据

where命令(在数据库中进行匹配查询的条件命令)中使用的参数以及作用:

参数

作用

=

相等

<>或!=

不相等

>

大于

<

小于

>=

大于或等于

<=

小于或等于

BETWEEN

在某个范围内

LIKE

搜索一个例子

IN

在列中搜索多个值

MariaDB [linuxprobe]> insert into mybook(name,price,pages) value('linuxprobe','55','550');

Query OK, 1 row affected (0.00 sec)

MariaDB [linuxprobe]> insert into mybook(name,price,pages) value('linuxcool','60','600');

Query OK, 1 row affected (0.01 sec)

MariaDB [linuxprobe]> insert into mybook(name,price,pages) value('linuxdown','90','200');

Query OK, 1 row affected (0.00 sec)

MariaDB [linuxprobe]> select * from mybook;

+------------+-------+-------+

| name       | price | pages |

+------------+-------+-------+

| linuxprobe |    55 |   550 |

| linuxcool  |    60 |   600 |

| linuxdown  |    90 |   200 |

+------------+-------+-------+

3 rows in set (0.01 sec)

MariaDB [linuxprobe]> select * from mybook where price>=60;

+-----------+-------+-------+

| name      | price | pages |

+-----------+-------+-------+

| linuxcool |    60 |   600 |

| linuxdown |    90 |   200 |

+-----------+-------+-------+

2 rows in set (0.01 sec)

MariaDB [linuxprobe]> select * from mybook where price!=60;

+------------+-------+-------+

| name       | price | pages |

+------------+-------+-------+

| linuxprobe |    55 |   550 |

| linuxdown  |    90 |   200 |

+------------+-------+-------+

2 rows in set (0.01 sec)

MariaDB [linuxprobe]>

5、数据库的备份与恢复

mysqldump命令用于备份数据库数据,mysql命令可用于恢复数据库。

命令格式:mysqldump [参数] [数据库名称] > 路径及备份数据库文件名.dump

参数:-u参数用于定义登录数据库账户的名称;-p参数代表密码提示符,命令执行后需要输入对应数据库账号的密码。

//使用mysqldump命令备份数据库

[root@linuxprobe home]# mysqldump -u root -p linuxprobe > /home/linuxprobeDBbak.dump

Enter password:

[root@linuxprobe home]# ll

-rw-r--r--. 1 root       root            1940 Mar 22 21:48 linuxprobeDBbak.dump

//以root账号删除linuxprobe数据库

[root@linuxprobe home]# mysql -u root -p

Enter password:

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 20

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)]> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| linuxprobe         |

| mysql              |

| performance_schema |

+--------------------+

4 rows in set (0.00 sec)

MariaDB [(none)]> drop database linuxprobe;

Query OK, 1 row affected (0.01 sec)

MariaDB [(none)]> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

+--------------------+

3 rows in set (0.00 sec)

MariaDB [(none)]>

//使用mysql命令恢复数据库,恢复数据库前需要先创建数据库

MariaDB [(none)]> create database linuxprobe;

Query OK, 1 row affected (0.01 sec)

MariaDB [(none)]> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| linuxprobe         |

| performance_schema |

+--------------------+

3 rows in set (0.00 sec)

MariaDB [(none)]>

//执行恢复数据库

[root@linuxprobe home]# mysql -u root -p linuxprobe < /home/linuxprobeDBbak.dump

Enter password:          //输入数据库管理员账号root的密码

//验证数据恢复是否成功

[root@linuxprobe home]# mysql -u root -p

Enter password:

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 35

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)]> use linuxprobe;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

MariaDB [linuxprobe]> show tables;

+----------------------+

| Tables_in_linuxprobe |

+----------------------+

| mybook               |

+----------------------+

1 row in set (0.00 sec)

MariaDB [linuxprobe]> describe mybook;

+-------+----------+------+-----+---------+-------+

| Field | Type     | Null | Key | Default | Extra |

+-------+----------+------+-----+---------+-------+

| name  | char(15) | YES  |     | NULL    |       |

| price | int(11)  | YES  |     | NULL    |       |

| pages | int(11)  | YES  |     | NULL    |       |

+-------+----------+------+-----+---------+-------+

3 rows in set (0.00 sec)

MariaDB [linuxprobe]> select * from mybook;

+------------+-------+-------+

| name       | price | pages |

+------------+-------+-------+

| linuxprobe |    55 |   550 |

| linuxcool  |    60 |   600 |

| linuxdown  |    90 |   200 |

+------------+-------+-------+

3 rows in set (0.00 sec)

MariaDB [linuxprobe]>

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!