RHCE-Day18-数据库

RHCE-Day18-数据库

使用MariaDB数据库管理系统

1.数据库管理系统

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

2.初始化mariaDB服务

相较于MySQL,MariaDB数据库管理系统有了很多新鲜的扩展特性,例如对微秒级别的支持、线程池、子查询优化、进程报告等。

安装部署MariaDB数据库主程序及服务端程序

[root@linuxprobe ~]# yum install mariadb mariadb-server
[root@linuxprobe ~]# systemctl start mariadb 
[root@linuxprobe ~]# systemctl enable mariadb 

在确认MariaDB数据库软件程序安装完毕并成功启动后请不要立即使用。为了确保数据库的安全性和正常运转,需要先对数据库程序进行初始化操作。这个初始化操作涉及下面5个步骤。

  1. 设置root管理员在数据库中的密码值(注意,该密码并非root管理员在系统中的密码,这里的密码值默认应该为空,可直接按回车键)。
  2. 设置root管理员在数据库中的专有密码。
  3. 随后删除匿名账户,并使用root管理员从远程登录数据库,以确保数据库上运行的业务的安全性。
  4. 删除默认的测试数据库,取消测试数据库的一系列访问权限。
  5. 刷新授权列表,让初始化的设定立即生效。

对于上述数据库初始化的操作步骤,刘遄老师已经在下面的输出信息旁边进行了简单注释,确保各位读者更直观地了解要输入的内容:

[root@linuxprobe ~]# 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
New password:输入要为root管理员设置的数据库密码
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管理员远程访问数据库,可在上面的初始化操作中设置策略,以允许root管理员从远程访问。

需要设置防火墙,使其放行对数据库服务程序的访问请求,数据库服务程序默认会占用3306端口,在防火墙策略中服务名称统一叫作mysql:

[root@linuxprobe ~]# firewall-cmd --permanent --add-service=mysql
success
[root@linuxprobe ~]# firewall-cmd --reload
success

登录MariaDB数据库

-u参数用来指定以root管理员的身份登录,而-p参数用来验证该用户在数据库中的密码值。

[root@linuxprobe ~]# mysql -u root -p
Enter password: 此处输入root管理员在数据库中的密码
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 5
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)]>

在登录MariaDB数据库后执行数据库命令时,都需要在命令后面用分号(;)结尾,

MariaDB [(none)]> SHOW databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.01 sec)

修改root管理员在数据库管理系统中的密码值

MariaDB [(none)]> SET password = PASSWORD('linuxprobe');
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> exit
Bye
[root@linuxprobe ~]# mysql -u root -p
Enter password:此处输入root管理员在数据库中的新密码

3.管理用户以及授权

创建数据库管理账户,分配合理的权限

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

MariaDB [(none)]> CREATE USER luke@localhost IDENTIFIED BY 'linuxprobe';
Query OK, 0 rows affected (0.00 sec)

创建的账户信息可以使用select命令语句来查询。下面命令查询的是账户luke的主机名称、账户名称以及经过加密的密码值信息:

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="luke";
+-----------+------+-------------------------------------------+
| host      | user | password                                  |
+-----------+------+-------------------------------------------+
| localhost | luke | *55D9962586BE75F4B7D421E6655973DB07D6869F |
+-----------+------+-------------------------------------------+

为账户进行授权

使用grant命令时需要写上要赋予的权限、数据库及表单名称,以及对应的账户及主机信息

表18-1 GRANT命令的常见格式以及解释

命令作用
GRANT 权限 ON 数据库.表单名称 TO 用户名@主机名对某个特定数据库中的特定表单给予授权
GRANT 权限 ON 数据库.* TO 用户名@主机名对某个特定数据库中的所有表单给予授权
GRANT 权限 ON . TO 用户名@主机名对所有数据库及所有表单给予授权
GRANT 权限1,权限2 ON 数据库.* TO 用户名@主机名对某个数据库中的所有表单给予多个授权
GRANT ALL PRIVILEGES ON . TO 用户名@主机名对所有数据库及所有表单给予全部授权(需谨慎操作)
[root@linuxprobe ~]# mysql -u root -p
Enter password:此处输入root管理员在数据库中的密码
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 luke@localhost;
Query OK, 0 rows affected (0.00 sec)

