From d9bc1fa5ecb30d66262d37aeb8266d2285a5c3a1 Mon Sep 17 00:00:00 2001 From: wxin <15253413025@163.com> Date: Wed, 14 Aug 2024 21:41:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20/?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 第三章:数据库查询.md | 975 +++++++++++++++++++++ 第二章:数据库管理及数据类型.md | 861 +++++++++++++++++++ 第五章:权限管理及数据备份与恢复.md | 1228 +++++++++++++++++++++++++++ 第四章:数据库日志管理.md | 135 +++ 4 files changed, 3199 insertions(+) create mode 100644 第三章:数据库查询.md create mode 100644 第二章:数据库管理及数据类型.md create mode 100644 第五章:权限管理及数据备份与恢复.md create mode 100644 第四章:数据库日志管理.md diff --git a/第三章:数据库查询.md b/第三章:数据库查询.md new file mode 100644 index 0000000..0542db7 --- /dev/null +++ b/第三章:数据库查询.md @@ -0,0 +1,975 @@ +

数据库查询

+ + + +------ + +## 一:基本查询 + +#### 1.简介 + +​ 单表查询 + +​ 简单查询 + +​ 通过条件查询 + +​ 查询排序 + +​ 限制查询记录数 + +​ 使用集合函数查询 + +​ 分组查询 + +​ 使用正则表达式查询 + +#### 2.案例 + +创建案例所需表:company.employee5 + +```shell + 雇员编号 id int + 雇员姓名 name varchar(30) + 雇员性别 sex enum + 雇用时期 hire_date date + 职位 post varchar(50) + 职位描述 job_description varchar(100) + 薪水 salary double(15,2) + 办公室 office int + 部门编号 dep_id int +``` + +```shell +MySQL [(none)]> CREATE TABLE company.employee5( + id int primary key AUTO_INCREMENT not null, + name varchar(30) not null, + sex enum('male','female') default 'male' not null, + hire_date date not null, + post varchar(50) not null, + job_description varchar(100), + salary double(15,2) not null, + office int, + dep_id int + ); +``` + +插入模拟数据: + +```shell +MySQL [(none)]> insert into company.employee5(name,sex,hire_date,post,job_description,salary,office,dep_id) values + ('jack','male','20180202','instructor','teach',5000,501,100), + ('tom','male','20180203','instructor','teach',5500,501,100), + ('robin','male','20180202','instructor','teach',8000,501,100), + ('alice','female','20180202','instructor','teach',7200,501,100), + ('','male','20180202','hr','hrcc',600,502,101), + ('harry','male','20180202','hr',NULL,6000,502,101), + ('emma','female','20180206','sale','salecc',20000,503,102), + ('christine','female','20180205','sale','salecc',2200,503,102), + ('zhuzhu','male','20180205','sale',NULL,2200,503,102), + ('gougou','male','20180205','sale','',2200,503,102); +``` + +语法格式: + +​ select 字段名称,字段名称2...... from 表名 [条件] + +##### a.简单查询 + +```shell +MySQL [company]> select * from employee5; ++----+-----------+--------+------------+------------+-----------------+----------+--------+--------+ +| id | name | sex | hire_date | post | job_description | salary | office | dep_id | ++----+-----------+--------+------------+------------+-----------------+----------+--------+--------+ +| 1 | jack | male | 2018-02-02 | instructor | teach | 5000.00 | 501 | 100 | +| 2 | tom | male | 2018-02-03 | instructor | teach | 5500.00 | 501 | 100 | +| 3 | robin | male | 2018-02-02 | instructor | teach | 8000.00 | 501 | 100 | +| 4 | alice | female | 2018-02-02 | instructor | teach | 7200.00 | 501 | 100 | +| 5 | | male | 2018-02-02 | hr | hrcc | 600.00 | 502 | 101 | +| 6 | harry | male | 2018-02-02 | hr | NULL | 6000.00 | 502 | 101 | +| 7 | emma | female | 2018-02-06 | sale | salecc | 20000.00 | 503 | 102 | +| 8 | christine | female | 2018-02-05 | sale | salecc | 2200.00 | 503 | 102 | +| 9 | zhuzhu | male | 2018-02-05 | sale | NULL | 2200.00 | 503 | 102 | +| 10 | gougou | male | 2018-02-05 | sale | | 2200.00 | 503 | 102 | ++----+-----------+--------+------------+------------+-----------------+----------+--------+--------+ +10 rows in set (0.00 sec) + +MySQL [company]> select name, salary, dep_id from employee5 where id <=5; ++-------+---------+--------+ +| name | salary | dep_id | ++-------+---------+--------+ +| jack | 5000.00 | 100 | +| tom | 5500.00 | 100 | +| robin | 8000.00 | 100 | +| alice | 7200.00 | 100 | +| | 600.00 | 101 | ++-------+---------+--------+ +5 rows in set (0.00 sec) +``` + +##### b.避免重复 + +​ 不能部分使用DISTINCT,通常仅用于某一字段 + +```shell +MySQL [company]> SELECT post FROM employee5; ++------------+ +| post | ++------------+ +| instructor | +| instructor | +| instructor | +| instructor | +| hr | +| hr | +| sale | +| sale | +| sale | +| sale | ++------------+ +10 rows in set (0.00 sec) + +MySQL [company]> SELECT distinct post FROM employee5; ++------------+ +| post | ++------------+ +| instructor | +| hr | +| sale | ++------------+ +3 rows in set (0.00 sec) +``` + +##### c.四则运算查询 + +```shell +MySQL [company]> SELECT name, salary, salary*14 FROM employee5; ++-----------+----------+-----------+ +| name | salary | salary*14 | ++-----------+----------+-----------+ +| jack | 5000.00 | 70000.00 | +| tom | 5500.00 | 77000.00 | +| robin | 8000.00 | 112000.00 | +| alice | 7200.00 | 100800.00 | +| | 600.00 | 8400.00 | +| harry | 6000.00 | 84000.00 | +| emma | 20000.00 | 280000.00 | +| christine | 2200.00 | 30800.00 | +| zhuzhu | 2200.00 | 30800.00 | +| gougou | 2200.00 | 30800.00 | ++-----------+----------+-----------+ +10 rows in set (0.01 sec) + +MySQL [company]> SELECT name, salary, salary*14 AS Annual_salary FROM employee5; ++-----------+----------+---------------+ +| name | salary | Annual_salary | ++-----------+----------+---------------+ +| jack | 5000.00 | 70000.00 | +| tom | 5500.00 | 77000.00 | +| robin | 8000.00 | 112000.00 | +| alice | 7200.00 | 100800.00 | +| | 600.00 | 8400.00 | +| harry | 6000.00 | 84000.00 | +| emma | 20000.00 | 280000.00 | +| christine | 2200.00 | 30800.00 | +| zhuzhu | 2200.00 | 30800.00 | +| gougou | 2200.00 | 30800.00 | ++-----------+----------+---------------+ +10 rows in set (0.00 sec) + +MySQL [company]> SELECT name, salary, salary*14 Annual_salary FROM employee5; ++-----------+----------+---------------+ +| name | salary | Annual_salary | ++-----------+----------+---------------+ +| jack | 5000.00 | 70000.00 | +| tom | 5500.00 | 77000.00 | +| robin | 8000.00 | 112000.00 | +| alice | 7200.00 | 100800.00 | +| | 600.00 | 8400.00 | +| harry | 6000.00 | 84000.00 | +| emma | 20000.00 | 280000.00 | +| christine | 2200.00 | 30800.00 | +| zhuzhu | 2200.00 | 30800.00 | +| gougou | 2200.00 | 30800.00 | ++-----------+----------+---------------+ +10 rows in set (0.00 sec) +``` + +##### d.定义显示格式 + +​ CONCAT() 函数用于连接字符串 + +```shell +MySQL [company]> SELECT concat(name, 's annual salary: ', salary*14) AS Annual_salary FROM employee5; ++------------------------------------+ +| Annual_salary | ++------------------------------------+ +| jacks annual salary: 70000.00 | +| toms annual salary: 77000.00 | +| robins annual salary: 112000.00 | +| alices annual salary: 100800.00 | +| s annual salary: 8400.00 | +| harrys annual salary: 84000.00 | +| emmas annual salary: 280000.00 | +| christines annual salary: 30800.00 | +| zhuzhus annual salary: 30800.00 | +| gougous annual salary: 30800.00 | ++------------------------------------+ +10 rows in set (0.00 sec) +``` + +##### e.单条件查询 + +```shell +MySQL [company]> SELECT name,post FROM employee5 WHERE post='hr'; ++-------+------+ +| name | post | ++-------+------+ +| | hr | +| harry | hr | ++-------+------+ +2 rows in set (0.00 sec) +``` + +##### f.多条件查询 + +```shell +MySQL [company]> SELECT name,salary FROM employee5 WHERE post='hr' AND salary>10000; +Empty set (0.00 sec) + +MySQL [company]> select * from employee5 where salary>5000 and salary<10000 or dep_id=102; ++----+-----------+--------+------------+------------+-----------------+----------+--------+--------+ +| id | name | sex | hire_date | post | job_description | salary | office | dep_id | ++----+-----------+--------+------------+------------+-----------------+----------+--------+--------+ +| 2 | tom | male | 2018-02-03 | instructor | teach | 5500.00 | 501 | 100 | +| 3 | robin | male | 2018-02-02 | instructor | teach | 8000.00 | 501 | 100 | +| 4 | alice | female | 2018-02-02 | instructor | teach | 7200.00 | 501 | 100 | +| 6 | harry | male | 2018-02-02 | hr | NULL | 6000.00 | 502 | 101 | +| 7 | emma | female | 2018-02-06 | sale | salecc | 20000.00 | 503 | 102 | +| 8 | christine | female | 2018-02-05 | sale | salecc | 2200.00 | 503 | 102 | +| 9 | zhuzhu | male | 2018-02-05 | sale | NULL | 2200.00 | 503 | 102 | +| 10 | gougou | male | 2018-02-05 | sale | | 2200.00 | 503 | 102 | ++----+-----------+--------+------------+------------+-----------------+----------+--------+--------+ +8 rows in set (0.00 sec) +``` + +##### g.关键字 + +​ BETWEEN AND + +```shell +MySQL [company]> SELECT name,salary FROM employee5 WHERE salary BETWEEN 5000 AND 15000; ++-------+---------+ +| name | salary | ++-------+---------+ +| jack | 5000.00 | +| tom | 5500.00 | +| robin | 8000.00 | +| alice | 7200.00 | +| harry | 6000.00 | ++-------+---------+ +5 rows in set (0.00 sec) + +MySQL [company]> SELECT name,salary FROM employee5 WHERE salary NOT BETWEEN 5000 AND 15000; ++-----------+----------+ +| name | salary | ++-----------+----------+ +| | 600.00 | +| emma | 20000.00 | +| christine | 2200.00 | +| zhuzhu | 2200.00 | +| gougou | 2200.00 | ++-----------+----------+ +5 rows in set (0.00 sec) +``` + +​ IS NULL + +```shell +MySQL [company]> SELECT name,job_description FROM employee5 WHERE job_description IS NULL; ++--------+-----------------+ +| name | job_description | ++--------+-----------------+ +| harry | NULL | +| zhuzhu | NULL | ++--------+-----------------+ +2 rows in set (0.00 sec) + +MySQL [company]> SELECT name,job_description FROM employee5 WHERE job_description IS NOT NULL; ++-----------+-----------------+ +| name | job_description | ++-----------+-----------------+ +| jack | teach | +| tom | teach | +| robin | teach | +| alice | teach | +| | hrcc | +| emma | salecc | +| christine | salecc | +| gougou | | ++-----------+-----------------+ +8 rows in set (0.00 sec) + +MySQL [company]> SELECT name,job_description FROM employee5 WHERE job_description=''; ++--------+-----------------+ +| name | job_description | ++--------+-----------------+ +| gougou | | ++--------+-----------------+ +1 row in set (0.00 sec) +``` + +注意:(NULL说明) + +​ 等价于没有任何值、是未知数 + +​ NULL与0、空字符串、空格都不同,NULL没有分配存储空间 + +​ 对空值做加、减、乘、除等运算操作,结果仍为空 + +​ 比较时使用关键字用“is null”和“is not null” + +​ 排序时比其他数据都小(索引默认是降序排列,小→大),所以NULL值总是排在最前 + +IN集合查询 + +```shell +MySQL [company]> SELECT name, salary FROM employee5 WHERE salary=4000 OR salary=5000 OR salary=6000 OR salary=9000 ; ++-------+---------+ +| name | salary | ++-------+---------+ +| jack | 5000.00 | +| harry | 6000.00 | ++-------+---------+ +2 rows in set (0.00 sec) + +MySQL [company]> SELECT name, salary FROM employee5 WHERE salary IN (4000,5000,6000,9000) ; ++-------+---------+ +| name | salary | ++-------+---------+ +| jack | 5000.00 | +| harry | 6000.00 | ++-------+---------+ +2 rows in set (0.00 sec) + +MySQL [company]> SELECT name, salary FROM employee5 WHERE salary NOT IN (4000,5000,6000,9000) ; ++-----------+----------+ +| name | salary | ++-----------+----------+ +| tom | 5500.00 | +| robin | 8000.00 | +| alice | 7200.00 | +| | 600.00 | +| emma | 20000.00 | +| christine | 2200.00 | +| zhuzhu | 2200.00 | +| gougou | 2200.00 | ++-----------+----------+ +8 rows in set (0.01 sec) +``` + +##### h.模糊查询 + +​ 关键字LIKE + +​ 通配符%:所有字符 + +​ 通配符_: 一个字符 + +```shell +MySQL [company]> SELECT * FROM employee5 WHERE name LIKE 'al%'; ++----+-------+--------+------------+------------+-----------------+---------+--------+--------+ +| id | name | sex | hire_date | post | job_description | salary | office | dep_id | ++----+-------+--------+------------+------------+-----------------+---------+--------+--------+ +| 4 | alice | female | 2018-02-02 | instructor | teach | 7200.00 | 501 | 100 | ++----+-------+--------+------------+------------+-----------------+---------+--------+--------+ +1 row in set (0.00 sec) + +MySQL [company]> SELECT * FROM employee5 WHERE name LIKE 'al___'; ++----+-------+--------+------------+------------+-----------------+---------+--------+--------+ +| id | name | sex | hire_date | post | job_description | salary | office | dep_id | ++----+-------+--------+------------+------------+-----------------+---------+--------+--------+ +| 4 | alice | female | 2018-02-02 | instructor | teach | 7200.00 | 501 | 100 | ++----+-------+--------+------------+------------+-----------------+---------+--------+--------+ +1 row in set (0.00 sec) +``` + +##### i.排序查询 + +```shell +MySQL [company]> select name,salary from employee5 order by salary; ++-----------+----------+ +| name | salary | ++-----------+----------+ +| | 600.00 | +| christine | 2200.00 | +| zhuzhu | 2200.00 | +| gougou | 2200.00 | +| jack | 5000.00 | +| tom | 5500.00 | +| harry | 6000.00 | +| alice | 7200.00 | +| robin | 8000.00 | +| emma | 20000.00 | ++-----------+----------+ +10 rows in set (0.01 sec) + +MySQL [company]> select name,salary from employee5 order by salary desc; ++-----------+----------+ +| name | salary | ++-----------+----------+ +| emma | 20000.00 | +| robin | 8000.00 | +| alice | 7200.00 | +| harry | 6000.00 | +| tom | 5500.00 | +| jack | 5000.00 | +| christine | 2200.00 | +| zhuzhu | 2200.00 | +| gougou | 2200.00 | +| | 600.00 | ++-----------+----------+ + +MySQL [company]> select name,salary from employee5 order by salary desc limit 3; //控制显示前3行 ++-------+----------+ +| name | salary | ++-------+----------+ +| emma | 20000.00 | +| robin | 8000.00 | +| alice | 7200.00 | ++-------+----------+ +3 rows in set (0.00 sec) + +MySQL [company]> select name,salary from employee5 order by salary desc limit 1,3; //从序号1开始显示三行的内容 ++-------+---------+ +| name | salary | ++-------+---------+ +| robin | 8000.00 | +| alice | 7200.00 | +| harry | 6000.00 | ++-------+---------+ +3 rows in set (0.00 sec) +``` + +注意: + +​ ascending 美音 /ə'sɛndɪŋ/ 升序 + +​ descending 美音 /dɪ'sɛndɪŋ/ 降序 + +##### j.集合函数查询 + +​ count:可以查看共有多少条记录 + +```shell +MySQL [company]> select count(*) from employee5; ++----------+ +| count(*) | ++----------+ +| 10 | ++----------+ +1 row in set (0.00 sec) + +MySQL [company]> select count(name) from employee5; ++-------------+ +| count(name) | ++-------------+ +| 10 | ++-------------+ +1 row in set (0.00 sec) + +``` + +​ max:查看最大值 + +```shell +MySQL [company]> select max(salary) from employee5; ++-------------+ +| max(salary) | ++-------------+ +| 20000.00 | ++-------------+ +1 row in set (0.00 sec) +``` + +​ min:查看最小值 + +```shell +MySQL [company]> select min(salary) from employee5; ++-------------+ +| min(salary) | ++-------------+ +| 600.00 | ++-------------+ +1 row in set (0.00 sec) +``` + +​ avg:查看平均值 + +```shell +MySQL [company]> select avg(salary) from employee5; ++-------------+ +| avg(salary) | ++-------------+ +| 5890.000000 | ++-------------+ +1 row in set (0.00 sec) +``` + +​ sum:求和 + +​ sale这个部门的总工资 + +```shell +MySQL [company]> select concat("Total Department Wages:",sum(salary)) from employee5 where post='sale'; ++-------------------------------------------------+ +| concat("Total Department Wages:",sum(salary)) | ++-------------------------------------------------+ +| Total Department Wages:26600.00 | ++-------------------------------------------------+ +1 row in set (0.00 sec) +``` + +​ 获取薪水最高的这个人的详细信息 + +```shell +MySQL [company]> select * from employee5 where salary = (select max(salary) from employee5); ++----+------+--------+------------+------+-----------------+----------+--------+--------+ +| id | name | sex | hire_date | post | job_description | salary | office | dep_id | ++----+------+--------+------------+------+-----------------+----------+--------+--------+ +| 7 | emma | female | 2018-02-06 | sale | salecc | 20000.00 | 503 | 102 | ++----+------+--------+------------+------+-----------------+----------+--------+--------+ +1 row in set (0.00 sec) +``` + +##### k.分组查询 + +​ GROUP BY和GROUP_CONCAT()函数一起使用 + +​ 获取部门ID相同的员工并把名字拼接到一起 + +```shell +MySQL [company]> SELECT dep_id,GROUP_CONCAT(name) FROM employee5 GROUP BY dep_id; ++--------+------------------------------+ +| dep_id | GROUP_CONCAT(name) | ++--------+------------------------------+ +| 100 | jack,tom,robin,alice | +| 101 | ,harry | +| 102 | emma,christine,zhuzhu,gougou | ++--------+------------------------------+ +3 rows in set (0.01 sec) +``` + +​ GROUP BY和集合函数一起使用 + +​ 获取部门最高薪资 + +```shell +MySQL [company]> SELECT post,max(salary) FROM employee5 GROUP BY post; ++------------+-------------+ +| post | max(salary) | ++------------+-------------+ +| hr | 6000.00 | +| instructor | 8000.00 | +| sale | 20000.00 | ++------------+-------------+ +3 rows in set (0.00 sec) +``` + +##### l.正则查询 + +```shell +以什么开头 +MySQL [company]> SELECT * FROM employee5 WHERE name REGEXP '^ali'; ++----+-------+--------+------------+------------+-----------------+---------+--------+--------+ +| id | name | sex | hire_date | post | job_description | salary | office | dep_id | ++----+-------+--------+------------+------------+-----------------+---------+--------+--------+ +| 4 | alice | female | 2018-02-02 | instructor | teach | 7200.00 | 501 | 100 | ++----+-------+--------+------------+------------+-----------------+---------+--------+--------+ +1 row in set (0.00 sec) + +以什么结尾 +MySQL [company]> SELECT * FROM employee5 WHERE name REGEXP 'ce$'; ++----+-------+--------+------------+------------+-----------------+---------+--------+--------+ +| id | name | sex | hire_date | post | job_description | salary | office | dep_id | ++----+-------+--------+------------+------------+-----------------+---------+--------+--------+ +| 4 | alice | female | 2018-02-02 | instructor | teach | 7200.00 | 501 | 100 | ++----+-------+--------+------------+------------+-----------------+---------+--------+--------+ +1 row in set (0.01 sec) + +连续出现n次 +MySQL [company]> SELECT * FROM employee5 WHERE name REGEXP 'm{2}'; ++----+------+--------+------------+------+-----------------+----------+--------+--------+ +| id | name | sex | hire_date | post | job_description | salary | office | dep_id | ++----+------+--------+------------+------+-----------------+----------+--------+--------+ +| 7 | emma | female | 2018-02-06 | sale | salecc | 20000.00 | 503 | 102 | ++----+------+--------+------------+------+-----------------+----------+--------+--------+ +1 row in set (0.00 sec) +``` + +## 二:多表联合查询 + +【扩展了解】 + +#### 1.数据准备 + +##### 表:company.employee6 + +创建表: + +```shell +MySQL [company]> create table employee6( + emp_id int auto_increment primary key not null, + emp_name varchar(50), + age int, + dept_id int); +Query OK, 0 rows affected (0.65 sec) +``` + +查看表结构: + +```shell +MySQL [company]> desc employee6; ++----------+-------------+------+-----+---------+----------------+ +| Field | Type | Null | Key | Default | Extra | ++----------+-------------+------+-----+---------+----------------+ +| emp_id | int(11) | NO | PRI | NULL | auto_increment | +| emp_name | varchar(50) | YES | | NULL | | +| age | int(11) | YES | | NULL | | +| dept_id | int(11) | YES | | NULL | | ++----------+-------------+------+-----+---------+----------------+ +4 rows in set (0.00 sec) +``` + +插入模拟数据: + +```shell +MySQL [company]> insert into employee6(emp_name,age,dept_id) values + ('',19,200), + ('tom',26,201), + ('jack',30,201), + ('alice',24,202), + ('robin',40,200), + ('xingdian',16,200), + ('natasha',28,204); +``` + +查看数据: + +```shell +MySQL [company]> select * from employee6; ++--------+----------+------+---------+ +| emp_id | emp_name | age | dept_id | ++--------+----------+------+---------+ +| 1 | | 19 | 200 | +| 2 | tom | 26 | 201 | +| 3 | jack | 30 | 201 | +| 4 | alice | 24 | 202 | +| 5 | robin | 40 | 200 | +| 6 | xingdian | 16 | 200 | +| 7 | natasha | 28 | 204 | ++--------+----------+------+---------+ +7 rows in set (0.00 sec) +``` + +##### 表:company.department6 + +创建表: + +```shell +MySQL [company]> create table department6( + dept_id int, + dept_name varchar(100) + ); +Query OK, 0 rows affected (0.33 sec) +``` + +查看表结构: + +```shell +MySQL [company]> desc department6; ++-----------+--------------+------+-----+---------+-------+ +| Field | Type | Null | Key | Default | Extra | ++-----------+--------------+------+-----+---------+-------+ +| dept_id | int(11) | YES | | NULL | | +| dept_name | varchar(100) | YES | | NULL | | ++-----------+--------------+------+-----+---------+-------+ +2 rows in set (0.00 sec) +``` + +模拟插入数据: + +```shell +MySQL [company]> insert into department6 values + (200,'hr'), + (201,'it'), + (202,'sale'), + (203,'fd'); +``` + +查看数据: + +```shell +MySQL [company]> select * from department6; ++---------+-----------+ +| dept_id | dept_name | ++---------+-----------+ +| 200 | hr | +| 201 | it | +| 202 | sale | +| 203 | fd | ++---------+-----------+ +4 rows in set (0.01 sec) +``` + +#### 2.多表的连接查询 + +​ 交叉连接:生成笛卡尔积,它不使用任何匹配条件;交叉联接返回左表中的所有行,左表中的每一行与右表中的所有行组合 + +​ 内连接:只连接匹配的行 + +​ 外连接 + +​ 左连接:会显示左边表内所有的值,不论在右边表内匹不匹配 + +​ 右连接:会显示右边表内所有的值,不论在左边表内匹不匹配 + +​ 全外连接:包含左、右两个表的全部行 + +##### 交叉连接 + +```shell +MySQL [company]> select employee6.emp_name,employee6.age,employee6.dept_id,department6.dept_name from employee6,department6; ++----------+------+---------+-----------+ +| emp_name | age | dept_id | dept_name | ++----------+------+---------+-----------+ +| | 19 | 200 | hr | +| | 19 | 200 | it | +| | 19 | 200 | sale | +| | 19 | 200 | fd | +| tom | 26 | 201 | hr | +| tom | 26 | 201 | it | +| tom | 26 | 201 | sale | +| tom | 26 | 201 | fd | +| jack | 30 | 201 | hr | +| jack | 30 | 201 | it | +| jack | 30 | 201 | sale | +| jack | 30 | 201 | fd | +| alice | 24 | 202 | hr | +| alice | 24 | 202 | it | +| alice | 24 | 202 | sale | +| alice | 24 | 202 | fd | +| robin | 40 | 200 | hr | +| robin | 40 | 200 | it | +| robin | 40 | 200 | sale | +| robin | 40 | 200 | fd | +| xingdian | 16 | 200 | hr | +| xingdian | 16 | 200 | it | +| xingdian | 16 | 200 | sale | +| xingdian | 16 | 200 | fd | +| natasha | 28 | 204 | hr | +| natasha | 28 | 204 | it | +| natasha | 28 | 204 | sale | +| natasha | 28 | 204 | fd | ++----------+------+---------+-----------+ +28 rows in set (0.00 sec) +``` + +##### 内连接 + +​ 获取有部门的员工 (部门表中没有natasha所在的部门) + +```shell +MySQL [company]> select employee6.emp_name,employee6.age,employee6.dept_id,department6.dept_name from employee6,department6 where employee6.dept_id=department6.dept_id; ++----------+------+---------+-----------+ +| emp_name | age | dept_id | dept_name | ++----------+------+---------+-----------+ +| | 19 | 200 | hr | +| tom | 26 | 201 | it | +| jack | 30 | 201 | it | +| alice | 24 | 202 | sale | +| robin | 40 | 200 | hr | +| xingdian | 16 | 200 | hr | ++----------+------+---------+-----------+ +6 rows in set (0.00 sec) + +MySQL [company]> select employee6.emp_name,department6.dept_name from employee6 inner join department6 on employee6.dept_id=department6.dept_id; ++----------+-----------+ +| emp_name | dept_name | ++----------+-----------+ +| | hr | +| tom | it | +| jack | it | +| alice | sale | +| robin | hr | +| xingdian | hr | ++----------+-----------+ +6 rows in set (0.01 sec) +``` + +##### 外连接 + +语法: + +​ SELECT 字段列表 FROM 表1 LEFT|RIGHT JOIN 表2 ON 表1.字段 = 表2.字段; + +注意: + +​ 先用谁谁就是左 + +###### 左连接 left join + +```shell +找出所有员工及所属的部门,包括没有部门的员工 +MySQL [company]> select emp_id,emp_name,dept_name from employee6 left join department6 on employee6.dept_id = department6.dept_id; ++--------+----------+-----------+ +| emp_id | emp_name | dept_name | ++--------+----------+-----------+ +| 1 | | hr | +| 5 | robin | hr | +| 6 | xingdian | hr | +| 2 | tom | it | +| 3 | jack | it | +| 4 | alice | sale | +| 7 | natasha | NULL | ++--------+----------+-----------+ +7 rows in set (0.00 sec) +``` + +###### 右连接right join + +``` +找出所有部门包含的员工,包括空部门 +MySQL [company]> select emp_id,emp_name,dept_name from employee6 right join department6 on employee6.dept_id = department6.dept_id; ++--------+----------+-----------+ +| emp_id | emp_name | dept_name | ++--------+----------+-----------+ +| 1 | | hr | +| 2 | tom | it | +| 3 | jack | it | +| 4 | alice | sale | +| 5 | robin | hr | +| 6 | xingdian | hr | +| NULL | NULL | fd | ++--------+----------+-----------+ +7 rows in set (0.00 sec) +``` + +##### 全外连接 + +```shell +MySQL [company]> select * from employee6 full join department6; ++--------+----------+------+---------+---------+-----------+ +| emp_id | emp_name | age | dept_id | dept_id | dept_name | ++--------+----------+------+---------+---------+-----------+ +| 1 | | 19 | 200 | 200 | hr | +| 1 | | 19 | 200 | 201 | it | +| 1 | | 19 | 200 | 202 | sale | +| 1 | | 19 | 200 | 203 | fd | +| 2 | tom | 26 | 201 | 200 | hr | +| 2 | tom | 26 | 201 | 201 | it | +| 2 | tom | 26 | 201 | 202 | sale | +| 2 | tom | 26 | 201 | 203 | fd | +| 3 | jack | 30 | 201 | 200 | hr | +| 3 | jack | 30 | 201 | 201 | it | +| 3 | jack | 30 | 201 | 202 | sale | +| 3 | jack | 30 | 201 | 203 | fd | +| 4 | alice | 24 | 202 | 200 | hr | +| 4 | alice | 24 | 202 | 201 | it | +| 4 | alice | 24 | 202 | 202 | sale | +| 4 | alice | 24 | 202 | 203 | fd | +| 5 | robin | 40 | 200 | 200 | hr | +| 5 | robin | 40 | 200 | 201 | it | +| 5 | robin | 40 | 200 | 202 | sale | +| 5 | robin | 40 | 200 | 203 | fd | +| 6 | xingdian | 16 | 200 | 200 | hr | +| 6 | xingdian | 16 | 200 | 201 | it | +| 6 | xingdian | 16 | 200 | 202 | sale | +| 6 | xingdian | 16 | 200 | 203 | fd | +| 7 | natasha | 28 | 204 | 200 | hr | +| 7 | natasha | 28 | 204 | 201 | it | +| 7 | natasha | 28 | 204 | 202 | sale | +| 7 | natasha | 28 | 204 | 203 | fd | ++--------+----------+------+---------+---------+-----------+ +28 rows in set (0.00 sec) +``` + +#### 3.复合条件连接查询 + +##### 案例一 + +​ 找出公司所有部门中年龄大于25岁的员工 + +​ 以内连接的方式查询employee6和department6表,并且employee6表中的age字段值必须大于25 + +```shell +MySQL [company]> select emp_id,emp_name,dept_name FROM employee6,department6 WHERE employee6.dept_id = department6.dept_id AND age > 25; ++--------+----------+-----------+ +| emp_id | emp_name | dept_name | ++--------+----------+-----------+ +| 5 | robin | hr | +| 2 | tom | it | +| 3 | jack | it | ++--------+----------+-----------+ +3 rows in set (0.01 sec) +``` + +##### 案例二 + +​ 以内连接的方式查询employee6和department6表,并且以age字段的升序方式显示 + +```shell +MySQL [company]> select emp_id,emp_name,dept_name FROM employee6,department6 WHERE employee6.dept_id = department6.dept_id ORDER BY age asc; ++--------+----------+-----------+ +| emp_id | emp_name | dept_name | ++--------+----------+-----------+ +| 6 | xingdian | hr | +| 1 | | hr | +| 4 | alice | sale | +| 2 | tom | it | +| 3 | jack | it | +| 5 | robin | hr | ++--------+----------+-----------+ +``` + +#### 4.子查询 + +​ 子查询是将一个查询语句嵌套在另一个查询语句中 + +​ 内层查询语句的查询结果,可以为外层查询语句提供查询条件 + +​ 子查询中可以包含:IN、NOT IN等关键字;还可以包含比较运算符:= 、 !=、> 、<等 + +##### 案例一 + +​ 带IN关键字的子查询;查询employee表,但dept_id必须在department表中出现过 + +```shell +MySQL [company]> select * from employee6 WHERE dept_id IN (select dept_id FROM department6); ++--------+----------+------+---------+ +| emp_id | emp_name | age | dept_id | ++--------+----------+------+---------+ +| 1 | | 19 | 200 | +| 2 | tom | 26 | 201 | +| 3 | jack | 30 | 201 | +| 4 | alice | 24 | 202 | +| 5 | robin | 40 | 200 | +| 6 | xingdian | 16 | 200 | ++--------+----------+------+---------+ +6 rows in set (0.00 sec) +``` + +##### 案例二 + +​ 带比较运算符的子查询;查询年龄大于等于25岁员工所在部门(查询老龄化的部门) + +```shell +MySQL [company]> select dept_id,dept_name FROM department6 WHERE dept_id IN (SELECT DISTINCT dept_id FROM employee6 WHERE age >=25); ++---------+-----------+ +| dept_id | dept_name | ++---------+-----------+ +| 201 | it | +| 200 | hr | ++---------+-----------+ +2 rows in set (0.00 sec) +``` \ No newline at end of file diff --git a/第二章:数据库管理及数据类型.md b/第二章:数据库管理及数据类型.md new file mode 100644 index 0000000..db37e92 --- /dev/null +++ b/第二章:数据库管理及数据类型.md @@ -0,0 +1,861 @@ +

数据库管理及数据类型

+ + + +------ + +## 一:数据类型 + +#### 1.数值类型 + +##### 整数类型 + +​ 整数类型:TINYINT SMALLINT MEDIUMINT INT BIGINT + +​ 作用:用于存储用户的年龄、游戏的Level、经验值等 + +![image-20220920124821420](https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20220920124821420.png) + +##### 浮点数类型 + +​ 浮点数类型:FLOAT DOUBLE + +​ 作用:用于存储用户的身高、体重、薪水等 + +![image-20220920124919373](https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20220920124919373.png) + +```shell +float(5,3) 5宽度 3精度 +注意: + 宽度不算小数点 + 宽度-精度=点前 +案例: +MySQL [(none)]> create database diandian; +Query OK, 1 row affected (0.00 sec) + +MySQL [(none)]> use diandian +Database changed +MySQL [diandian]> create table t1(id float(6,2)); +Query OK, 0 rows affected (0.24 sec) + +MySQL [diandian]> insert into t1 values('2.22'); +``` + +##### 定点数类型 + +​ 定点数类型:DEC + +​ 定点数在MySQL内部以字符串形式存储,比浮点数更精确,适合用来表示货币等精度高的数据 + +##### 位类型 + +​ 位类型:BIT + +​ BIT(M)可以用来存放多位二进制数,M范围从1~64,如果不写默认为1位 + +#### 2.字符串类型 + +​ CHAR系列 CHAR VARCHAR + +​ TEXT系列 TINYTEXT TEXT MEDIUMTEXT LONGTEXT + +​ BLOB 系列 TINYBLOB BLOB MEDIUMBLOB LONGBLOB + +​ BINARY系列 BINARY VARBINARY + +![image-20220920132114919](https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20220920132114919.png) + +##### 枚举类型 + +​ 枚举类型:枚举列可以把一些不重复的字符串存储成一个预定义的集合 + +```shell +mysql> create table enum_table( e ENUM('fish','apple','dog')); +Query OK, 0 rows affected (0.35 sec) +mysql> insert into enum_table(e) values('fish'); +Query OK, 1 row affected (0.11 sec) + +mysql> select * from enum_table; ++------+ +| e | ++------+ +| fish | ++------+ +1 row in set (0.00 sec) + +mysql> insert into enum_table(e) values('nihao'); +ERROR 1265 (01000): Data truncated for column 'e' at row 1 +``` + +##### 时间和日期类型 + +​ 时间和日期类型:DATE TIME DATETIME TIMESTAMP YEAR + +​ 作用:用于存储用户的注册时间,文章的发布时间,文章的更新时间,员工的入职时间等 + +![image-20220920132630856](https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20220920132630856.png) + +```shell +mysql> create table t8 ( + id1 timestamp NOT NULL default CURRENT_TIMESTAMP, + id2 datetime default NULL +); + + timestamp 类型的列还有个特性:默认情况下,在 insert, update 数据时,timestamp 列会自动以当前时间(CURRENT_TIMESTAMP)填充/更新。“自动”的意思就是,你不去管它,MySQL 会替你去处理。 + +mysql> insert into t8(id1) values('20180109000000'); +mysql> select * from t8; ++---------------------+------+ +| id1 | d2 | ++---------------------+------+ +| 2018-01-09 00:00:00 | NULL | ++---------------------+------+ +1 row in set (0.00 sec) + +扩展: +select now();查看当前时间 +``` + +## 二:表操作 + +#### 1.案例 + +表::school.student1 + +``` +字段 字段 字段 +id name sex age +1 tom male 23 记录 +2 jack male 21 记录 +3 alice female 19 记录 +``` + +语法: + +```shell +create table 表名(自定义)( + 字段名1 类型[(宽度) 约束条件], + 字段名2 类型[(宽度) 约束条件], + 字段名3 类型[(宽度) 约束条件] +)[存储引擎 字符集]; +==在同一张表中,字段名是不能相同 +==宽度和约束条件可选 +==字段名和类型是必须的 +``` + +创建库表: + +```shell +mysql> CREATE DATABASE school; //创建数据库school +mysql> use school; +mysql> create table student1( + id int, + name varchar(50), + sex enum('m','f'), + age int + ); +Query OK, 0 rows affected (0.03 sec) +``` + +查看库: + +```shell +mysql> show tables; ++------------------+ +| Tables_in_school | ++------------------+ +| student1 | ++------------------+ +1 row in set (0.00 sec) +``` + +插入语法: + +``` +insert into 表名(字段1,字段2...) values(字段值列表...); +``` + +插入数据: + +```shell +mysql> insert into student1(id,name,sex,age) values(1,'xingdia','m','26'); +``` + +查看表结构: + +```shell +mysql> desc student1; ++-------+---------------+------+-----+---------+-------+ +| Field | Type | Null | Key | Default | Extra | ++-------+---------------+------+-----+---------+-------+ +| id | int(11) | YES | | NULL | | +| name | varchar(50) | YES | | NULL | | +| sex | enum('m','f') | YES | | NULL | | +| age | int(11) | YES | | NULL | | ++-------+---------------+------+-----+---------+-------+ +4 rows in set (0.00 sec) +``` + +查询数据: + +```shell +mysql> select id,name,sex,age from student1; //查询表中所有字段的值 +Empty set (0.00 sec) + +mysql> select * from student1; /查询表中所有字段的值 +Empty set (0.00 sec) + +mysql> select name,age from student1; //查询表中指定字段的值 +Empty set (0.00 sec) +``` + +扩展插入: + +```shell +mysql> insert into student1 values (1,'xingdian','m',33),(2,'alice','m',20),(3,'jack','m',40); //顺序插入 +Query OK, 3 rows affected (0.14 sec) +Records: 3 Duplicates: 0 Warnings: 0 + +mysql> insert into student1(name,age) values ('zhuzhu',10),('gougou',20); //只向指定的字段插入值 +Query OK, 2 rows affected (0.12 sec) +Records: 2 Duplicates: 0 Warnings: 0 +``` + +#### 2.案例 + +表:school.student2 + +```shell + 字段名 数据类型 +编号 id int +姓名 name varchar(50) +出生年份 born_year year +生日 birthday date +上课时间 class_time time +注册时间 reg_time datetime +``` + +创建表: + +```shell +mysql> create table student2( + id int, + name varchar(50), + born_year year, + birthday date, + class_time time, + reg_time datetime + ); +``` + +插入数据: + +```shell +mysql> insert into student2 values(1,'tom',now(),now(),now(),now()); +mysql> insert into student2 values(2,'jack',1982,19821120,123000,20140415162545); +``` + +表:school.student3 + +```shell +id id int +姓名 name varchar(50) +性别 sex enum('male','female') +爱好 hobby set('music','book','game','disc') +``` + +创建表: + +```shell +mysql> create table student3( + id int, + name varchar(50), + sex enum('male','female'), + hobby set('music','book','game','disc') + ); +``` + +查看表结构: + +```shell +mysql> desc student3; +mysql> show create table student3\G +``` + +插入数据: + +```shell +mysql> insert into student3 values (1,'tom','male','book,game'); +mysql> insert into student3 values (2,'jack','male','film'); +``` + +注意: + +​ DESCRIBE查看表结构 + +```shell +DESCRIBE 表名; +DESC 表名; +``` + +​ 查看表详细结构 + +```shell +SHOW CREATE TABLE 表名; +``` + +## 三:表完整性约束 + +#### 1.作用 + +​ 用于保证数据的完整性和一致性 + +#### 2.约束条件 + +PRIMARY KEY (PK) 标识该字段为该表的主键,可以唯一的标识记录,不可以为空 UNIQUE + NOT NULL + +FOREIGN KEY (FK) 标识该字段为该表的外键,实现表与表(父表主键/子表1外键/子表2外键)之间的关联 + +NOT NULL 标识该字段不能为空 + +UNIQUE KEY (UK) 标识该字段的值是唯一的,可以为空,一个表中可以有多个UNIQUE KEY + +AUTO_INCREMENT 标识该字段的值自动增长(整数类型,而且为主键) + +DEFAULT 为该字段设置默认值 + +注意: + +​ 是否允许为空,默认NULL,可设置NOT NULL,字段不允许为空,必须赋值 + +​ 字段是否有默认值,缺省的默认值是NULL,如果插入记录时不给字段赋值,此字段使用默认值 + +```shell +MySQL [(none)]> sex enum('male','female') not null default 'male' +MySQL [(none)]> age int unsigned NOT NULL default 20 必须为正值(无符号) 不允许为空 默认是20 +``` + +​ 是否是key 主键 primary key 外键 forengn key + +#### 3.NOT NULL + +表:school.student4 + +创建表:(注意前提需要有库) + +```shell +mysql> create table school.student4( + id int not null, + name varchar(50) not null, + sex enum('m','f') default 'm' not null, + age int unsigned default 18 not null, + hobby set('music','disc','dance','book') default 'book,dance' + ); +``` + +插入数据:(注意观察查询到的数据) + +```shell +MySQL [(none)]> insert into school.student4(id,name) values(2,'robin'); +Query OK, 1 row affected (0.08 sec) + +MySQL [(none)]> select * from school.student4; ++----+-------+-----+-----+------------+ +| id | name | sex | age | hobby | ++----+-------+-----+-----+------------+ +| 2 | robin | m | 18 | dance,book | ++----+-------+-----+-----+------------+ +1 row in set (0.00 sec) +``` + +注意报错的原因: + +```shell +MySQL [(none)]> insert into school.student4 values(3,NULL,'m',40,'book'); +ERROR 1048 (23000): Column 'name' cannot be null +``` + +#### 4.唯一约束 + +作用: + +​ MySQL索引的建立对于MySQL的高效运行是很重要的,索引可以大大提高MySQL的检索速度 + +表:company.department1 + +创建表: + +```shell +MySQL [(none)]> create database company; +Query OK, 1 row affected (0.01 sec) +MySQL [(none)]> CREATE TABLE company.department1 (dept_id INT,dept_name VARCHAR(30) UNIQUE,comment VARCHAR(50)); +Query OK, 0 rows affected (0.34 sec) +``` + +查看表结构: + +```shell +MySQL [(none)]> desc company.department1; ++-----------+-------------+------+-----+---------+-------+ +| Field | Type | Null | Key | Default | Extra | ++-----------+-------------+------+-----+---------+-------+ +| dept_id | int(11) | YES | | NULL | | +| dept_name | varchar(30) | YES | UNI | NULL | | +| comment | varchar(50) | YES | | NULL | | ++-----------+-------------+------+-----+---------+-------+ +3 rows in set (0.00 sec) +``` + +数据插入:(注意查看插入数据时的提示) + +```shell +MySQL [(none)]> insert into company.department1 values ("1","xingdian","hr"); +Query OK, 1 row affected (0.04 sec) + +MySQL [(none)]> insert into company.department1 values ("1","xingdian","hr"); +ERROR 1062 (23000): Duplicate entry 'xingdian' for key 'dept_name' +``` + +#### 5.主键约束 + +注意:primary key 字段的值是不允许重复,且不允许不NULL(UNIQUE + NOT NULL) + +表:school.student6 + +创建表: + +```shell +MySQL [(none)]> create table school.student6( + id int primary key not null auto_increment, + name varchar(50) not null, + sex enum('male','female') not null default 'male', + age int not null default 18 + ); +Query OK, 0 rows affected (0.47 sec) +``` + +插入数据: + +```shell +MySQL [(none)]> insert into school.student6 values (1,'alice','female',22); +Query OK, 1 row affected (0.18 sec) + +MySQL [(none)]> insert into school.student6(name,sex,age) values + -> ('jack','male',19), + -> ('tom','male',23); +Query OK, 2 rows affected (0.14 sec) +Records: 2 Duplicates: 0 Warnings: 0 + +MySQL [(none)]> select * from school.student6; ++----+-------+--------+-----+ +| id | name | sex | age | ++----+-------+--------+-----+ +| 1 | alice | female | 22 | +| 2 | jack | male | 19 | +| 3 | tom | male | 23 | ++----+-------+--------+-----+ +3 rows in set (0.00 sec) + +``` + +## 四:修改表 + +语法格式: + +修改表名 + +​ ALTER TABLE 表名 RENAME 新表名; + +增加字段 + +​ ALTER TABLE 表名 + +​ ADD 字段名 数据类型 [完整性约束条件…], + +​ ADD 字段名 数据类型 [完整性约束条件…]; + +​ ALTER TABLE 表名 + +​ ADD 字段名 数据类型 [完整性约束条件…] AFTER 字段名; +删除字段 + +​ ALTER TABLE 表名 DROP 字段名; + +修改字段 + +​ ALTER TABLE 表名 MODIFY 字段名 数据类型 [完整性约束条件…]; + +​ ALTER TABLE 表名 CHANGE 旧字段名 新字段名 旧数据类型 [完整性约束条件…]; + +​ ALTER TABLE 表名 CHANGE 旧字段名 新字段名 新数据类型 [完整性约束条件…]; + +#### 1.修改数据库引擎 + +```shell +mysql> alter table service engine=innodb; //engine=myisam|memory|.... +``` + +#### 2.添加字段 + +```shell +mysql> create table student10 (id int); +mysql> alter table student10 add name varchar(20) not null, add age int not null default 22; + +mysql> alter table student10 add stu_num int not null after name; //添加name字段之后 + +mysql> alter table student10 add sex enum('male','female') default 'male' first; //添加到最前面 +``` + +#### 3.删除字段 + +```shell +mysql> alter table student10 drop sex; +``` + +#### 4.修改字段类型 + +```shell +MySQL [school]> desc student10; ++-------+---------+------+-----+---------+-------+ +| Field | Type | Null | Key | Default | Extra | ++-------+---------+------+-----+---------+-------+ +| id | int(11) | YES | | NULL | | +| age | int(11) | YES | | NULL | | ++-------+---------+------+-----+---------+-------+ +2 rows in set (0.01 sec) + +MySQL [school]> alter table student10 modify age tinyint not null ; +Query OK, 0 rows affected (0.04 sec) +Records: 0 Duplicates: 0 Warnings: 0 + +MySQL [school]> desc student10; ++-------+------------+------+-----+---------+-------+ +| Field | Type | Null | Key | Default | Extra | ++-------+------------+------+-----+---------+-------+ +| id | int(11) | YES | | NULL | | +| age | tinyint(4) | NO | | NULL | | ++-------+------------+------+-----+---------+-------+ +2 rows in set (0.00 sec) + +MySQL [school]> alter table student10 modify id int not null primary key ; ////修改字段类型、约束、主键 +Query OK, 0 rows affected (0.02 sec) +Records: 0 Duplicates: 0 Warnings: 0 + +MySQL [school]> desc student10; ++-------+------------+------+-----+---------+-------+ +| Field | Type | Null | Key | Default | Extra | ++-------+------------+------+-----+---------+-------+ +| id | int(11) | NO | PRI | NULL | | +| age | tinyint(4) | NO | | NULL | | ++-------+------------+------+-----+---------+-------+ +``` + +#### 5.增加约束 + +```shell +MySQL [school]> alter table student10 modify id int not null primary key ; +Query OK, 0 rows affected (0.02 sec) +Records: 0 Duplicates: 0 Warnings: 0 + +MySQL [school]> desc student10; ++-------+------------+------+-----+---------+-------+ +| Field | Type | Null | Key | Default | Extra | ++-------+------------+------+-----+---------+-------+ +| id | int(11) | NO | PRI | NULL | | +| age | tinyint(4) | NO | | NULL | | ++-------+------------+------+-----+---------+-------+ +2 rows in set (0.00 sec) + +MySQL [school]> alter table student10 modify id int not null primary key auto_increment; +ERROR 1068 (42000): Multiple primary key defined //错误,该字段已经是primary key + +MySQL [school]> alter table student10 modify id int not null auto_increment; +Query OK, 0 rows affected (0.04 sec) +Records: 0 Duplicates: 0 Warnings: 0 + +MySQL [school]> desc student10; ++-------+------------+------+-----+---------+----------------+ +| Field | Type | Null | Key | Default | Extra | ++-------+------------+------+-----+---------+----------------+ +| id | int(11) | NO | PRI | NULL | auto_increment | +| age | tinyint(4) | NO | | NULL | | ++-------+------------+------+-----+---------+----------------+ +``` + +#### 6.增加主键 + +```shell +MySQL [school]> desc student1; ++-------+---------+------+-----+---------+-------+ +| Field | Type | Null | Key | Default | Extra | ++-------+---------+------+-----+---------+-------+ +| id | int(11) | YES | | NULL | | +| age | int(11) | YES | | NULL | | +| name | char(1) | YES | | NULL | | ++-------+---------+------+-----+---------+-------+ +3 rows in set (0.00 sec) + +MySQL [school]> alter table student1 add primary key(id); + +MySQL [school]> desc student1; ++-------+---------+------+-----+---------+-------+ +| Field | Type | Null | Key | Default | Extra | ++-------+---------+------+-----+---------+-------+ +| id | int(11) | NO | PRI | NULL | | +| age | int(11) | YES | | NULL | | +| name | char(1) | YES | | NULL | | ++-------+---------+------+-----+---------+-------+ +3 rows in set (0.00 sec) +``` + +#### 7.修改主键和自增 + +```shell +MySQL [school]> alter table student1 modify id int auto_increment; +Query OK, 0 rows affected (0.03 sec) +Records: 0 Duplicates: 0 Warnings: 0 + +MySQL [school]> desc student1; ++-------+---------+------+-----+---------+----------------+ +| Field | Type | Null | Key | Default | Extra | ++-------+---------+------+-----+---------+----------------+ +| id | int(11) | NO | PRI | NULL | auto_increment | +| age | int(11) | YES | | NULL | | +| name | char(1) | YES | | NULL | | ++-------+---------+------+-----+---------+----------------+ +``` + +#### 8.删除主键 + +```shell +MySQL [school]> desc student10; ++-------+------------+------+-----+---------+----------------+ +| Field | Type | Null | Key | Default | Extra | ++-------+------------+------+-----+---------+----------------+ +| id | int(11) | NO | PRI | NULL | auto_increment | +| age | tinyint(4) | NO | | NULL | | ++-------+------------+------+-----+---------+----------------+ +2 rows in set (0.00 sec) + +MySQL [school]> alter table student10 drop primary key; +ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key + +删除自增 +ySQL [school]> alter table student10 modify id int not null; +Query OK, 0 rows affected (0.04 sec) +Records: 0 Duplicates: 0 Warnings: 0 + +MySQL [school]> desc student10; ++-------+------------+------+-----+---------+-------+ +| Field | Type | Null | Key | Default | Extra | ++-------+------------+------+-----+---------+-------+ +| id | int(11) | NO | PRI | NULL | | +| age | tinyint(4) | NO | | NULL | | ++-------+------------+------+-----+---------+-------+ + +MySQL [school]> alter table student10 drop primary key; +Query OK, 0 rows affected (0.03 sec) +Records: 0 Duplicates: 0 Warnings: 0 + +MySQL [school]> desc student10; ++-------+------------+------+-----+---------+-------+ +| Field | Type | Null | Key | Default | Extra | ++-------+------------+------+-----+---------+-------+ +| id | int(11) | NO | | NULL | | +| age | tinyint(4) | NO | | NULL | | ++-------+------------+------+-----+---------+-------+ +2 rows in set (0.00 sec) +``` + +#### 9.复制表 + +复制表结构+记录 (key不会复制: 主键、外键和索引)复制表结构/记录+表结构,不会将Key复制 + +```shell +mysql> create table new_service select * from service; +``` + +只复制表结构 + +```shell +mysql> create table new1_service select * from service where 1=2; //条件为假,查不到任何记录 +``` + +可以复制主键,只复制表结构 + +```shell +mysql> create table t4 like employees; +``` + +#### 10.删除表 + +```shell +mysql> DROP TABLE 表名; +``` + +#### 11.修改数据表中字段的值 + +语法: + +​ Update 表名 set 列名=值where 条件 + +```shell +mysql> update student set name='123' where id=1; +``` + +删除某一行: + +​ delete from 表名 where id=1 + +```shell +mysql> delete from type where id=1; +``` + +## 五:库操作 + +#### 1.简介 + +​ 系统自带库的含义及作用 + +```shell +MySQL [(none)]> show databases; ++--------------------+ +| Database | ++--------------------+ +| information_schema | +| mysql | +| performance_schema | +| sys | ++--------------------+ +``` + +information_schema:虚拟库,主要存储了系统中的一些数据库对象的信息,例如用户表信息、列信息、权限信息、字符信息等 + +performance_schema:主要存储数据库服务器的性能参数 + +mysql:授权库,主要存储系统用户的权限信息 + +sys:主要存储数据库服务器的性能参数 + +注意:information_schema + +​ SCHEMATA 存放的是系统中的库 + +```shell +MySQL [information_schema]> select * from information_schema.SCHEMATA; ++--------------+--------------------+----------------------------+------------------------+----------+ +| CATALOG_NAME | SCHEMA_NAME | DEFAULT_CHARACTER_SET_NAME | DEFAULT_COLLATION_NAME | SQL_PATH | ++--------------+--------------------+----------------------------+------------------------+----------+ +| def | information_schema | utf8 | utf8_general_ci | NULL | +| def | mysql | latin1 | latin1_swedish_ci | NULL | +| def | performance_schema | utf8 | utf8_general_ci | NULL | +| def | school | latin1 | latin1_swedish_ci | NULL | +| def | sys | utf8 | utf8_general_ci | NULL | ++--------------+--------------------+----------------------------+------------------------+----------+ + +目录_名称 +实际库_名称 +默认_字符_设置_名称 +默认_分类_名称 +``` + +​ TABLES 存储表名 + +```shell +MySQL [information_schema]> select * from information_schema.TABLES\G +*************************** 283. row *************************** + TABLE_CATALOG: def + TABLE_SCHEMA: xingdian + TABLE_NAME: t1 + TABLE_TYPE: BASE TABLE + ENGINE: InnoDB + VERSION: 10 + ROW_FORMAT: Dynamic + TABLE_ROWS: 1 + AVG_ROW_LENGTH: 16384 + DATA_LENGTH: 16384 +MAX_DATA_LENGTH: 0 + INDEX_LENGTH: 0 + DATA_FREE: 0 + AUTO_INCREMENT: NULL + CREATE_TIME: 2022-09-22 08:18:38 + UPDATE_TIME: 2022-09-22 08:18:54 + CHECK_TIME: NULL +TABLE_COLLATION: latin1_swedish_ci + CHECKSUM: NULL + CREATE_OPTIONS: + TABLE_COMMENT: +283 rows in set (0.02 sec) +``` + +​ COLUMNS 存储字段 + +```shell +*************************** 3083. row *************************** + TABLE_CATALOG: def + TABLE_SCHEMA: xingdian + TABLE_NAME: t1 + COLUMN_NAME: id + ORDINAL_POSITION: 1 + COLUMN_DEFAULT: NULL + IS_NULLABLE: YES + DATA_TYPE: int +CHARACTER_MAXIMUM_LENGTH: NULL + CHARACTER_OCTET_LENGTH: NULL + NUMERIC_PRECISION: 10 + NUMERIC_SCALE: 0 + DATETIME_PRECISION: NULL + CHARACTER_SET_NAME: NULL + COLLATION_NAME: NULL + COLUMN_TYPE: int(11) + COLUMN_KEY: + EXTRA: + PRIVILEGES: select,insert,update,references + COLUMN_COMMENT: + GENERATION_EXPRESSION: +3083 rows in set (0.03 sec) +``` + +#### 2.创建库 + +方案一:交互式操作 + +```shell +mysql> create database xingdian; +``` + +数据库命名规则: + +​ 区分大小写 + +​ 唯一性 + +​ 不能使用关键字如 create select + +​ 不能单独使用数字 + +方案二:非交互式 + +```shell +mysql -u root -pQianFeng@123 -e "create database diandian" +``` + +#### 3.查看数据库 + +```shell +mysql> show databases; +mysql> show create database xingdian; +mysql> select database(); 查看当前所在的库 +``` + +#### 4.切换数据库 + +```shell +mysql> use xingdian; +``` + +#### 5.删除数据库 + +```shell +mysql> DROP DATABASE 数据库名; +``` diff --git a/第五章:权限管理及数据备份与恢复.md b/第五章:权限管理及数据备份与恢复.md new file mode 100644 index 0000000..3cb6c07 --- /dev/null +++ b/第五章:权限管理及数据备份与恢复.md @@ -0,0 +1,1228 @@ +

