Binlog日志介绍
二进制log,别于数据库引擎的redo log。
binlog记录记录DML DDL 。
Binlog的记录模式
statement 有点像redis的 rdb持久化。
Binlog的文件结构
这个log的格式了解即可,查看log时再来查询。
Binlog的写入机制
Binlog 文件操作
如何做到删库而不跑路..............
binlog状态查看 & 开启binlog功能
windows 的mysql配置文件是my.ini .
linux下的mysql配置文件是 my.cnf。
查看binlog是否开启、binlog目录、
mysql> show variables like '%log_bin%' -> ; +---------------------------------+--------------------------------+ | Variable_name | Value | +---------------------------------+--------------------------------+ | log_bin | ON | | log_bin_basename | /var/lib/mysql/mysql-bin | | log_bin_index | /var/lib/mysql/mysql-bin.index | | log_bin_trust_function_creators | OFF | | log_bin_use_v1_row_events | OFF | | sql_log_bin | ON | +---------------------------------+--------------------------------+ 6 rows in set (0.00 sec)
查看binlog命令
mysql> show binary logs; +------------------+------------+ | Log_name | File_size | +------------------+------------+ | mysql-bin.000343 | 1073742436 | | mysql-bin.000344 | 1073829002 | | mysql-bin.000345 | 1073742690 | | mysql-bin.000346 | 1073747797 | | mysql-bin.000347 | 1073742425 | | mysql-bin.000348 | 1073755538 | | mysql-bin.000349 | 326803210 | +------------------+------------+ 7 rows in set (0.00 sec) mysql> show master logs; +------------------+------------+ | Log_name | File_size | +------------------+------------+ | mysql-bin.000343 | 1073742436 | | mysql-bin.000344 | 1073829002 | | mysql-bin.000345 | 1073742690 | | mysql-bin.000346 | 1073747797 | | mysql-bin.000347 | 1073742425 | | mysql-bin.000348 | 1073755538 | | mysql-bin.000349 | 326804487 | +------------------+------------+ 7 rows in set (0.00 sec)
使用mysq lbinlog恢复数据
使用mysqlbinlog 把sql文本dump出来直接用编辑器打开。
~]# locate mysql-bin.000348 /var/lib/mysql/mysql-bin.000348 ~]# mysqlbinlog /var/lib/mysql/mysql-bin.000348 > /opt/0103.sql ~]# ll /opt/ |grep 0103 -rw-r--r-- 1 root root 1296229905 Jan 3 00:34 0103.sql ~]# ll -ths /opt/ |grep 0103 1.3G -rw-r--r-- 1 root root 1.3G Jan 3 00:34 0103.sql
默认大小是1.3G.
删库了,该跑路了。不用,冷静思考不要慌。
先确认一下binlog在不在,现存哪些日志。
1、show master logs;
查看binlog内容
2、show binlog events in '';
红色框的位置就是恢复的开始和结束位置。
3、使用mysqlbinlog恢复
mysqlbinlog
mysqldump: 定期全部备份数据库,mysqlbinlog可以做增量备份和数据恢复
删除binlog
binlog超过几天自动删除:
mysql> show variables like '%expire_logs%' -> ; +------------------+-------+ | Variable_name | Value | +------------------+-------+ | expire_logs_days | 7 | +------------------+-------+