增量备份也有很多我就来讲下用binlog日志做增量备份及恢复吧。
binlog日志概述
什么是binlog日志
也称做 二进制日志
mysql服务日志文件的一种
记录除查询之外的所有SQL命令
可用于数据备份及恢复
配置mysql主从同步的必要条件
启用日志
· server_id=数字 //指定id值(1-255)
log_bin[=目录/文件名] //启用binlog日志
max_binlog_size=数值m //指定日志文件容量,默认1G
写到mysql主配置文件中案例如下
vim /etc/my.cnf
[mysql]
.........
log_bin
server_id=51
max_binlog_size=2048m
:wq
systemctl restart mysqld //修改完配置文件后重启服务
启用日志(续1)
binlog相关文件
主机名-bin.index //索引文件
主机名-bin.000001 //第一个二进制文件
主机名-bin.000002 //第二个二进制文件
手动生成新的日志文件命令有入下所示:
systemctl restart mysqld //这是重启服务也可以刷新日志文件
mysql > flush logs; //在数据库中用这个也可以刷新日志文件
mysql -uroot -p123456 -e 'flush log' //在命令行中这样也可以刷新日志文件
mysqldump --flush-logs //这个也是在命令行中刷新日志文件
//这个就看你自己想用那一个了
清理日志文件
删除指定编号的binlog日志文件命令格式
mysql > purge master logs to “binlog文件名”
删除所有binlog日志, 重建新日志命令格式
mysql > reset master;
分析日志文件
查看日志文件当前记录格式:
mysql > show variables like “binlog_format”;
一共有三种模式分别是:
statement 报表模式
row 行模式
mixed 混合模式
修改日志记录格式
[root@localhost~]# vim /etc/my.cnf
[mysql]
.........
binlog_format="名称"
systemctl restart mysqld //重启服务
恢复数据
基本思路
使用 mysqlbinlog 提取历史SQL命令操作
用管道交给mysql命令执行
命令格式
mysqlbinlog 日志文件 | mysql -uroot -p123456
应用示列
使用编号为1的日志文件恢复数据
mysqlbinlog /mylog/plj-bin.000001 | mysql -uroot -p123456