21.mariadb 数据库
文章目录
- MariaDB 数据库管理
- 介绍 MariaDB
- 数据库介绍
- 数据库种类
- 关系数据库
- MariaDB 介绍
- 部署 MariaDB
- 安装 MariaDB
- 安装软件包
- 启动并启用服务
- 配置防火墙
- 加固 MariaDB
- 连接 MariaDB
- 配置 MariaDB
- MariaDB 中 SQL
- 描述 SQL
- 连接数据库
- 数据库操作
- 查询数据库列表
- 创建数据库
- 使用数据库
- 删除数据库
- 表操作
- 环境准备
- 查询表
- 查询表结构
- 查询表中数据
- 查询表所有记录的所有字段
- 查询表中所有特定字符
- where字句
- 多表查询
- 函数
- 创建表
- 插入记录
- 更新记录
- 删除记录
- 删除表
- 管理 MariaDB 用户
- 创建用户账户
- 控制用户权限
- 查询用户权限
- 授予用户权限
- 回收用户权限
- 删除用户
- 更改用户密码
- 排故数据库访问
- 忘记 root 用户密码
- 备份和恢复
- 备份方式
- 逻辑备份
- 物理备份特征
- 物理备份
- 逻辑备份
MariaDB 数据库管理
章节内容:
- 介绍 MariaDB 数据库
- 部署 MariaDB 数据库
- 实践 MariaDB 中 SQL
- 管理 MariaDB 用户
- 备份和恢复 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 Server或Nginx,提供Web服务器。
- MariaDB,MySQL或其他关系数据库,例如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:12:25]# yum install -y mariadb-server#客户端安装
[root@client ~ 10:12:23]# yum install -y mariadb
启动并启用服务
[root@server ~ 10:13:08]# systemctl enable mariadb.service --now
配置防火墙
关闭防火墙即可
或者 firewall-cmd --permanent --add-service=mysql
firewall-cmd --reload
加固 MariaDB
MariaDB数据库默认具有test数据库和一些不太安全的配置。运行mysql_secure_installation
修改这些配置。
交互式提示您进行更改,包括:
- 为root帐户设置密码。
- 禁止root帐户从本地主机外部访问数据库。
- 删除匿名用户帐户。
- 删除用于演示的test数据库。
[root@server ~ 10:14:40]# 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] #回车... 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] #回车... 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] - 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] #回车... 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!
连接 MariaDB
MariaDB客户端可以通过两种方式连接到服务器:
- 如果客户端与服务器在同一台计算机上运行,则它可以使用特殊的套接字文件(socket)与MariaDB通信。 这种方式更安全,因为MariaDB不需要侦听来自网络客户端的连接。 但是,使用该数据库的服务都需要在MariaDB服务器上运行,会影响性能。
[root@server ~ 10:19:00]# mysql -u root -p
Enter password: #输入密码登录
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10
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)]>
- 客户端可以使用TCP/IP网络连接到MariaDB服务。 远程服务器与MariaDB服务器运行在不同主机。 此时服务器需要配置侦听端口3306/TCP上的连接。
#刚刚mysql_secure_installation 设置了禁止root远程登录
#现在利用root创建一个用户,可以远程登录MariaDB [(none)]> create user dcr@'%' identified by '123';
Query OK, 0 rows affected (0.00 sec)
#dcr@'%',%就是可以远程登录
#刷新
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
#检验
[root@client ~ 10:23:06]# mysql -u dcr -p -h 10.1.8.10
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 13
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)]> quit
配置 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:36:33]# ls /etc/my.cnf.d/
client.cnf mysql-clients.cnf
[root@client ~ 10:38:38]# vim /etc/my.cnf.d/client.cnf
[mysqld]
user=root
password=123#设置后即可不用输入用户名和密码直接登录
[root@server ~ 10:41:23]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 16
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)]>
客户端配置示例:
[root@client ~ 10:40:05]# vim /etc/my.cnf.d/client.cnf
[mysqld]
user=dcr
password=123
host=server (10.1.8.10)
port=3306
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表的格式显示。
- 非交互执行时,结果以制表符分隔的格式显示。
执行交互式SQL语句时,MariaDB提示符在方括号中显示您当前选择数据库。
数据库操作
查询数据库列表
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
数据库说明:
- mysql 数据库,是一个系统数据库,保存数据库用户及其访问权限等信息。
- INFORMATION_SCHEMA 数据库,保存关于数据库或者数据表的元数据信息。
- **PERFORMANCE_SCHEMA **数据库,保存数据库服务器性能信息。
创建数据库
MariaDB [(none)]> create database webapp;
Query OK, 1 row affected (0.00 sec)MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| webapp |
+--------------------+
4 rows in set (0.00 sec)
SQL语句不区分大小写,但数据库名称区分大小写。 通常的做法是数据库名称全为小写字母,SQL语句全为大写字母,以区分SQL语句与语句的目标或参数。
使用数据库
MariaDB [(none)]> use webapp;
Database changed
MariaDB [webapp]> show tabels;
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 'tabels' at line 1
MariaDB [webapp]>
删除数据库
DROP DATABASE语句删除数据库中的所有表并删除数据库。 这将破坏数据库中的所有数据。 只有对该数据库具有DROP特权的用户才能运行此语句。 这不会更改数据库的用户特权。 如果重新创建具有该名称的数据库,则为旧数据库设置的用户权限仍然有效。
MariaDB [webapp]> drop database webapp;
Query OK, 0 rows affected (0.00 sec)
表操作
SQL CRUD操作(create 添加数据 read读取数据 update 修改数据delete删除数据)。
环境准备
#导入备份
[root@server ~ 11:35:48]# rz -E[root@server ~ 11:41:17]# ls
anaconda-ks.cfg inventory.sql
[root@server ~ 11:41:20]# mysql inventory < inventory.sql
查询表
#查询表列表
[root@server ~ 11:12:32]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 18
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)]> 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]> desc category;
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(100) | NO | | NULL | |
+-------+--------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)MariaDB [inventory]> desc manufacturer;
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(100) | NO | | NULL | |
| seller | varchar(100) | YES | | NULL | |
| phone_number | varchar(17) | YES | | NULL | |
+--------------+--------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
输出显示,表格中有六列(属性):
- Field 列,显示该属性名称。
- Type 列,显示该属性的数据必须采用的格式。例如,stock属性必须是最多11位数字的整数。
- Null 列,指示此属性是否可以为null。
- Default 列,指示如果未指定该属性,则是否设置了默认值。
- Key 列,显示属性ID是primary key。 主键是表中一行的唯一标识符。 该属性的任何其他行都不能具有相同的值。
- Extra列,提供该列额外信息,对于id字段标记为auto_increment。 这意味着每次将新项目插入表中时,该条目的属性值都会增加。 这样可以更轻松地使数字主键保持唯一。
查询表中数据
查询表所有记录的所有字段
select * from table-name;
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)
查询表中所有特定字符
select 属性1,属性2 from table-name;
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 from manufacturer;
+-----------+
| name |
+-----------+
| SanDisk |
| Kingston |
| Asus |
| Lenovo |
| Dell |
| HP |
| Logitech |
| Samsung |
| Acer |
| Microsoft |
+-----------+
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 name,price,stock from product where price > 1000;
+-------------------------+---------+-------+
| name | price | stock |
+-------------------------+---------+-------+
| ThinkServer RD630 | 2379.14 | 20 |
| Dell XPS 15 | 1299.99 | 15 |
| Microsoft Surface Pro 9 | 1199.99 | 12 |
| HP Z2 Mini Workstation | 1499.99 | 8 |
+-------------------------+---------+-------+
4 rows in set (0.00 sec)
条件操作数
Operator | Description |
---|---|
= | 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 |
BETWEEN | Between an inclusive range |
LIKE | Search for a pattern |
IN | Specify multiple possible values for a column |
- 条件操作符包括=,<>,<,>,>=,<=
MariaDB [inventory]> select name,price,stock from product where stock <=20 ;
+-------------------------+---------+-------+
| name | price | stock |
+-------------------------+---------+-------+
| ThinkServer TS140 | 539.88 | 20 |
| ThinkServer RD630 | 2379.14 | 20 |
| RT-AC68U | 219.99 | 10 |
| Dell XPS 15 | 1299.99 | 15 |
| Acer Swift 3 | 699.99 | 18 |
| Microsoft Surface Pro 9 | 1199.99 | 12 |
| Dell OptiPlex 7010 | 899.99 | 16 |
| HP Z2 Mini Workstation | 1499.99 | 8 |
| HP LaserJet Pro M428fdw | 499.99 | 12 |
| Dell Ultrasharp U2419H | 299.99 | 20 |
| Acer Nitro AN515-57 | 999.99 | 14 |
+-------------------------+---------+-------+
11 rows in set (0.00 sec)
- between,匹配2个数字之间(包括数字本身)的记录
MariaDB [inventory]> select name,price,stock from product where stock between 20 and 30 ;
+------------------------+---------+-------+
| name | price | stock |
+------------------------+---------+-------+
| ThinkServer TS140 | 539.88 | 20 |
| ThinkServer RD630 | 2379.14 | 20 |
| HP Pavilion 14 | 799.99 | 25 |
| Samsung 27-inch 4K | 349.99 | 30 |
| Asus TUF Gaming VG27AQ | 399.99 | 22 |
| Dell Ultrasharp U2419H | 299.99 | 20 |
+------------------------+---------+-------+
6 rows in set (0.00 sec)
- in 匹配列表中记录
#显示id为1和3的信息
MariaDB [inventory]> select id,name,price,stock from product where id in (1,3);
+----+-------------------+--------+-------+
| id | name | price | stock |
+----+-------------------+--------+-------+
| 1 | ThinkServer TS140 | 539.88 | 20 |
| 3 | RT-AC68U | 219.99 | 10 |
+----+-------------------+--------+-------+
2 rows in set (0.00 sec)MariaDB [inventory]> select * from category where name in ('Ser%','Mice');
+----+------+
| id | name |
+----+------+
| 7 | Mice |
+----+------+
1 row in set (0.00 sec)#in时,字符必须写完整MariaDB [inventory]> select * from category where name in ('Servers','Mice');
+----+---------+
| id | name |
+----+---------+
| 2 | Servers |
| 7 | Mice |
+----+---------+
2 rows in set (0.00 sec)
- like 由于字符匹配
%表示一个或多个字符,_表示一个字符,
[charlist]表示字符列中的任何单一字符,[ ^charlist ]或者[!charlist]不在字符列中的任何单一字符。
MariaDB [inventory]> select id,name,price,stock from product where name like 'ThinkServer TS140';
+----+-------------------+--------+-------+
| id | name | price | stock |
+----+-------------------+--------+-------+
| 1 | ThinkServer TS140 | 539.88 | 20 |
+----+-------------------+--------+-------+
1 row in set (0.00 sec)MariaDB [inventory]> select * from category where name like 'Ser%';
+----+---------+
| id | name |
+----+---------+
| 2 | Servers |
+----+---------+
1 row in set (0.00 sec)
- 逻辑与and
MariaDB [inventory]> select id,name,price,stock from product where stock > 20 and price <1000;
+----+-------------------------------+--------+-------+
| id | name | price | stock |
+----+-------------------------------+--------+-------+
| 4 | X110 64GB | 73.84 | 100 |
| 6 | HP Pavilion 14 | 799.99 | 25 |
| 7 | Samsung 27-inch 4K | 349.99 | 30 |
| 8 | Logitech Mechanical K845 | 89.99 | 50 |
| 11 | Kingston A400 240GB | 49.99 | 80 |
| 12 | Asus TUF Gaming VG27AQ | 399.99 | 22 |
| 15 | Logitech G502 Hero | 79.99 | 40 |
| 17 | Samsung T7 Shield 2TB | 199.99 | 35 |
| 18 | Kingston Fury Beast 16GB DDR4 | 44.99 | 60 |
+----+-------------------------------+--------+-------+
9 rows in set (0.00 sec)
- 逻辑或or
MariaDB [inventory]> select id,name,price,stock from product where stock > 20 or price <400;
+----+-------------------------------+--------+-------+
| id | name | price | stock |
+----+-------------------------------+--------+-------+
| 3 | RT-AC68U | 219.99 | 10 |
| 4 | X110 64GB | 73.84 | 100 |
| 6 | HP Pavilion 14 | 799.99 | 25 |
| 7 | Samsung 27-inch 4K | 349.99 | 30 |
| 8 | Logitech Mechanical K845 | 89.99 | 50 |
| 11 | Kingston A400 240GB | 49.99 | 80 |
| 12 | Asus TUF Gaming VG27AQ | 399.99 | 22 |
| 15 | Logitech G502 Hero | 79.99 | 40 |
| 17 | Samsung T7 Shield 2TB | 199.99 | 35 |
| 18 | Kingston Fury Beast 16GB DDR4 | 44.99 | 60 |
| 19 | Dell Ultrasharp U2419H | 299.99 | 20 |
+----+-------------------------------+--------+-------+
11 rows in set (0.00 sec)
- order by 关键字用于对结果集进行排序
#默认顺序排序
MariaDB [inventory]> select id,name,price,stock from product where stock<20 order by price;
+----+-------------------------+---------+-------+
| id | name | price | stock |
+----+-------------------------+---------+-------+
| 3 | RT-AC68U | 219.99 | 10 |
| 16 | HP LaserJet Pro M428fdw | 499.99 | 12 |
| 9 | Acer Swift 3 | 699.99 | 18 |
| 13 | Dell OptiPlex 7010 | 899.99 | 16 |
| 20 | Acer Nitro AN515-57 | 999.99 | 14 |
| 10 | Microsoft Surface Pro 9 | 1199.99 | 12 |
| 5 | Dell XPS 15 | 1299.99 | 15 |
| 14 | HP Z2 Mini Workstation | 1499.99 | 8 |
+----+-------------------------+---------+-------+
8 rows in set (0.00 sec)#desc 倒序排序
MariaDB [inventory]> select id,name,price,stock from product where stock<20 order by price desc;
+----+-------------------------+---------+-------+
| id | name | price | stock |
+----+-------------------------+---------+-------+
| 14 | HP Z2 Mini Workstation | 1499.99 | 8 |
| 5 | Dell XPS 15 | 1299.99 | 15 |
| 10 | Microsoft Surface Pro 9 | 1199.99 | 12 |
| 20 | Acer Nitro AN515-57 | 999.99 | 14 |
| 13 | Dell OptiPlex 7010 | 899.99 | 16 |
| 9 | Acer Swift 3 | 699.99 | 18 |
| 16 | HP LaserJet Pro M428fdw | 499.99 | 12 |
| 3 | RT-AC68U | 219.99 | 10 |
+----+-------------------------+---------+-------+
8 rows in set (0.00 sec)
多表查询
# 示例1:产品类型是Servers的产品名称和价格
#方法1:
MariaDB [inventory]> select product.name,product.price from product,category where product.id_category = category.id and category.name like 'Ser%';
+------------------------+---------+
| 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)
#方法2:
MariaDB [inventory]> select name,price from product where id_category = (select id from category where name like '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.01 sec)# 示例2:查询厂商是Lenovo的产品名称和价格
#方法1:
MariaDB [inventory]> select product.name,product.price from product,manufacturer where id_manufacturer = manufacturer.id and manufacturer.name like 'Len%';
+-------------------+---------+
| name | price |
+-------------------+---------+
| ThinkServer TS140 | 539.88 |
| ThinkServer RD630 | 2379.14 |
+-------------------+---------+
2 rows in set (0.00 sec)#方法2:
MariaDB [inventory]> select name,price from product where id_manufacturer = (select id from manufacturer where name like 'Len%');
+-------------------+---------+
| name | price |
+-------------------+---------+
| ThinkServer TS140 | 539.88 |
| ThinkServer RD630 | 2379.14 |
+-------------------+---------+
2 rows in set (0.00 sec)
函数
#平均值 avg
MariaDB [inventory]> select avg(price) from product;
+-------------------+
| avg(price) |
+-------------------+
| 631.3844999999999 |
+-------------------+
1 row in set (0.00 sec)
#最大值 max
MariaDB [inventory]> select max(price) from product;
+------------+
| max(price) |
+------------+
| 2379.14 |
+------------+
1 row in set (0.00 sec)
#最小值 min
MariaDB [inventory]> select min(price) from product;
+------------+
| min(price) |
+------------+
| 44.99 |
+------------+
1 row in set (0.01 sec)
#总和 sum
MariaDB [inventory]> select sum(stock) from product;
+------------+
| sum(stock) |
+------------+
| 607 |
+------------+
1 row in set (0.00 sec)
#个数 count
MariaDB [inventory]> select count(stock) from product where stock > 20;
+--------------+
| count(stock) |
+--------------+
| 9 |
+--------------+
1 row in set (0.00 sec)#group by 语句用于结合聚合函数,根据一个或多个列对结果集进行分组
MariaDB [inventory]> select id_category,sum(stock) from product group by id_category;
+-------------+------------+
| id_category | sum(stock) |
+-------------+------------+
| 1 | 10 |
| 2 | 64 |
| 3 | 180 |
| 4 | 84 |
| 5 | 72 |
| 6 | 50 |
| 7 | 40 |
| 8 | 12 |
| 9 | 35 |
| 10 | 60 |
+-------------+------------+
10 rows 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]> show tables;
+---------------------+
| Tables_in_inventory |
+---------------------+
| category |
| manufacturer |
| product |
| staff |
+---------------------+
4 rows in set (0.00 sec)
插入记录
MariaDB [inventory]> insert into staff(id,name,age,id_department)-> VALUES (1,'tong',35,10);
Query OK, 1 row affected (0.00 sec)MariaDB [inventory]> insert into staff(id,name,age,id_department) VALUES (1,'tian',28,11);
Query OK, 1 row affected (0.00 sec)MariaDB [inventory]> select * from staff;
+----+------+------+---------------+
| id | name | age | id_department |
+----+------+------+---------------+
| 1 | tong | 35 | 10 |
| 1 | tian | 28 | 11 |
+----+------+------+---------------+
2 rows in set (0.00 sec)
更新记录
MariaDB [inventory]> update staff set age=18 where name = 'tong';
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 | tong | 18 | 10 |
| 1 | tian | 28 | 11 |
+----+------+------+---------------+
2 rows in set (0.00 sec)
删除记录
MariaDB [inventory]> delete from staff where name = 'tian';
Query OK, 1 row affected (0.00 sec)MariaDB [inventory]> select * from staff;
+----+------+------+---------------+
| id | name | age | id_department |
+----+------+------+---------------+
| 1 | tong | 18 | 10 |
+----+------+------+---------------+
1 row in set (0.00 sec)
删除表
MariaDB [inventory]> drop table staff;
Query OK, 0 rows affected (0.00 sec)MariaDB [inventory]> show tables;
+---------------------+
| Tables_in_inventory |
+---------------------+
| category |
| manufacturer |
| product |
+---------------------+
3 rows in set (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 [inventory]> create user tong@'%' identified by 'abc';
Query OK, 0 rows affected (0.00 sec)
#刷新
MariaDB [inventory]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
当前,tong帐户只能使用密码abc从%**连接。
用户的密码,会被加密存在在mysql.user表:
MariaDB [inventory]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| inventory |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)MariaDB [inventory]> use mysql;
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 [mysql]> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| event |
| func |
| general_log |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| host |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| servers |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
24 rows in set (0.00 sec)#查找user表
MariaDB [mysql]> select user,host,password from user;
+------+-----------+-------------------------------------------+
| user | host | password |
+------+-----------+-------------------------------------------+
| root | localhost | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| root | 127.0.0.1 | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| root | ::1 | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| dcr | % | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| tong | % | *0D3CED9BEC10A777AEC23CCC353A8C08A633045E |
+------+-----------+-------------------------------------------+
5 rows in set (0.00 sec)
用户帐户定义示例
Account | Description |
---|---|
laoma | User 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用户只能访问最少的帐户信息,大多数其他操作均被拒绝。
root | localhost | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257#如localhostMariaDB [mysql]> select user,host,password from user;
+------+-----------+-------------------------------------------+
| user | host | password |
+------+-----------+-------------------------------------------+
| root | localhost | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| root | 127.0.0.1 | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| root | ::1 | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| dcr | % | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| tong | % | *0D3CED9BEC10A777AEC23CCC353A8C08A633045E |
+------+-----------+-------------------------------------------+
5 rows in set (0.00 sec)MariaDB [mysql]> update user set host='localhost' where user='tong';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)MariaDB [mysql]> select user,host,password from user;
+------+-----------+-------------------------------------------+
| user | host | password |
+------+-----------+-------------------------------------------+
| root | localhost | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| root | 127.0.0.1 | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| root | ::1 | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| dcr | % | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| tong | localhost | *0D3CED9BEC10A777AEC23CCC353A8C08A633045E |
+------+-----------+-------------------------------------------+
5 rows in set (0.00 sec)
privileges是用户在MariaDB中拥有的能力,确定用户可以做什么以及用户在MariaDB中可以做什么。特权按范围进行组织:
-
全局特权,用于管理MariaDB数据库服务器本身,例如CREATE USER。
-
数据库特权,用于在MariaDB服务器上创建数据库和使用数据库,例如CREATE DATABASE。
-
表特权,用于在特定数据库中创建表和处理数据,例如CRUD命令。
CRUD是指在做计算处理时的增加(Create)、查询(Retrieve)(重新得到数据)、更新(Update)和删除(Delete)几个单词的首字母简写。
-
列特权,用于授予类似表的命令用法,但用于特定的列(通常很少见)。
详情参考本节末尾引用的MariaDB文档。
查询用户权限
show grants for user@host;
MariaDB [mysql]> show grants for root@localhost;
+----------------------------------------------------------------------------------------------------------------------------------------+
| Grants for root@localhost |
+----------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*23AE809DDACAF96AF0FD78ED04B6A265E05AA257' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION |
+----------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)MariaDB [mysql]> show grants for dcr@'%';
+----------------------------------------------------------------------------------------------------+
| Grants for dcr@% |
+----------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'dcr'@'%' IDENTIFIED BY PASSWORD '*23AE809DDACAF96AF0FD78ED04B6A265E05AA257' |
+----------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
授予用户权限
GRANT语句可用于向帐户授予特权。 要授予GRANT特权,连接的用户必须具有GRANT OPTION且必须具有他们所授予的特定特权。
例如,laoma用户无法授予数据库表SELECT特权,除非他们已经具有SELECT特权和GRANT OPTION表特权。
在此示例中,MariaDB根用户向清单数据库中类别表上的tong用户授予CRUD特权。
#to
MariaDB [mysql]> grant select,update,delete,insert on inventory.category to dcr@'%';
Query OK, 0 rows affected (0.00 sec)MariaDB [mysql]> show grants for dcr@'%';
+----------------------------------------------------------------------------------------------------+
| Grants for dcr@% |
+----------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'dcr'@'%' IDENTIFIED BY PASSWORD '*23AE809DDACAF96AF0FD78ED04B6A265E05AA257' |
| GRANT SELECT, INSERT, UPDATE, DELETE ON `inventory`.`category` TO 'dcr'@'%' |
+----------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
授权示例
Grant | Description |
---|---|
GRANT SELECT ON database.table TO username@hostname | Grant 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@hostname | Grant 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@hostname | Grant all available privileges for all databases to a specific user, effectively creating a superuser, similar toroot. |
回收用户权限
REVOKE语句从帐户中删除特权。 连接的用户必须具有GRANT OPTION特权,并且必须具有被撤消的特定特权。
#from
MariaDB [mysql]> revoke select,insert on inventory.category from dcr@'%';
Query OK, 0 rows affected (0.00 sec)MariaDB [mysql]> show grants for dcr@'%';
+----------------------------------------------------------------------------------------------------+
| Grants for dcr@% |
+----------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'dcr'@'%' IDENTIFIED BY PASSWORD '*23AE809DDACAF96AF0FD78ED04B6A265E05AA257' |
| GRANT UPDATE, DELETE ON `inventory`.`category` TO 'dcr'@'%' |
+----------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
删除用户
当不再需要特定的用户帐户时,可以使用DROP USER将其从数据库中删除。
用户名使用**‘user’@‘host’**格式。
MariaDB [mysql]> drop user tong@localhost;
Query OK, 0 rows affected (0.00 sec)MariaDB [mysql]> select user,host,password from user;
+------+-----------+-------------------------------------------+
| user | host | password |
+------+-----------+-------------------------------------------+
| root | localhost | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| root | 127.0.0.1 | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| root | ::1 | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| dcr | % | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
+------+-----------+-------------------------------------------+
4 rows in set (0.00 sec)
如果删除了当前连接的帐户,则在关闭连接之前不会删除该帐户,关闭连接之后才会删除该帐户。
更改用户密码
# root用户修改普通用户账户密码
MariaDB [mysql]> update user set password=password('abc') where user='dcr';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)MariaDB [mysql]> select user,host,password from user;
+------+-----------+-------------------------------------------+
| user | host | password |
+------+-----------+-------------------------------------------+
| root | localhost | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| root | 127.0.0.1 | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| root | ::1 | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| dcr | % | *0D3CED9BEC10A777AEC23CCC353A8C08A633045E |
+------+-----------+-------------------------------------------+
4 rows in set (0.00 sec)# 普通用户修改自己账户密码
MariaDB [(none)]> SET PASSWORD = PASSWORD('mypass');
MariaDB [(none)]> FLUSH PRIVILEGES;
排故数据库访问
下表总结了用户在身份验证和访问方面可能遇到的一些问题,以及可能的原因。
问题 | 解决方法 |
---|---|
已授予用户从任何主机进行连接的访问权限,但只能使用数据库服务器本地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 用户密码
- 编辑**/etc/my.cnf.d/server.cnf**配置文件,在[mysqld]下添加
skip-grant-tables=1
。 - 重启 mariadb 服务。
- 执行
mysql -u root
命令进入mysql命令行,修改root用户密码。
#测试前需要先把之前/etc/my.cnf.d/client.cnf 中配置的user和password注销
[root@server ~]# mysql -u root
MariaDB [(none)]> UPDATE mysql.user SET password=PASSWORD('新密码') where USER='root';
MariaDB [(none)]> exit
备份和恢复
备份方式
逻辑备份
逻辑备份,将数据库导出为包含重新创建数据库所需的SQL命令的文本文件。
- 通过查询数据库,检索数据库结构。
- 备份速度较慢,因为服务器必须访问数据库信息并将其转换为逻辑格式。
- 逻辑备份具有高度的可移植性,在某些情况下可以还原到其他数据库提供程序(例如PostgreSQL)。
- 在服务器联机时执行。
- 备份不包括日志或配置文件。
物理备份特征
物理备份,复制原始数据库目录和包含数据库内容的文件。
- 备份可以包括日志和配置文件。
- 仅可移植到具有类似硬件和软件的其他计算机上。
- 比逻辑备份更快。
- 在服务器脱机或数据库中的所有表均已锁定时执行,以防止在备份期间进行更改。
物理备份
#1. 停止mariadb服务[root@server ~ 15:21:07]# systemctl stop mariadb.service
[root@server ~ 15:56:35]# ls /var/lib/
alternatives dhclient machines os-prober rpm systemd
authconfig games misc plymouth rpm-state tuned
chrony initramfs mysql polkit-1 rsyslog vmware
dbus logrotate NetworkManager postfix stateless yum#2. tar命令打包 /var/lib/mysql 目录为mysql.tar.gz,保留原先权限 -p选项[root@server ~ 15:58:49]# tar -pczf mysql.tar.gz /var/lib/mysql/
tar: 从成员名中删除开头的“/”
[root@server ~ 15:59:22]# ls
anaconda-ks.cfg inventory.sql inventory.sql.0 mysql.tar.gz#3. 启动mariadb服务,删除inventory库[root@server ~ 15:59:24]# systemctl start mariadb.service
[root@server ~ 16:00:00]# 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 [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| inventory |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)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)MariaDB [(none)]> quit
Bye#4. 停止mariadb 服务,mv /var/lib/mysql /var/lib/mysql.old
#将原来的mysql移除[root@server ~ 16:01:10]# systemctl stop mariadb.service
[root@server ~ 16:01:23]# mv /var/lib/mysql/ /var/lib/mysql.old#5. 提权mysql.tar.gz内容,恢复到/var/lib/mysql[root@server ~ 16:03:28]# tar -xf mysql.tar.gz
[root@server ~ 16:03:46]# ls
anaconda-ks.cfg inventory.sql inventory.sql.0 mysql.tar.gz var
[root@server ~ 16:04:00]# ls
anaconda-ks.cfg inventory.sql inventory.sql.0 mysql.tar.gz var
[root@server ~ 16:05:02]# ls /var/lib/
alternatives dhclient machines os-prober rpm systemd
authconfig games misc plymouth rpm-state tuned
chrony initramfs mysql.old polkit-1 rsyslog vmware
dbus logrotate NetworkManager postfix stateless yum
[root@server ~ 16:05:12]# mv var/lib/mysql/ /var/lib/mysql
[root@server ~ 16:06:21]# ls /var/lib/
alternatives dhclient machines NetworkManager postfix stateless yum
authconfig games misc os-prober rpm systemd
chrony initramfs mysql plymouth rpm-state tuned
dbus logrotate mysql.old polkit-1 rsyslog vmware#6. 启动mariadb服务验证[root@server ~ 16:06:24]# systemctl start mariadb.service
[root@server ~ 16:06:44]# 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 [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| inventory |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)MariaDB [(none)]>
物理备份前一定要记得先停止mariadb 服务,防止有数据正在运行
逻辑备份
您在运行mysqldump时所连接的MariaDB用户至少需要对转储表具有SELECT特权,对转储视图至少需要SHOW VIEW,对于转储触发器至少需要TRIGGER特权。
使用mysqldump命令执行逻辑备份。
[root@server ~ 22:59:28]# mysqldump -u root -p inventory > ./inventory.dump
Enter password:
#只备份了inventory里三个表
[root@server ~ 23:01:18]# mysqldump -u root -p --all-databases > ./mariadb.dump
Enter password:
#备份数据库
[root@server ~ 23:02:34]# ls
anaconda-ks.cfg inventory.dump mariadb.dump mysql.tar.gz var#恢复逻辑备份
[root@server ~ 23:02:37]# mysql -u root -p inventory < ./mariadb.dump
Enter password: