MySQL数据备份与恢复及sql语句用法
1.Mysqldump简介
mysqldump是逻辑备份工具,支持MyISAM和InnoDB引擎。数据库运行时,MyISAM引擎只支持温备,InnoDB支持热备和温备。
2.MySQLdump的应用
2.1全量备份与恢复
全量备份就是指对某一个时间点上的所有数据或应用进行的一个完全拷贝。
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| xiongke |
+--------------------+
4 rows in set (0.003 sec)MariaDB [(none)]> use xiongke;
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 [xiongke]> show tables;
+-------------------+
| Tables_in_xiongke |
+-------------------+
| xk |
+-------------------+
1 row in set (0.000 sec)MariaDB [xiongke]> select * from xk;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 1 | tom | 20 |
| 2 | xiongke | 25 |
| 3 | sean | 28 |
| 4 | zhangshan | 26 |
| 5 | zhangshan | 20 |
| 6 | lisi | 50 |
| 7 | chensuo | 10 |
| 8 | qiuyi | 15 |
+----+-----------+------+
8 rows in set (0.000 sec)MariaDB [xiongke]>
进行全量备份
[root@master mysqldump]# mysqldump -uroot -p1 --all-databases > /root/mysqldump/all-`date +%F`.sql
[root@master mysqldump]# ls
all-2021-08-25.sql all-210826.sql site-2021-08-25.sql
all-2021-08-26.sql all_.sql
[root@master mysqldump]# cat all-2021-08-26.sql
-- MySQL dump 10.19 Distrib 10.3.28-MariaDB, for Linux (x86_64)
--
-- Host: localhost Database:
-- ------------------------------------------------------
-- Server version 10.3.28-MariaDB/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
误删数据库
MariaDB [(none)]> drop database xiongke;
Query OK, 1 row affected (0.002 sec)MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.000 sec)MariaDB [(none)]>
文件恢复
[root@master mysqldump]# mysql -uroot -p1 < /root/mysqldump/all-2021-08-26.sql
[root@master mysqldump]# mysql -uroot -p1
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 16
Server version: 10.3.28-MariaDB MariaDB ServerCopyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| xiongke |
+--------------------+
4 rows in set (0.000 sec)MariaDB [(none)]> select * from xiongke.xk;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 1 | tom | 20 |
| 2 | xiongke | 25 |
| 3 | sean | 28 |
| 4 | zhangshan | 26 |
| 5 | zhangshan | 20 |
| 6 | lisi | 50 |
| 7 | chensuo | 10 |
| 8 | qiuyi | 15 |
+----+-----------+------+
8 rows in set (0.000 sec)MariaDB [(none)]>
2.2差异备份
开启mysql服务器的二进制日志功能
[root@xk ~]# vim /etc/my.cnf
[root@xk ~]# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[root@xk ~]# tail -15 /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolveserver-id = 1 // 设置服务器标识
log-bin = mysql_bin //开启二进制功能数据库日志文件
[root@xk ~]# ll /opt/data/
-rw-r-----. 1 mysql mysql 154 2月 22 16:37 mysql_bin.000001
-rw-r-----. 1 mysql mysql 19 2月 22 16:37 mysql_bin.index
对数据库进行完全备份
[root@xk ~]# mysqldump -uroot -p1 --single-transaction --flush-logs --master-data=2 --all-databases --delete-master-logs > all-201902221646
mysqldump: [Warning] Using a password on the command line interface can be insecure.
在数据数据库中增加类容
mysql> show databases-> ;
+--------------------+
| Database |
+--------------------+
| information_schema |
| ming |
| mysql |
| performance_schema |
| sys |
| zabbix |
+--------------------+
6 rows in set (0.00 sec)mysql> drop database ming;
Query OK, 2 rows affected (0.00 sec)mysql> show databases-> ;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| zabbix |
+--------------------+
5 rows in set (0.00 sec)mysql>
模拟删库
mysql> show databases-> ;
+--------------------+
| Database |
+--------------------+
| information_schema |
| ming |
| mysql |
| performance_schema |
| sys |
| zabbix |
+--------------------+
6 rows in set (0.00 sec)mysql> drop database ming;
Query OK, 2 rows affected (0.00 sec)mysql> show databases-> ;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| zabbix |
+--------------------+
5 rows in set (0.00 sec)mysql>
恢复完全备份
[root@xk ~]# mysql -uroot -p < all-20210826
Enter password:
[root@xk ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.7.23-log MySQL Community Server (GPL)Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.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 |
| ming |
| mysql |
| performance_schema |
| sys |
| zabbix |
+--------------------+
6 rows in set (0.00 sec)mysql> // 查看表发现没有内容
mysql> select * from student;
Empty set (0.00 sec)mysql>
查看binlog 日志,发现1180为删库前,所以恢复到1180
mysql> show binlog events in 'mysql_bin.000002'\G
*************************** 1. row ***************************Log_name: mysql_bin.000002Pos: 4Event_type: Format_descServer_id: 1
End_log_pos: 123Info: Server ver: 5.7.23-log, Binlog ver: 4
*************************** 2. row ***************************Log_name: mysql_bin.000002Pos: 123Event_type: Previous_gtidsServer_id: 1
End_log_pos: 154Info:
*************************** 18. row ***************************Log_name: mysql_bin.000002Pos: 1115Event_type: Anonymous_GtidServer_id: 1
End_log_pos: 1180Info: SET @@SESSION.GTID_NEXT= 'ANONYMOUS'
*************************** 19. row ***************************Log_name: mysql_bin.000002Pos: 1180Event_type: QueryServer_id: 1
End_log_pos: 1272Info: drop database ming
*************************** 20. row ***************************//恢复到删库前[root@xk ~]# mysql -uroot -p < all-201902221646
Enter password:
[root@xk ~]#
[root@xk ~]#
[root@xk ~]#
[root@xk ~]# mysqlbinlog --stop-position=1180 /opt/data/mysql_bin.000002 |mysql -uroot -pming123
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@xk ~]# mysql -uroot -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@xk ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 5.7.23-log MySQL Community Server (GPL)Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.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 |
| ming |
| mysql |
| performance_schema |
| sys |
| zabbix |
+--------------------+
6 rows in set (0.00 sec)mysql> use ming;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase changed
mysql> select * from student;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | xiongke | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 6 | zhangshan | 20 |
| 7 | lisi | NULL |
| 8 | chenshuo | 10 |
| 9 | wangwu | 3 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
| 12 | ming | 22 |
| 13 | wbk | 25 |
+----+-------------+------+
13 rows in set (0.00 sec)
3.left join、right join、inner join、group by的应用
- left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录
- right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录
- inner join(等值连接) 只返回两个表中联结字段相等的行
MariaDB [xx]> select *from A;
±----±----------+
| aID | aNum |
±----±----------+
| 1 | a20050111 |
| 2 | a20050112 |
| 3 | a20050113 |
| 4 | a20050114 |
| 5 | a20050115 |
±----±----------+MariaDB [xx]> select *from B;
±----±---------+
| aID | aNum |
±----±---------+
| 1 | 20050111 |
| 2 | 20050112 |
| 3 | 20050113 |
| 4 | 20050114 |
| 8 | 20050115 |
±----±---------+
5 rows in set (0.00 sec)
left join
sql语句如下:
select * from A
left join B
on A.aID = B.aID
结果如下:
MariaDB [xx]> select * from A left join B on A.aID = B.aID;
+-----+-----------+------+----------+
| aID | aNum | aID | aNum |
+-----+-----------+------+----------+
| 1 | a20050111 | 1 | 20050111 |
| 2 | a20050112 | 2 | 20050112 |
| 3 | a20050113 | 3 | 20050113 |
| 4 | a20050114 | 4 | 20050114 |
| 5 | a20050115 | NULL | NULL |
+-----+-----------+------+----------+
所影响的行数为 5 行)
结果说明:
left join是以A表的记录为基础的,A可以看成左表,B可以看成右表,left join是以左表为准的.
换句话说,左表(A)的记录将会全部表示出来,而右表(B)只会显示符合搜索条件的记录(例子中为: A.aID = B.aID).
B表记录不足的地方均为NULL.
right join
sql语句如下:
select * from A
right join B
on A.aID = B.aID
结果如下:
MariaDB [xx]> select * from A right join B on A.aID = B.aID;
+------+-----------+-----+----------+
| aID | aNum | aID | aNum |
+------+-----------+-----+----------+
| 1 | a20050111 | 1 | 20050111 |
| 2 | a20050112 | 2 | 20050112 |
| 3 | a20050113 | 3 | 20050113 |
| 4 | a20050114 | 4 | 20050114 |
| NULL | NULL | 8 | 20050115 |
+------+-----------+-----+----------+
(所影响的行数为 5 行)
结果说明:
仔细观察一下,就会发现,和left join的结果刚好相反,这次是以右表(B)为基础的,A表不足的地方用NULL填充.
inner join
sql语句如下:
select * from A
innerjoin B
on A.aID = B.aID
结果如下:
MariaDB [xx]> select *from A inner join B on A.aID = B.aID;
+-----+-----------+-----+----------+
| aID | aNum | aID | aNum |
+-----+-----------+-----+----------+
| 1 | a20050111 | 1 | 20050111 |
| 2 | a20050112 | 2 | 20050112 |
| 3 | a20050113 | 3 | 20050113 |
| 4 | a20050114 | 4 | 20050114 |
+-----+-----------+-----+----------+
4 rows in set (0.00 sec)
结果说明:
很明显,这里只显示出了 A.aID = B.aID的记录.这说明inner join并不以谁为基础,它只显示符合条件的记录.
group by
group by语句用于结合聚合函数,根据一个或多个列对结果集进行分组。
group by语法:
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name;
test 表如下:
MariaDB [xx]> select *from test;
+----------+------+
| name | age |
+----------+------+
| xiaoming | 10 |
| xiaofang | 20 |
| xiaowu | 25 |
| xiaohu | 14 |
| xiaoming | 11 |
| xiaohu | 15 |
+----------+------+
1.1计数 count 函数
sql语句如下:
select name,count
from test
group by name;
MariaDB [xx]> select name,count(1) from test group by name;
+----------+----------+
| name | count(1) |
+----------+----------+
| xiaofang | 1 |
| xiaohu | 2 |
| xiaoming | 2 |
| xiaowu | 1 |
+----------+----------+
4 rows in set (0.00 sec)
结果说明:
来统计这个表名字相同的人数
1.2 sum求和函数
sql语句如下:
select
sum(age)
from test;
MariaDB [xx]> select sum(age) from test;
+----------+
| sum(age) |
+----------+
| 95 |
+----------+
1 row in set (0.00 sec)
结果说明:
计算表中的age字段总和
实例
创建两个表 web和log
MariaDB [xx]> select *from web;
+----+---------------+
| id | name |
+----+---------------+
| 1 | google |
| 2 | taobao |
| 3 | cainiao |
| 4 | weibo |
| 5 | facebook |
| 6 | stackoverflow |
+----+---------------+
6 rows in set (0.00 sec)MariaDB [xx]> select *from log;
+-----+---------+-------+
| aid | site_id | count |
+-----+---------+-------+
| 1 | 1 | 45 |
| 2 | 3 | 100 |
| 3 | 1 | 230 |
| 4 | 2 | 10 |
| 5 | 5 | 205 |
| 6 | 4 | 13 |
| 7 | 3 | 220 |
| 8 | 5 | 545 |
| 9 | 3 | 201 |
+-----+---------+-------+
9 rows in set (0.00 sec)
GROUP BY 简单应用
统计 log 各个 site_id 的访问量:
sql语句如下:
select
site_id,
sum(log.count)
as
nums
from log
group by
site_id
MariaDB [cha]> select site_id,sum(log.count) as nums from log group by site_id;
+---------+------+
| site_id | nums |
+---------+------+
| 1 | 275 |
| 2 | 10 |
| 3 | 521 |
| 4 | 13 |
| 5 | 750 |
+---------+------+
5 rows in set (0.00 sec)
SQL GROUP BY 多表连接
下面的 SQL 语句统计有记录的网站的记录数量:
sql语句如下:
select
web.name
,count(log.aid)
as
nums
from log
left join web on log.site_id = web.id group by web.name;
MariaDB [xx]> select web.name,count(log.aid) as nums from log left join web on log.site_id = web.id group by web.name;
+----------+------+
| name | nums |
+----------+------+
| cainiao | 3 |
| facebook | 2 |
| google | 2 |
| taobao | 1 |
| weibo | 1 |
+----------+------+
5 rows in set (0.00 sec)
MySQL数据备份与恢复及sql语句用法相关推荐
- MySQL数据备份与SQL语句
MySQL数据备份与SQL语句 1.mysql数据库备份与恢复 1.1 数据库常用备份方案 数据库备份方案: 全量备份 增量备份 差异备份 备份方案 特点 全量备份 全量备份就是指对某一个时间点上的所 ...
- Oracle和Mysql数据备份的sql语句
说明: 今天在某处看到SELECT INTO 语句可用于创建表的备份复件,尝试了Oracle和Mysql都不能被使用.经过了多方查询发现,Oracle和Mysql也有相似功能的不同实现方式,现在一一列 ...
- 命令行客户端MySQL基本命令的使用(登录、登出、数据库操作的SQL语句、表结构的SQL语句、表数据操作的SQL语句)
1. 登录和登出数据库 登录数据库: 输入下面命令: mysql -uroot -p 说明: -u 后面是登录的用户名 [写成-u root也是可以的] -p 后面是登录密码, 如果不填写, 回车之 ...
- mysql 删除语句多表关联_MySQL多表关联数据同时删除sql语句
MySQL多表关联数据同时删除sql语句 有需要的朋友可参考. DELETE删除多表数据,怎样才能同时删除多个关联表的数据呢?这里做了深入的解释: 代码如下 1 delete from t1 wher ...
- mysql 新增字段在首列_MariaDB数据库命令与SQL语句
MariaDB数据库命令与SQL语句 --------------------------------------------------------------------------------- ...
- mysql纵表 主键_数据库面试题-sql语句
原标题:数据库面试题-sql语句 1,写出一条Sql语句:取出表A中第31到第40记录(SQLServer,以自动增长的ID作为主键,注意:ID可能不是连续的. 答: → 解1: select top ...
- mysql数据备份与恢复_MySQL数据备份与恢复
常见的MySQL管工具 mysql 命令行 跨平台 MySQL官方bundle包自带 MySQL-Workbench 图形 跨平台 MySQL官方提供 MySQL-Front 图形 Windows 开 ...
- 【MySQL数据备份与恢复】【安装xtrabackup 备份工具】【完全备份流程】【增量备份流程】【差异备份流程】
[MySQL数据备份与恢复] 1.为什么要备份 备份:能够防止由于机械故障以及人为误操作带来的数据丢失,例如将数据库文件保存在了其它地方. 冗余: 数据有多份冗余,但不等备份,只能防止机械故障带来的数 ...
- Mysql增删改查|SQL语句(史上最全|实战教学)
文章目录 关于数据库的操作 1.查看数据库 2.创建数据库 3.选择要操作的数据库 4.查看自己所处的位置及默认所在的位置 5.在命令行选择默认的数据库 6.删除数据库 关于表的操作 1. 查看库有哪 ...
最新文章
- IBM powerVM VIOS
- 用extjs4做个登录框
- SigmaStar SSD202 openwrt 系统下ubi根文件系统挂载过程
- rxjava 背压_背压加载文件– RxJava常见问题解答
- 作者:Gopakumar Gopalakrishnan,男,印度科技大学管理学博士,印孚瑟斯技术有限公司高级研究科学家。...
- java白皮书关键术语
- Nginx 多域名多网站绑定及禁止IP访问
- java基本语法(运算符)
- 【Java项目开发】(一) 购物网站项目经验总结
- 综合布线系统工程设计规范GB50311-2007
- c#学习笔记之Application.DoEvents应用
- PHP获取Opcode及C源码
- 祝大家2022幸福安康
- 幽默故事:1、有实力的女人;2、小小挖蕨鸡(木子家原创)
- 专访|十年程序员董一凡:生命不息,学习不止
- 鸽子学Python 之 Pandas数据分析库
- Excel电子表格向Word文档的导入
- eclipse svn切换用户信息
- 安卓手机备份_手机备份数据迁移全网最全指南(必看)
- kibana 没有匹配搜索条件的空间
热门文章
- C语言基础知识之define宏定义表达式,undef,内存对齐,a和a的区别,数组知识点,int (*)[10] p,二维数组参数与二维指针参数,函数指针数组,常见的内存错误及对策
- iPhone6分辨率与适配(一)
- connection closed gracefully问题
- 软件经济工程学考试 针对基础概念
- 2021新年算法小专题—2.股票买卖利润刷题(Java)
- [译] VINE:一种开源的神经进化(Neuroevolution)交互式数据可视化工具
- golang按key值进行升序排序(ksort),对字符串进行sha1哈希
- 一名优秀的销售人员应该具备哪些素质和能力?
- pr cpu100%_PR全套插件合集,一键安装,快速搞定!
- 解析新浪微博JSON数据