mysql中binlog是二进制文件了,下面我们就来为各位介绍mysql中安全删除binlog日志、使用binlog恢复数据了,希望例子可以帮助到各位朋友哦.
在数据库正常使用的时候,默认binlog会一直放在一个文件中(大约1.1G),因此导致一个文件非常大,因此在必要的时候可能需要删除一些binlog日志文件.
删除日志文件:
方式一:
[root@Master-Mysql data]# grep -E "expire_logs_days" /etc/my.cnf
expire_logs_days = 7 #删除7天前的binlog记录
方式二:
- mysql>RESETMASTER;#重置binlog
- mysql>PURGEMASTERLOGSTO'mysql-bin.000003';#删除mysql-bin.000003之前的日志
- mysql>PURGEMASTERLOGSBEFORE'2014-07-1615:07:00';#删除2014-07-1615:07:00之前的binlog日志
- mysql>PURGEMASTERLOGSBEFOREDATE_SUB(NOW(),INTERVAL3DAY);
恢复日志文件:
- [root@Master-Mysql~]#/usr/local/mysql/bin/mysql-uroot-p''-e'SHOWBINLOGEVENTS\G'
- Enterpassword:
- ***************************1.row***************************
- [root@Master-Mysql~]#/etc/init.d/mysqldrestart
- [root@Master-Mysql~]#ll/usr/local/mysql/data/
- -rw-rw----.1mysqlmysql120Jul1821:01mysql-bin.000316#重新启动生成的binlog
- [root@Master-Mysql~]#/usr/local/mysql/bin/mysql-uroot
- mysql>createdatabasehahaha;
- mysql>usehahaha;
- mysql>createtabletest(idintauto_incrementnotnullprimarykey,
- ->valint,datavarchar(20));
- mysql>insertintotest(val,data)values(10,'liang');
- mysql>insertintotest(val,data)values(20,'jia');
- mysql>insertintotest(val,data)values(30,'hui');
- mysql>showtables;
- +------------------+
- |Tables_in_hahaha|
- +------------------+
- |test|
- +------------------+
- 1rowinset(0.00sec)
- mysql>select*fromtest;
- +----+------+-------+
- |id|val|data|
- +----+------+-------+
- |1|10|liang|
- |3|20|jia|
- |5|30|hui|
- +----+------+-------+
- mysql>flushlogs;#mysql-bin.000317
- [root@Master-Mysql~]#ll/usr/local/mysql/data/
- -rw-rw----.1mysqlmysql120Jul1821:08mysql-bin.000317#刷新binlog新产生的日志,其他依次类推;
- mysql>insertintotest(val,data)values(40,'aaa');
- mysql>insertintotest(val,data)values(50,'bbb');
- mysql>insertintotest(val,data)values(60,'ccc');
- mysql>select*fromtest;
- +----+------+-------+
- |id|val|data|
- +----+------+-------+
- |1|10|liang|
- |3|20|jia|
- |5|30|hui|
- |7|40|aaa|
- |9|50|bbb|
- |11|60|ccc|
- +----+------+-------+
- mysql>deletefromtestwhereidbetween4and5;
- mysql>select*fromtest;
- +----+------+-------+
- |id|val|data|
- +----+------+-------+
- |1|10|liang|
- |3|20|jia|
- |7|40|aaa|
- |9|50|bbb|
- |11|60|ccc|
- +----+------+-------+
- mysql>insertintotest(val,data)values(70,'ddd');
- mysql>select*fromtest;
- +----+------+-------+
- |id|val|data|
- +----+------+-------+
- |1|10|liang|
- |3|20|jia|
- |7|40|aaa|
- |9|50|bbb|
- |11|60|ccc|
- |13|70|ddd|
- +----+------+-------+
- mysql>flushlogs;#mysql-bin.000318
- mysql>insertintotest(val,data)values(80,'dddd');
- mysql>insertintotest(val,data)values(90,'eeee');
- mysql>flushlogs;#mysql-bin.000319
- mysql>droptabletest;
- mysql>flushlogs;#mysql-bin.000320
- mysql>dropdatabasehahaha;
- ####################以上为测试数据####################
- [root@Master-Mysql~]#/usr/local/mysql/bin/mysqlbinlog/usr/local/mysql/data/mysql-bin.000317|/usr/local/mysql/bin/mysql-uroot
- mysql>showdatabases;
- mysql>showdatabases;
- +--------------------+
- |Database|
- +--------------------+
- |hahaha|
- 14rowsinset(0.00sec)
- mysql>select*fromtest;
- +----+------+-------+
- |id|val|data|
- +----+------+-------+
- |1|10|liang|
- |3|20|jia|
- |5|30|hui|
- +----+------+-------+
- 3rowsinset(0.00sec)
- [root@Master-Mysql~]#/usr/local/mysql/bin/mysqlbinlog/usr/local/mysql/data/mysql-bin.000318-dhahaha|/usr/local/mysql/bin/mysql-uroot
- mysql>select*fromtest;
- +----+------+-------+
- |id|val|data|
- +----+------+-------+
- |1|10|liang|
- |3|20|jia|
- |7|40|aaa|
- |9|50|bbb|
- |11|60|ccc|
- |13|70|ddd|
- +----+------+-------+
- 6rowsinset(0.00sec)
- ########################################################
- [root@Master-Mysql~]#ll/usr/local/mysql/data/
- -rw-rw----.1mysqlmysql1518Jul1821:12mysql-bin.000318
- -rw-rw----.1mysqlmysql723Jul1821:13mysql-bin.000319
- -rw-rw----.1mysqlmysql293Jul1821:14mysql-bin.000320
- -rw-rw----.1mysqlmysql3697Jul1821:26mysql-bin.000321
- [root@Master-Mysql~]#/usr/local/mysql/bin/mysqlbinlog/usr/local/mysql/data/mysql-bin.000318|grepend_log_pos
- #14071821:12:23serverid1end_log_pos120CRC320x38d71bd6Start:binlogv4,serverv5.6.16-logcreated14071821:12:23
- #14071821:13:40serverid1end_log_pos723CRC320xcaa8ac7fRotatetomysql-bin.000320pos:4
- [root@Master-Mysql~]#/usr/local/mysql/bin/mysqlbinlog--start-position=120--stop-position=1518/usr/local/mysql/data/mysql-bin.000319-dhahaha|/usr/local/mysql/bin/mysql-uroot
- mysql>select*fromtest;
- +----+------+-------+
- |id|val|data|
- +----+------+-------+
- |1|10|liang|
- |3|20|jia|
- |7|40|aaa|
- |9|50|bbb|
- |11|60|ccc|
- |13|70|ddd|
- |15|80|dddd|
- |17|90|eeee|
- +----+------+-------+
- 8rowsinset(0.00sec)
- ########################################################
- [root@Master-Mysql~]#/usr/local/mysql/bin/mysqlbinlog--start-datetime="2014-07-1821:13:59"--stop-datetime="2014-07-1821:14:03"/usr/local/mysql/data/mysql-bin.000320-dhahaha|/usr/local/mysql/bin/mysql-uroot
- mysql>showtables;
- Emptyset(0.00sec)