34.MariaDB 数据库
文章目录
- MariaDB 数据库
- MariaDB 数据库是什么
- 关系型数据库
- 部署MariaDB
- 安装 MariaDB
- 启用并启动服务
- 配置防火墙
- 加固MariaDB
- 安装mysql 命令行补全
- MariaDB 配置文件
- 数据库操作
- 连接数据库
- 查看数据库清单
- 使用数据库
- 创建数据库
- 删除数据库
- 表操作
- 查询表
- 查询表列表
- 查询表结构
- 查询表中数据
- 查询表中所有数据记录
- 查询表中所有记录特定字段
- where子句设置条件
- 条件操作
- 多表查询
- 创建表
- 插入数据
- 更新数据
- 删除数据
- 删除表
- MariaDB用户管理
- 创建用户账户
- 控制用户权限
- 查询用户权限
- 授予用户权限
- 回收用户权限
- 设置用户登录主机
- 修改用户密码
- 删除用户
- 备份和恢复
- 物理备份和恢复
- 逻辑备份和恢复
MariaDB 数据库
MariaDB 数据库是什么
MariaDB 是一个开源的关系型数据库管理系统,由 MySQL 的原始开发者创建,完全兼容 MySQL。
关系型数据库
关系型数据库是使用表格(表)来存储和管理数据的数据库,就像多个相互关联的 Excel 工作表。
这里的表格特指二维表,不能是其他表项,由行和列组成,行是一条具体的数据记录,列是数据的属性
部署MariaDB
安装 MariaDB
# 安装服务端
[root@server ~ 17:22:31]# yum install -y mariadb-server
# 安装客户端
[root@server ~ 17:23:00]# yum install -y mariadb
启用并启动服务
[root@server ~ 17:23:46]# systemctl enable mariadb
[root@server ~ 17:24:39]# systemctl start mariadb --now
配置防火墙
# 直接关闭防火墙
[root@server ~ 17:25:18]# systemctl stop firewalld
加固MariaDB
MariaDB数据库默认具有test数据库和一些不太安全的配置。运行mysql_secure_installation
修改这些配置。
[root@server ~ 17:26:09]# mysql_secure_installation
交互式提示您进行更改,包括:
- 为root帐户设置密码。
- 禁止root帐户从本地主机外部访问数据库。
- 删除匿名用户帐户。
- 删除用于演示的test数据库。
安装mysql 命令行补全
[root@server ~ 17:30:20]# yum -y install python-pip python-devel
[root@server ~ 17:30:37]# yum -y install gcc libffi-devel python-devel openssl-devel
[root@server ~ 17:31:22]# mkdir ~/.pip
[root@server ~ 17:31:37]# vim ~/.pip/pip.conf
[root@server ~ 17:32:43]# cat ~/.pip/pip.conf
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host = mirrors.aliyun.com
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:40:15]# vim /etc/my.cnf.d/client.cnf
[client]
user=hxl
password=123
host=server
port=3306
数据库操作
连接数据库
mariadb软件包提供了命令mysql,该命令支持对MariaDB数据库的交互式和非交互式访问。
-
交互式执行时,结果以ASCII表的格式显示。
-
非交互执行时,结果以制表符分隔的格式显示。
示例:
[root@server ~ 17:33:33]# mysql -u root -h localhost -p
选项解释:
-u:指定用户
-h:指定要连接的服务器主机地址
-p:需要输入用户密码来连接数据库
首次安装时,MariaDB默认设置root账户无需要密码即可进行访问。
[root@server ~ 17:46:50]# mysql -u root
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 11
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)]>
执行交互式SQL语句时,MariaDB提示符在方括号中显示您当前选择数据库。
查看数据库清单
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)MariaDB [(none)]>
说明:
- mysql数据库,是一个系统数据库,保存数据库用户及其访问权限等信息。
- INFORMATION_SCHEMA数据库,保存关于数据库或者数据表的元数据信息。
- PERFORMANCE_SCHEMA数据库,保存数据库服务器性能信息。
使用数据库
您可以使用 USE 语句选择数据库之一,例如:USE mysq1;
,后续默认操作的表属于mysql数据库。
MariaDB [(none)]> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase changed
MariaDB [mysql]>
SQL语句不区分大小写,但数据库名称区分大小写。通常的做法是数据库名称全为小写字母,SQL语句全为大写字母,以区分SQL语句与语句的目标或参数。
创建数据库
MariaDB [mysql]> create database hxl;
Query OK, 1 row affected (0.00 sec)MariaDB [mysql]> use hxl;
Database changed
MariaDB [hxl]>
删除数据库
MariaDB [mysql]> drop database hxl;
Query OK, 0 rows affected (0.00 sec)
DROP DATABASE语句删除数据库中的所有表并删除数据库。这将破坏数据库中的所有数据。
只有对该数据库具有DROP特权的用户才能运行此语句。这不会更改数据库的用户特权。
如果重新创建具有该名称的数据库,则为旧数据库设置的用户权限仍然有效。
表操作
SQL 语句操作:
- create添加数据
- read读取数据
- update修改数据
- delete删除数据
这里我们事先导入一个数据库,里面有我们测试的表
[root@server ~ 18:03:29]# mysql inventory < inventory.sql
查询表
查询表列表
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]> 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.01 sec)
输出解释:
- Field:显示属性名称
- Type :显示该属性的数据采用的格式
- Null:指示此属性是否可以为NULL
- Key:显示属性ID是primary key。主键是表中一行的唯一标识符。该属性的任何其他行的值都不能相同。
- Default:指示是否有默认值
- Extra:提供该列的额外信息,对于id字段标记为auto_increment。意味着每次将新项目插入表中时,该值都会增加。这样可以更轻松地使数字主键保持唯一。
查询表中数据
查询表中所有数据记录
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 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>100;
+-------------------------+---------+-------+
| name | price | stock |
+-------------------------+---------+-------+
| ThinkServer TS140 | 539.88 | 20 |
| ThinkServer RD630 | 2379.14 | 20 |
| RT-AC68U | 219.99 | 10 |
| Dell XPS 15 | 1299.99 | 15 |
| HP Pavilion 14 | 799.99 | 25 |
| Samsung 27-inch 4K | 349.99 | 30 |
| Acer Swift 3 | 699.99 | 18 |
| Microsoft Surface Pro 9 | 1199.99 | 12 |
| Asus TUF Gaming VG27AQ | 399.99 | 22 |
| Dell OptiPlex 7010 | 899.99 | 16 |
| HP Z2 Mini Workstation | 1499.99 | 8 |
| HP LaserJet Pro M428fdw | 499.99 | 12 |
| Samsung T7 Shield 2TB | 199.99 | 35 |
| Dell Ultrasharp U2419H | 299.99 | 20 |
| Acer Nitro AN515-57 | 999.99 | 14 |
+-------------------------+---------+-------+
15 rows in set (0.00 sec)
条件操作
操作符 | 描述 |
---|---|
= | 等于 |
<> | 不等于。注意:在某些 SQL 版本中,此操作符可能写作 != |
> | 大于 |
< | 小于 |
>= | 大于或等于 |
<= | 小于或等于 |
BETWEEN | 在某个包含性范围内 |
LIKE | 搜索匹配某种模式的值 |
IN | 指定某个列的多个可能值 |
多表查询
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 name,price-> from product-> where id = (select id from category where name='Servers');
+-------------------+---------+
| name | price |
+-------------------+---------+
| ThinkServer RD630 | 2379.14 |
+-------------------+---------+
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]> 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,'hxl',21,10);
Query OK, 1 row affected (0.00 sec)MariaDB [inventory]> insert into staff (id,name,age)-> values (2,'zhangsan',28);
Query OK, 1 row affected (0.00 sec)MariaDB [inventory]> insert into staff (id,name)-> values (3,'lisi');
Query OK, 1 row affected (0.00 sec)MariaDB [inventory]> select * from staff;
+----+----------+------+---------------+
| id | name | age | id_department |
+----+----------+------+---------------+
| 1 | hxl | 21 | 10 |
| 2 | zhangsan | 28 | NULL |
| 3 | lisi | 10 | NULL |
+----+----------+------+---------------+
3 rows in set (0.00 sec)
更新数据
MariaDB [inventory]> update staff set age=30 where id=3;
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 | hxl | 21 | 10 |
| 2 | zhangsan | 28 | NULL |
| 3 | lisi | 30 | NULL |
+----+----------+------+---------------+
3 rows in set (0.00 sec)MariaDB [inventory]> update staff set age=30;
Query OK, 2 rows affected (0.00 sec)
Rows matched: 3 Changed: 2 Warnings: 0MariaDB [inventory]> select * from staff;
+----+----------+------+---------------+
| id | name | age | id_department |
+----+----------+------+---------------+
| 1 | hxl | 30 | 10 |
| 2 | zhangsan | 30 | NULL |
| 3 | lisi | 30 | NULL |
+----+----------+------+---------------+
3 rows in set (0.00 sec)
如果不使用where子句,表中所有记录都会更新
删除数据
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 | hxl | 30 | 10 |
| 2 | zhangsan | 30 | NULL |
+----+----------+------+---------------+
2 rows in set (0.00 sec)MariaDB [inventory]> delete from staff;
Query OK, 2 rows affected (0.00 sec)MariaDB [inventory]> select * from staff;
Empty set (0.00 sec)
如果不使用where子句,表中所有记录都会删除
删除表
MariaDB [inventory]> drop table staff;
MariaDB用户管理
创建用户账户
MariaDB [(none)]> create user laoma@'%' identified by '123';
Query OK, 0 rows affected (0.01 sec)
当前,laoma账户只能使用密码123从%连接。
这里的%是通配符,意思是laoma@*用户都能连接数据库
用户密码会被加密存在mysql.user表中
MariaDB [(none)]> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase changed
MariaDB [mysql]> select host,user,password from user where user='laoma';
+------+-------+-------------------------------------------+
| host | user | password |
+------+-------+-------------------------------------------+
| % | laoma | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
+------+-------+-------------------------------------------+
1 row in set (0.00 sec)
控制用户权限
查询用户权限
MariaDB [(none)]> show grants for root@localhost;
授予用户权限
MariaDB [(none)]> grant select,update,delete,insert-> on inventory.category-> to laoma@localhost;
命令解释:授予给laoma@localhost用户,select,update,delete,insert这四个操作权限,在数据库inventory的category表上。
回收用户权限
MariaDB [(none)]> revoke select,update,delete,insert-> on inventory.category-> from laoma@localhost;
命令解释:回收给laoma@localhost用户,select,update,delete,insert这四个操作权限,在数据库inventory的category表上。
设置用户登录主机
设置laoma用户只能从localhost连接
MariaDB [(none)]> update mysql.user set host='localhost' where user='laoma';
修改用户密码
将laoma用户的密码修改为abc
MariaDB [(none)]> update mysql.user set password=password('abc') where user='laoma';
删除用户
MariaDB [(none)]> drop user laoma@localhost;
备份和恢复
物理备份和恢复
-
停止mariadb服务
[root@server ~ 15:55:34]# systemctl stop mariadb
-
tar命令打包 /var/lib/mysql 目录为mysql.tar.gz,保留原先权限 -p选项
[root@server ~ 15:57:12]# tar -pczf mysql.tar.gz /var/lib/mysql [root@server ~ 15:57:43]# ls anaconda-ks.cfg inventory.sql mysql.tar.gz
-
启动mariadb服务,删除inventory库中product表
[root@server ~ 15:57:45]# systemctl start mariadb.service --now [root@server ~ 15:58:13]# mysql -u rootMariaDB [(none)]> use inventory;MariaDB [inventory]> drop table product;MariaDB [inventory]> exit
-
停止mariadb 服务,mv /var/lib/mysql /var/lib/mysql.old
[root@server ~ 15:59:30]# systemctl stop mariadb.service [root@server ~ 15:59:51]# mv /var/lib/mysql /var/lib/mysql.lod
-
提取mysql.tar.gz内容,恢复到/var/lib/mysql
[root@server ~ 16:17:24]# tar -pxzf mysql.tar.gz [root@server ~ 16:17:33]# ls anaconda-ks.cfg inventory.sql mysql.tar.gz var [root@server ~ 16:18:29]# cp -ar var/lib/mysql /var/lib/mysql [root@server ~ 16:19:34]# ls -ld /var/lib/mysql drwxr-xr-x 5 mysql mysql 164 9月 28 15:55 /var/lib/mysql
-
启动mariadb服务验证
[root@server ~ 16:19:48]# systemctl start mariadb.service --now [root@server ~ 16:20:11]# mysql -u root 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)
逻辑备份和恢复
-
备份特定表
[root@server ~ 18:45:36]# 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 */; ... ...
-
特定库(库中所有表),不包括创建数据库命令
[root@server ~ 18:45:52]# 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 */; ... ...
-
特定库(库中所有表),包括创建数据库命令
[root@server ~ 18:46:36]# 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 */; ... ...
-
恢复
# 格式:mysql < sql [root@server ~ 18:46:36]# mysql -u root -p inventory < /backup/mariadb.dump