当前位置: 首页 > news >正文

MariaDB数据库管理

文章目录

  • MariaDB数据库管理
    • 介绍MariaDB数据库
      • 数据库介绍
      • 数据库种类
      • 关系数据库
      • MariaDB 介绍
    • 部署MariaDB
      • 安装MariaDB
        • 安装
        • 启动并启用服务
        • 配置防火墙
      • 加固 MariaDB
      • 连接 MariaDB
      • 配置 MariaDB
    • MariaDB 中 SQL
      • 描述 SQL
      • 连接数据库
      • 数据库操作
        • 查询数据库列表
        • 使用数据库
        • 创建数据库
        • 删除数据库
      • 表操作
        • 环境准备
        • 查询表
          • 查询表列表
          • 查询表结构
          • 查询表中数据
          • WHERE子句
          • 多表查询
          • 函数
        • 创建表
        • 插入记录
        • 更新记录
        • 删除记录
        • 删除表
    • 管理 MariaDB 用户
      • 创建用户账户
      • 控制用户权限
        • 查询用户权限
        • 授予用户权限
        • 回收用户权限
      • 删除用户
      • 更改用户密码
      • 排故数据库访问
      • 忘记 root 用户密码
    • 备份和恢复
      • 备份方式
        • 逻辑备份
        • 物理备份特征
      • 执行备份
        • 执行逻辑备份
        • 执行物理备份
      • 执行恢复
        • 恢复逻辑备份
        • 恢复物理备份
          • 正确做法:
          • 错误:

MariaDB数据库管理

介绍MariaDB数据库

数据库介绍

**数据库,是一个存放计算机数据的仓库。**这个仓库是按照一定的数据结构来对数据进行组织和存 储的,我们可以通过数据库提供的多种方法来管理其中的数据。

数据结构,是指数据的组织形式或数据之间的联系。

数据库种类

按照早期的数据库理论,比较流行的数据库模型有三种,分别为:

  • 层次式数据库,以层次模型建立的数据库,例如文件系统。
  • 网状数据库,以网状模型建立的数据库,例如网络通信。
  • 关系型数据库,以二维表模型建议的数据库,例如图书馆管理系统。

在当今的互联网企业中,最常用的数据库模式主要有两种:

  • 关系型数据库,把复杂的数据结构归结为简单的二维表形式。在关系型数据库中,对数据的操作几乎全部建立在一个或多个关系表格上,通过对这些关联的表格分类、合并、连接或选取等运算来实现数据库的管理。例如:Oracle、MySql和PostgreSQL等。
  • 非关系型数据库,也被称为 NoSQL 数据库,本意是“Not Only SQL”,而非“NO SQL”的意思,因此,NoSQL的产生并不是要彻底否定关系型数据库,而是作为传统数据库的一个有效补充。典型产品有Redis(持久化缓存)、Mongodb、Memcached(纯内存)等。

关系数据库

关系数据库将结构化数据存储在一组相互关联的表中。 这意味着一个表中的信息可以轻松地与另一表中的信息组合在一起,以处理和提供有关该数据的报告。

关系数据库管理系统(RDBMS-relational database management system)是用于管理关系数据库的软件。大多数关系数据库管理系统都允许您使用结构化查询语言(SQL)查找和管理数据库中的数据。

许多组织使用关系数据库存储业务信息,例如:库存,销售和财务。 关系数据库管理系统在许多应用中起着关键作用。 例如,许多需要支持动态生成内容的Web应用程序都是围绕LAMP(LNMP)解决方案构建:

  • Linux 操作系统提供了基本的环境。
  • Apache HTTPS ServerNginx,提供Web服务器。
  • MariaDBMySQL其他关系数据库,例如PostgreSQL,用于存储站点数据。
  • 由 Web 服务器运行的编程语言,例如 PHP,Python,Perl,Ruby,Java,服务器端JavaScript或其他,可以更新数据库中的数据并使用它为用户动态构建网页。

由于关系数据库通常与系统管理员管理的应用程序集成在一起,因此即使您不是数据库管理员,也有必要了解这些服务及其数据的安装和管理知识。

MariaDB 介绍

MariaDB数据库管理系统是MySQL数据库的一个分支,主要由开源社区维护,采用GPL授权许可。开发这个MariaDB数据库分支的可能原因之一是:甲骨文公司收购了MySQL后,有将MySQL闭源的潜在风险,因此MySQL开源社区采用分支的方式来避开这个风险。

MariaDB数据库完全兼容MySQL数据库,包括API和命令行,使之能轻松的成为MySQL的代替品。

MariaDB数据库管理系统可以包含多个database,每个database包涵多张表。

在这里插入图片描述

关系数据库的表采用二维表格来存储数据,类似于Excle工作表。

  • 表中的一行即为一个元组,或称为一条记录
  • 数据表中的每一列称为一个字段(属性),表是由其包含的各种字段定义,每个字段描述了它所含有的数据意义,为每个字段分配一个数据类型,定义它们的数据长度和其他属性。
  • 行和列的交叉位置表示某个属性值

部署MariaDB

安装MariaDB

安装
#安装客户端
[root@server ~ 10:18:26]# yum install -y mariadb-server#安装服务端
[root@client ~ 10:19:30]# yum install -y mariadb-server
启动并启用服务
[root@server ~ 10:20:06]# systemctl enable mariadb --now
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
配置防火墙

查询:关闭状态

加固 MariaDB

MariaDB数据库默认具有test数据库和一些不太安全的配置。运行mysql_secure_installation修改这些配置。

[root@server ~ 10:22:33]# mysql_secure_installation NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDBSERVERS 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: 
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... 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- 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帐户从本地主机外部访问数据库。
  • 删除匿名用户帐户。
  • 删除用于演示的test数据库。

连接 MariaDB

MariaDB客户端可以通过两种方式连接到服务器:

  • 如果客户端与服务器在同一台计算机上运行,则它可以使用特殊的套接字文件(socket)与MariaDB通信。 这种方式更安全,因为MariaDB不需要侦听来自网络客户端的连接。 但是,使用该数据库的服务都需要在MariaDB服务器上运行,会影响性能。
[root@server ~ 10:28:42]# mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 17
Server version: 5.5.68-MariaDB MariaDB ServerCopyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MariaDB [(none)]> create user dyx@'%' identified by '123'-> ;
Query OK, 0 rows affected (0.00 sec)MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
  • 客户端可以使用TCP/IP网络连接到MariaDB服务。 远程服务器与MariaDB服务器运行在不同主机。 此时服务器需要配置侦听端口3306/TCP上的连接。
[root@client ~ 10:47:30]# mysql -u dyx -p -h 10.1.8.10
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 20
Server version: 5.5.68-MariaDB MariaDB ServerCopyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MariaDB [(none)]> Ctrl-C -- exit!
Aborted

配置 MariaDB

默认情况下,MariaDB 侦听系统中所有网络地址上3306/TCP端口。

