CentOS8服务篇6:配置MariaDB网络数据库服务

文章详细介绍了如何在Linux环境中安装和初始化MariaDB数据库,包括设置ROOT管理员密码、删除匿名账户和测试数据库、授权与权限管理。此外,还讨论了如何创建和管理数据库账户,以及如何使用GRANT和REVOKE命令进行权限分配和撤销。文章最后提到了数据库的备份和恢复,通过mysqldump进行数据导出和导入,确保数据安全。
摘要由CSDN通过智能技术生成

一、MariaDB的基本配置

yum install  -y mariadb mariadb-server

systemctl start mariadb 

systemctl enable mariadb 

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

1步:登录系统,MariaDB数据库管理系统的默认密码为空(注意,该密码并非ROOT管理员在系统中的密码),可直接按回车键,进入MariaDB数据库初识化设计步骤。 

2步:设置ROOT管理员密码,设置ROOT管理员在数据库中的专有密码。

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

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

5步:刷新授权列表,使初始化的设定立即生效。

mysql_secure_installation

[root@mail ~]# mysql_secure_installation

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

【firewall-cmd --permanent --add-service=mysql

【firewall-cmd --reload】

success

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

mysql -u root -p】

Enter password:  此处输入刚才设置的ROOT管理员在数据库中的密码

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

Your MariaDB connection id is 10

Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

在登录MariaDB数据库执行数据库命令时,需要在命令后面用分号(;)结尾,这也是与Linux命令最显著的区别。下面执行【SHOW databases;】命令,查看当前数据库管理系统中都有哪些数据库,如图所示。

 接下来使用数据库命令【SET password = PASSWORD('linux-yhy');】,将ROOT管理员在数据库管理系统中的密码修改为linux-yhy。退出后再尝试登录,如果还输入原来的密码,将会提示访问失败。

二、账户的授权与移除

为了保障数据库系统的安全性,在生产环境中不能一直使用ROOT管理员账户,同时也需要其他用户协同管理数据库,可以在MariaDB数据库管理系统中创建多个专用的数据库管理账户,再为其分配合理的权限,以满足其工作需求。为此,可使用ROOT管理员登录数据库管理系统,然后按照“CREATE USER 用户名@主机名 IDENTIFIED BY '密码'; ”的格式创建数据库管理账户yhy,命令为【CREATE USER yhy@localhost IDENTIFIED BY 'linux-yhy';】。需要注意的是每条数据库命令后面的分号(;)。

MariaDB [(none)]>

Query OK, 0 rows affected (0.01 sec)

创建的账户信息可以使用select命令语句来查询。【SELECT HOST,USER,PASSWORD FROM user WHERE USER="yhy";】命令查询的是账户yhy的主机名称、账户名称及经过加密的密码值信息:

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="yhy";

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

| HOST      | USER | PASSWORD                                  |

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

| localhost | yhy  | *02DBD63F97607D554501DC5D89DA88B6A0C9D277 |

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

1 row in set (0.00 sec)

MariaDB [mysql]>

不过,用户yhy只是一个普通账户,它没有数据库的任何操作权限。需要对其进行授权,它才能操作数据库。GRANT命令用于为账户进行授权,如表15-1所示。在使用GRANT命令时需要写上要赋予的权限、数据库及表单名称,以及对应的账户及主机信息。

   

   

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

对某个特定数据库中的特定数据表s授予权限

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

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

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

对所有数据库及所有数据表授予权限

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

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

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

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

账户的授权工作需要数据库管理员执行。下面以ROOT管理员的身份登录到数据库管理系统中,针对mysql数据库中的user数据表向账户yhy授予查询、更新、删除及插入等权限。

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 yhy@localhost;

Query OK, 0 rows affected (0.00 sec)

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

MariaDB [mysql]> SHOW GRANTS FOR yhy@localhost;

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

|  Grants for yhy@localhost    |

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

|GRANT USAGE ON *.* TO 'yhy'@'localhost' IDENTIFIED BY PASSWORD '*02DBD63F9

7607D554501DC5D89DA88B6A0C9D277' |

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

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

2 rows in set (0.00 sec)

MariaDB [mysql]>