在执行完上述授权操作之后,我们再查看一下账户luke的权限:

MariaDB [(none)]>  SHOW GRANTS FOR luke@localhost;
+-------------------------------------------------------------------------------------------------------------+
| Grants for luke@localhost |
+-------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'luke'@'localhost' IDENTIFIED BY PASSWORD '*55D9962586BE75F4B7D421E6655973DB07D6869F' |
| GRANT SELECT, INSERT, UPDATE, DELETE ON `mysql`.`user` TO 'luke'@'localhost' |
+-------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

移除授权

[root@linuxprobe ~]# mysql -u root -p
Enter password:此处输入root管理员在数据库中的密码
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 [(none)]> REVOKE SELECT,UPDATE,DELETE,INSERT ON mysql.user FROM luke@localhost;
Query OK, 0 rows affected (0.00 sec)

再来查看账户luke的信息:

MariaDB [(none)]> SHOW GRANTS FOR luke@localhost;
+-------------------------------------------------------------------------------------------------------------+
| Grants for luke@localhost |
+-------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'luke'@'localhost' IDENTIFIED BY PASSWORD '*55D9962586BE75F4B7D421E6655973DB07D6869F' |
+-------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

4.创建数据库与表单

在MariaDB数据库管理系统中,一个数据库可以存放多个数据表,数据表单是数据库中最重要最核心的内容。

表18-2 用于创建数据库的命令以及作用

用法作用
CREATE database 数据库名称。创建新的数据库
DESCRIBE 表单名称;描述表单
UPDATE 表单名称 SET attribute=新值 WHERE attribute > 原始值;更新表单中的数据
USE 数据库名称;指定使用的数据库
SHOW databases;显示当前已有的数据库
SHOW tables;显示当前数据库中的表单
SELECT * FROM 表单名称;从表单中选中某个记录值
DELETE FROM 表单名 WHERE attribute=值;从表单中删除某个记录值

创建一个名为linuxprobe的数据库

MariaDB [(none)]> CREATE DATABASE linuxprobe;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> SHOW databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| linuxprobe         |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.04 sec)

创建数据表单

需要先切换到某个指定的数据库中。比如在新建的linuxprobe数据库中创建表单mybook,然后进行表单的初始化,即定义存储数据内容的结构。我们分别定义3个字段项,其中,长度为15个字符的字符型字段name用来存放图书名称,整型字段price和pages分别存储图书的价格和页数。当执行完下述命令之后,就可以看到表单的结构信息了:

MariaDB [(none)]> use linuxprobe;
Database changed
MariaDB [linuxprobe]> CREATE TABLE mybook (name char(15),price int,pages int);
Query OK, 0 rows affected (0.16 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.02 sec)

5.管理表单及数据

需要使用INSERT命令,并在命令中写清表单名称以及对应的字段项。

select命令查询表单内容时,需要加上想要查询的字段;如果想查看表单中的所有内容,则可以使用星号(*)通配符来显示:

MariaDB [linuxprobe]> INSERT INTO mybook(name,price,pages) VALUES('linuxprobe','60', '518');
Query OK, 1 row affected (0.00 sec)
MariaDB [linuxprobe]> select * from mybook;
+------------+-------+-------+
| name       | price | pages |
+------------+-------+-------+
| linuxprobe |    60 |   518 |
+------------+-------+-------+
1 rows in set (0.01 sec)

使用update命令修改某个数据表单中的内容

MariaDB [linuxprobe]> UPDATE mybook SET price=55 ;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
MariaDB [linuxprobe]> SELECT name,price FROM mybook;
+------------+-------+
| name       | price |
+------------+-------+
| linuxprobe |    55 |
+------------+-------+
1 row in set (0.00 sec)

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

