[root@localhost ~]# systemctl stop mysqld
[root@localhost ~]# tar zcf /opt/mysql_all-$(date +%F).tar.gz /usr/local/mysql/data/
[root@localhost mnt]# mkdir /bak/
[root@localhost mnt]# mv /usr/local/mysql/data/ /mnt/bak/ ## 将出现问题的数据移/mnt/bak里面
[root@localhost opt]# tar zxf /opt/mysql_all-2020-08-23.tar.gz -C /mnt/ ## 解压备份的文件到mnt下
[root@localhost mnt]# mv usr/local/mysql/data/ /usr/local/mysql/ ## 将备份文件移动至库文件夹下
[root@localhost ~]# systemctl start mysqld ## 重新启动服务
mysqldump -u 用户名 -p [密码][选项][数据库] > /备份路径/备份文件名
例:
[root@localhost mysql]# mysqldump -uroot -p students > /opt/students.sql
Enter password:
mysqldump -u 用户名 -p [密码][选项] --all-databases > /备份路径/备份文件名
例:
[root@localhost mysql]# mysqldump -uroot -p --all-databases > /opt/all_data.sql
mysqldump -u 用户名 -p [密码][选项] 数据库名 表名 > /备份路径/备份文件名
例:
[root@localhost mysql]# mysqldump -uroot -p students test > /opt/students-test.sql
使用mysqldump导出的脚本,可使用导入的方法
source命令 mysql命令
使用source 恢复数据库的步骤
登录到MySQL数据库
执行source 备份sql脚本的路径
source 恢复的示例
mysql> source /opt/students.sql
[root@localhost mysql]# mysql -uroot -pabc123 students < /opt/students.sql ## mysql单个库的还原需要指定库,
## 两个或两个以上的恢复,就不需要,因为此时本分的文件里有了创建数据库的操作,单个的话没有
mysql -u [用户名] -p [密码] < 表备份脚本的路径
是上一次备份后增加备份/变化的文件或者内容(增量备份的基础是上一次的完全备份)
mysql没有直接的备份方法,可通过mysql提供的二进制日志间接备份
一般恢复:将所有二进制日志内容全部恢复
mysqlbinlog [--no-defaults] 增量备份文件 | mysql -u 用户名 -p
基于位置恢复:在某一错误操作段进行恢复,可跳过错误的操作进行恢复
恢复数据到指定位置(即到指定位置停止)
mysqlbinlog [--no-defaults] --stop-position='操作 id' 二进制文件 | mysql -u 用户名 -p 密码
mysqlbinlog [--no-defaults] --start-position='操作 id' 二进制文件 | mysql -u 用户名 -p 密码
基于时间点恢复:跳过存在错误操作的时间点
mysqlbinlog [--no-defaults] --stop-datetime='年-月-日 小时:分钟:秒' 二进制日志 | mysql -u 用户名 -p 密码``
mysqlbinlog [--no-defaults] --start-datetime='年-月-日 小时:分钟:秒' 二进制日志 | mysql -u 用户名 -p 密码
mysqlbinlog [--no-defaults] --start-datetime='年-月-日 小时:分钟:秒' --stop-datetime='年-月-日 小时:分钟:秒' 二进制日志 | mysql -u 用户名 -p 密码
vim /etc/cnf
在mysqld 中加 log-bin=mysql-bin
mysqlbinlog --no-defaults mysql-bin.000001 ## mysql-bin.000001为日志增量名
mysqlbinlog --no-defaults --base64-output=decode-rows -v mysql-bin.000001 >/opt/bk02.txt
mysqladmin -uroot -p flush-logs ## 刷新日志,生成新的存储日志的文件,备份增量日志
[root@localhost ~]# systemctl stop mysqld ## 先关闭数据库
[root@localhost ~]# vim /etc/my.cnf
[mysqld]
省略不写……
log-bin=mysql-bin ## 在mysqld模块下添加二进制日志功能
[root@localhost ~]# ls /usr/local/mysql/data/ ## mysql-bin.000001 发现二进制日志文件
auto.cnf ibdata1 ib_logfile1 mysql mysql-bin.index students
ib_buffer_pool ib_logfile0 ibtmp1 mysql-bin.000001 performance_schema sys
[root@localhost ~]# mysqldump -uroot -p students > /mnt/students.sql ## 进行数据库students的备份
[root@localhost data]# mysqladmin -uroot -pabc123 flush-logs ## 生成新的增量备份文件
mysql> select * from xuesheng; ## 查看未操作的表
+----+----------+-------+----------+-------+
| id | name | score | address | hobby |
+----+----------+-------+----------+-------+
| 1 | zhangsan | 90 | nanjing | 1 |
| 2 | lisi | 88 | chengdu | 1 |
| 3 | wangwu | 75 | shanghai | 2 |
| 4 | zhaoliu | 60 | beijing | 2 |
| 5 | tianqi | 78 | hangzhou | 1 |
| 6 | heiba | 78 | hangzhou | 1 |
| 7 | heiba1 | 78 | hangzhou | 1 |
| 8 | heiba2 | 78 | hangzhou | 1 |
+----+----------+-------+----------+-------+
8 rows in set (0.00 sec)
mysql> insert into xuesheng(name,score,address,hobby) values('tom',85,'guangzhou',1);
Query OK, 1 row affected (0.00 sec)
mysql> insert into xuesheng(name,score,address,hobby) values('jerry',62,'chongqing',2);
Query OK, 1 row affected (0.01 sec)
mysql> delete from xuesheng where id=2; ## 模拟误操作
Query OK, 1 row affected (0.00 sec)
mysql> insert into xuesheng(name,score,address,hobby) values('lilei',73,'wuhan',1);
Query OK, 1 row affected (0.00 sec)
mysql> select * from xuesheng;
+----+----------+-------+-----------+-------+
| id | name | score | address | hobby |
+----+----------+-------+-----------+-------+
| 1 | zhangsan | 90 | nanjing | 1 |
| 3 | wangwu | 75 | shanghai | 2 |
| 4 | zhaoliu | 60 | beijing | 2 |
| 5 | tianqi | 78 | hangzhou | 1 |
| 6 | heiba | 78 | hangzhou | 1 |
| 7 | heiba1 | 78 | hangzhou | 1 |
| 8 | heiba2 | 78 | hangzhou | 1 |
| 9 | tom | 85 | guangzhou | 1 |
| 10 | jerry | 62 | chongqing | 2 |
| 11 | lilei | 73 | wuhan | 1 |
+----+----------+-------+-----------+-------+
10 rows in set (0.00 sec)
[root@localhost data]# mysqladmin -uroot -pabc123 flush-logs ## 保存刚刚的增量备份二进制文件,生成新的备份文件
乱码看不懂 需解码
64位解码 -v 显示更详细信息
mysqlbinlog --no-defaults --base64-output=decode-rows -v mysql-bin.000002 >/mnt/bk02.txt
[root@localhost mnt]# vim /mnt/bk02.txt
# at 891 ## 误操作时间与位置
#200824 10:21:46 server id 1 end_log_pos 954 CRC32 0x0d15a891 Table_map: `students`.`xuesheng` mapped to number 124
# at 954
#200824 10:21:46 server id 1 end_log_pos 1015 CRC32 0xc2261844 Delete_rows: table id 124 flags: STMT_END_F
### DELETE FROM `students`.`xuesheng`
### WHERE
### @1=2
### @2='lisi'
### @3=88
### @4='chengdu'
### @5=1
# at 1187 ## 紧接着插入lilei的时间与位置
#200824 10:22:23 server id 1 end_log_pos 1250 CRC32 0xfa311240 Table_map: `students`.`xuesheng` mapped to number 124
# at 1250
#200824 10:22:23 server id 1 end_log_pos 1310 CRC32 0x9bce74dd Write_rows: table id 124 flags: STMT_END_F
### INSERT INTO `students`.`xuesheng`
### SET
### @1=11
### @2='lilei'
### @3=73
### @4='wuhan'
### @5=1
mysql> drop table xuesheng; ## 删除操作错误后存在的表
Query OK, 0 rows affected (0.01 sec)
mysql> source /mnt/students.sql ## 用完整备份的文件恢复
mysql> select * from xuesheng;
+----+----------+-------+----------+-------+
| id | name | score | address | hobby |
+----+----------+-------+----------+-------+
| 1 | zhangsan | 90 | nanjing | 1 |
| 2 | lisi | 88 | chengdu | 1 |
| 3 | wangwu | 75 | shanghai | 2 |
| 4 | zhaoliu | 60 | beijing | 2 |
| 5 | tianqi | 78 | hangzhou | 1 |
| 6 | heiba | 78 | hangzhou | 1 |
| 7 | heiba1 | 78 | hangzhou | 1 |
| 8 | heiba2 | 78 | hangzhou | 1 |
+----+----------+-------+----------+-------+
8 rows in set (0.00 sec)
[root@localhost data]# mysqlbinlog --no-defaults --stop-position='891' /usr/local/mysql/data/mysql-bin.000002 | mysql -u root -p
mysql> select * from xuesheng; ## 此时已经恢复了tom 和jerry
+----+----------+-------+-----------+-------+
| id | name | score | address | hobby |
+----+----------+-------+-----------+-------+
| 1 | zhangsan | 90 | nanjing | 1 |
| 2 | lisi | 88 | chengdu | 1 |
| 3 | wangwu | 75 | shanghai | 2 |
| 4 | zhaoliu | 60 | beijing | 2 |
| 5 | tianqi | 78 | hangzhou | 1 |
| 6 | heiba | 78 | hangzhou | 1 |
| 7 | heiba1 | 78 | hangzhou | 1 |
| 8 | heiba2 | 78 | hangzhou | 1 |
| 9 | tom | 85 | guangzhou | 1 |
| 10 | jerry | 62 | chongqing | 2 |
+----+----------+-------+-----------+-------+
[root@localhost data]# mysqlbinlog --no-defaults --start-position='1187' /usr/local/mysql/data/mysql-bin.000002 | mysql -uroot -p
mysql> select * from xuesheng; ## 恢复lilei 未删除lisi
+----+----------+-------+-----------+-------+
| id | name | score | address | hobby |
+----+----------+-------+-----------+-------+
| 1 | zhangsan | 90 | nanjing | 1 |
| 2 | lisi | 88 | chengdu | 1 |
| 3 | wangwu | 75 | shanghai | 2 |
| 4 | zhaoliu | 60 | beijing | 2 |
| 5 | tianqi | 78 | hangzhou | 1 |
| 6 | heiba | 78 | hangzhou | 1 |
| 7 | heiba1 | 78 | hangzhou | 1 |
| 8 | heiba2 | 78 | hangzhou | 1 |
| 9 | tom | 85 | guangzhou | 1 |
| 10 | jerry | 62 | chongqing | 2 |
| 11 | lilei | 73 | wuhan | 1 |
+----+----------+-------+-----------+-------+
[root@localhost data]# mysqlbinlog --no-defaults --stop-datetime='2020-08-24 10:21:46' /usr/local/mysql/data/mysql-bin.000002 |mysql -uroot -p
[root@localhost data]# mysqlbinlog --no-defaults --start-datetime='2020-08-24 10:22:23' /usr/local/mysql/data/mysql-bin.000002 | mysql -uroot -p