MariaDB 配置文件:

  • 主配置文件 /etc/my.cnf
  • 辅助配置文件 /etc/my.conf.d/*

/etc/my.cnf.d/mariadb-server.cnf 文件是数据库服务主要配置。 在该文件的 [mysqld] 块中可以定义以下参数:

  • bind-address,该指令指定MariaDB用来侦听客户端连接的网络地址。 只能输入一个选项。 可能的选项包括:
    • 单个IPv4地址。
    • 单个IPv6地址。
    • ::连接到所有可用地址(IPv6和IPv4)。
    • 对于所有IPv4地址,保留为空白(或设置为0.0.0.0)。
    • 如果只希望本地客户端访问MariaDB,则可以使用127.0.0.1或**::1**作为网络地址。
  • skip-networking,如果在配置文件的[mysqld]部分中设置skip-networking或skip-networking = 1,则将禁用网络连接,并且客户端必须使用套接字文件与MariaDB通信。 如果您设置skip-networking = 0(默认值),MariaDB会侦听网络连接。
  • port, 您可以使用此设置指定3306/TCP以外的网络端口。

客户端配置示例:

[root@client ~ 10:32:03]# vim /etc/my.cnf.d/client.cnf[client]
user=dyx
password=123
host=server
port=3306

服务端

[root@server ~ 11:30:27]# vim /etc/my.cnf.d/client.cnf
[client]
user=root
password=123

MariaDB 中 SQL

描述 SQL

SQL(Structured Query Language),结构化查询语言,是1986年10 月由美国国家标准局颁布的数据库语言。国际标准化组织(ISO)也颁布了SQL国际标准。

结构化查询语言是关系数据库最重要的操作语言,它的影响已经超出数据库领域,得到其他领域的重视和采用,如人工智能领域的数据检索,第四代软件开发工具中嵌入SQL的语言等。

SQL语句可简单分为如下几类:

  • 数据查询语言(DQL:Data Query Language):也称为“数据检索语句”,用以从表中获得数据,保留字SELECT WHERE,ORDER BY,GROUP BY和HAVING。
  • 数据操作语言(DML:Data Manipulation Language):其语句包括动词INSERT,UPDATE和DELETE。
  • 数据定义语言(DDL):其语句包括动词CREATE和DROP。例如,在数据库中创建新表(CREATE TABLE)、删除表(DROP TABLE),为表添加索引等。
  • 事务处理语言(TPL):它的语句能确保被DML语句影响的表的所有行及时得以更新。TPL语句包括BEGIN TRANSACTION,COMMIT和ROLLBACK。
  • 数据控制语言(DCL):它的语句通过GRANT或REVOKE获得许可,确定单个用户和用户组对数据库对象的访问。

连接数据库

mariadb软件包提供了命令mysql,该命令支持对MariaDB数据库的交互式和非交互式访问。

  • 交互式执行时,结果以ASCII表的格式显示。
  • 非交互执行时,结果以制表符分隔的格式显示。

示例:

[root@server ~ 11:23:50]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.68-MariaDB MariaDB ServerCopyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

首次安装时,MariaDB默认设置root用户帐户无需密码即可进行访问。 执行交互式SQL语句时,MariaDB提示符在方括号中显示您当前选择数据库。

数据库操作

查询数据库列表
[root@server ~ 10:28:42]# mysql -u root -pMariaDB [(none)]> show databases-> ;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

数据库说明:

  • mysql 数据库,是一个系统数据库,保存数据库用户及其访问权限等信息。
  • INFORMATION_SCHEMA 数据库,保存关于数据库或者数据表的元数据信息。
  • **PERFORMANCE_SCHEMA **数据库,保存数据库服务器性能信息。
使用数据库

您可以使用 USE 语句选择数据库之一,例如:USE mysql;,后续默认操作的表属于mysql数据库。

MariaDB [(none)]> create database inventory;MariaDB [(none)]> use inventory
Database changed

SQL语句不区分大小写,但数据库名称区分大小写。 通常的做法是数据库名称全为小写字母,SQL语句全为大写字母,以区分SQL语句与语句的目标或参数。

创建数据库
MariaDB [(none)]> create database dyx;
Query OK, 1 row affected (0.00 sec)MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| dyx                |
| inventory          |
| mysql              |
| performance_schema |
+--------------------+
5 rows in set (0.00 sec)
删除数据库

DROP DATABASE语句删除数据库中的所有表并删除数据库。 这将破坏数据库中的所有数据。 只有对该数据库具有DROP特权的用户才能运行此语句。 这不会更改数据库的用户特权。 如果重新创建具有该名称的数据库,则为旧数据库设置的用户权限仍然有效。

MariaDB [(none)]> drop database dyx;
Query OK, 0 rows affected (0.00 sec)MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| inventory          |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)

表操作

SQL CRUD操作(create 添加数据 read读取数据 update 修改数据delete删除数据)。

环境准备
[root@server ~ 16:14:45]rz -E
[root@server ~ 16:15:22]# mysql inventory < inventory.sql
查询表
查询表列表
MariaDB [inventory]> show tables;+---------------------+
| Tables_in_inventory |
+---------------------+
| category            |
| manufacturer        |
| product             |
+---------------------+
3 rows in set (0.00 sec)
查询表结构
Database changed
MariaDB [inventory]> describe product;
+-----------------+--------------+------+-----+---------+----------------+
| Field           | Type         | Null | Key | Default | Extra          |
+-----------------+--------------+------+-----+---------+----------------+
| id              | int(11)      | NO   | PRI | NULL    | auto_increment |
| name            | varchar(100) | NO   |     | NULL    |                |
| price           | double       | NO   |     | NULL    |                |
| stock           | int(11)      | NO   |     | NULL    |                |
| id_category     | int(11)      | NO   |     | NULL    |                |
| id_manufacturer | int(11)      | NO   |     | NULL    |                |
+-----------------+--------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)

输出显示,表格中有六列(属性):

  • Field 列,显示该属性名称。
  • Type 列,显示该属性的数据必须采用的格式。例如,stock属性必须是最多11位数字的整数。
  • Null 列,指示此属性是否可以为null。
  • Default 列,指示如果未指定该属性,则是否设置了默认值。
  • Key 列,显示属性ID是primary key。 主键是表中一行的唯一标识符。 该属性的任何其他行都不能具有相同的值。
  • Extra列,提供该列额外信息,对于id字段标记为auto_increment。 这意味着每次将新项目插入表中时,该条目的属性值都会增加。 这样可以更轻松地使数字主键保持唯一。
查询表中数据

查询表中所有记录所有字段

MariaDB [inventory]> select * from category;+----+---------------+
| id | name          |
+----+---------------+
|  1 | Networking    |
|  2 | Servers       |
|  3 | Ssd           |
|  4 | Laptops       |
|  5 | Monitors      |
|  6 | Keyboards     |
|  7 | Mice          |
|  8 | Printers      |
|  9 | External HDDs |
| 10 | RAM Modules   |
+----+---------------+
10 rows in set (0.00 sec)MariaDB [inventory]> select * from product;
+----+-------------------------------+---------+-------+-------------+-----------------+
| id | name                          | price   | stock | id_category | id_manufacturer |
+----+-------------------------------+---------+-------+-------------+-----------------+
|  1 | ThinkServer TS140             |  539.88 |    20 |           2 |               4 |
|  2 | ThinkServer RD630             | 2379.14 |    20 |           2 |               4 |
|  3 | RT-AC68U                      |  219.99 |    10 |           1 |               3 |
|  4 | X110 64GB                     |   73.84 |   100 |           3 |               1 |
|  5 | Dell XPS 15                   | 1299.99 |    15 |           4 |               5 |
|  6 | HP Pavilion 14                |  799.99 |    25 |           4 |               6 |
|  7 | Samsung 27-inch 4K            |  349.99 |    30 |           5 |               8 |
|  8 | Logitech Mechanical K845      |   89.99 |    50 |           6 |               7 |
|  9 | Acer Swift 3                  |  699.99 |    18 |           4 |               9 |
| 10 | Microsoft Surface Pro 9       | 1199.99 |    12 |           4 |              10 |
| 11 | Kingston A400 240GB           |   49.99 |    80 |           3 |               2 |
| 12 | Asus TUF Gaming VG27AQ        |  399.99 |    22 |           5 |               3 |
| 13 | Dell OptiPlex 7010            |  899.99 |    16 |           2 |               5 |
| 14 | HP Z2 Mini Workstation        | 1499.99 |     8 |           2 |               6 |
| 15 | Logitech G502 Hero            |   79.99 |    40 |           7 |               7 |
| 16 | HP LaserJet Pro M428fdw       |  499.99 |    12 |           8 |               6 |
| 17 | Samsung T7 Shield 2TB         |  199.99 |    35 |           9 |               8 |
| 18 | Kingston Fury Beast 16GB DDR4 |   44.99 |    60 |          10 |               2 |
| 19 | Dell Ultrasharp U2419H        |  299.99 |    20 |           5 |               5 |
| 20 | Acer Nitro AN515-57           |  999.99 |    14 |           4 |               9 |
+----+-------------------------------+---------+-------+-------------+-----------------+
20 rows in set (0.00 sec)MariaDB [inventory]> select * from manufacturer;
+----+-----------+----------------+-------------------+
| id | name      | seller         | phone_number      |
+----+-----------+----------------+-------------------+
|  1 | SanDisk   | John Miller    | +1 (941) 329-8855 |
|  2 | Kingston  | Mike Taylor    | +1 (341) 375-9999 |
|  3 | Asus      | Wilson Jackson | +1 (432) 367-8899 |
|  4 | Lenovo    | Allen Scott    | +1 (876) 213-4439 |
|  5 | Dell      | Sarah Clark    | +1 (567) 890-1234 |
|  6 | HP        | David Lewis    | +1 (234) 567-8901 |
|  7 | Logitech  | Emily White    | +1 (678) 901-2345 |
|  8 | Samsung   | Robert Brown   | +1 (345) 678-9012 |
|  9 | Acer      | Lisa Garcia    | +1 (789) 012-3456 |
| 10 | Microsoft | James Wilson   | +1 (456) 789-0123 |
+----+-----------+----------------+-------------------+
10 rows in set (0.00 sec)

查询表中所有记录特定字段


MariaDB [inventory]> select name,price,stock from product;
+-------------------------------+---------+-------+
| name                          | price   | stock |
+-------------------------------+---------+-------+
| ThinkServer TS140             |  539.88 |    20 |
| ThinkServer RD630             | 2379.14 |    20 |
| RT-AC68U                      |  219.99 |    10 |
| X110 64GB                     |   73.84 |   100 |
| Dell XPS 15                   | 1299.99 |    15 |
| HP Pavilion 14                |  799.99 |    25 |
| Samsung 27-inch 4K            |  349.99 |    30 |
| Logitech Mechanical K845      |   89.99 |    50 |
| Acer Swift 3                  |  699.99 |    18 |
| Microsoft Surface Pro 9       | 1199.99 |    12 |
| Kingston A400 240GB           |   49.99 |    80 |
| Asus TUF Gaming VG27AQ        |  399.99 |    22 |
| Dell OptiPlex 7010            |  899.99 |    16 |
| HP Z2 Mini Workstation        | 1499.99 |     8 |
| Logitech G502 Hero            |   79.99 |    40 |
| HP LaserJet Pro M428fdw       |  499.99 |    12 |
| Samsung T7 Shield 2TB         |  199.99 |    35 |
| Kingston Fury Beast 16GB DDR4 |   44.99 |    60 |
| Dell Ultrasharp U2419H        |  299.99 |    20 |
| Acer Nitro AN515-57           |  999.99 |    14 |
+-------------------------------+---------+-------+
20 rows in set (0.00 sec)
WHERE子句
MariaDB [inventory]> select * from product where price >100;
+----+-------------------------+---------+-------+-------------+-----------------+
| id | name                    | price   | stock | id_category | id_manufacturer |
+----+-------------------------+---------+-------+-------------+-----------------+
|  1 | ThinkServer TS140       |  539.88 |    20 |           2 |               4 |
|  2 | ThinkServer RD630       | 2379.14 |    20 |           2 |               4 |
|  3 | RT-AC68U                |  219.99 |    10 |           1 |               3 |
|  5 | Dell XPS 15             | 1299.99 |    15 |           4 |               5 |
|  6 | HP Pavilion 14          |  799.99 |    25 |           4 |               6 |
|  7 | Samsung 27-inch 4K      |  349.99 |    30 |           5 |               8 |
|  9 | Acer Swift 3            |  699.99 |    18 |           4 |               9 |
| 10 | Microsoft Surface Pro 9 | 1199.99 |    12 |           4 |              10 |
| 12 | Asus TUF Gaming VG27AQ  |  399.99 |    22 |           5 |               3 |
| 13 | Dell OptiPlex 7010      |  899.99 |    16 |           2 |               5 |
| 14 | HP Z2 Mini Workstation  | 1499.99 |     8 |           2 |               6 |
| 16 | HP LaserJet Pro M428fdw |  499.99 |    12 |           8 |               6 |
| 17 | Samsung T7 Shield 2TB   |  199.99 |    35 |           9 |               8 |
| 19 | Dell Ultrasharp U2419H  |  299.99 |    20 |           5 |               5 |
| 20 | Acer Nitro AN515-57     |  999.99 |    14 |           4 |               9 |
+----+-------------------------+---------+-------+-------------+-----------------+
15 rows in set (0.00 sec)

条件操作数

OperatorDescription
=Equal
<>Not equal. Note: In some versions of SQL, this operator may be written as !=
>Greater than
<Less than
>=Greater than or equal
<=Less than or equal
BETWEENBetween an inclusive range
LIKESearch for a pattern
INSpecify multiple possible values for a column
# 条件操作符包括:=、<>、>、<、>=、<=
MariaDB [inventory]> select * from product where price >100;
+----+-------------------------+---------+-------+-------------+-----------------+
| id | name                    | price   | stock | id_category | id_manufacturer |
+----+-------------------------+---------+-------+-------------+-----------------+
|  1 | ThinkServer TS140       |  539.88 |    20 |           2 |               4 |
|  2 | ThinkServer RD630       | 2379.14 |    20 |           2 |               4 |
|  3 | RT-AC68U                |  219.99 |    10 |           1 |               3 |
|  5 | Dell XPS 15             | 1299.99 |    15 |           4 |               5 |
|  6 | HP Pavilion 14          |  799.99 |    25 |           4 |               6 |
|  7 | Samsung 27-inch 4K      |  349.99 |    30 |           5 |               8 |
|  9 | Acer Swift 3            |  699.99 |    18 |           4 |               9 |
| 10 | Microsoft Surface Pro 9 | 1199.99 |    12 |           4 |              10 |
| 12 | Asus TUF Gaming VG27AQ  |  399.99 |    22 |           5 |               3 |
| 13 | Dell OptiPlex 7010      |  899.99 |    16 |           2 |               5 |
| 14 | HP Z2 Mini Workstation  | 1499.99 |     8 |           2 |               6 |
| 16 | HP LaserJet Pro M428fdw |  499.99 |    12 |           8 |               6 |
| 17 | Samsung T7 Shield 2TB   |  199.99 |    35 |           9 |               8 |
| 19 | Dell Ultrasharp U2419H  |  299.99 |    20 |           5 |               5 |
| 20 | Acer Nitro AN515-57     |  999.99 |    14 |           4 |               9 |
+----+-------------------------+---------+-------+-------------+-----------------+
15 rows in set (0.00 sec)|# BETWEEN,匹配2个数字之间(包括数字本身)的记录。
MariaDB [inventory]> select * from product where id between 1 and 3;
+----+-------------------+---------+-------+-------------+-----------------+
| id | name              | price   | stock | id_category | id_manufacturer |
+----+-------------------+---------+-------+-------------+-----------------+
|  1 | ThinkServer TS140 |  539.88 |    20 |           2 |               4 |
|  2 | ThinkServer RD630 | 2379.14 |    20 |           2 |               4 |
|  3 | RT-AC68U          |  219.99 |    10 |           1 |               3 |
+----+-------------------+---------+-------+-------------+-----------------+
3 rows in set (0.00 sec)# IN,匹配列表中记录。
MariaDB [inventory]> select * from product  where id in (1,3);
+----+-------------------+--------+-------+-------------+-----------------+
| id | name              | price  | stock | id_category | id_manufacturer |
+----+-------------------+--------+-------+-------------+-----------------+
|  1 | ThinkServer TS140 | 539.88 |    20 |           2 |               4 |
|  3 | RT-AC68U          | 219.99 |    10 |           1 |               3 |
+----+-------------------+--------+-------+-------------+-----------------+
2 rows in set (0.00 sec)MariaDB [inventory]> select * from category where name in ('Servers','Ssd');
+----+---------+
| id | name    |
+----+---------+
|  2 | Servers |
|  3 | Ssd     |
+----+---------+
2 rows in set (0.00 sec)# LIKE,用于匹配字符串。%表示一个或多个字符,_表示一个字符,[charlist]表示字符列中的任何单一字符,[^charlist]或者[!charlist]不在字符列中的任何单一字符。
MariaDB [inventory]> select * from product where name like '%
Server%';
+----+-------------------+---------+-------+-------------+-----------------+
| id | name              | price   | stock | id_category | id_manufacturer |
+----+-------------------+---------+-------+-------------+-----------------+
|  1 | ThinkServer TS140 |  539.88 |    20 |           2 |               4 |
|  2 | ThinkServer RD630 | 2379.14 |    20 |           2 |               4 |
+----+-------------------+---------+-------+-------------+-----------------+
2 rows in set (0.00 sec)# 逻辑与AND
MariaDB [inventory]> select * from product where name like '%RD%' and price>1000;
+----+-------------------+---------+-------+-------------+-----------------+
| id | name              | price   | stock | id_category | id_manufacturer |
+----+-------------------+---------+-------+-------------+-----------------+
|  2 | ThinkServer RD630 | 2379.14 |    20 |           2 |               4 |
+----+-------------------+---------+-------+-------------+-----------------+
1 row in set (0.00 sec)# 逻辑或or
MariaDB [inventory]> select * from product where name like '%RD%' or price>1000;# ORDER BY 关键字用于对结果集进行排序。
MariaDB [inventory]> select * from product order by price;
+----+-------------------------------+---------+-------+-------------+-----------------+
| id | name                          | price   | stock | id_category | id_manufacturer |
+----+-------------------------------+---------+-------+-------------+-----------------+
| 18 | Kingston Fury Beast 16GB DDR4 |   44.99 |    60 |          10 |               2 |
| 11 | Kingston A400 240GB           |   49.99 |    80 |           3 |               2 |
|  4 | X110 64GB                     |   73.84 |   100 |           3 |               1 |
| 15 | Logitech G502 Hero            |   79.99 |    40 |           7 |               7 |
|  8 | Logitech Mechanical K845      |   89.99 |    50 |           6 |               7 |
| 17 | Samsung T7 Shield 2TB         |  199.99 |    35 |           9 |               8 |
|  3 | RT-AC68U                      |  219.99 |    10 |           1 |               3 |
| 19 | Dell Ultrasharp U2419H        |  299.99 |    20 |           5 |               5 |
|  7 | Samsung 27-inch 4K            |  349.99 |    30 |           5 |               8 |
| 12 | Asus TUF Gaming VG27AQ        |  399.99 |    22 |           5 |               3 |
| 16 | HP LaserJet Pro M428fdw       |  499.99 |    12 |           8 |               6 |
|  1 | ThinkServer TS140             |  539.88 |    20 |           2 |               4 |
|  9 | Acer Swift 3                  |  699.99 |    18 |           4 |               9 |
|  6 | HP Pavilion 14                |  799.99 |    25 |           4 |               6 |
| 13 | Dell OptiPlex 7010            |  899.99 |    16 |           2 |               5 |
| 20 | Acer Nitro AN515-57           |  999.99 |    14 |           4 |               9 |
| 10 | Microsoft Surface Pro 9       | 1199.99 |    12 |           4 |              10 |
|  5 | Dell XPS 15                   | 1299.99 |    15 |           4 |               5 |
| 14 | HP Z2 Mini Workstation        | 1499.99 |     8 |           2 |               6 |
|  2 | ThinkServer RD630             | 2379.14 |    20 |           2 |               4 |
+----+-------------------------------+---------+-------+-------------+-----------------+
20 rows in set (0.00 sec)#降序
MariaDB [inventory]> select * from product order by price desc;
+----+-------------------------------+---------+-------+-------------+-----------------+
| id | name                          | price   | stock | id_category | id_manufacturer |
+----+-------------------------------+---------+-------+-------------+-----------------+
|  2 | ThinkServer RD630             | 2379.14 |    20 |           2 |               4 |
| 14 | HP Z2 Mini Workstation        | 1499.99 |     8 |           2 |               6 |
|  5 | Dell XPS 15                   | 1299.99 |    15 |           4 |               5 |
| 10 | Microsoft Surface Pro 9       | 1199.99 |    12 |           4 |              10 |
| 20 | Acer Nitro AN515-57           |  999.99 |    14 |           4 |               9 |
| 13 | Dell OptiPlex 7010            |  899.99 |    16 |           2 |               5 |
|  6 | HP Pavilion 14                |  799.99 |    25 |           4 |               6 |
|  9 | Acer Swift 3                  |  699.99 |    18 |           4 |               9 |
|  1 | ThinkServer TS140             |  539.88 |    20 |           2 |               4 |
| 16 | HP LaserJet Pro M428fdw       |  499.99 |    12 |           8 |               6 |
| 12 | Asus TUF Gaming VG27AQ        |  399.99 |    22 |           5 |               3 |
|  7 | Samsung 27-inch 4K            |  349.99 |    30 |           5 |               8 |
| 19 | Dell Ultrasharp U2419H        |  299.99 |    20 |           5 |               5 |
|  3 | RT-AC68U                      |  219.99 |    10 |           1 |               3 |
| 17 | Samsung T7 Shield 2TB         |  199.99 |    35 |           9 |               8 |
|  8 | Logitech Mechanical K845      |   89.99 |    50 |           6 |               7 |
| 15 | Logitech G502 Hero            |   79.99 |    40 |           7 |               7 |
|  4 | X110 64GB                     |   73.84 |   100 |           3 |               1 |
| 11 | Kingston A400 240GB           |   49.99 |    80 |           3 |               2 |
| 18 | Kingston Fury Beast 16GB DDR4 |   44.99 |    60 |          10 |               2 |
+----+-------------------------------+---------+-------+-------------+-----------------+
20 rows in set (0.00 sec)
多表查询
# 示例:产品类型是Servers的产品名称和价格
MariaDB [inventory]> select product.name,product.price from product,category where product.id_category=category.id and category.name='Servers';
+------------------------+---------+
| name                   | price   |
+------------------------+---------+
| ThinkServer TS140      |  539.88 |
| ThinkServer RD630      | 2379.14 |
| Dell OptiPlex 7010     |  899.99 |
| HP Z2 Mini Workstation | 1499.99 |
+------------------------+---------+
4 rows in set (0.00 sec)
函数
# 查询产品价格平均值
MariaDB [inventory]> select avg(price) from product;
+-------------------+
| avg(price)        |
+-------------------+
| 631.3844999999999 |
+-------------------+
1 row in set (0.00 sec)# 查询产品价格最大值
MariaDB [inventory]> select max(price) from product;
+------------+
| max(price) |
+------------+
|    2379.14 |
+------------+
1 row in set (0.00 sec)# 查询产品价格最小值
MariaDB [inventory]> select min(price) from product;
+------------+
| min(price) |
+------------+
|      44.99 |
+------------+
1 row in set (0.00 sec)# 查询产品存量
MariaDB [inventory]> select sum(price) from product;
+--------------------+
| sum(price)         |
+--------------------+
| 12627.689999999997 |
+--------------------+
1 row in set (0.00 sec)MariaDB [inventory]> select * from product where price=73.84;+----+-----------+-------+-------+-------------+-----------------+
| id | name      | price | stock | id_category | id_manufacturer |
+----+-----------+-------+-------+-------------+-----------------+
|  4 | X110 64GB | 73.84 |   100 |           3 |               1 |
+----+-----------+-------+-------+-------------+-----------------+
1 row in set (0.00 sec)#或者
MariaDB [inventory]> select * from product where price=(select min(price) from product);
+----+-------------------------------+-------+-------+-------------+-----------------+
| id | name                          | price | stock | id_category | id_manufacturer |
+----+-------------------------------+-------+-------+-------------+-----------------+
| 18 | Kingston Fury Beast 16GB DDR4 | 44.99 |    60 |          10 |               2 |
+----+-------------------------------+-------+-------+-------------+-----------------+
1 row in set (0.00 sec)
创建表
MariaDB [inventory]> create table staff( id int(11) not null, name varchar(100) not null, age int(11) default 10, id_department int(11) );
Query OK, 0 rows affected (0.00 sec)
插入记录
MariaDB [inventory]> insert into staff (id,name,age,id_department) values (1,'dyx',21,10);
Query OK, 1 row affected (0.00 sec)#查询表中内容
MariaDB [inventory]> select * from staff-> ;
+----+------+------+---------------+
| id | name | age  | id_department |
+----+------+------+---------------+
|  1 | dyx  |   21 |            10 |
+----+------+------+---------------+
1 row in set (0.00 sec)MariaDB [inventory]> insert into staff (id,name,age) values (1,'dyx1',22);
Query OK, 1 row affected (0.00 sec)MariaDB [inventory]> select * from staff;
+----+------+------+---------------+
| id | name | age  | id_department |
+----+------+------+---------------+
|  1 | dyx  |   21 |            10 |
|  1 | dyx1 |   22 |          NULL |
+----+------+------+---------------+
2 rows in set (0.00 sec)MariaDB [inventory]> insert into staff (id,name,age) values (3,'dyx3',22);
Query OK, 1 row affected (0.00 sec)MariaDB [inventory]> select * from staff;
+----+------+------+---------------+
| id | name | age  | id_department |
+----+------+------+---------------+
|  1 | dyx  |   21 |            10 |
|  2 | dyx1 |   22 |          NULL |
|  3 | dyx3 |   22 |          NULL |
+----+------+------+---------------+
3 rows in set (0.00 sec)
更新记录
MariaDB [inventory]> update staff set id=2 where name='dyx1'; 
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0MariaDB [inventory]> select * from staff;
+----+------+------+---------------+
| id | name | age  | id_department |
+----+------+------+---------------+
|  1 | dyx  |   21 |            10 |
|  2 | dyx1 |   22 |          NULL |
+----+------+------+---------------+
2 rows in set (0.00 sec)

如果使用不带WHERE子句的UPDATE,则表中的所有记录都会更新。

删除记录
MariaDB [inventory]> delete from staff where id=3;
Query OK, 1 row affected (0.00 sec)MariaDB [inventory]> select * from staff;
+----+------+------+---------------+
| id | name | age  | id_department |
+----+------+------+---------------+
|  1 | dyx  |   21 |            10 |
|  2 | dyx1 |   22 |          NULL |
+----+------+------+---------------+
2 rows in set (0.00 sec)MariaDB [inventory]> delete from staff ;
Query OK, 2 rows affected (0.00 sec)

如果使用不带WHERE子句的DELETE子句,则表中的所有记录都会删除。

删除表
MariaDB [inventory]> select * from staff;
Empty set (0.00 sec)MariaDB [inventory]> drop table staff;
Query OK, 0 rows affected (0.00 sec)

管理 MariaDB 用户

创建用户账户

默认情况下,MariaDB有自己的用户和密码,与本地系统的用户和密码分开。这意味着MariaDB数据库用户与服务器的Linux用户不同, 即使用户帐户具有相同的名称。

为了控制用户对数据库服务器的访问级别,必须在MariaDB中设置数据库用户并授予他们在服务器及其数据上执行操作的权限。

您可以配置MariaDB使用pam身份验证插件将系统用户帐户和密码集成为MariaDB数据库用户,本课程不介绍该配置。 在大多数情况下,最好分开管理数据库和系统用户账户。

要创建新用户,您需要以下权限级别之一:

  • MariaDB的root用户。
  • 被授予全局CREATE USER特权的用户。
  • 被授予对mysql数据库的INSERT特权的用户。

我们可以使用CREATE USER语句,在mysql数据库的user表中创建一条新记。

用户名格式:user_name@host_name。 这样就可以根据主机源,创建具有相同名称但具有不同特权的多个用户帐户。

MariaDB [mysql]> create user dyx1@'%' identified by '123';
Query OK, 0 rows affected (0.00 sec)

当前,dyx帐户只能使用密码123从**%**连接。

用户的密码,会被加密存在在mysql.user表:

MariaDB [mysql]> select user,host.password from user ;
ERROR 1054 (42S22): Unknown column 'host.password' in 'field list'
MariaDB [mysql]> select user,host,password from user ;
+------+-----------+-------------------------------------------+
| user | host      | password                                  |
+------+-----------+-------------------------------------------+
| root | localhost | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| root | 127.0.0.1 | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| root | ::1       | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| dyx  | %         | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| dyx1 | %         | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
+------+-----------+-------------------------------------------+
5 rows in set (0.00 sec)

用户帐户定义示例

AccountDescription
laomaUser laoma can connect from any host.
laoma@‘%’User laoma can connect from any host.
laoma@‘localhost’User laoma can only connect from localhost.
laoma@‘192.168.1.5’User laoma can only connect from the IP address192.168.1.5.
laoma@‘192.168.1.%’User laoma can connect from any address that belongs to the network 192.168.1.0/24.
laoma@‘2001:db8:18:b51:c32:a21’User laoma can connect from the IP address 2001:db8:18:b51:c32:a21.

控制用户权限

默认情况下,新帐户被授予最小特权。 在不授予其他特权的情况下,laoma用户只能访问最少的帐户信息,大多数其他操作均被拒绝。

#用户可以查询,更新
MariaDB [mysql]> grant select ,update on inventory.* to dyx1@'%';
Query OK, 0 rows affected (0.00 sec)#验证
[root@client ~ 15:07:46]# mysql -u dyx1 -p -h 10.1.8.10
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 5.5.68-MariaDB MariaDB ServerCopyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MariaDB [(none)]> show databases-> ;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| inventory          |
+--------------------+
2 rows in set (0.00 sec)MariaDB [(none)]> use inventory
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase changed
MariaDB [inventory]> show tables-> ;
+---------------------+
| Tables_in_inventory |
+---------------------+
| category            |
| manufacturer        |
| product             |
+---------------------+
3 rows in set (0.00 sec)MariaDB [inventory]> select * from product;
+----+-------------------------------+---------+-------+-------------+-----------------+
| id | name                          | price   | stock | id_category | id_manufacturer |
+----+-------------------------------+---------+-------+-------------+-----------------+
|  1 | ThinkServer TS140             |  539.88 |    20 |           2 |               4 |
|  2 | ThinkServer RD630             | 2379.14 |    20 |           2 |               4 |
|  3 | RT-AC68U                      |  219.99 |    10 |           1 |               3 |
|  4 | X110 64GB                     |   73.84 |   100 |           3 |               1 |
|  5 | Dell XPS 15                   | 1299.99 |    15 |           4 |               5 |
|  6 | HP Pavilion 14                |  799.99 |    25 |           4 |               6 |
|  7 | Samsung 27-inch 4K            |  349.99 |    30 |           5 |               8 |
|  8 | Logitech Mechanical K845      |   89.99 |    50 |           6 |               7 |
|  9 | Acer Swift 3                  |  699.99 |    18 |           4 |               9 |
| 10 | Microsoft Surface Pro 9       | 1199.99 |    12 |           4 |              10 |
| 11 | Kingston A400 240GB           |   49.99 |    80 |           3 |               2 |
| 12 | Asus TUF Gaming VG27AQ        |  399.99 |    22 |           5 |               3 |
| 13 | Dell OptiPlex 7010            |  899.99 |    16 |           2 |               5 |
| 14 | HP Z2 Mini Workstation        | 1499.99 |     8 |           2 |               6 |
| 15 | Logitech G502 Hero            |   79.99 |    40 |           7 |               7 |
| 16 | HP LaserJet Pro M428fdw       |  499.99 |    12 |           8 |               6 |
| 17 | Samsung T7 Shield 2TB         |  199.99 |    35 |           9 |               8 |
| 18 | Kingston Fury Beast 16GB DDR4 |   44.99 |    60 |          10 |               2 |
| 19 | Dell Ultrasharp U2419H        |  299.99 |    20 |           5 |               5 |
| 20 | Acer Nitro AN515-57           |  999.99 |    14 |           4 |               9 |
+----+-------------------------------+---------+-------+-------------+-----------------+
20 rows in set (0.00 sec)#没有插入权限
MariaDB [inventory]> insert into product (21,'abc',1500,14,4);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '21,'abc',1500,14,4)' at line 1

privileges是用户在MariaDB中拥有的能力,确定用户可以做什么以及用户在MariaDB中可以做什么。特权按范围进行组织:

  • 全局特权,用于管理MariaDB数据库服务器本身,例如CREATE USER

  • 数据库特权,用于在MariaDB服务器上创建数据库和使用数据库,例如CREATE DATABASE

  • 表特权,用于在特定数据库中创建表和处理数据,例如CRUD命令。

    CRUD是指在做计算处理时的增加(Create)、查询(Retrieve)(重新得到数据)、更新(Update)和删除(Delete)几个单词的首字母简写。

  • 列特权,用于授予类似表的命令用法,但用于特定的列(通常很少见)。

查询用户权限
MariaDB [mysql]> show grants for dyx@'%';
+----------------------------------------------------------------------------------------------------+
| Grants for dyx@%                                                                                   |
+----------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'dyx'@'%' IDENTIFIED BY PASSWORD '*23AE809DDACAF96AF0FD78ED04B6A265E05AA257' |
+----------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
授予用户权限

GRANT语句可用于向帐户授予特权。 要授予GRANT特权,连接的用户必须具有GRANT OPTION必须具有他们所授予的特定特权

例如,laoma用户无法授予数据库表SELECT特权,除非他们已经具有SELECT特权和GRANT OPTION表特权。

在此示例中,MariaDB根用户向清单数据库中类别表上的laoma用户授予CRUD特权。

MariaDB [mysql]> grant select ,update on inventory.* to dyx1@'%';
Query OK, 0 rows affected (0.00 sec)

验证权限

MariaDB [inventory]> select * from product;
+----+-------------------------------+---------+-------+-------------+-----------------+
| id | name                          | price   | stock | id_category | id_manufacturer |
+----+-------------------------------+---------+-------+-------------+-----------------+
|  1 | ThinkServer TS140             |  539.88 |    20 |           2 |               4 |
|  2 | ThinkServer RD630             | 2379.14 |    20 |           2 |               4 |
|  3 | RT-AC68U                      |  219.99 |    10 |           1 |               3 |
|  4 | X110 64GB                     |   73.84 |   100 |           3 |               1 |
|  5 | Dell XPS 15                   | 1299.99 |    15 |           4 |               5 |
|  6 | HP Pavilion 14                |  799.99 |    25 |           4 |               6 |
|  7 | Samsung 27-inch 4K            |  349.99 |    30 |           5 |               8 |
|  8 | Logitech Mechanical K845      |   89.99 |    50 |           6 |               7 |
|  9 | Acer Swift 3                  |  699.99 |    18 |           4 |               9 |
| 10 | Microsoft Surface Pro 9       | 1199.99 |    12 |           4 |              10 |
| 11 | Kingston A400 240GB           |   49.99 |    80 |           3 |               2 |
| 12 | Asus TUF Gaming VG27AQ        |  399.99 |    22 |           5 |               3 |
| 13 | Dell OptiPlex 7010            |  899.99 |    16 |           2 |               5 |
| 14 | HP Z2 Mini Workstation        | 1499.99 |     8 |           2 |               6 |
| 15 | Logitech G502 Hero            |   79.99 |    40 |           7 |               7 |
| 16 | HP LaserJet Pro M428fdw       |  499.99 |    12 |           8 |               6 |
| 17 | Samsung T7 Shield 2TB         |  199.99 |    35 |           9 |               8 |
| 18 | Kingston Fury Beast 16GB DDR4 |   44.99 |    60 |          10 |               2 |
| 19 | Dell Ultrasharp U2419H        |  299.99 |    20 |           5 |               5 |
| 20 | Acer Nitro AN515-57           |  999.99 |    14 |           4 |               9 |
+----+-------------------------------+---------+-------+-------------+-----------------+
20 rows in set (0.00 sec)#没有插入权限
MariaDB [inventory]> insert into product (21,'abc',1500,14,4);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '21,'abc',1500,14,4)' at line 1

授权示例

GrantDescription
GRANT SELECT ON database.table TO username@hostnameGrant SELECT privilege for a specific table in a specific database to a specific user.
GRANT SELECT ON database. TO username@hostname*Grant SELECT privilege for all tables in a specific database to a specific user.
GRANT SELECT ON *.* TO username@hostnameGrant SELECT privilege for all tables in all databases to a specific user.
GRANT CREATE, ALTER, DROP ON database. to username@hostname*Grant CREATE, ALTER, and DROP TABLES privileges in a specific database to a specific user.
GRANT ALL PRIVILEGES ON *.* to username@hostnameGrant all available privileges for all databases to a specific user, effectively creating a superuser, similar toroot.
回收用户权限

REVOKE语句从帐户中删除特权。 连接的用户必须具有GRANT OPTION特权,并且必须具有被撤消的特定特权。

MariaDB [mysql]> revoke select,update on inventory.* from dyx1@'%';
Query OK, 0 rows affected (0.00 sec)

删除用户

当不再需要特定的用户帐户时,可以使用DROP USER将其从数据库中删除。

用户名使用**‘user’@‘host’**格式。

MariaDB [mysql]> drop user dyx1@'%';
Query OK, 0 rows affected (0.00 sec)

如果删除了当前连接的帐户,则在关闭连接之前不会删除该帐户,关闭连接之后才会删除该帐户。

更改用户密码

MariaDB [mysql]> select user,password from user;
+------+-------------------------------------------+
| user | password                                  |
+------+-------------------------------------------+
| root | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| root | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| root | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| dyx  | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
+------+-------------------------------------------+
4 rows in set (0.00 sec)# root用户修改用户账户密码
MariaDB [mysql]> update user set password=password('abc') where user='root';
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0MariaDB [mysql]> select user,password from user;
+------+-------------------------------------------+
| user | password                                  |
+------+-------------------------------------------+
| root | *0D3CED9BEC10A777AEC23CCC353A8C08A633045E |
| root | *0D3CED9BEC10A777AEC23CCC353A8C08A633045E |
| root | *0D3CED9BEC10A777AEC23CCC353A8C08A633045E |
| dyx  | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
+------+-------------------------------------------+
4 rows in set (0.00 sec)#修改登陆方式
MariaDB [mysql]> update user set host='localhost' where user='dyx';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0MariaDB [mysql]> select user,password,host from user;
+------+-------------------------------------------+-----------+
| user | password                                  | host      |
+------+-------------------------------------------+-----------+
| root | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | localhost |
| root | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | 127.0.0.1 |
| root | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | ::1       |
| dyx  | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | localhost |
+------+-------------------------------------------+-----------+
4 rows in set (0.00 sec)

排故数据库访问

下表总结了用户在身份验证和访问方面可能遇到的一些问题,以及可能的原因。

问题解决方法
已授予用户从任何主机进行连接的访问权限,但只能使用数据库服务器本地mysql命令进行连接。如果在配置文件/etc/my.cnf.d/mariadb-server.cnf中设置了skip-networking,请删除该指令并重新启动服务。
用户可以与localhost上的任何应用程序连接,但不能远程连接。确保/etc/my.cnf.d/mariadb-server.cnf中的bind-address配置正确。 确保用户表中包含用户要尝试与其连接的主机的条目。
用户可以连接,但看不到除information_schema以外的任何数据库。确保已授予用户访问其数据库的权限。 刚创建的用户具有最小权限,会遇到这个问题。
用户可以连接,但不能创建任何数据库。考虑向用户授予全局CREATE特权(这也授予DROP特权)。
用户可以连接,但不能读取或写入任何数据。向用户授予他们打算使用的数据库的CRUD特权。

忘记 root 用户密码

  1. 编辑**/etc/my.cnf.d/mariadb-server.cnf**配置文件,在[mysqld]下添加skip-grant-tables=1

  2. 重启 mariadb 服务。

  3. 执行mysql -u root命令进入mysql命令行,修改root用户密码。