以上输出信息中显示账户yhy已经拥有了针对mysql数据库中的user数据表的一系列权限了。这时再切换到账户yhy,就能够看到mysql数据库了,还能看到数据表user(其余数据表会因无操作权限而继续隐藏):

mysql -u yhy -p】

Enter password:此处输入yhy用户在数据库中的密码

MariaDB [(none)]> SHOW DATABASES;

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

| Database           |

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

| information_schema |

| mysql              |

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

2 rows in set (0.01 sec)

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.01 sec)

MariaDB [mysql]> exit

Bye

当前,先切换回ROOT管理员账户,移除刚才的授权。

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 yhy@localhost;

Query OK, 0 rows affected (0.00 sec)

可以看到,除移除授权的命令(REVOKE)与授权命令(GRANT)不同以外,其余部分都是一致的。这样不仅好记还容易理解。执行移除授权命令后,再来查看账户yhy的信息。

MariaDB [(none)]>SHOW GRANTS FOR yhy@localhost;

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

| Grants for yhy@localhost |

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

| GRANT USAGE ON *.* TO 'yhy'@'localhost' IDENTIFIED BY PASSWORD '*02DBD63F97607D554501DC5D89DA88B6A0C9D277' |

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

1 row in set (0.00 sec)

三、操MariaDB数据库

MariaDB数据库管理系统中,一个数据库可以存放多个数据表,数据表是数据库的核心内容。用户可以根据自己的需求自定义数据表结构,然后在其中合理地存放数据,以便后期轻松地维护和修改数据。下表所示为部分数据库的命令及其作用。

命令

作用

CREATE DATABASE 数据库名称

创建新的数据库

DESCRIBE数据表名称

描述数据表

UPDATE数据表名称SET attribute=新值WHERE attribute>原始值

更新数据表中的数据

USE数据库名称

指定使用的数据库

SHOW databases

显示当前已有的数据库

SHOW tables

显示当前数据库中的数据表

SELECT * FROM数据表名称

从数据表中选中某个记录值

DELETE FROM数据表名称WHERE attribute=

从数据表中删除某个记录值

创建数据库是管理数据的起点。现在尝试创建一个名为linux-yhy的数据库,再查看数据库列表,就能看到它了:

MariaDB [(none)]> CREATE DATABASE linux-yhy;

Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> SHOW databases;

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

| Database           |

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

information_schema |

linux-yhy         |

mysql              |

performance_schema |

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

4 rows in set (0.04 sec)

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

MariaDB [(none)]> use linux-yhy;

Database changed

MariaDB [linux-yhy]> CREATE TABLE mybook (name char(15),price int,pages int);

Query OK, 0 rows affected (0.16 sec)

MariaDB [linux-yhy]> 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)

接下来在mybook数据表单中插一条图书信息。为此需要使用INSERT命令,并在命令中写清数据表名称及对应的字段项。执行该命令之后即可完成插入图书信息。下面使用该命令插入一条图书信息,其中书名为linux-yhy,价格和页数分别是49元和300页。执行命令后图书信息会被成功写入数据表,然后就可以查询数据表中的内容了。在使用select命令查询数据表内容时,需要加上想要查询的字段,如果想查看数据表中的所有内容,可以使用星号(*)通配符来显示:

MariaDB [linux-yhy]> INSERT INTO mybook(name,price,pages) VALUES('linux-yhy',

'60', '518');

Query OK, 1 row affected (0.00 sec)

MariaDB [linux-yhy]> SELECT * FROM mybook;

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

| name       | price | pages |

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

linux-yhy |    49 |   300 |

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

1 rows in set (0.01 sec)

对数据库运维人员而言,需要做好四门功课增、删、改、查。这意味着创建数据表并在其中插入内容仅仅是第一步,还需要掌握数据表内容的修改方法。例如,可以使用update命令将刚才插入的linux-yhy图书信息的价格修改为45元,然后使用select命令查看该图书的名称和定价信息。注意,因为这里只查看图书的名称和定价,而不涉及页码,所以无须再用星号通配符来显示所有内容。

MariaDB [linux-yhy]> UPDATE mybook SET price=55 ;

Query OK, 1 row affected (0.00 sec)

Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [linux-yhy]> SELECT name,price FROM mybook;

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