MariaDB [linuxprobe]> DELETE FROM mybook;
Query OK, 1 row affected (0.01 sec)
MariaDB [linuxprobe]> SELECT * FROM mybook;
Empty set (0.00 sec)

下面先使用insert插入命令依次插入4条图书信息:

MariaDB [linuxprobe]> INSERT INTO mybook(name,price,pages) VALUES('linuxprobe1','30','518');
Query OK, 1 row affected (0.05 sec)
MariaDB [linuxprobe]> INSERT INTO mybook(name,price,pages) VALUES('linuxprobe2','50','518');
Query OK, 1 row affected (0.05 sec)
MariaDB [linuxprobe]> INSERT INTO mybook(name,price,pages) VALUES('linuxprobe3','80','518');
Query OK, 1 row affected (0.01 sec)
MariaDB [linuxprobe]> INSERT INTO mybook(name,price,pages) VALUES('linuxprobe4','100','518');
Query OK, 1 row affected (0.00 sec)

结合使用select与where命令了。其中,where命令是在数据库中进行匹配查询的条件命令。通过设置查询条件,就可以仅查找出符合该条件的数据。

where命令中使用的参数以及作用

参数作用
=相等
<>或!=不相等
>大于
<小于
>=大于或等于
<=小于或等于
BETWEEN在某个范围内
LIKE搜索一个例子
IN在列中搜索多个值

在mybook表单中查找出价格大于75元或价格不等于80元的图书

MariaDB [linuxprobe]> SELECT * FROM mybook WHERE price>75;
+-------------+-------+-------+
| name        | price | pages |
+-------------+-------+-------+
| linuxprobe3 |    80 |   518 |
| linuxprobe4 |   100 |   518 |
+-------------+-------+-------+
2 rows in set (0.06 sec)
MariaDB [linuxprobe]> SELECT * FROM mybook WHERE price!=80;
+-------------+-------+-------+
| name | price | pages        |
+-------------+-------+-------+
| linuxprobe1  | 30  | 518    |
| linuxprobe2  | 50  | 518    |
| linuxprobe4  | 100 | 518    |
+-------------+-------+-------+
3 rows in set (0.01 sec)
MariaDB [mysql]> exit
Bye

6.数据库的备份及恢复

数据库的备份

mysqldump命令用于备份数据库数据,格式为“mysqldump [参数] [数据库名称]”。其中参数与mysql命令大致相同,-u参数用于定义登录数据库的账户名称,-p参数代表密码提示符。下面将linuxprobe数据库中的内容导出成一个文件,并保存到root管理员的家目录中:

[root@linuxprobe ~]# mysqldump -u root -p linuxprobe > /root/linuxprobeDB.dump
Enter password:此处输入root管理员在数据库中的密码

然后进入MariaDB数据库管理系统,彻底删除linuxprobe数据库,这样mybook数据表单也将被彻底删除。然后重新建立linuxprobe数据库:

MariaDB [(none)]> DROP DATABASE linuxprobe;
Query OK, 1 row affected (0.04 sec)
MariaDB [(none)]> SHOW databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.02 sec)
MariaDB [(none)]> CREATE DATABASE linuxprobe;
Query OK, 1 row affected (0.00 sec)

数据恢复

使用输入重定向符把刚刚备份的数据库文件导入到mysql命令中,然后执行该命令。接下来登录到MariaDB数据库,就又能看到linuxprobe数据库以及mybook数据表单了。数据库恢复成功!

[root@linuxprobe ~]# mysql -u root -p linuxprobe < /root/linuxprobeDB.dump 
Enter password: 此处输入root管理员在数据库中的密码值
[root@linuxprobe ~]# mysql -u root -p
Enter password: 此处输入root管理员在数据库中的密码值
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.05 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.02 sec)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值