MariaDB [mysql]> update user set password=password('abc') where user='root';
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0
  1. 把**/etc/my.cnf.d/mariadb-server.cnf配置文件中的skip-grant-tables=1注释掉,然后重启mariadb**服务。

备份和恢复

备份方式

逻辑备份

逻辑备份,将数据库导出为包含重新创建数据库所需的SQL命令的文本文件。

  • 通过查询数据库,检索数据库结构。
  • 备份速度较慢,因为服务器必须访问数据库信息并将其转换为逻辑格式。
  • 逻辑备份具有高度的可移植性,在某些情况下可以还原到其他数据库提供程序(例如PostgreSQL)。
  • 在服务器联机时执行。
  • 备份不包括日志或配置文件。
物理备份特征

物理备份,复制原始数据库目录和包含数据库内容的文件。

  • 备份可以包括日志和配置文件。
  • 仅可移植到具有类似硬件和软件的其他计算机上。
  • 比逻辑备份更快。
  • 在服务器脱机或数据库中的所有表均已锁定时执行,以防止在备份期间进行更改。

执行备份

执行逻辑备份

您在运行mysqldump时所连接的MariaDB用户至少需要对转储表具有SELECT特权,对转储视图至少需要SHOW VIEW,对于转储触发器至少需要TRIGGER特权。