权限管理及数据备份与恢复

+ + + +------ + +## 一:权限管理 + +#### 1.权限级别 + +​ Global level:系统级,所有库,所有表的权限 + +​ Database level:某个数据库中的所有表的权限 + +​ Table level:库中的某个表的权限 + +​ Column level:表中的某个字段的权限 + +​ procs level:某个存储过程的权限 + +​ proxies level:代理服务器的权限 + +#### 2.查看权限记录表 + +​ 因为超级管理员默认已经设置;所以直接查询权限即可 + +##### Global level + +``` + mysql> select * from mysql.user\G +*************************** 1. row *************************** + Host: localhost + User: root + Select_priv: Y + Insert_priv: Y + Update_priv: Y + Delete_priv: Y + Create_priv: Y + Drop_priv: Y + Reload_priv: Y + Shutdown_priv: Y + Process_priv: Y + File_priv: Y + Grant_priv: Y + References_priv: Y + Index_priv: Y + Alter_priv: Y + Show_db_priv: Y + Super_priv: Y + Create_tmp_table_priv: Y + Lock_tables_priv: Y + Execute_priv: Y + Repl_slave_priv: Y + Repl_client_priv: Y + Create_view_priv: Y + Show_view_priv: Y + Create_routine_priv: Y + Alter_routine_priv: Y + Create_user_priv: Y + Event_priv: Y + Trigger_priv: Y +Create_tablespace_priv: Y + ssl_type: + ssl_cipher: + x509_issuer: + x509_subject: + max_questions: 0 + max_updates: 0 + max_connections: 0 + max_user_connections: 0 + plugin: mysql_native_password + authentication_string: *B1DD4ADE47888D9AEC4D705C85230F1B52D2A817 + password_expired: N + password_last_changed: 2022-09-25 14:44:38 + password_lifetime: NULL + account_locked: N +``` + +字段介绍: + +```shell +用户字段:root +权限字段:Select_priv +安全字段:*B1DD4ADE47888D9AEC4D705C85230F1B52D2A817 + +Select_priv:查询权限 +Insert_priv:插入权限 +Update_priv:更新权限 +Delete_priv:删除权限 +...... +``` + +##### Database level + +```shell +mysql> select * from mysql.db\G; +*************************** 1. row *************************** + Host: localhost + Db: performance_schema + User: mysql.session + Select_priv: Y + Insert_priv: N + Update_priv: N + Delete_priv: N + Create_priv: N + Drop_priv: N + Grant_priv: N + References_priv: N + Index_priv: N + Alter_priv: N +Create_tmp_table_priv: N + Lock_tables_priv: N + Create_view_priv: N + Show_view_priv: N + Create_routine_priv: N + Alter_routine_priv: N + Execute_priv: N + Event_priv: N + Trigger_priv: N +``` + +测试库权限: + +```shell +mysql> create database t1; +Query OK, 1 row affected (0.00 sec) + +mysql> grant all on t1.* to 't1'@'localhost' identified by 'QianFeng@123'; +Query OK, 0 rows affected, 1 warning (0.00 sec) +``` + +查看: + +``` +mysql> select * from mysql.db\G +*************************** 3. row *************************** + Host: localhost + Db: t1 + User: t1 + Select_priv: Y + Insert_priv: Y + Update_priv: Y + Delete_priv: Y + Create_priv: Y + Drop_priv: Y + Grant_priv: N + References_priv: Y + Index_priv: Y + Alter_priv: Y +Create_tmp_table_priv: Y + Lock_tables_priv: Y + Create_view_priv: Y + Show_view_priv: Y + Create_routine_priv: Y + Alter_routine_priv: Y + Execute_priv: Y + Event_priv: Y + Trigger_priv: Y +3 rows in set (0.00 sec) +``` + +验证: + +```shell +[root@xingdian ~]# mysql -u t1 -pQianFeng@123 +mysql: [Warning] Using a password on the command line interface can be insecure. +Welcome to the MySQL monitor. Commands end with ; or \g. +Your MySQL connection id is 4 +Server version: 5.7.39-log MySQL Community Server (GPL) + +Copyright (c) 2000, 2022, Oracle and/or its affiliates. + +Oracle is a registered trademark of Oracle Corporation and/or its +affiliates. Other names may be trademarks of their respective +owners. + +Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. + +mysql> show databases; ++--------------------+ +| Database | ++--------------------+ +| information_schema | +| t1 | ++--------------------+ +2 rows in set (0.00 sec) + +``` + +##### Table level + +```shell +mysql> select * from mysql.tables_priv\G; +*************************** 1. row *************************** + Host: localhost + Db: mysql + User: mysql.session + Table_name: user + Grantor: boot@connecting host + Timestamp: 0000-00-00 00:00:00 + Table_priv: Select +Column_priv: +*************************** 2. row *************************** + Host: localhost + Db: sys + User: mysql.sys + Table_name: sys_config + Grantor: root@localhost + Timestamp: 2022-09-25 14:40:58 + Table_priv: Select +Column_priv: +2 rows in set (0.00 sec) +``` + +创建库表验证: + +```shell +mysql> create database t2; +Query OK, 1 row affected (0.00 sec) + +mysql> use t2; +Database changed +mysql> create table u1(id int); +Query OK, 0 rows affected (0.01 sec) + +mysql> insert into u1 values (1); +Query OK, 1 row affected (0.01 sec) + +mysql> grant all on t2.u1 to 't2'@'localhost' identified by 'QianFeng@123'; +Query OK, 0 rows affected, 1 warning (0.00 sec) + +mysql> create table u2(id int); +Query OK, 0 rows affected (0.01 sec) + +mysql> show tables; ++--------------+ +| Tables_in_t2 | ++--------------+ +| u1 | +| u2 | ++--------------+ +2 rows in set (0.00 sec) +``` + +权限查看: + +```shell +mysql> select * from mysql.tables_priv\G; +*************************** 3. row *************************** + Host: localhost + Db: t2 + User: t2 + Table_name: u1 + Grantor: root@localhost + Timestamp: 0000-00-00 00:00:00 + Table_priv: Select,Insert,Update,Delete,Create,Drop,References,Index,Alter,Create View,Show view,Trigger +Column_priv: +3 rows in set (0.00 sec) +``` + +验证:(登录t2账户,看到u1表,看不到u2代表权限成功) + +```shell +[root@xingdian ~]# mysql -u t2 -pQianFeng@123 +mysql: [Warning] Using a password on the command line interface can be insecure. +Welcome to the MySQL monitor. Commands end with ; or \g. +Your MySQL connection id is 6 +Server version: 5.7.39-log MySQL Community Server (GPL) + +Copyright (c) 2000, 2022, Oracle and/or its affiliates. + +Oracle is a registered trademark of Oracle Corporation and/or its +affiliates. Other names may be trademarks of their respective +owners. + +Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. + +mysql> show databases; ++--------------------+ +| Database | ++--------------------+ +| information_schema | +| t2 | ++--------------------+ +2 rows in set (0.00 sec) + +mysql> use t2; +Reading table information for completion of table and column names +You can turn off this feature to get a quicker startup with -A + +Database changed +mysql> show tables; ++--------------+ +| Tables_in_t2 | ++--------------+ +| u1 | ++--------------+ +1 row in set (0.00 sec) +``` + +##### Column level + +```shell +[root@xingdian ~]# mysql -uroot -pQianFeng@123 +mysql: [Warning] Using a password on the command line interface can be insecure. +Welcome to the MySQL monitor. Commands end with ; or \g. +Your MySQL connection id is 7 +Server version: 5.7.39-log MySQL Community Server (GPL) + +Copyright (c) 2000, 2022, Oracle and/or its affiliates. + +Oracle is a registered trademark of Oracle Corporation and/or its +affiliates. Other names may be trademarks of their respective +owners. + +Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. + +mysql> select * from mysql.columns_priv\G; +Empty set (0.00 sec) + +mysql> insert into mysql.columns_priv(host,db,user,table_name,column_name,column_priv) values('%','t2','t2','u1','id','select'); +Query OK, 1 row affected (0.00 sec) + +mysql> select * from mysql.columns_priv\G; +*************************** 1. row *************************** + Host: % + Db: t2 + User: t2 + Table_name: u1 +Column_name: id + Timestamp: 2022-09-25 15:34:05 +Column_priv: Select +1 row in set (0.00 sec) +``` + +注意: + +​ 前提是有库,有表,有权限 + +## 二:用户管理 + +#### 1.登录和退出 + +```shell +[root@xingdian ~]# mysql -h 192.168.18.160 -P 30042 -u root -pmysql -e "show databases;" +[root@xingdian ~]# mysql -h 192.168.18.160 -P 30042 -u root -pmysql mysql -e "show tables;" + + -h 指定主机名 【默认为localhost】 + -P MySQL服务器端口 【默认3306】 + -u 指定用户名 【默认root】 + -p 指定登录密码 【默认为空密码】 + 此处mysql为指定登录的数据库 + -e 接SQL语句 (在脚本中使用) + +``` + +#### 2.创建用户 + +方式一: + +```shell +mysql> create user xingdian; +ERROR 1819 (HY000): Your password does not satisfy the current policy requirements +注意: + 该报错是因为密码强度问题,取消密码强度即可创建用户 + +mysql> create user xingdian@'%' identified by 'QianFeng@123'; +Query OK, 0 rows affected (0.00 sec) +``` + +方式二: + +```shell +mysql> grant all on *.* to 'diange'@'localhost' identified by 'QianFeng@123'; +Query OK, 0 rows affected, 1 warning (0.00 sec) +``` + +注意: + +​ 该方式采用授权的方式 + +​ ALL 所有权限 select 单独某一个权限(多个权限用逗号隔开) + +```shell +mysql> grant select on *.* to 'dianye'@'localhost' identified by 'QianFeng@123'; +Query OK, 0 rows affected, 1 warning (0.00 sec) +``` + +```shell +*.* 所有的库所有的表 也可以单独某一个库某一个表 +``` + +```shell +xingdian@localhost 用户有则授权无则创建 localhost % 10.19.40.% 10.19.40.11 +``` + +#### 3.删除用户 + +方式一: + +```shell +MySQL [(none)]> Drop user xingdian@'%'; +Query OK, 0 rows affected (0.00 sec) +``` + +方法二: + +```shell +MySQL [(none)]> delete from mysql.user where user='diandian' AND Host='%'; +Query OK, 1 row affected (0.00 sec) +``` + +#### 4.修改密码 + +方式一: + +```shell +[root@xingdian ~]# mysqladmin -uroot -p'123' password 'new_password' //123为旧密码 +``` + +方式二: + +```shell +MySQL [(none)]> update mysql.user set authentication_string=password(123456) where user='diange' And Host='%'; +``` + +注意: + +​ 刷新授权表后生效:flush privileges + +自己设置自己密码: + +```shell +MySQL [(none)]> set password='123'; +Query OK, 0 rows affected (0.00 sec) +``` + +root用户修改其他用户密码: + +方法一: + +```shell +mysql> SET PASSWORD FOR user3@'localhost'='new_password'; +``` + +方法二: + +```shell +UPDATE mysql.user SET authentication_string=password('new_password') WHERE user='user3' AND host='localhost'; +``` + +#### 5.查看密码策略 + +```shell +mysql> SHOW VARIABLES LIKE 'validate_password%'; ++--------------------------------------+--------+ +| Variable_name | Value | ++--------------------------------------+--------+ +| validate_password_check_user_name | OFF | +| validate_password_dictionary_file | | +| validate_password_length | 8 | +| validate_password_mixed_case_count | 1 | +| validate_password_number_count | 1 | +| validate_password_policy | MEDIUM | +| validate_password_special_char_count | 1 | ++--------------------------------------+--------+ +7 rows in set (0.00 sec) +``` + +参数解释: + +​ validate_password_dictionary_file 指定密码验证的文件路径 + +​ validate_password_length 密码最小长度 + +​ validate_password_mixed_case_count 密码至少要包含的小写字母个数和大写字母个数 + +​ validate_password_number_count 密码至少要包含的数字个数 + +​ validate_password_policy 密码强度检查等级,对应等级为:0/LOW、1/MEDIUM、2/STRONG,默认为1 + +​ 0/LOW:只检查长度 + +​ 1/MEDIUM:检查长度、数字、大小写、特殊字符 + +​ 2/STRONG:检查长度、数字、大小写、特殊字符字典文件 + +​ validate_password_special_char_count密码至少要包含的特殊字符数 + +修改密码策略: + +```shell +mysql> SHOW VARIABLES LIKE 'validate_password%'; ++--------------------------------------+--------+ +| Variable_name | Value | ++--------------------------------------+--------+ +| validate_password_check_user_name | OFF | +| validate_password_dictionary_file | | +| validate_password_length | 8 | +| validate_password_mixed_case_count | 1 | +| validate_password_number_count | 1 | +| validate_password_policy | MEDIUM | +| validate_password_special_char_count | 1 | ++--------------------------------------+--------+ +7 rows in set (0.00 sec) + +mysql> set global validate_password_length=4; +Query OK, 0 rows affected (0.00 sec) + +mysql> SHOW VARIABLES LIKE 'validate_password%'; ++--------------------------------------+--------+ +| Variable_name | Value | ++--------------------------------------+--------+ +| validate_password_check_user_name | OFF | +| validate_password_dictionary_file | | +| validate_password_length | 4 | +| validate_password_mixed_case_count | 1 | +| validate_password_number_count | 1 | +| validate_password_policy | MEDIUM | +| validate_password_special_char_count | 1 | ++--------------------------------------+--------+ +7 rows in set (0.00 sec) +``` + +关闭密码策略: + +``` +修改配置文件,添加以下参数: +validate_password=off +``` + +## 三:数据备份及恢复 + +#### 1.概述 + +​ 所有备份数据都应放在非数据库本地,而且建议有多份副本 + +备份: 能够防止由于机械故障以及人为误操作带来的数据丢失,例如将数据库文件保存在了其它地方 + +冗余: 数据有多份冗余,但不等备份,只能防止机械故障还来的数据丢失,例如主备模式、数据库集群 + +备份考虑的因素: + +​ 数据的一致性 + +​ 服务的可用性 + +分类: + +​ 逻辑备份 + +​ 备份的是建表、建库、插入等操作所执行SQL语句;适用于中小型数据库,效率相对较低(mysqldump) + +​ 物理备份 + +​ 直接复制数据库文件,适用于大型数据库环境,不受存储引擎的限制,但不能恢复到不同的MySQL版本(tar、xtrabackup) + +备份方式分类: + +​ 完全备份 + +​ 备份所有数据 + +​ 增量备份 + +​ 每次备份上一次备份到现在产生的新数据 + +image-20220925184632820 + +​ 差异备份 + +​ 只备份跟完整备份不一样的 + + + +#### 2.tar备份 + +​ 注意:备份期间,服务不可用 + +备份过程:完全物理备份 + +​ 停止数据库 + +```shell +[root@xingdian ~]# systemctl stop mysqld +``` + +​ tar备份数据 + +```shell +[root@xingdian ~]# mkdir /backup +[root@xingdian ~]# cd /var/lib/mysql +[root@xingdian ~]# tar -zcvf /backup/`date +%F`-mysql-all.tar.gz ./* +``` + +​ 启动数据库(备份完成后启动数据库,继续为其他服务提供服务) + +```shell +[root@xingdian ~]# systemctl start mysqld +``` + +恢复过程:模拟数据丢失,恢复数据 + +​ 停止数据库 + +```shell +[root@xingdian ~]# systemctl stop mysqld +``` + +​ 清理环境 + +```shell +[root@xingdian ~]# rm -rf /var/lib/mysql/* +``` + +​ 导入备份数据 + +```shell +[root@xingdian ~]# tar -xvf /backup/2019-08-20-mysql-all.tar.gz -C /usr/lib/mysql +[root@xingdian ~]# chown mysql.mysql /var/lib/mysql/* -R +``` + +​ 启动数据库(恢复后验证数据是否恢复成功) + +```shell +[root@xingdian ~]# systemctl start mysqld +``` + +#### 3.xtrabackup备份 + +简介: + +​ percona-xtrabackup是开源免费的支持MySQL 数据库热备份的软件;能对InnoDB和XtraDB存储引擎的数据库非阻塞地备份;它不暂停服务创建Innodb热备份;为mysql做增量备份;在mysql服务器之间做在线表迁移;使创建replication更加容易;备份mysql而不增加服务器的负载 + +![image-20220925185829126](https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20220925185829126.png) + +安装软件: + +```shell +[root@xingdian ~]# yum install https://repo.percona.com/yum/percona-release-latest.noarch.rpm -y +[root@xingdian ~]# yum install percona-xtrabackup-24 -y +``` + +##### 完整备份 + +​ 创建备份目录: + +```shell +[root@xingdian ~]# mkdir -p /xtrabackup/full/ +``` + +​ 备份: + +```shell +[root@xingdian ~]# innobackupex --user=root --password='QianFeng@123' /xtrabackup/full/ +``` + +​ 查看备份数据: + +```shell +[root@xingdian ~]# ls /xtrabackup/full/ +2022-09-25_19-40-47 +``` + +​ 模拟数据丢失数据恢复:(以下操作模拟数据丢失) + +丢失前数据库中的数据: + +```shell +[root@xingdian ~]# mysql -u root -pQianFeng@123 +mysql: [Warning] Using a password on the command line interface can be insecure. +Welcome to the MySQL monitor. Commands end with ; or \g. +Your MySQL connection id is 7 +Server version: 5.7.39 MySQL Community Server (GPL) + +Copyright (c) 2000, 2022, Oracle and/or its affiliates. + +Oracle is a registered trademark of Oracle Corporation and/or its +affiliates. Other names may be trademarks of their respective +owners. + +Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. + +mysql> show databases; ++--------------------+ +| Database | ++--------------------+ +| information_schema | +| mysql | +| performance_schema | +| sys | +| t1 | ++--------------------+ +5 rows in set (0.00 sec) +``` + +数据丢失: + +```shell +[root@xingdian ~]# systemctl stop mysqld +[root@xingdian ~]# rm -rf /var/lib/mysql/* +[root@xingdian ~]# rm -rf /var/log/mysqld.log +[root@xingdian ~]# rm -rf /var/log/mysql-slow/slow.log (有则删除,无则不需要操作) +``` + +​ 恢复前的验证: + +```shell +[root@xingdian ~]# innobackupex --apply-log /xtrabackup/full/2022-09-25_19-40-47/ +``` + +​ 恢复之前需要确认配置文件内有数据库目录指定,不然xtrabackup不知道恢复到哪里 + +```shell +[root@xingdian ~]# cat /etc/my.cnf +datadir=/var/lib/mysql +``` + +​ 恢复数据: + +```shell +[root@xingdian ~]# innobackupex --copy-back /xtrabackup/full/2022-09-25_19-40-47/ +``` + +​ 修改权限: + +```shell +[root@xingdian ~]# chown mysql.mysql /var/lib/mysql -R +``` + +​ 启动服务: + +```shell +[root@xingdian ~]# systemctl start mysqld +``` + +​ 验证: + +```shell +[root@xingdian ~]# mysql -u root -pQianFeng@123 +mysql: [Warning] Using a password on the command line interface can be insecure. +Welcome to the MySQL monitor. Commands end with ; or \g. +Your MySQL connection id is 2 +Server version: 5.7.39 MySQL Community Server (GPL) + +Copyright (c) 2000, 2022, Oracle and/or its affiliates. + +Oracle is a registered trademark of Oracle Corporation and/or its +affiliates. Other names may be trademarks of their respective +owners. + +Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. + +mysql> show databases; ++--------------------+ +| Database | ++--------------------+ +| information_schema | +| mysql | +| performance_schema | +| sys | +| t1 | ++--------------------+ +5 rows in set (0.00 sec) +``` + +##### 增量备份 + +原理:每次备份上一次备份到现在产生的新数据 + +注意:在进行增量备份前先进行完整备份 + +案例:周一进行全备,周二到周天进行增量备份 + +​ 完整备份:(周一) + +```shell +[root@xingdian ~]# innobackupex --user=root --password='QianFeng@123' /xtrabackup/full +``` + +​ 创建增量备份存放数据目录: + +```shell +[root@xingdian ~]# mkdir /xtrabackup/zeng -p +``` + +​ 模拟数据增加(略) + +​ 第一次增量备份:(周二) + +```shell +[root@xingdian ~]# innobackupex --user=root --password='QianFeng@123' --incremental /xtrabackup/zeng/ --incremental-basedir=/xtrabackup/full/2022-09-25_19-40-47/ + +第一次增量备份的数据: +[root@xingdian ~]# ls /xtrabackup/zeng/ +2022-09-25_19-56-00 +``` + +​ 模拟数据增加(略) + +​ 第二次增量备份:(周三) + +```shell +[root@xingdian ~]# innobackupex --user=root --password='QianFeng@123' --incremental /xtrabackup/zeng/ --incremental-basedir=/xtrabackup/zeng/2022-09-25_19-56-00/ + +第二次增量备份的数据: +[root@xingdian ~]# ls /xtrabackup/zeng/ +2022-09-25_19-56-00 2022-09-25_19-58-12 +``` + +​ 后面的增量备份重复上面的操作(略) + +增量备份数据恢复流程:(需要模拟数据的丢失) + +​ 停止数据库: + +```shell +[root@xingdian ~]# systemctl stop mysqld +``` + +​ 删除数据: + +```shell +[root@xingdian ~]# rm -rf /var/lib/mysql/* +[root@xingdian ~]# rm -rf /var/log/mysqld.log + +其他数据根据实际情况删除 +``` + +​ 依次重演回滚: + +```shell +全备回滚: +[root@xingdian ~]# innobackupex --apply-log --redo-only /xtrabackup/full/2022-09-25_19-40-47/ + +第一次增量回滚: +[root@xingdian ~]# innobackupex --apply-log --redo-only /xtrabackup/full/2022-09-25_19-40-47/ --incremental-dir=/xtrabackup/zeng/2022-09-25_19-56-00/ + +第二次增量回滚: +[root@xingdian ~]# innobackupex --apply-log --redo-only /xtrabackup/full/2022-09-25_19-40-47/ --incremental-dir=/xtrabackup/zeng/2022-09-25_19-58-12/ + +根据实际增量备份的次数回滚,可以想恢复到那个时间节点就回滚到那个时间节点,所有的回滚都给全备 +``` + +​ 恢复数据: + +```shell +[root@xingdian ~]# innobackupex --copy-back /xtrabackup/full/2022-09-25_19-40-47/ +``` + +​ 修改权限: + +```shell +[root@xingdian ~]# chown mysql.mysql /var/lib/mysql -R +``` + +​ 启动数据库: + +```shell +[root@xingdian ~]# systemctl start mysqld +``` + +​ 验证: + +```shell +[root@xingdian ~]# mysql -u root -pQianFeng@123 +mysql: [Warning] Using a password on the command line interface can be insecure. +Welcome to the MySQL monitor. Commands end with ; or \g. +Your MySQL connection id is 2 +Server version: 5.7.39 MySQL Community Server (GPL) + +Copyright (c) 2000, 2022, Oracle and/or its affiliates. + +Oracle is a registered trademark of Oracle Corporation and/or its +affiliates. Other names may be trademarks of their respective +owners. + +Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. + +mysql> show databases; ++--------------------+ +| Database | ++--------------------+ +| information_schema | +| mysql | +| performance_schema | +| sys | +| t1 | +| t2 | +| t3 | ++--------------------+ +``` + +##### 差异备份 + +原理:只备份跟完整备份不一样的 + +注意:在进行增量备份前先进行完整备份 + +案例:周一进行全备,周二到周天进行差异备份 + +​ 完整备份:(周一) + +```shell +[root@xingdian ~]# mkdir -p /xtrabackup/full +[root@xingdian ~]# innobackupex --user=root --password=QianFeng@123 /xtrabackup/full +``` + +​ 模拟数据增加(略) + +​ 第一次差异备份:(周二) + +```shell +[root@xingdian ~]# mkdir -p /xtrabackup/jian +[root@xingdian ~]# innobackupex --user=root --password=QianFeng@123 --incremental /xtrabackup/jian --incremental-basedir=/xtrabackup/full/2022-09-25_20-10-52/ + +查看第一次差异备份的数据: +[root@xingdian ~]# ls /xtrabackup/jian/ +2022-09-25_20-12-55 +``` + +​ 模拟数据增加(略) + +​ 第二次差异备份:(周三) + +```shell +[root@xingdian ~]# innobackupex --user=root --password=QianFeng@123 --incremental /xtrabackup/jian --incremental-basedir=/xtrabackup/full/2022-09-25_20-10-52/ + +查看第二次差异备份的数据: +[root@xingdian ~]# ls /xtrabackup/jian/ +2022-09-25_20-12-55 2022-09-25_20-14-32 + +注意:后面的差异备份跟之前一样,根据需求可以继续差异备份 +``` + +差异备份恢复流程:(模拟数据丢失) + +​ 停止数据库: + +```shell +[root@xingdian ~]# systemctl stop mysqld +``` + +​ 删除数据: + +```shell +[root@xingdian ~]# rm -rf /var/lib/mysql/* +[root@xingdian ~]# rm -rf /var/log/mysqld.log +``` + +​ 重演数据回滚: + +```shell +完整备份回滚: +[root@xingdian ~]# innobackupex --apply-log --redo-only /xtrabackup/full/2022-09-25_20-10-52/ + +差异备份回滚(根据差异备份的原理,如果恢复所有数据只需要将最后依次差异回滚) +[root@xingdian ~]# innobackupex --apply-log --redo-only /xtrabackup/full/2022-09-25_20-10-52/ --incremental-dir=/xtrabackup/jian/2022-09-25_20-14-32/ +``` + +​ 恢复数据: + +```shell +[root@xingdian ~]# innobackupex --copy-back /xtrabackup/full/2022-09-25_20-10-52/ +``` + +​ 修改权限: + +```shell +[root@xingdian ~]# chown mysql.mysql /var/lib/mysql -R +``` + +​ 启动数据库: + +```shell +[root@xingdian ~]# systemctl start mysqld +``` + +​ 数据验证: + +```shell +[root@xingdian ~]# mysql -u root -pQianFeng@123 +mysql: [Warning] Using a password on the command line interface can be insecure. +Welcome to the MySQL monitor. Commands end with ; or \g. +Your MySQL connection id is 2 +Server version: 5.7.39 MySQL Community Server (GPL) + +Copyright (c) 2000, 2022, Oracle and/or its affiliates. + +Oracle is a registered trademark of Oracle Corporation and/or its +affiliates. Other names may be trademarks of their respective +owners. + +Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. + +mysql> show databases; ++--------------------+ +| Database | ++--------------------+ +| information_schema | +| k1 | +| k2 | +| mysql | +| performance_schema | +| sys | ++--------------------+ +9 rows in set (0.00 sec) +``` + +#### 4.mysqldump备份 + +备份表:(前提有库有表) + +```shell +[root@xingdian ~]# mysqldump -u root -pQianFeng@123 k1 t1 > /t1.sql +``` + +恢复表:(恢复之前模拟数据丢失) + +```shell +[root@xingdian ~]# mysql -u root -pQianFeng@123 k1 < /t1.sql +mysql: [Warning] Using a password on the command line interface can be insecure. +``` + +验证: + +```shell +[root@xingdian ~]# mysql -u root -pQianFeng@123 -e "use k1;show tables" +mysql: [Warning] Using a password on the command line interface can be insecure. ++--------------+ +| Tables_in_k1 | ++--------------+ +| t1 | ++--------------+ +``` + +备份一个库: + +```shell +[root@xingdian ~]# mysqldump -u root -pQianFeng@123 k1 > /k1.sql +``` + +备份多个库: + +```shell +[root@xingdian ~]# mysqldump -u root -pQianFeng@123 -B k1 k2 > /kall.sql +``` + +备份所有库: + +```shell +[root@xingdian ~]# mysqldump -u root -pQianFeng@123 -A > /all.sql +``` + +数据恢复: + +​ 为保证数据一致性,应在恢复数据之前停止数据库对外的服务,停止binlog日志 + +​ binlog使用binlog日志恢复数据时也会产生binlog日志(如果开启的话,需要关闭) + +```shell +mysql> set sql_log_bin=0; +Query OK, 0 rows affected (0.00 sec) +``` + +​ 模拟数据丢失(略) + +```shell +[root@xingdian ~]# mysql -u root -pQianFeng@123 -D k1 < /k1.sql +mysql: [Warning] Using a password on the command line interface can be insecure. +ERROR 1049 (42000): Unknown database 'k1' +出现该错误是因为在恢复的时候需要有库的存在 + +[root@xingdian ~]# mysql -u root -pQianFeng@123 -e "create database k1" +[root@xingdian ~]# mysql -u root -pQianFeng@123 -D k1 < /k1.sql + +[root@xingdian ~]# mysql -u root -pQianFeng@123 -e "create database k1" +[root@xingdian ~]# mysql -u root -pQianFeng@123 -e "create database k2" +[root@xingdian ~]# mysql -u root -pQianFeng@123 -D k1 k2 < /kall.sql + +或者 +mysql> source /k1.sql +``` + +验证: + +```shell +[root@xingdian ~]# mysql -u root -pQianFeng@123 -e "use k1; show tables;" +mysql: [Warning] Using a password on the command line interface can be insecure. ++--------------+ +| Tables_in_k1 | ++--------------+ +| t1 | ++--------------+ +[root@xingdian ~]# mysql -u root -pQianFeng@123 -e "use k2; show tables;" +mysql: [Warning] Using a password on the command line interface can be insecure. ++--------------+ +| Tables_in_k2 | ++--------------+ +| t1 | ++--------------+ +``` + +#### 5.binlog日志备份 + +原理:日志方法备份恢复数据 + +日志默认存储位置: + +​ rpm : /var/lib/mysql + +​ 编译: 安装目录的var下 + +产生日志: + +​ 方式一:编译安装 + +```shell +[root@xingdian ~]# mysqld_safe --log-bin --user=mysql --server-id=1 & + +查看binlog日志 +[root@xingdian ~]# mysqlbinlog slave2-bin.000001 -v --base64-output=decode-rows + 时间点 : 141126 14:04:49 + 位置点 : at 106 +``` + +​ 方式二:rpm安装(永久) + +```shell +[root@xingdian ~]# vim /etc/my.cnf +log-bin=mylog +server-id=1 //做主从复制使用 + +[root@xingdian ~]# systemctl restart mysqld + +查看: +[root@xingdian ~]# ls /var/lib/mysql +auto.cnf client-key.pem ib_logfile1 mysql private_key.pem sys +ca-key.pem ib_buffer_pool ibtmp1 mysql.sock public_key.pem xingdian-bin.index +ca.pem ibdata1 mylog.000001 mysql.sock.lock server-cert.pem xtrabackup_info +client-cert.pem ib_logfile0 mylog.index + +[root@xingdian ~]# mysqlbinlog /var/lib/mysql/mylog.000001 -v --base64-output=decode-rows +/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/; +/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/; +DELIMITER /*!*/; +# at 4 +#220925 21:12:47 server id 1 end_log_pos 123 CRC32 0x52358645 Start: binlog v 4, server v 5.7.39-log created 220925 21:12:47 at startup +# Warning: this binlog is either in use or was not closed properly. +ROLLBACK/*!*/; +# at 123 +#220925 21:12:47 server id 1 end_log_pos 154 CRC32 0xa84d8536 Previous-GTIDs +# [empty] +# at 154 +#220925 21:13:38 server id 1 end_log_pos 219 CRC32 0xc2b00431 Anonymous_GTID last_committed=0 sequence_number=1 rbr_only=no +SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/; +# at 219 +#220925 21:13:38 server id 1 end_log_pos 307 CRC32 0x635401a5 Query thread_id=2 exec_time=0 error_code=0 +SET TIMESTAMP=1664111618/*!*/; +SET @@session.pseudo_thread_id=2/*!*/; +SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/; +SET @@session.sql_mode=1436549152/*!*/; +SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/; +/*!\C utf8 *//*!*/; +SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=8/*!*/; +SET @@session.lc_time_names=0/*!*/; +SET @@session.collation_database=DEFAULT/*!*/; +create database t1 +/*!*/; +SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/; +DELIMITER ; +# End of log file +/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/; +/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/; + +方法二: +mysql> show binlog events in "mylog.000001"; ++--------------+-----+----------------+-----------+-------------+---------------------------------------+ +| Log_name | Pos | Event_type | Server_id | End_log_pos | Info | ++--------------+-----+----------------+-----------+-------------+---------------------------------------+ +| mylog.000001 | 4 | Format_desc | 1 | 123 | Server ver: 5.7.39-log, Binlog ver: 4 | +| mylog.000001 | 123 | Previous_gtids | 1 | 154 | | +| mylog.000001 | 154 | Anonymous_Gtid | 1 | 219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' | +| mylog.000001 | 219 | Query | 1 | 307 | create database t1 | ++--------------+-----+----------------+-----------+-------------+---------------------------------------+ +4 rows in set (0.00 sec) + +默认查看第一个 +mysql> show binlog events; ++--------------+-----+----------------+-----------+-------------+---------------------------------------+ +| Log_name | Pos | Event_type | Server_id | End_log_pos | Info | ++--------------+-----+----------------+-----------+-------------+---------------------------------------+ +| mylog.000001 | 4 | Format_desc | 1 | 123 | Server ver: 5.7.39-log, Binlog ver: 4 | +| mylog.000001 | 123 | Previous_gtids | 1 | 154 | | +| mylog.000001 | 154 | Anonymous_Gtid | 1 | 219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' | +| mylog.000001 | 219 | Query | 1 | 307 | create database t1 | ++--------------+-----+----------------+-----------+-------------+---------------------------------------+ +4 rows in set (0.00 sec) +``` + +数据恢复: + +​ 根据时间点恢复数据: + +```shell +[root@xingdian ~]# mysqlbinlog --start-datetime='2022-9-25 21:12:47' --stop-datetime='2022-9-25 21:16:55' /var/lib/mysql/mylog.000001 | mysql -u root -pQianFeng@123 +``` + +​ 根据位置点恢复数据: + +```shell +mysql> show binlog events; ++--------------+-----+----------------+-----------+-------------+---------------------------------------+ +| Log_name | Pos | Event_type | Server_id | End_log_pos | Info | ++--------------+-----+----------------+-----------+-------------+---------------------------------------+ +| mylog.000001 | 4 | Format_desc | 1 | 123 | Server ver: 5.7.39-log, Binlog ver: 4 | +| mylog.000001 | 123 | Previous_gtids | 1 | 154 | | +| mylog.000001 | 154 | Anonymous_Gtid | 1 | 219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' | +| mylog.000001 | 219 | Query | 1 | 307 | create database t1 | +| mylog.000001 | 307 | Anonymous_Gtid | 1 | 372 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' | +| mylog.000001 | 372 | Query | 1 | 453 | drop database t1 | +| mylog.000001 | 453 | Anonymous_Gtid | 1 | 518 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' | +| mylog.000001 | 518 | Query | 1 | 606 | create database t1 | +| mylog.000001 | 606 | Anonymous_Gtid | 1 | 671 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' | +| mylog.000001 | 671 | Query | 1 | 752 | drop database t1 | ++--------------+-----+----------------+-----------+-------------+---------------------------------------+ +[root@xingdian ~]# mysqlbinlog --start-position 219 --stop-position 307 /var/lib/mysql/mylog.000001 | mysql -u root -pQianFeng@123 +``` \ No newline at end of file diff --git a/第四章:数据库日志管理.md b/第四章:数据库日志管理.md new file mode 100644 index 0000000..4388809 --- /dev/null +++ b/第四章:数据库日志管理.md @@ -0,0 +1,135 @@ +