| name       | price |

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

| linux-yhy |    45 |

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

1 row in set (0.00 sec)

还可以使用delete命令删除某个数据表中的内容。下面使用delete命令删除数据表mybook中的所有内容,再查看该数据表中的内容,可以发现该数据表的内容为空。

MariaDB [linux-yhy]> DELETE FROM mybook;

Query OK, 1 row affected (0.01 sec)

MariaDB [linux-yhy]> SELECT * FROM mybook;

Empty set (0.00 sec)

一般而言,数据表中会存放成千上万条数据信息。比如刚刚创建的用于保存图书信息的mybook数据表,随着时间的推移,里面的图书信息也会越来越多。在这样的情况下,如果只想查看价格大于某个数值的图书,该如何定义查询语句呢?

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

MariaDB [linux-yhy]> INSERT INTO mybook(name,price,pages) VALUES('linux-yhy1',

'30', '518');

Query OK, 1 row affected (0.05 sec)

MariaDB [linux-yhy]> INSERT INTO mybook(name,price,pages) VALUES('linux-yhy2',

'50', '518');

Query OK, 1 row affected (0.05 sec)

MariaDB [linux-yhy]> INSERT INTO mybook(name,price,pages) VALUES('linux-yhy3',

'80', '518');

Query OK, 1 row affected (0.01 sec)

MariaDB [linux-yhy]> INSERT INTO mybook(name,price,pages) VALUES('linux-yhy4',

'100', '518');

Query OK, 1 row affected (0.00 sec)

要想让查询结果更加精准,需要结合使用selectwhere命令。其中,where命令是在数据库中进行匹配查询的条件命令。通过设置查询条件,可以查找出符合该条件的数据。where命令中常用的查询参数及作用如下表所示。

   

   

=

相等

<>!=

不相等

大于

小于

>=

大于或等于

<=

小于或等于

BETWEEN

在某个范围内

LIKE

搜索一个例子

IN

在列表中搜索多个值

现在进入手动环节。分别在mybook数据表中查找出价格大于75元或价格不等于80元的图书,其对应的命令如下所示。在熟悉了这两个查询条件之后,大家可以自行尝试精确查找图书名为linux-yhy2的图书信息。

MariaDB [linux-yhy]> SELECT * FROM mybook WHERE price>75;

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

| name        | price | pages |

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

| linux-yhy3 |    80 |   518 |

| linux-yhy4 |   100 |   518 |

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

2 rows in set (0.06 sec)

MariaDB [linux-yhy]> SELECT * FROM mybook WHERE price!=80;

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

| name | price | pages        |

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

| linux-yhy1  | 30  | 518    |

| linux-yhy2  | 50  | 518    |

| linux-yhy4  | 100 | 518    |

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

3 rows in set (0.01 sec)

MariaDB [mysql]> exit

Bye

四、数据库的备份及恢复

MariaDB备份数据库的命令为mysqldump,该命令的工作原理很简单。它先查出需要备份的数据表的结构,再在文本文件中生成一个CREATE语句。然后将表中的所有记录转换成一条INSERT语句。最后通过这些语句就能创建数据表并插入数据了。

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

mysqldump -u root -p linux-yhy > /root/linux-yhyDB.dump

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

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

MariaDB [(none)]> DROP DATABASE linux-yhy;

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 linux-yhy;

Query OK, 1 row affected (0.00 sec)

接下来验证数据恢复的效果,使用输入重定向符把刚刚备份的数据库文件导入mysql命令,执行该命令。然后登录MariaDB数据库,就能看到linux-yhy数据库及mybook数据表了。数据库恢复成功。

mysql -u root -p linux-yhy < /root/linux-yhyDB.dump

Enter password: 此处输入ROOT管理员在数据库中的密码值

mysql -u root -p】

Enter password: 此处输入ROOT管理员在数据库中的密码值

MariaDB [(none)]> use linux-yhy;

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 [linux-yhy]> SHOW tables;

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

| Tables_in_linux-yhy |

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

| mybook               |

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

1 row in set (0.05 sec)

MariaDB [linux-yhy]> 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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

北方的流星

你的鼓励是我创作最大动力。

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

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

打赏作者

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

抵扣说明:

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

余额充值