使用mysqldump命令执行逻辑备份。

语法:

[root@server ~ 17:39:45]# man mysqldump
MYSQLDUMP(1)                 MySQL Database System                 MYSQLDUMP(1)NAMEmysqldump - a database backup programSYNOPSISmysqldump [options] [db_name [tbl_name ...]]
... ...
[root@server ~ 17:41:02]# mysqldump --help
mysqldump  Ver 10.14 Distrib 5.5.68-MariaDB, for Linux (x86_64)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.Dumping structure and contents of MySQL databases and tables.
Usage: mysqldump [OPTIONS] database [tables]
OR     mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
OR     mysqldump [OPTIONS] --all-databases [OPTIONS]... ...
[root@server ~ 17:39:24]# myaqldump -u root -p inventory > /backup/inventory.dump
[root@server ~ 17:39:32]#  mysqldump -u root -p --all-databases > /backup/mariadb.dump
#备份特定表
[root@server ~ 21:25:27]# mysqldump inventory product
-- MySQL dump 10.14  Distrib 5.5.68-MariaDB, for Linux (x86_64)
--
-- Host: localhost    Database: inventory
-- ------------------------------------------------------
-- Server version	5.5.68-MariaDB/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;--
-- Table structure for table `product`
--DROP TABLE IF EXISTS `product`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `product` (`id` int(11) NOT NULL AUTO_INCREMENT,`name` varchar(100) NOT NULL,`price` double NOT NULL,`stock` int(11) NOT NULL,`id_category` int(11) NOT NULL,`id_manufacturer` int(11) NOT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;--
-- Dumping data for table `product`
--LOCK TABLES `product` WRITE;
/*!40000 ALTER TABLE `product` DISABLE KEYS */;
INSERT INTO `product` VALUES (1,'ThinkServer TS140',539.88,20,2,4),(2,'ThinkServer RD630',2379.14,20,2,4),(3,'RT-AC68U',219.99,10,1,3),(4,'X110 64GB',73.84,100,3,1),(5,'Dell XPS 15',1299.99,15,4,5),(6,'HP Pavilion 14',799.99,25,4,6),(7,'Samsung 27-inch 4K',349.99,30,5,8),(8,'Logitech Mechanical K845',89.99,50,6,7),(9,'Acer Swift 3',699.99,18,4,9),(10,'Microsoft Surface Pro 9',1199.99,12,4,10),(11,'Kingston A400 240GB',49.99,80,3,2),(12,'Asus TUF Gaming VG27AQ',399.99,22,5,3),(13,'Dell OptiPlex 7010',899.99,16,2,5),(14,'HP Z2 Mini Workstation',1499.99,8,2,6),(15,'Logitech G502 Hero',79.99,40,7,7),(16,'HP LaserJet Pro M428fdw',499.99,12,8,6),(17,'Samsung T7 Shield 2TB',199.99,35,9,8),(18,'Kingston Fury Beast 16GB DDR4',44.99,60,10,2),(19,'Dell Ultrasharp U2419H',299.99,20,5,5),(20,'Acer Nitro AN515-57',999.99,14,4,9);
/*!40000 ALTER TABLE `product` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;-- Dump completed on 2025-09-28 21:34:11
#备份特定库(库中所有表)不包括创建数据库命令
[root@server ~ 21:34:11]# mysqldump inventory 
-- MySQL dump 10.14  Distrib 5.5.68-MariaDB, for Linux (x86_64)
--
-- Host: localhost    Database: inventory
-- ------------------------------------------------------
-- Server version	5.5.68-MariaDB/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;--
-- Table structure for table `category`
--DROP TABLE IF EXISTS `category`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `category` (`id` int(11) NOT NULL AUTO_INCREMENT,`name` varchar(100) NOT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;--
-- Dumping data for table `category`
--LOCK TABLES `category` WRITE;
/*!40000 ALTER TABLE `category` DISABLE KEYS */;
INSERT INTO `category` VALUES (1,'Networking'),(2,'Servers'),(3,'Ssd'),(4,'Laptops'),(5,'Monitors'),(6,'Keyboards'),(7,'Mice'),(8,'Printers'),(9,'External HDDs'),(10,'RAM Modules');
/*!40000 ALTER TABLE `category` ENABLE KEYS */;
UNLOCK TABLES;--
-- Table structure for table `manufacturer`
--DROP TABLE IF EXISTS `manufacturer`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `manufacturer` (`id` int(11) NOT NULL AUTO_INCREMENT,`name` varchar(100) NOT NULL,`seller` varchar(100) DEFAULT NULL,`phone_number` varchar(17) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;--
-- Dumping data for table `manufacturer`
--LOCK TABLES `manufacturer` WRITE;
/*!40000 ALTER TABLE `manufacturer` DISABLE KEYS */;
INSERT INTO `manufacturer` VALUES (1,'SanDisk','John Miller','+1 (941) 329-8855'),(2,'Kingston','Mike Taylor','+1 (341) 375-9999'),(3,'Asus','Wilson Jackson','+1 (432) 367-8899'),(4,'Lenovo','Allen Scott','+1 (876) 213-4439'),(5,'Dell','Sarah Clark','+1 (567) 890-1234'),(6,'HP','David Lewis','+1 (234) 567-8901'),(7,'Logitech','Emily White','+1 (678) 901-2345'),(8,'Samsung','Robert Brown','+1 (345) 678-9012'),(9,'Acer','Lisa Garcia','+1 (789) 012-3456'),(10,'Microsoft','James Wilson','+1 (456) 789-0123');
/*!40000 ALTER TABLE `manufacturer` ENABLE KEYS */;
UNLOCK TABLES;--
-- Table structure for table `product`
--DROP TABLE IF EXISTS `product`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `product` (`id` int(11) NOT NULL AUTO_INCREMENT,`name` varchar(100) NOT NULL,`price` double NOT NULL,`stock` int(11) NOT NULL,`id_category` int(11) NOT NULL,`id_manufacturer` int(11) NOT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;--
-- Dumping data for table `product`
--LOCK TABLES `product` WRITE;
/*!40000 ALTER TABLE `product` DISABLE KEYS */;
INSERT INTO `product` VALUES (1,'ThinkServer TS140',539.88,20,2,4),(2,'ThinkServer RD630',2379.14,20,2,4),(3,'RT-AC68U',219.99,10,1,3),(4,'X110 64GB',73.84,100,3,1),(5,'Dell XPS 15',1299.99,15,4,5),(6,'HP Pavilion 14',799.99,25,4,6),(7,'Samsung 27-inch 4K',349.99,30,5,8),(8,'Logitech Mechanical K845',89.99,50,6,7),(9,'Acer Swift 3',699.99,18,4,9),(10,'Microsoft Surface Pro 9',1199.99,12,4,10),(11,'Kingston A400 240GB',49.99,80,3,2),(12,'Asus TUF Gaming VG27AQ',399.99,22,5,3),(13,'Dell OptiPlex 7010',899.99,16,2,5),(14,'HP Z2 Mini Workstation',1499.99,8,2,6),(15,'Logitech G502 Hero',79.99,40,7,7),(16,'HP LaserJet Pro M428fdw',499.99,12,8,6),(17,'Samsung T7 Shield 2TB',199.99,35,9,8),(18,'Kingston Fury Beast 16GB DDR4',44.99,60,10,2),(19,'Dell Ultrasharp U2419H',299.99,20,5,5),(20,'Acer Nitro AN515-57',999.99,14,4,9);
/*!40000 ALTER TABLE `product` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;-- Dump completed on 2025-09-28 21:36:49
#备份特定库(库中所有表)包括创建数据库命令
[root@server ~ 21:36:49]# mysqldump --database inventory 
-- MySQL dump 10.14  Distrib 5.5.68-MariaDB, for Linux (x86_64)
--
-- Host: localhost    Database: inventory
-- ------------------------------------------------------
-- Server version	5.5.68-MariaDB/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;--
-- Current Database: `inventory`
--CREATE DATABASE /*!32312 IF NOT EXISTS*/ `inventory` /*!40100 DEFAULT CHARACTER SET latin1 */;USE `inventory`;--
-- Table structure for table `category`
--DROP TABLE IF EXISTS `category`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `category` (`id` int(11) NOT NULL AUTO_INCREMENT,`name` varchar(100) NOT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;--
-- Dumping data for table `category`
--LOCK TABLES `category` WRITE;
/*!40000 ALTER TABLE `category` DISABLE KEYS */;
INSERT INTO `category` VALUES (1,'Networking'),(2,'Servers'),(3,'Ssd'),(4,'Laptops'),(5,'Monitors'),(6,'Keyboards'),(7,'Mice'),(8,'Printers'),(9,'External HDDs'),(10,'RAM Modules');
/*!40000 ALTER TABLE `category` ENABLE KEYS */;
UNLOCK TABLES;--
-- Table structure for table `manufacturer`
--DROP TABLE IF EXISTS `manufacturer`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `manufacturer` (`id` int(11) NOT NULL AUTO_INCREMENT,`name` varchar(100) NOT NULL,`seller` varchar(100) DEFAULT NULL,`phone_number` varchar(17) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;--
-- Dumping data for table `manufacturer`
--LOCK TABLES `manufacturer` WRITE;
/*!40000 ALTER TABLE `manufacturer` DISABLE KEYS */;
INSERT INTO `manufacturer` VALUES (1,'SanDisk','John Miller','+1 (941) 329-8855'),(2,'Kingston','Mike Taylor','+1 (341) 375-9999'),(3,'Asus','Wilson Jackson','+1 (432) 367-8899'),(4,'Lenovo','Allen Scott','+1 (876) 213-4439'),(5,'Dell','Sarah Clark','+1 (567) 890-1234'),(6,'HP','David Lewis','+1 (234) 567-8901'),(7,'Logitech','Emily White','+1 (678) 901-2345'),(8,'Samsung','Robert Brown','+1 (345) 678-9012'),(9,'Acer','Lisa Garcia','+1 (789) 012-3456'),(10,'Microsoft','James Wilson','+1 (456) 789-0123');
/*!40000 ALTER TABLE `manufacturer` ENABLE KEYS */;
UNLOCK TABLES;--
-- Table structure for table `product`
--DROP TABLE IF EXISTS `product`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `product` (`id` int(11) NOT NULL AUTO_INCREMENT,`name` varchar(100) NOT NULL,`price` double NOT NULL,`stock` int(11) NOT NULL,`id_category` int(11) NOT NULL,`id_manufacturer` int(11) NOT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;--
-- Dumping data for table `product`
--LOCK TABLES `product` WRITE;
/*!40000 ALTER TABLE `product` DISABLE KEYS */;
INSERT INTO `product` VALUES (1,'ThinkServer TS140',539.88,20,2,4),(2,'ThinkServer RD630',2379.14,20,2,4),(3,'RT-AC68U',219.99,10,1,3),(4,'X110 64GB',73.84,100,3,1),(5,'Dell XPS 15',1299.99,15,4,5),(6,'HP Pavilion 14',799.99,25,4,6),(7,'Samsung 27-inch 4K',349.99,30,5,8),(8,'Logitech Mechanical K845',89.99,50,6,7),(9,'Acer Swift 3',699.99,18,4,9),(10,'Microsoft Surface Pro 9',1199.99,12,4,10),(11,'Kingston A400 240GB',49.99,80,3,2),(12,'Asus TUF Gaming VG27AQ',399.99,22,5,3),(13,'Dell OptiPlex 7010',899.99,16,2,5),(14,'HP Z2 Mini Workstation',1499.99,8,2,6),(15,'Logitech G502 Hero',79.99,40,7,7),(16,'HP LaserJet Pro M428fdw',499.99,12,8,6),(17,'Samsung T7 Shield 2TB',199.99,35,9,8),(18,'Kingston Fury Beast 16GB DDR4',44.99,60,10,2),(19,'Dell Ultrasharp U2419H',299.99,20,5,5),(20,'Acer Nitro AN515-57',999.99,14,4,9);
/*!40000 ALTER TABLE `product` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;-- Dump completed on 2025-09-28 21:37:34#还可以添加选项 --add-drop-database,添加删除数据库命令
执行物理备份

mariabackup工具由AppStream存储库中的mariadb-backup包提供。 mariabackup工具执行MariaDB服务器的完整物理备份。

[root@server ~ 20:50:50]# cd /var/lib/mysql
[root@server mysql 21:00:52]# ls
aria_log.00000001  ib_logfile0  mysql
aria_log_control   ib_logfile1  mysql.sock
ibdata1            inventory    performance_schema
[root@server mysql 21:00:53]# ls inventory/
category.frm  db.opt  manufacturer.frm  product.frm
[root@server mysql 21:01:26]# cp -a inventory/ ~
MariaDB [(none)]> drop database inventory;
Query OK, 3 rows affected (0.00 sec)MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)[root@server mysql 21:02:03]# ls
aria_log.00000001  ibdata1      ib_logfile1  mysql.sock
aria_log_control   ib_logfile0  mysql        performance_schema

drop 数据库层面删除,MySQL已经删除这个记录。MySQL管理系统未登记对应的数据库。

执行恢复

还原备份时,它将用备份的内容覆盖数据库服务器的内容。 如果数据库中的数据比备份中的数据新,该部分数据将会丢失。

恢复逻辑备份
mysql < sql
恢复物理备份
正确做法:
  1. 停止mariadb服务

    [root@server ~ 21:22:37]# systemctl stop mariadb.service 
    
  2. tar命令打包 /var/lib/mysql 目录为mysql.tar.gz,保留原先权限 -p选项

    [root@server ~ 21:23:00]# tar -pczf mysql.tar.gz /var/lib/mysql
    tar: 从成员名中删除开头的“/”
  3. 启动mariadb服务,删除inventory库中product表

    [root@server ~ 21:23:36]# rm -fr /var/lib/mysqlMariaDB [(none)]> drop database inventory;
    Query OK, 3 rows affected (0.00 sec)MariaDB [(none)]> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    +--------------------+
    3 rows in set (0.00 sec)
    
  4. 停止mariadb 服务,mv /var/lib/mysql /var/lib/mysql.old

    [root@server ~ 16:04:28]# systemctl stop mariadb.service[root@server ~ 16:07:46]# mv /var/lib/mysql.old /var/lib/mysql
  5. 提权mysql.tar.gz内容,恢复到/var/lib/mysql

    [root@server ~ 21:23:46]# tar -xf mysql.tar.gz 
    [root@server ~ 21:23:56]# ls -l
    总用量 616
    -rw-------. 1 root  root    1447 92 10:23 anaconda-ks.cfg
    drwx------  2 mysql mysql     83 928 16:15 inventory
    -rw-r--r--  1 root  root    5221 928 11:26 inventory.sql
    -rw-r--r--  1 root  root  614605 928 21:23 mysql.tar.gz
    drwxr-xr-x  3 root  root      17 928 16:03 var
    [root@server ~ 21:24:01]# ls -l  var/lib
    总用量 0
    drwxr-xr-x 5 mysql mysql 164 928 21:22 mysql
    [root@server ~ 21:24:33]# cp -a var/lib/mysql /var/lib/
  6. 启动mariadb服务验证

[root@server ~ 21:25:12]# systemctl start mariadb.serviceMariaDB [inventory]> select * from product;
+----+-------------------------------+---------+-------+-------------+-----------------+
| id | name                          | price   | stock | id_category | id_manufacturer |
+----+-------------------------------+---------+-------+-------------+-----------------+
|  1 | ThinkServer TS140             |  539.88 |    20 |           2 |               4 |
|  2 | ThinkServer RD630             | 2379.14 |    20 |           2 |               4 |
|  3 | RT-AC68U                      |  219.99 |    10 |           1 |               3 |
|  4 | X110 64GB                     |   73.84 |   100 |           3 |               1 |
|  5 | Dell XPS 15                   | 1299.99 |    15 |           4 |               5 |
|  6 | HP Pavilion 14                |  799.99 |    25 |           4 |               6 |
|  7 | Samsung 27-inch 4K            |  349.99 |    30 |           5 |               8 |
|  8 | Logitech Mechanical K845      |   89.99 |    50 |           6 |               7 |
|  9 | Acer Swift 3                  |  699.99 |    18 |           4 |               9 |
| 10 | Microsoft Surface Pro 9       | 1199.99 |    12 |           4 |              10 |
| 11 | Kingston A400 240GB           |   49.99 |    80 |           3 |               2 |
| 12 | Asus TUF Gaming VG27AQ        |  399.99 |    22 |           5 |               3 |
| 13 | Dell OptiPlex 7010            |  899.99 |    16 |           2 |               5 |
| 14 | HP Z2 Mini Workstation        | 1499.99 |     8 |           2 |               6 |
| 15 | Logitech G502 Hero            |   79.99 |    40 |           7 |               7 |
| 16 | HP LaserJet Pro M428fdw       |  499.99 |    12 |           8 |               6 |
| 17 | Samsung T7 Shield 2TB         |  199.99 |    35 |           9 |               8 |
| 18 | Kingston Fury Beast 16GB DDR4 |   44.99 |    60 |          10 |               2 |
| 19 | Dell Ultrasharp U2419H        |  299.99 |    20 |           5 |               5 |
| 20 | Acer Nitro AN515-57           |  999.99 |    14 |           4 |               9 |
+----+-------------------------------+---------+-------+-------------+-----------------+
20 rows in set (0.00 sec)
错误:
[root@server ~ 20:59:54]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 5.5.68-MariaDB MariaDB ServerCopyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| inventory          |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)[root@server mysql 21:02:43]# cp -a ~/inventory .
[root@server mysql 21:03:13]# ls
aria_log.00000001  ib_logfile0  mysql
aria_log_control   ib_logfile1  mysql.sock
ibdata1            inventory    performance_schemaMariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| inventory          |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)[root@server mysql 21:03:15]# 
 6 |

| 15 | Logitech G502 Hero | 79.99 | 40 | 7 | 7 |
| 16 | HP LaserJet Pro M428fdw | 499.99 | 12 | 8 | 6 |
| 17 | Samsung T7 Shield 2TB | 199.99 | 35 | 9 | 8 |
| 18 | Kingston Fury Beast 16GB DDR4 | 44.99 | 60 | 10 | 2 |
| 19 | Dell Ultrasharp U2419H | 299.99 | 20 | 5 | 5 |
| 20 | Acer Nitro AN515-57 | 999.99 | 14 | 4 | 9 |
±—±------------------------------±--------±------±------------±----------------+
20 rows in set (0.00 sec)

##### 错误:```bash
[root@server ~ 20:59:54]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 5.5.68-MariaDB MariaDB ServerCopyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| inventory          |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)[root@server mysql 21:02:43]# cp -a ~/inventory .
[root@server mysql 21:03:13]# ls
aria_log.00000001  ib_logfile0  mysql
aria_log_control   ib_logfile1  mysql.sock
ibdata1            inventory    performance_schemaMariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| inventory          |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)[root@server mysql 21:03:15]# 
http://www.dtcms.com/a/418419.html

相关文章:

  • 21.mariadb 数据库
  • GFM100 地线连续性检测监控器:破解工业接地痛点,筑牢电力系统安全防线
  • 2、Nginx 与 Spring Cloud Gateway 详细对比:定位、场景与分工
  • 玳瑁的嵌入式日记---0928(ARM--I2C)
  • 微服务故障排查
  • 离散时间马尔可夫链
  • 怎么做网站快照网站域名跳转代码html
  • 基于 OpenCV + 深度学习的实时人脸检测与年龄性别识别系统
  • c++ opencv 复现Fiji 配对拼接算法中的加权融合
  • 中秋国庆双节餐饮零售破局!Deepoc 具身模型外拓板打造 “假日智能运营新范式
  • 瑞安网站建设电话百度商桥接入网站
  • 嵌入式硬件——I.MX6ULL EPIT(增强型周期中断定时器)
  • 降低测试成本缩短测试周期 | 车辆OBD数据采集方案
  • 一级消防工程师考试时间新闻类网站怎么做seo
  • window显示驱动开发—确定显示适配器上的 VidPN 支持
  • Kafka05-入门-尚硅谷
  • Visual Studio 2022
  • 【不背八股】19.GPT-2:不再微调,聚焦零样本
  • 5、焊接质量检验及缺陷分析
  • 【AI】大模型的分析力横评:一次“伪异步”排查后的真实思考
  • 养老院服务质量建设专项网站广州seo关键词优化外包
  • 第一章 假设三:走势包含无序运动状态(混沌现象)和有序运动状态(下跌或上涨)
  • ElementUI-Table 表格实现行拖拽
  • Logit论文阅读
  • 网站cms建设wordpress模仿app启动广告屏弹窗
  • 21. 一个像素对应多个纹素
  • 设备沉睡的“心跳”难题:BLE休眠后无法被手机唤醒的分析与优化
  • 【计算机基础】网络系列(二)TCP
  • 网站seo整站优化淘宝网站的建设
  • Python 自动化导出PDF表格:List、Dictionary、Pandas DataFrame和数据库实例演示