Linux系统------------MySQL数据库 备份与恢复
是对整个数据库、数据库结构和文件结构的备份保存的是备份完成时刻的数据库是差异备份与增量备份的基础。
目录
2.2从数据库的备份策略角度:备份可分为完全备份、差异备份、 增量备份
一、数据备份的重要性
备份的主要目的是灾难恢复
在生产环境中,数据的安全性至关重要
任何数据的丢失都可能产生严重的后果
造成数据丢失的原因
- 程序错误
- 人为操作错误
- 运算错误
- 磁盘故障
- 灾难(如火灾、地震)和盗窃
二、数据库备份的分类
2.1从物理与逻辑的角度,备份可分为物理备份与逻辑备份
①物理备份:对数据库操作系统的物理文件(如数据文件、 日志文件等)的备份
物理备份的方法:
- 冷备份(脱机备份):是在关闭数据库的时候进行的
- 热备份(联机备份):数据库处于运行状态,依赖于数据库的日志文件
- 温备份:数据库锁定表格(不可写入但可读)的状态下进行备份操作
②逻辑备份:对数据库逻辑组件(如:表等数据库对象)的备份,表示为逻辑数据库结构
这种类型的备份适用于可以编辑数据值或表结构
2.2从数据库的备份策略角度:备份可分为完全备份、差异备份、 增量备份
- 完全备份:每次对数据库进行完整的备份
- 差异备份:备份自从上次完全备份之后被修改过的文件
- 增量备份:只有在上次完全备份或者增量备份后被修改的文件才会被备份
2.2.1完全备份
2.2.2差异备份
2.2.3增量备份
2.2.4备份方式比较
备份方式 | 完全备份 | 差异备份 | 增量备份 |
完全备份时的状态 | 表1、 表2 | 表1、 表2 | 表1、 表2 |
第一次添加内容 | 创建表3 | 创建表3 | 创建表3 |
备份内容 | 表1、 表2、表3 | 表3 | 表3 |
第二次添加内容 | 创建表4 | 创建表4 | 创建表4 |
备份内容 | 表1、 表2、表3、表4 | 表3、表4 | 表4 |
逻辑备份的策略(增量备份、全、差异备份)
如何选择逻辑备份策略(频率)
合理值区间⭐⭐⭐
一周一次的全备,全备的时间需要在不提供业务的时间区间进行 PM 10点 AM 5:00之间进行全备 (一般凌晨 1:00~5:00 )
增量:3天/2天/1天一次增量备份
差异:选择特定的场景进行备份
一个处理(NFS)提供额外空间给与mysql 服务器用
三、常见的备份方法
物理冷备
- 备份时数据库处于关闭状态,直接打包 (tar)数据库文件
- 备份速度快,恢复时也是最简单的
专用备份工具mydump或mysqlhotcopy
- mysqldump常用的逻辑备份工具
- mysqlhotcopy仅拥有备份MyISAM和ARCHIVE表
启用二进制日志进行增量备份
- 进行增量备份,需要刷新二进制日志
MySQL支持增量备份,进行增量备份时必须启用二进制日志。二进制日志文件为用户提供复制,对执行备份点后进行的数据库更改所需的信息进行恢复。如果进行增量备份(包含自上次完全备份或增量备份以来发生的数据修改),需要刷新二进制日志。
第三方工具备份
- 免费的MySQL热备份软件Percona XtraBackup
四、MySQL完全备份
4.1MySQL完全备份介绍
- 是对整个数据库、数据库结构和文件结构的备份
- 保存的是备份完成时刻的数据库
- 是差异备份与增量备份的基础
4.2MySQL完全备份优缺点
优点
- 备份与恢复操作简单方便
缺点
- 数据存在大量的重复
- 占用大量的备份空间
- 备份与恢复时间长
4.3数据库完全备份分类
4.3.1物理冷备份与恢复
- 关闭MySQL数据库
- 使用tar命令直接打包数据库文件夹
- 直接替换现有MySQL目录即可
4.3.2mysqldump备份与恢复
- MySQL自带的备份工具,可方便实现对MySQL的备份
- 可以将指定的库、表导出为SQL脚本
- 使用命令mysql导入备份的数据
mysql导入备份数据 mysqldump导出备份数据 将指定的库、表导出为SQL脚本
五、MySQL完全备份与恢复
InnoDB 存储引擎的数据库在磁盘上存储成三个文件:
db.opt(表属性文件)
表名.frm(表结构文件)
表名.ibd(表数据文件)
环境准备:
[root@localhost ~]#mysql -uroot -p123123
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 3
Server version: 5.7.17 Source distribution
Copyright (c) 2000, 2016, 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 |
| anhui |
| beijing |
| class |
| hunan |
| mysql |
| nanjing |
| performance_schema |
| sys |
+--------------------+
9 rows in set (0.01 sec)
mysql> use anhui; #切换anhui库
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; #查看表
5.1物理冷备份与恢复
systemctl stop mysqld
yum -y install xz
#压缩备份
tar Jcvf /opt/mysql_all_$(date +%F).tar.xz /usr/local/mysql/data/
mv /usr/local/mysql/data/ /opt/
#解压恢复
tar Jxvf /opt/mysql_all_2020-11-22.tar.xz -C /usr/local/mysql/data/
cd /usr/local/mysql/data
mv /usr/local/mysql/data/* ./
tar 备份数据库所有备份文件 一般使用tar打包
数据恢复:
xz 备份数据库所有备份文件
[root@localhost mysql]#tar Jcvf /opt/mysql_all$(date +%F).tar.xz /usr/local/mysql/data/
root@localhost mysql]#cd /opt/
[root@localhost opt]#ls
boost_1_59_0.tar.gz data.tar.gz mysql-5.7.17 mysql-5.7.17.tar.gz mysql_all2024-03-25.tar.xz rh
[root@localhost opt]#tar Jxvf mysql_all2024-03-25.tar.xz -C /home/mysql/data/
恢复数据 指定解压目录是 /home/mysql/data 注意目录 看你要去那个目录
恢复数据 指定解压目录是 /usr/local/mysql/data 注意目录 看你要去那个目录
[root@localhost mysql]#cd /opt
[root@localhost opt]#ls
boost_1_59_0.tar.gz data data.tar.gz mysql-5.7.17 mysql-5.7.17.tar.gz mysql_all2024-03-25.tar.xz rh
[root@localhost opt]#tar Jxvf mysql_all2024-03-25.tar.xz -C /usr/local/mysql/data/
这种数据用于数据库整体迁数据
5.2 mysqldump 备份与恢复(温备份)
(1)完全备份一个或多个完整的库 (包括其中所有的表)
mysqldump -u root -p[密码] --databases 库名1 [库名2] ... > /备份路径/备份文件名.sql#导出的就是数据库脚本文件
示例:
mysqldump -u root -p --databases kgc > /opt/kgc.sql #备份一个kgc库
mysqldump -u root -p --databases mysql kgc > /opt/mysql-kgc.sql #备份mysql与 kgc两个库
环境准备:
[root@localhost ~]#mysql -uroot -p123123
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| anhui |
| beijing |
| mysql |
| performance_schema |
| sys |
| usr |
+--------------------+
7 rows in set (0.00 sec)
mysql>
mysql> use anhui;
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_anhui |
+-----------------+
| hefei |
+-----------------+
1 row in set (0.00 sec)
mysql> select * from hefei;
+----+------+--------+
| id | name | cardid |
+----+------+--------+
| 1 | xh | 12222 |
| 2 | xw | 13332 |
| 3 | sx | 13332 |
+----+------+--------+
3 rows in set (0.00 sec)
mysql> use beijing;
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_beijing |
+-------------------+
| qhua |
+-------------------+
1 row in set (0.00 sec)
mysql> select * from qhua;
+----+------+-------+
| id | name | phone |
+----+------+-------+
| 1 | lss | 13333 |
| 2 | ngmt | 13366 |
+----+------+-------+
2 rows in set (0.00 sec)
mysql>
mysql> create table beida(id int(3) not null,name varchar(6),hobby varchar(11));
Query OK, 0 rows affected (0.01 sec)
mysql> insert into beida values(1,'khui','xue xi');
Query OK, 1 row affected (0.00 sec)
mysql> insert into beida values(2,'gquan','hui hua');
Query OK, 1 row affected (0.00 sec)
mysql> select * from beida;
+----+-------+---------+
| id | name | hobby |
+----+-------+---------+
| 1 | khui | xue xi |
| 2 | gquan | hui hua |
+----+-------+---------+
2 rows in set (0.00 sec)
mysql> use beijing;
Database changed
mysql> show tables;
+-------------------+
| Tables_in_beijing |
+-------------------+
| beida |
| qhua |
+-------------------+
2 rows in set (0.00 sec)
mysql>
5.2.1完全备份一个完整的数据库 (包括其中所有的表)
mysqldump -uroot -p123123 --databases anhui > anhui.sql
5.2.2完全备份多个完整的数据库 (包括其中所有的表)
mysqldump -uroot -p123123 --databases anhui beijing > /opt/anhui_beijing.sql
[root@localhost opt]#mysqldump -uroot -p123123 --databases anhui beijing > /opt/anhui_beijing.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@localhost opt]#ll
总用量 134048
-rw-r--r--. 1 root root 2805 3月 25 18:09 anhui_beijing.sql
-rw-r--r--. 1 root root 2033 3月 25 18:00 anhui.sql
-rw-r--r--. 1 root root 83709983 3月 19 13:31 boost_1_59_0.tar.gz
drwxr-xr-x. 2 root root 6 3月 25 17:04 data
-rw-r--r--. 1 root root 1385218 3月 25 16:40 data.tar.gz
drwxr-xr-x. 37 7161 31415 4096 3月 19 21:01 mysql-5.7.17
-rw-r--r--. 1 root root 51433090 3月 19 13:31 mysql-5.7.17.tar.gz
-rw-r--r--. 1 root root 714336 3月 25 16:49 mysql_all2024-03-25.tar.xz
drwxr-xr-x. 2 root root 6 3月 26 2015 rh
[root@localhost opt]#cat anhui_beijing.sql
-- MySQL dump 10.13 Distrib 5.7.17, for Linux (x86_64)
--
-- Host: localhost Database: anhui
-- ------------------------------------------------------
-- Server version 5.7.17
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Current Database: `anhui`
--
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `anhui` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `anhui`;
--
-- Table structure for table `hefei`
--
DROP TABLE IF EXISTS `hefei`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `hefei` (
`id` int(11) NOT NULL,
`name` varchar(10) NOT NULL,
`cardid` varchar(18) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `hefei`
--
LOCK TABLES `hefei` WRITE;
/*!40000 ALTER TABLE `hefei` DISABLE KEYS */;
INSERT INTO `hefei` VALUES (1,'xh','12222'),(2,'xw','13332'),(3,'sx','13332');
/*!40000 ALTER TABLE `hefei` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Current Database: `beijing`
--
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `beijing` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `beijing`;
--
-- Table structure for table `qhua`
--
DROP TABLE IF EXISTS `qhua`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `qhua` (
`id` int(3) NOT NULL,
`name` varchar(6) DEFAULT NULL,
`phone` varchar(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `qhua`
--
LOCK TABLES `qhua` WRITE;
/*!40000 ALTER TABLE `qhua` DISABLE KEYS */;
INSERT INTO `qhua` VALUES (1,'lss','13333'),(2,'ngmt','13366');
/*!40000 ALTER TABLE `qhua` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2024-03-25 18:09:52
5.2.3完全备份 MySQL 服务器中所有的库
(2) 完全备份 MySQL 服务器中所有的库
mysqldump -u root -p[密码] --all-databases > /备份路径/备份文件名.sql
例:
mysqldump -u root -p --all-databases > /opt/all.sql
[root@localhost opt]#mysqldump -uroot -p123123 --all-databases > /opt/all-databases.sql
5.2.4完全备份指定库中的部分表
(3) 完全备份指定库中的部分表
mysqldump -u root -p[密码] 库名 [表名1] [表名2] ... > /备份路径/备份文件名.sql
例:
mysqldump -u root -p [-d] kgc info1 info2 > /opt/kgc_info1.sql
#使用“-d”选项,说明只保存数据库的表结构
#不使用“-d"选项,说明表数据也进行备份
#做为一个表结构模板
[root@localhost opt]#mysqldump -uroot -p123123 beijing beida > /opt/beijing_beida.sql
5.2.5完全备份指定库中的多张表
[root@localhost opt]#mysqldump -uroot -p123123 beijing beida qhua > /opt/beijing_beida_qhua_$(date +%F).sql
完全备份指定库中的指定表,只保存数据库的表结构 -d
[root@localhost opt]#mysqldump -uroot -p123123 -d beijing beida > /opt/beijing_beida_jiegou.sql
[root@localhost opt]#cat beijing_beida_jiegou.sql #查看
[root@localhost opt]#mysqldump -uroot -p123123 -d beijing beida > /opt/beijing_beida_jiegou.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@localhost opt]#ll
总用量 134820
-rw-r--r--. 1 root root 776975 3月 25 18:28 all-databases.sql
-rw-r--r--. 1 root root 2805 3月 25 18:09 anhui_beijing.sql
-rw-r--r--. 1 root root 2033 3月 25 18:00 anhui.sql
-rw-r--r--. 1 root root 1642 3月 25 18:48 beijing_beida_jiegou.sql
-rw-r--r--. 1 root root 2513 3月 25 18:43 beijing_beida_qhua_2024-03-25.sql
-rw-r--r--. 1 root root 1887 3月 25 18:38 beijing_beida.sql
-rw-r--r--. 1 root root 83709983 3月 19 13:31 boost_1_59_0.tar.gz
drwxr-xr-x. 2 root root 6 3月 25 17:04 data
-rw-r--r--. 1 root root 1385218 3月 25 16:40 data.tar.gz
drwxr-xr-x. 37 7161 31415 4096 3月 19 21:01 mysql-5.7.17
-rw-r--r--. 1 root root 51433090 3月 19 13:31 mysql-5.7.17.tar.gz
-rw-r--r--. 1 root root 714336 3月 25 16:49 mysql_all2024-03-25.tar.xz
drwxr-xr-x. 2 root root 6 3月 26 2015 rh
[root@localhost opt]#cat beijing_beida_jiegou.sql
-- MySQL dump 10.13 Distrib 5.7.17, for Linux (x86_64)
--
-- Host: localhost Database: beijing
-- ------------------------------------------------------
-- Server version 5.7.17
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `beida`
--
DROP TABLE IF EXISTS `beida`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `beida` (
`id` int(3) NOT NULL,
`name` varchar(6) DEFAULT NULL,
`hobby` varchar(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2024-03-25 18:48:08
[root@localhost opt]#
5.2.6查看备份文件
(4)查看备份文件
grep -v "^--" /opt/kgc_info1.sql | grep -v "^/" | grep -v "^$"
[root@localhost opt]#grep -v "^--" /opt/beijing_beida_jiegou.sql | grep -v "^/" | grep -v "^$"
六、Mysql 完全备份恢复
#恢复数据库
1.使用mysqldump导出的文件,可使用导入的方法
source命令
mysql命令2.使用source恢复数据库的步骤
登录到MySQL数据库
执行source备份sql脚本的路径3.source恢复的示例
MySQL [(none)]> source /backup/all-data.sql
6.1使用source命令恢复数据
[root@localhost opt]#mysqldump -uroot -p123123 beijing beida > /opt/beijing_beida.sql
①先备份
登录数据库查看
[root@localhost opt]#mysql -uroot -p123123 -e 'drop table beijing.beida;'
②模拟删除
再次去数据库查看表
③恢复
6.2使用mysql命令恢复数据
①先备份
②删除表
③恢复beida表
mysql -e是指在bash环境执行SQL语句,-e指调用命令(此命令行方便在Shell脚本中运行)
使用mysql导入恢复,如果你恢复的是库下面的表,就要加库,如果直接恢复库,就不用加库
[root@localhost opt]#pwd
/opt
[root@localhost opt]#mysql -uroot -p123123 -e 'show tables from beijing;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+-------------------+
| Tables_in_beijing |
+-------------------+
| beida |
| qhua |
+-------------------+
[root@localhost opt]#mysql -uroot -p123123 -e 'drop table beijing.beida;'
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@localhost opt]#mysql -uroot -p123123 -e 'show tables from beijing;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+-------------------+
| Tables_in_beijing |
+-------------------+
| qhua |
+-------------------+
[root@localhost opt]#mysql -uroot -p123123 beijing < beijing_beida.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@localhost opt]#mysql -uroot -p123123 -e 'show tables from beijing;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+-------------------+
| Tables_in_beijing |
+-------------------+
| beida |
| qhua |
+-------------------+
[root@localhost opt]#
6.3有无--database的区别
Mysqldump严格来说是属于温备份,需要对表进行写入的锁定。
在全量备份与恢复中,beijing数据库,beijing数据库中有beida数据表
- 当备份增加--database时,表示针对beijing整个数据库;
- 当备份不增加--databases时,表示只针对beijing数据库下所有的数据表
mysql -e是指在bash环境执行SQL语句,-e指调用命令(此命令行方便在Shell脚本中运行)
6.3.1有database
当备份增加--database时,表示针对beijing整个数据库;
[root@localhost opt]#mysql -uroot -p123123 -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| anhui |
| beijing |
| mysql |
| performance_schema |
| sys |
| usr |
+--------------------+
[root@localhost opt]#mysql -uroot -p123123 -e 'show tables from beijing;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+-------------------+
| Tables_in_beijing |
+-------------------+
| beida |
| qhua |
+-------------------+
[root@localhost opt]#mysqldump -uroot -p123123 --dababases beijing > /opt/beijing_all.sql
#使用mysqldump命令 指定root用户 指定密码 备份beijing数据库下所有内容到opt下取名为beijing_all.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [ERROR] unknown option '--dababases'
[root@localhost opt]#mysqldump -uroot -p123123 --databases beijing > /opt/beijing_all.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@localhost opt]#ls
all-databases.sql beijing_beida_jiegou.sql data mysql_all2024-03-25.tar.xz
anhui_beijing.sql beijing_beida_qhua_2024-03-25.sql data.tar.gz rh
anhui.sql beijing_beida.sql mysql-5.7.17
beijing_all.sql boost_1_59_0.tar.gz mysql-5.7.17.tar.gz
[root@localhost opt]#mysql -uroot -p123123 -e 'drop database beijing;'
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@localhost opt]#mysql -uroot -p123123 -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| anhui |
| mysql |
| performance_schema |
| sys |
| usr |
+--------------------+
[root@localhost opt]#
①先备份
②模拟删除数据库
③恢复数据库
[root@localhost opt]#mysql -uroot -p123123 < /opt/beijing_all.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@localhost opt]#mysql -uroot -p123123 -e 'show databases;' #不进入数据库查看库 -e
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| anhui |
| beijing |
| mysql |
| performance_schema |
| sys |
| usr |
+--------------------+
[root@localhost opt]#mysql -uroot -p123123 -e 'select * from beijing.beida;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+----+-------+---------+
| id | name | hobby |
+----+-------+---------+
| 1 | khui | xue xi |
| 2 | gquan | hui hua |
+----+-------+---------+
[root@localhost opt]#
6.3.2无database
当备份不增加--databases时,表示只针对beijing数据库下所有的数据表
①先备份
[root@localhost opt]#mysql -uroot -p123123 -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| anhui |
| beijing |
| mysql |
| performance_schema |
| sys |
| usr |
+--------------------+
[root@localhost opt]#mysqldump -uroot -p123123 beijing > /opt/beijing_all.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@localhost opt]#ls /opt/
all-databases.sql beijing_beida_jiegou.sql data mysql_all2024-03-25.tar.xz
anhui_beijing.sql beijing_beida_qhua_2024-03-25.sql data.tar.gz rh
anhui.sql beijing_beida.sql mysql-5.7.17
beijing_all.sql boost_1_59_0.tar.gz mysql-5.7.17.tar.gz
②数据恢复
[root@localhost opt]#mysql -uroot -p123123 -e 'create database beijing;'
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@localhost opt]#mysql -uroot -p123123 beijing < /opt/beijing_all.sql #把备份导入
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@localhost opt]#mysql -uroot -p123123 -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| anhui |
| beijing |
| mysql |
| performance_schema |
| sys |
| usr |
+--------------------+
[root@localhost opt]#mysql -uroot -p123123 -e 'select * from beijing.beida;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+----+-------+---------+
| id | name | hobby |
+----+-------+---------+
| 1 | khui | xue xi |
| 2 | gquan | hui hua |
+----+-------+---------+
[root@localhost opt]#
记得创建beijing数据库,要不会报错,
-e 可以不进入数据库,看相关信息
mysqldump 导出数据库
mysql 导入数据库
6.4Crontab -e——执行定时备份
0 1 * * 6 /usr/local/mysql/bin/mysqldump -uroot -p123456 beijing > beijing_all_$(date +%F).sql;
/usr/local/mysql/bin/mysqladmin -uroot -p flush-logs
或者
0 1 * * 6 /usr/local/mysql/bin/mysqldump -uroot -p123456 beijing > beijing_all_$(date +%Y%m%d).sql;
/usr/local/mysql/bin/mysqladmin -uroot -p flush-logs
七、Mysql日志
MySQL数据库增量恢复
1.一般恢复将所有备份的二进制日志内容全部恢复
2.基于位置恢复
数据库在某一时间点可能既有错误的操作也有正确的操作
可以基于精准的位置跳过错误的操作
发生错误节点之前的一个节点,上一次正确操作的位置点停止3.基于时间点恢复
跳过某个发生错误的时间点实现数据恢复
在错误时间点停止,在下一个正确时间点开始
换一台机器操作,方便看数据,环境准备:
[root@localhost ~]#systemctl stop firewalld
[root@localhost ~]#setenforce 0
setenforce: SELinux is disabled
[root@localhost ~]#mysql -uroot -p123123
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 3
Server version: 5.7.17 Source distribution
Copyright (c) 2000, 2016, 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> create database henan;
Query OK, 1 row affected (0.01 sec)
mysql> use henan;
Database changed
mysql> show tables;
Empty set (0.00 sec)
mysql> create table laojunshan(id int(5),name varchar(8),phone varchar(11));
Query OK, 0 rows affected (0.03 sec)
mysql> insert into laojunshan values(1,'zh',11111);
Query OK, 1 row affected (0.08 sec)
mysql> insert into laojunshan values(2,'dzl',66666);
Query OK, 1 row affected (0.00 sec)
mysql> select * from laojunshan;
+------+------+-------+
| id | name | phone |
+------+------+-------+
| 1 | zh | 11111 |
| 2 | dzl | 66666 |
+------+------+-------+
2 rows in set (0.00 sec)
mysql> show tables;
+-----------------+
| Tables_in_henan |
+-----------------+
| laojunshan |
+-----------------+
1 row in set (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| henan |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.01 sec)
mysql>
数据库日志对于数据库的备份和恢复中起着至关重要的作用
日志默认存放位置/usr/local/mysql/data文件夹下
7.1开启二进制日志功能
vim /etc/my.cnf
[mysqld]
log-bin=mysql-bin
binlog_format = MIXED
#可选,指定二进制日志(binlog)的记录格式为MIXED(混合输入)
server-id = 1
#可加可不加该命令
systemctl restart mysqld
log-error=/usr/local/mysql/data/mysql_error.log
general_log=ON
general_log_file=/usr/local/mysql/data/mysql_general.log
log-bin=mysql-bin
slow_query_log=ON
slow_query_log_file=/usr/local/mysql/data/mysql_slow_query.log
long_query_time=5
binlog_format = MIXED
解释如下:
[root@localhost ~]#vim /etc/my.cnf
log-error=/usr/local/mysql/data/mysql_error.log
#错误日志 存放位置/usr/local/mysql/data/目录下 错误日志文件名为mysql_error.log
general_log=ON
#通用查询日志开启
general_log_file=/usr/local/mysql/data/mysql_general.log
#通用查询日志 保存位置在/usr/local/mysql/data目录下 通用查询日志文件名为mysql_general.log
log-bin=mysql-bin
#二进制日志(binlog):用来记录所有更新了数据或者已经潜在更新了数据的语句,记录了数据的更改,可用于数据恢复,默认开启
slow_query_log=ON
#慢查询开启 慢查询:用来记录所有执行时间超过long_query_time秒的语句,可以找到哪些查询语句执行时间长,以便提醒优化,默认关闭
slow_query_log_file=/usr/local/mysql/data/mysql_slow_query.log
#慢查询日志默认存放位置 /usr/local/mysql/data/ 慢查询日志文件名为mysql_slow_query.log
long_query_time=5
#设置超过5秒执行的语句被记录 缺省时为10秒
binlog_format = MIXED
#指定二进制日志(binlog)的记录格式为MIXED(混合输入)
二进制日志开启后,重启mysql会在data目录中看到二进制日志(mysql-bin.000001,mysql-bin.000002...文件)
开启二进制日志,会产生一个索引文件及索引列表(mysql-bin.index)
其中,索引文件记录更新的sql语句
索引文件刷新方式
- 重启Mysql服务的时候会更新索引文件,用于记录新的更新的sql语句
- 刷新二进制日志
mysql-bin.index #二进制日志文件的索引
7.1.1二进制日志(binlog)有3种不同的记录格式:
STATEMENT (基于SQL语句)
ROW(基于行)
MIXED(混合模式)
默认格式是STATEMENT
7.1.1.1 STATEMENT(基于SQL语句)
每一条涉及到被修改的sql 都会记录在binlog中
- 缺点:日志量过大,如sleep()函数,last_insert_id()>,以及user-defined fuctions(udf)、主从复制等架构记录日志时会出现问题
- 总结:增删改查通过sql语句来实现记录,如果用高并发可能会出错,可能时间差异或者延迟,可能不是我们想想的恢复可能你先删除或者在修改,可能会倒过来,准确率低
如果使用STATEMENT记录格式,假如删除数据库数据表中的第二行的数据,再次恢复数据表的数据,不一定是第二行,有可能将第二行恢复到最后
7.1.1.2 ROW(基于行)
只记录变动的记录,不记录sql的上下文环境
- 缺点:如果遇到update......set....where true 那么binlog的数据量会越来越大
- 总结:update、delete以多行数据起作用,来用行记录下来,只记录变动的记录,不记录sql的上下文环境,比如sql语句记录一行,但是ROW就可能记录10行,但是准确性高,高并发的时候由于操作量,性能变低, 比较大所以记录都记下来
7.1.1.3MIXED 推荐使用
一般的语句使用statement,函数使用ROW方式存储。
7.2查看数据库日志
systemctl restart mysqld
mysql -u root -P
show variables like 'general%'; #查看通用查询日志是否开启
show variables like 'log_bin%'; #查看二进制日志是否开启
show variables like '%slow%'; #查看慢查询日功能是否开启
show variables like 'long_query_time'; #查看慢查询时间设置
set global slow_query_log=ON; #在数据库中设置开启慢查询的方法PS:variables 表示变量 like 表示模糊查询
#xxx(字段)
xxx% 以xxx为开头的字段
%xxx 以xxx为结尾的字段
%xxx% 只要出现xxx字段的都会显示出来
xxx 精准查询
mysql> show variables like 'log_bin%';
#查看二进制日志是否开启
+---------------------------------+---------------------------------------+
| Variable_name | Value |
+---------------------------------+---------------------------------------+
| log_bin | ON |
| log_bin_basename | /usr/local/mysql/data/mysql-bin |
| log_bin_index | /usr/local/mysql/data/mysql-bin.index |
| log_bin_trust_function_creators | OFF |
| log_bin_use_v1_row_events | OFF |
+---------------------------------+---------------------------------------+
5 rows in set (0.00 sec)
mysql> show variables like 'general%';
#查看通用查询日志状态
+------------------+-----------------------------------------+
| Variable_name | Value |
+------------------+-----------------------------------------+
| general_log | ON |
| general_log_file | /usr/local/mysql/data/mysql_general.log |
+------------------+-----------------------------------------+
2 rows in set (0.00 sec)
mysql> show variables like '%slow%';
#查看慢查询日志是否开启
+---------------------------+--------------------------------------------+
| Variable_name | Value |
+---------------------------+--------------------------------------------+
| log_slow_admin_statements | OFF |
| log_slow_slave_statements | OFF |
| slow_launch_time | 2 |
| slow_query_log | ON |
| slow_query_log_file | /usr/local/mysql/data/mysql_slow_query.log |
+---------------------------+--------------------------------------------+
5 rows in set (0.00 sec)
mysql> show variables like 'long_query_time';
#查看慢查询时间设置
+-----------------+----------+
| Variable_name | Value |
+-----------------+----------+
| long_query_time | 5.000000 |
+-----------------+----------+
1 row in set (0.00 sec)
7.3查看二进制日志文件的内容
cp /usr/local/mysql/data/mysql-bin.000001 /opt/
mysqlbinlog --no-defaults /opt/mysql-bin.000001
mysqlbinlog --no-defaults --base64-output=decode-rows -v /opt/mysql-bin.000001
#--base64-output=decode-rows:使用64位编码机制去解码(decode)并按行读取(rows)
#-v: 显示详细内容
#--no-defaults : 默认字符集(不加会报UTF-8的错误)
PS: 可以将解码后的文件导出为txt格式,方便查阅
mysqlbinlog --no-defaults --base64-output=decode-rows -v /opt/mysql-bin.000001 > /opt/mysql-bin.000001
[root@localhost ~]#cd /usr/local/mysql/data/
[root@localhost data]#ls
auto.cnf ib_buffer_pool ib_logfile0 ibtmp1 mysql-bin.000001 mysql_error.log mysql_slow_query.log sys
henan ibdata1 ib_logfile1 mysql mysql-bin.index mysql_general.log performance_schema
[root@localhost data]#pwd
/usr/local/mysql/data
[root@localhost data]#cp /usr/local/mysql/data/mysql-bin.000001 /opt
[root@localhost data]#cat /opt/mysql-bin.000001 _?bin?fw{5.7.17-log?f8
**4Yo??f#j?[root@localhost data]#
[root@localhost data]#mysqlbinlog --no-defaults /opt/mysql-bin.000001
#使用mysql服务自带的binlog二进制解释器默认字符集查看/opt目录下的/mysql-bin.000001数据文件
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#240326 22:25:37 server id 1 end_log_pos 123 CRC32 0x84a06f59 Start: binlog v 4, server v 5.7.17-log created 240326 22:25:37 at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
BINLOG '
4doCZg8BAAAAdwAAAHsAAAABAAQANS43LjE3LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAADh2gJmEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA
AVlvoIQ=
'/*!*/;
# at 123
#240326 22:25:37 server id 1 end_log_pos 154 CRC32 0x1af2af6a Previous-GTIDs
# [empty]
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*/;
[root@localhost data]#mysqlbinlog --no-defaults --base64-output=decode-rows -v /opt/mysql-bin.000001
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#240326 22:25:37 server id 1 end_log_pos 123 CRC32 0x84a06f59 Start: binlog v 4, server v 5.7.17-log created 240326 22:25:37 at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
# at 123
#240326 22:25:37 server id 1 end_log_pos 154 CRC32 0x1af2af6a Previous-GTIDs
# [empty]
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*/;
7.4二进制日志中需要关注的部分
1、at :开始的位置点
2、end_log_pos:结束的位置
3、时间戳: 210712 11:50:30
4、SQL语句
八、MySQL 增量备份
做增量备份要先开启二进制日志,然后再插入数据,不然日志是空的
8.1使用二进制日志备份与恢复( 一般恢复)
[root@localhost data]#mysqladmin -uroot -p123123 flush-logs
环境搭建:
[root@localhost ~]#mysql -uroot -p123123
mysql> create database anhui;
Query OK, 1 row affected (0.01 sec)
mysql> use anhui;
Database changed
mysql> show tables;
Empty set (0.00 sec)
mysql> create table feixi(id int(5),name varchar(8),hobby varchar(11));
Query OK, 0 rows affected (0.03 sec)
mysql> insert into feixi values(2,'zsx','du shu');
Query OK, 1 row affected (0.01 sec)
mysql> insert into feixi values(1,'axs','lan qiu');
Query OK, 1 row affected (0.00 sec)
mysql> select * from feixi;
+------+------+---------+
| id | name | hobby |
+------+------+---------+
| 2 | zsx | du shu |
| 1 | axs | lan qiu |
+------+------+---------+
2 rows in set (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| anhui |
| henan |
| mysql |
| performance_schema |
| sys |
+--------------------+
在重新创建数据,就有日志了
[root@localhost opt]#mysqladmin -uroot -p123123 flush-logs #刷新日志
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
[root@localhost opt]#cd -
/usr/local/mysql/data
[root@localhost data]#ls
anhui ib_buffer_pool ib_logfile1 mysql-bin.000001 mysql-bin.index mysql_slow_query.log
auto.cnf ibdata1 ibtmp1 mysql-bin.000002 mysql_error.log performance_schema
henan ib_logfile0 mysql mysql-bin.000003 mysql_general.log sys
[root@localhost data]#cp mysql-bin.000002 /opt/ #此时日志在000002中
cp:是否覆盖"/opt/mysql-bin.000002"? yes
[root@localhost data]#mysqlbinlog --no-defaults --base64-output=decode-rows -v /opt/mysql-bin.000002
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#240326 23:25:22 server id 1 end_log_pos 123 CRC32 0xbe3a670f Start: binlog v 4, server v 5.7.17-log created 240326 23:25:22
# at 123
#240326 23:25:22 server id 1 end_log_pos 154 CRC32 0x99ab220f Previous-GTIDs
# [empty]
# at 154
#240326 23:37:29 server id 1 end_log_pos 219 CRC32 0x9fa96e75 Anonymous_GTID last_committed=0 sequence_number=1
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 219
#240326 23:37:29 server id 1 end_log_pos 316 CRC32 0x6782688e Query thread_id=3 exec_time=0 error_code=0
SET TIMESTAMP=1711467449/*!*/;
SET @@session.pseudo_thread_id=3/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1437073414/*!*/;
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=33/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
create database anhui
/*!*/;
# at 316
#240326 23:38:04 server id 1 end_log_pos 381 CRC32 0x1867d084 Anonymous_GTID last_committed=1 sequence_number=2
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 381
#240326 23:38:04 server id 1 end_log_pos 520 CRC32 0xb0c7b2a9 Query thread_id=3 exec_time=0 error_code=0
use `henan`/*!*/;
SET TIMESTAMP=1711467484/*!*/;
create table feixi(id int(5),name varchar(8),hobby varchar(11))
/*!*/;
# at 520
#240326 23:38:24 server id 1 end_log_pos 585 CRC32 0xcbd04d9f Anonymous_GTID last_committed=2 sequence_number=3
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 585
#240326 23:38:24 server id 1 end_log_pos 666 CRC32 0x673daeb4 Query thread_id=3 exec_time=0 error_code=0
SET TIMESTAMP=1711467504/*!*/;
BEGIN
/*!*/;
# at 666
#240326 23:38:24 server id 1 end_log_pos 790 CRC32 0x5bfcf1c4 Query thread_id=3 exec_time=0 error_code=0
SET TIMESTAMP=1711467504/*!*/;
insert into laojunshan values(1,'dzl','lan qiu')
/*!*/;
# at 790
#240326 23:38:24 server id 1 end_log_pos 821 CRC32 0x45abb21b Xid = 13
COMMIT/*!*/;
# at 821
#240326 23:38:44 server id 1 end_log_pos 886 CRC32 0x2a46d6fb Anonymous_GTID last_committed=3 sequence_number=4
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 886
#240326 23:38:44 server id 1 end_log_pos 967 CRC32 0xc60b4330 Query thread_id=3 exec_time=0 error_code=0
SET TIMESTAMP=1711467524/*!*/;
BEGIN
/*!*/;
# at 967
#240326 23:38:44 server id 1 end_log_pos 1090 CRC32 0xbd861b65 Query thread_id=3 exec_time=0 error_code=0
SET TIMESTAMP=1711467524/*!*/;
insert into laojunshan values(2,'zsx','du shu')
/*!*/;
# at 1090
#240326 23:38:44 server id 1 end_log_pos 1121 CRC32 0xef7ae1c4 Xid = 14
COMMIT/*!*/;
# at 1121
#240326 23:39:47 server id 1 end_log_pos 1186 CRC32 0xf375ab6c Anonymous_GTID last_committed=4 sequence_number=5
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 1186
#240326 23:39:47 server id 1 end_log_pos 1267 CRC32 0x60d34f7e Query thread_id=3 exec_time=0 error_code=0
SET TIMESTAMP=1711467587/*!*/;
BEGIN
/*!*/;
# at 1267
#240326 23:39:47 server id 1 end_log_pos 1385 CRC32 0x545613c8 Query thread_id=3 exec_time=0 error_code=0
SET TIMESTAMP=1711467587/*!*/;
insert into feixi values(2,'zsx','du shu')
/*!*/;
# at 1385
#240326 23:39:47 server id 1 end_log_pos 1416 CRC32 0x46f969de Xid = 17
COMMIT/*!*/;
# at 1416
#240326 23:40:07 server id 1 end_log_pos 1481 CRC32 0xe0e81a3a Anonymous_GTID last_committed=5 sequence_number=6
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 1481
#240326 23:40:07 server id 1 end_log_pos 1562 CRC32 0x2e65f65e Query thread_id=3 exec_time=0 error_code=0
SET TIMESTAMP=1711467607/*!*/;
BEGIN
/*!*/;
# at 1562
#240326 23:40:07 server id 1 end_log_pos 1681 CRC32 0x224e7eee Query thread_id=3 exec_time=0 error_code=0
SET TIMESTAMP=1711467607/*!*/;
insert into feixi values(1,'axs','lan qiu')
/*!*/;
# at 1681
#240326 23:40:07 server id 1 end_log_pos 1712 CRC32 0xd2069861 Xid = 18
COMMIT/*!*/;
# at 1712
#240326 23:42:10 server id 1 end_log_pos 1759 CRC32 0xe7b67c5b Rotate to mysql-bin.000003 pos: 4
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*/;
[root@localhost data]#
000001里创建了anhui库,000002由于数据插入错误,000003日志中插入前两条数据
由于刚才表添加错误,没有use anhui,再重新刷新下日志备份下:
①数据备份
②删除数据:
[root@localhost data]#mysql -uroot -p123123 -e 'select * from anhui.feixi;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------+------+---------+
| id | name | hobby |
+------+------+---------+
| 2 | zsx | du shu |
| 1 | axs | lan qiu |
+------+------+---------+
[root@localhost data]#mysql -uroot -p123123 -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| anhui |
| henan |
| mysql |
| performance_schema |
| sys |
+--------------------+
[root@localhost data]#mysql -uroot -p123123 -e 'show tables from anhui;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+-----------------+
| Tables_in_anhui |
+-----------------+
| feixi |
+-----------------+
[root@localhost data]#mysql -uroot -p123123 -e 'drop table anhui.feixi;' #删除feixi表
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@localhost data]#mysql -uroot -p123123 -e 'show tables from anhui;'
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@localhost data]#
③数据恢复
[root@localhost data]#mysqlbinlog --no-defaults /opt/mysql-bin.000003| mysql -uroot -p123123
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@localhost data]#mysql -uroot -p123123 -e 'show tables from anhui;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+-----------------+
| Tables_in_anhui |
+-----------------+
| feixi |
+-----------------+
[root@localhost data]#mysql -uroot -p123123 -e 'select * from anhui.feixi;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------+------+---------+
| id | name | hobby |
+------+------+---------+
| 2 | zsx | du shu |
| 1 | axs | lan qiu |
+------+------+---------+
[root@localhost data]#
8.2基于位置点恢复
8.2.1断点恢复
数据库在某一时间点可能既有错误的操作也有正确的操作 可以基于精准的位置跳过错误的操作
#仅恢复到操作 ID 为“623"之前的数据,即不恢复"user4"的数据
mysqlbinlog --no-defaults --stop-position='623' /opt/mysql-bin.000002 | mysql -uroot -p
#仅恢复"user4"的数据,跳过"user3"的数据恢复
mysqlbinlog --no-defaults --start-position='623' /opt/mysql-bin.000002 | mysql -uroot -pmysqlbinlog --no-defaults --start-position='400' --stop-position='623' /opt/mysql-bin.000002 | mysql -uroot -p #恢复从位置为400开始到位置为623为止
①插入一些数据
mysql> select * from feixi;
+------+------+---------+
| id | name | hobby |
+------+------+---------+
| 2 | zsx | du shu |
| 1 | axs | lan qiu |
+------+------+---------+
2 rows in set (0.00 sec)
mysql> insert into feixi values(3,'jian','cahng ge');
Query OK, 1 row affected (0.01 sec)
mysql> insert into feixi values(4,'lili','shu fa');
Query OK, 1 row affected (0.01 sec)
mysql> insert into feixi values(5,'xjin','hui hua');
Query OK, 1 row affected (0.01 sec)
mysql> select * from feixi;
+------+------+----------+
| id | name | hobby |
+------+------+----------+
| 2 | zsx | du shu |
| 1 | axs | lan qiu |
| 3 | jian | cahng ge |
| 4 | lili | shu fa |
| 5 | xjin | hui hua |
+------+------+----------+
5 rows in set (0.00 sec)
mysql>
②备份二进制日志文件
查看内容:数据信息
[root@localhost data]#mysqladmin -uroot -p123123 flush-logs
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
[root@localhost data]#ls
anhui ib_buffer_pool ib_logfile1 mysql-bin.000001 mysql-bin.000004 mysql_error.log performance_schema
auto.cnf ibdata1 ibtmp1 mysql-bin.000002 mysql-bin.000005 mysql_general.log sys
henan ib_logfile0 mysql mysql-bin.000003 mysql-bin.index mysql_slow_query.log
[root@localhost data]#pwd
/usr/local/mysql/data
[root@localhost data]#cp /usr/local/mysql/data/mysql-bin.000004 /opt
[root@localhost data]#mysqlbinlog --no-defaults --base64-output=decode-rows -v /opt/mysql-bin.000004
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#240327 0:02:54 server id 1 end_log_pos 123 CRC32 0xb6d3b975 Start: binlog v 4, server v 5.7.17-log created 240327 0:02:54
# at 123
#240327 0:02:54 server id 1 end_log_pos 154 CRC32 0x80ade486 Previous-GTIDs
# [empty]
# at 154
#240327 0:10:38 server id 1 end_log_pos 219 CRC32 0xefb155fb Anonymous_GTID last_committed=0 sequence_number=1
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 219
#240327 0:10:38 server id 1 end_log_pos 342 CRC32 0x8985723e Query thread_id=19 exec_time=0 error_code=0
SET TIMESTAMP=1711469438/*!*/;
SET @@session.pseudo_thread_id=19/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1437073414/*!*/;
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=33/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
DROP TABLE "anhui"."feixi" /* generated by server */
/*!*/;
# at 342
#240327 0:00:17 server id 1 end_log_pos 407 CRC32 0x6868494a Anonymous_GTID last_committed=1 sequence_number=2
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 407
#240327 0:00:17 server id 1 end_log_pos 546 CRC32 0xfbb8ad12 Query thread_id=3 exec_time=985 error_code=0
use `anhui`/*!*/;
SET TIMESTAMP=1711468817/*!*/;
create table feixi(id int(5),name varchar(8),hobby varchar(11))
/*!*/;
# at 546
#240327 0:00:24 server id 1 end_log_pos 611 CRC32 0xe6803da1 Anonymous_GTID last_committed=2 sequence_number=3
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 611
#240327 0:00:24 server id 1 end_log_pos 692 CRC32 0x1c26c75c Query thread_id=3 exec_time=978 error_code=0
SET TIMESTAMP=1711468824/*!*/;
BEGIN
/*!*/;
# at 692
#240327 0:00:24 server id 1 end_log_pos 810 CRC32 0xffd5c239 Query thread_id=3 exec_time=978 error_code=0
SET TIMESTAMP=1711468824/*!*/;
insert into feixi values(2,'zsx','du shu')
/*!*/;
# at 810
#240327 0:00:24 server id 1 end_log_pos 841 CRC32 0xe5c0468d Xid = 128
COMMIT/*!*/;
# at 841
#240327 0:00:31 server id 1 end_log_pos 906 CRC32 0xe4774092 Anonymous_GTID last_committed=3 sequence_number=4
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 906
#240327 0:00:31 server id 1 end_log_pos 987 CRC32 0x383cc4e8 Query thread_id=3 exec_time=971 error_code=0
SET TIMESTAMP=1711468831/*!*/;
BEGIN
/*!*/;
# at 987
#240327 0:00:31 server id 1 end_log_pos 1106 CRC32 0x71440208 Query thread_id=3 exec_time=971 error_code=0
SET TIMESTAMP=1711468831/*!*/;
insert into feixi values(1,'axs','lan qiu')
/*!*/;
# at 1106
#240327 0:00:31 server id 1 end_log_pos 1137 CRC32 0x14865556 Xid = 134
COMMIT/*!*/;
# at 1137
#240327 0:17:32 server id 1 end_log_pos 1202 CRC32 0x450b34c6 Anonymous_GTID last_committed=4 sequence_number=5
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 1202
#240327 0:17:32 server id 1 end_log_pos 1325 CRC32 0xb63888c7 Query thread_id=24 exec_time=0 error_code=0
SET TIMESTAMP=1711469852/*!*/;
DROP TABLE "anhui"."feixi" /* generated by server */
/*!*/;
# at 1325
#240327 0:00:17 server id 1 end_log_pos 1390 CRC32 0x4e4aea82 Anonymous_GTID last_committed=5 sequence_number=6
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 1390
#240327 0:00:17 server id 1 end_log_pos 1529 CRC32 0x8971b4f1 Query thread_id=3 exec_time=1052 error_code=0
use `anhui`/*!*/;
SET TIMESTAMP=1711468817/*!*/;
create table feixi(id int(5),name varchar(8),hobby varchar(11))
/*!*/;
# at 1529
#240327 0:00:24 server id 1 end_log_pos 1594 CRC32 0xc25f1a7f Anonymous_GTID last_committed=6 sequence_number=7
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 1594
#240327 0:00:24 server id 1 end_log_pos 1675 CRC32 0x59013848 Query thread_id=3 exec_time=1045 error_code=0
SET TIMESTAMP=1711468824/*!*/;
BEGIN
/*!*/;
# at 1675
#240327 0:00:24 server id 1 end_log_pos 1793 CRC32 0x18ea0602 Query thread_id=3 exec_time=1045 error_code=0
SET TIMESTAMP=1711468824/*!*/;
insert into feixi values(2,'zsx','du shu')
/*!*/;
# at 1793
#240327 0:00:24 server id 1 end_log_pos 1824 CRC32 0xa3ee2272 Xid = 171
COMMIT/*!*/;
# at 1824
#240327 0:00:31 server id 1 end_log_pos 1889 CRC32 0x072fe015 Anonymous_GTID last_committed=7 sequence_number=8
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 1889
#240327 0:00:31 server id 1 end_log_pos 1970 CRC32 0x604fa7c6 Query thread_id=3 exec_time=1038 error_code=0
SET TIMESTAMP=1711468831/*!*/;
BEGIN
/*!*/;
# at 1970
#240327 0:00:31 server id 1 end_log_pos 2089 CRC32 0x40665d54 Query thread_id=3 exec_time=1038 error_code=0
SET TIMESTAMP=1711468831/*!*/;
insert into feixi values(1,'axs','lan qiu')
/*!*/;
# at 2089
#240327 0:00:31 server id 1 end_log_pos 2120 CRC32 0x04b40d3d Xid = 177
COMMIT/*!*/;
# at 2120
#240327 0:29:55 server id 1 end_log_pos 2185 CRC32 0x04cd0143 Anonymous_GTID last_committed=8 sequence_number=9
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 2185
#240327 0:29:55 server id 1 end_log_pos 2266 CRC32 0x1ecfa84f Query thread_id=3 exec_time=0 error_code=0
SET TIMESTAMP=1711470595/*!*/;
BEGIN
/*!*/;
# at 2266
#240327 0:29:55 server id 1 end_log_pos 2387 CRC32 0x03b9763c Query thread_id=3 exec_time=0 error_code=0
SET TIMESTAMP=1711470595/*!*/;
insert into feixi values(3,'jian','cahng ge')
/*!*/;
# at 2387
#240327 0:29:55 server id 1 end_log_pos 2418 CRC32 0x3660c58a Xid = 190
COMMIT/*!*/;
# at 2418
#240327 0:30:19 server id 1 end_log_pos 2483 CRC32 0x5f53c905 Anonymous_GTID last_committed=9 sequence_number=10
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 2483
#240327 0:30:19 server id 1 end_log_pos 2564 CRC32 0x3a41501f Query thread_id=3 exec_time=0 error_code=0
SET TIMESTAMP=1711470619/*!*/;
BEGIN
/*!*/;
# at 2564
#240327 0:30:19 server id 1 end_log_pos 2683 CRC32 0xec06c70b Query thread_id=3 exec_time=0 error_code=0
SET TIMESTAMP=1711470619/*!*/;
insert into feixi values(4,'lili','shu fa')
/*!*/;
# at 2683
#240327 0:30:19 server id 1 end_log_pos 2714 CRC32 0xe701aaf8 Xid = 191
COMMIT/*!*/;
# at 2714
#240327 0:30:44 server id 1 end_log_pos 2779 CRC32 0xcbf49285 Anonymous_GTID last_committed=10 sequence_number=11
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 2779
#240327 0:30:44 server id 1 end_log_pos 2860 CRC32 0x34af4911 Query thread_id=3 exec_time=0 error_code=0
SET TIMESTAMP=1711470644/*!*/;
BEGIN
/*!*/;
# at 2860
#240327 0:30:44 server id 1 end_log_pos 2980 CRC32 0x2c1cf0e7 Query thread_id=3 exec_time=0 error_code=0
SET TIMESTAMP=1711470644/*!*/;
insert into feixi values(5,'xjin','hui hua')
/*!*/;
# at 2980
#240327 0:30:44 server id 1 end_log_pos 3011 CRC32 0x6a04023a Xid = 192
COMMIT/*!*/;
# at 3011
#240327 0:34:40 server id 1 end_log_pos 3058 CRC32 0xe5401f6e Rotate to mysql-bin.000005 pos: 4
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*/;
[root@localhost data]#
③删除数据库
④恢复最原始数据
000001里创建了anhui库,000002由于数据插入错误,000003日志中插入前两条数据,000004中插入了5条数据,我们恢复到000003即恢复到2条数据
如何恢复数据3
[root@localhost data]#pwd
/usr/local/mysql/data
[root@localhost data]#mysqlbinlog --no-defaults --base64-output=decode-rows -v /opt/mysql-bin.000004
[root@localhost data]#mysqlbinlog --no-defaults --stop-position='2564' /usr/local/mysql/data/mysql-bin.000004|mysql -u root -p123123
恢复数据5
恢复:
[root@localhost opt]#mysqlbinlog --no-defaults --start-position='2860' /usr/local/mysql/data/mysql-bin.000004|mysql -u root -p123123
8.3基于时间点恢复
mysqlbinlog [--no-defaults] --start-datetime='年-月-日 小时:分钟:秒' --stop-datetime='年-月-日小时:分钟:秒' 二进制日志 | mysql -u 用户名 -p 密码
#仅恢复到16:41:24 之前的数据,即不恢复"user4"的数据
mysqlbinlog --no-defaults --stop-datetime='2020-11-22 16:41:24' /opt/mysql-bin.000002 | mysql -uroot -p#仅恢复"user4"的数据,跳过"user3"的数据恢复
mysqlbinlog --no-defaults --start-datetime='2020-11-22 16:41:24' /opt/mysql-bin.000002 | mysql -uroot -p如果恢复某条SQL语之前的所有数据,就stop在这个语句的位置节点或者时间点
如果恢复某条SQL语句以及之后的所有数据,就从这个语句的位置节点或者时间点start
数据恢复:
[root@localhost data]#date
2024年 03月 27日 星期三 01:05:53 CST
[root@localhost data]#mysqlbinlog --no-defaults --start-datetime='2024-03-27 0:30:19' /usr/local/mysql/data/mysql-bin.000004|mysql -u root -p123123
date记得看下时间对不对哦,本机时间忘记矫正了,就用这个时间了
因为时间节点'2024-03-27 0:30:19'在数据3和数据4之间
#所以只要恢复这个节点之后的数据就可以
start:恢复之后的数据
温故而知新
1.物理冷备份
- 关闭Mysqld服务
- 使用tar命令进行打包data目录
- 恢复直接解压即可
2.逻辑备份
- mysqldump -u -p --database 备份库1,库2 > xxx.sql #备份多个数据库
- mysqldump -u -p --all-database > xxx.sql #备份所有数据库
- mysql -u -p 库1,库2 表1,表2 > xxx.sql #备份多库多表
3.完全恢复
- mysql -u -p < xxx.sql #恢复整个库
- mysql -u -p 库名 < xxx.sql #恢复表
4.增量备份
- 要先开启二进制日志,设置二进制格式为MIXED
- 进行一次完全备份,可每周备份一次,通过crontable -e 进行编辑
- mysqladmin -uroot -p flush-logs 刷新二进制日志文件 分割出二进制日志文件,由于刷新之前的数据都会记录在老的二进制文件里
- 可以通过mysqlbinlog --no-defaults --base64-output=decode-rows -v 二进制文件名称 可以查看日志内容
- 可以通过mysqlbinlog --no-defaults 二进制日志文件名 mysql -uroot -p 恢复丢失的数据库
位置恢复——position
- start:--start-position
- stop:--stop-position
- start、stop:--start-position --stop-position
时间恢复——datetime
- start:--start-datetime
- stop:--stop-datetime
- start、stop:--start-datetime --stop-datetime
更多推荐
所有评论(0)