数据库日志管理

+ + + +------ + +## 一:日志管理 + +image-20220925214046253 + +#### 1.日志分类 + +​ 错误日志 :启动,停止,关闭失败报错。rpm安装日志位置 /var/log/mysqld.log + +​ 通用查询日志:所有的查询都记下来 + +​ 二进制日志:实现备份,增量备份。只记录改变数据,除了select都记 + +​ 中继日志:读取主服务器的binlog,在本地回放。保持一致 + +​ slow log:慢查询日志,指导调优,定义某一个查询语句,定义超时时间,通过日志提供调优建议给开发人员 + +​ DDL log: 定义语句的日志 + +#### 2.Error Log + +```shell +log-error=/var/log/mysqld.log +``` + +#### 3.Binary Log + +``` +log-bin=/var/log/mysql-bin/slave2 +server-id=2 + +[root@slave2 ~]# mkdir /var/log/mysql-bin +[root@slave2 ~]# chown mysql.mysql /var/log/mysql-bin/ +[root@slave2 ~]# systemctl restart mysqld +``` + +注意: + +​ 需要提前开启 + +查看binlog日志: + +```shel +[root@slave2 ~]# mysqlbinlog slave2-bin.000001 -v --base64-output=decode-rows + 时间点 : 141126 14:04:49 + 位置点 : at 106 +注: +1. 重启mysqld 会截断 +2. flush logs 会截断 +3. reset master 删除所有binlog rm -rf /var/lib/mysql/*.000001 +4. 删除部分 +PURGE BINARY LOGS TO 'mysql-bin.010'; +PURGE BINARY LOGS BEFORE '2019-04-02 22:46:26'; + +截取binlog +all: +# mysqlbinlog mysql.000002 + +datetime: +# mysqlbinlog mysql.000002 --start-datetime="2018-12-05 10:02:56" +# mysqlbinlog mysql.000002 --stop-datetime="2018-12-05 11:02:54" +# mysqlbinlog mysql.000002 --start-datetime="2018-12-05 10:02:56" --stop-datetime="2018-12-05 11:02:54" + +position: +# mysqlbinlog mysql.000002 --start-position=260 +# mysqlbinlog mysql.000002 --stop-position=260 +# mysqlbinlog mysql.000002 --start-position=260 --stop-position=930 +``` + +#### 4.Slow Query Log + +开启慢查询日志: + +```shell +[root@xingdian ~]# vim /etc/my.cnf +slow_query_log=1 +slow_query_log_file=/var/log/mysql-slow/slow.log +long_query_time=3 设置慢查询超时时间 单位是:秒 +``` + +创建对应目录: + +```shell +[root@xingdian ~]# mkdir /var/log/mysql-slow +[root@xingdian ~]# chown mysql.mysql mysql-slow +``` + +重启服务: + +```shell +[root@xingdian ~]# systemctl restart mysqld +``` + +验证: + +```shell +[root@xingdian ~]# mysql -uroot -pQianFeng@123 +mysql: [Warning] Using a password on the command line interface can be insecure. +Welcome to the MySQL monitor. Commands end with ; or \g. +Your MySQL connection id is 2 +Server version: 5.7.39-log MySQL Community Server (GPL) + +Copyright (c) 2000, 2022, Oracle and/or its affiliates. + +Oracle is a registered trademark of Oracle Corporation and/or its +affiliates. Other names may be trademarks of their respective +owners. + +Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. + +mysql> select sleep(6); ++----------+ +| sleep(6) | ++----------+ +| 0 | ++----------+ +1 row in set (6.00 sec) + +mysql> exit +Bye +[root@xingdian ~]# cat /var/log/mysql-slow/slow.log +/usr/sbin/mysqld, Version: 5.7.39-log (MySQL Community Server (GPL)). started with: +Tcp port: 0 Unix socket: /var/lib/mysql/mysql.sock +Time Id Command Argument +# Time: 2022-09-25T06:58:05.496205Z +# User@Host: root[root] @ localhost [] Id: 2 +# Query_time: 6.007094 Lock_time: 0.000000 Rows_sent: 1 Rows_examined: 0 +SET timestamp=1664089085; +select sleep(6); +```