mysql> show global variables like '%bin%';

+---------------------------------+----------------------+

| Variable_name                   | Value                |

+---------------------------------+----------------------+

| binlog_cache_size               | 32768                |

| innodb_locks_unsafe_for_binlog  | OFF                  |

| log_bin                         | ON                   |

| log_bin_trust_function_creators | OFF                  |

| max_binlog_cache_size           | 18446744073709547520 |

| max_binlog_size                 | 104857600            |

| sync_binlog                     | 0                    |

+---------------------------------+----------------------+

7 rows in set (0.00 sec)

如果使用了配置文件,则可以修改 /etc/my.cnf 把里面的log-bin这一行注释掉,重启mysql服务即可关闭bin日志的记录。

如果没有主从复制,可以通过reset master的方式,重置数据库日志,清除之前的日志文件:

mysql> reset master;

Query OK, 0 rows affected (8.51 sec)

但是如果存在复制关系,应当通过PURGE的方式来清理bin日志:

语法如下:

PURGE {MASTER | BINARY} LOGS TO 'log_name'

PURGE {MASTER | BINARY} LOGS BEFORE 'date'



  用于删除列于在指定的日志或日期之前的日志索引中的所有二进制日志。这些日志也会从记录在日志索引文件中的清单中被删除,这样被给定的日志成为第一个。



  例如:



  PURGE MASTER LOGS TO 'mysql-bin.010';

  PURGE MASTER LOGS BEFORE '2008-06-23 15:00:00';

BINLOG就是一个记录SQL语句的过程,和普通的LOG一样。不过只是她是二进制存储,普通的是十进制存储罢了。

1、配置文件里要写的东西:

[mysqld]

log-bin=mysql-bin(名字可以改成自己的,如果不改名字的话,默认是以主机名字命名)

重新启动MSYQL服务。

二进制文件里面的东西显示的就是执行所有语句的详细记录,当然一些语句不被记录在内,要了解详细的,见手册页。



2、查看自己的BINLOG的名字是什么。

show binlog events;

query result(1 records)

Log_name Pos Event_type Server_id End_log_pos Info
yueliangdao_binglog.000001 4 Format_desc 1 106 Server ver: 5.1.22-rc-community-log, Binlog ver: 4

3、我做了几次操作后,她就记录了下来。

又一次 show binlog events 的结果。

query result(4 records)

Log_name Pos Event_type Server_id End_log_pos Info
yueliangdao_binglog.000001 4 Format_desc 1 106 Server ver: 5.1.22-rc-community-log, Binlog ver: 4
yueliangdao_binglog.000001 106 Intvar 1 134 INSERT_ID=1
yueliangdao_binglog.000001 134 Query 1 254 use `test`; create table a1(id int not null auto_increment primary key, str varchar(1000)) engine=myisam
yueliangdao_binglog.000001 254 Query 1 330 use `test`; insert into a1(str) values ('I love you'),('You love me')
yueliangdao_binglog.000001 330 Query 1 485 use `test`; drop table a1


4、用mysqlbinlog 工具来显示记录的二进制结果,然后导入到文本文件,为了以后的恢复。

详细过程如下:

D:\LAMP\MYSQL5\data>mysqlbinlog --start-position=4 --stop-position=106 yueliangd

ao_binglog.000001 > c:\\test1.txt

test1.txt的文件内容:



/*!40019 SET @@session.max_insert_delayed_threads=0*/;

/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;

DELIMITER /*!*/;

# at 4

#7122 16:9:18 server id 1 end_log_pos 106     Start: binlog v 4, server v 5.1.22-rc-community-log created 7122 16:9:18 at startup

# Warning: this binlog was not closed properly. Most probably mysqld crashed writing it.

ROLLBACK/*!*/;

DELIMITER ;

# End of log file

ROLLBACK /* added by mysqlbinlog */;

/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;

第二行的记录:

D:\LAMP\MYSQL5\data>mysqlbinlog --start-position=106 --stop-position=134 yuelian

gdao_binglog.000001 > c:\\test1.txt

test1.txt内容如下:



/*!40019 SET @@session.max_insert_delayed_threads=0*/;

/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;

DELIMITER /*!*/;

# at 106

#7122 16:22:36 server id 1 end_log_pos 134     Intvar

SET INSERT_ID=1/*!*/;

DELIMITER ;

# End of log file

ROLLBACK /* added by mysqlbinlog */;

/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;



第三行记录:

D:\LAMP\MYSQL5\data>mysqlbinlog --start-position=134 --stop-position=254 yuelian

gdao_binglog.000001 > c:\\test1.txt

内容:

/*!40019 SET @@session.max_insert_delayed_threads=0*/;

/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;

DELIMITER /*!*/;

# at 134

#7122 16:55:31 server id 1 end_log_pos 254     Query    thread_id=1    exec_time=0    error_code=0

use test/*!*/;

SET TIMESTAMP=1196585731/*!*/;

SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=1, @@session.unique_checks=1/*!*/;

SET @@session.sql_mode=1344274432/*!*/;

/*!\C utf8 *//*!*/;

SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=33/*!*/;

create table a1(id int not null auto_increment primary key,

str varchar(1000)) engine=myisam/*!*/;

DELIMITER ;

# End of log file

ROLLBACK /* added by mysqlbinlog */;

/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;



/*!40019 SET @@session.max_insert_delayed_threads=0*/;



第四行的记录:

D:\LAMP\MYSQL5\data>mysqlbinlog --start-position=254 --stop-position=330 yuelian

gdao_binglog.000001 > c:\\test1.txt

/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;

DELIMITER /*!*/;

# at 254

#7122 16:22:36 server id 1 end_log_pos 330     Query    thread_id=1    exec_time=0    error_code=0

use test/*!*/;

SET TIMESTAMP=1196583756/*!*/;

SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=1, @@session.unique_checks=1/*!*/;

SET @@session.sql_mode=1344274432/*!*/;

/*!\C utf8 *//*!*/;

SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=33/*!*/;

use `test`; insert into a1(str) values ('I love you'),('You love me')/*!*/;

DELIMITER ;

# End of log file

ROLLBACK /* added by mysqlbinlog */;

/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;



5、查看这些东西是为了恢复数据,而不是为了好玩。所以我们最中还是为了要导入结果到MYSQL中。



D:\LAMP\MYSQL5\data>mysqlbinlog --start-position=134 --stop-position=330 yuelian

gdao_binglog.000001 | mysql -uroot -p



或者

D:\LAMP\MYSQL5\data>mysqlbinlog --start-position=134 --stop-position=330 yuelian

gdao_binglog.000001 >test1.txt

进入MYSQL导入

mysql> source c:\\test1.txt

Query OK, 0 rows affected (0.00 sec)



Query OK, 0 rows affected (0.00 sec)



Database changed

Query OK, 0 rows affected (0.00 sec)



Query OK, 0 rows affected (0.00 sec)



Query OK, 0 rows affected (0.00 sec)



Charset changed

Query OK, 0 rows affected (0.00 sec)



Query OK, 0 rows affected (0.03 sec)



Query OK, 0 rows affected (0.00 sec)



Query OK, 0 rows affected (0.00 sec)

6、查看数据:

mysql> show tables;

+----------------+

| Tables_in_test |

+----------------+

| a1             |

+----------------+

1 row in set (0.01 sec)



mysql> select * from a1;

+----+-------------+

| id | str         |

+----+-------------+

| 1 | I love you |

| 2 | You love me |

+----+-------------+

2 rows in set (0.00 sec)
 
 
将一个mysqlbinlog文件导为sql文件
cd  cd /usr/local/mysql

./mysqlbinlog /usr/local/mysql/data/mysql-bin.000001 > /opt/001.sql
将mysql-bin.000001日志文件导成001.sql
可以在mysqlbinlog语句中通过--start-date和--stop-date选项指定DATETIME格式的起止时间
./mysqlbinlog --stop-date="2009-04-10 17:41:28" /usr/local/mysql/data/mysql-bin.000002 > /opt/004.sql
将mysql-bin.000002文件中截止到2009-04-10 17:41:28的日志导成004.sql
 
./mysqlbinlog --start-date="2009-04-10 17:30:05" --stop-date="2009-04-10 17:41:28" /usr/local/mysql/data/mysql-bin.000002  /usr/local/mysql/data/mysql-bin.0000023> /opt/004.sql
----如果有多个binlog文件,中间用空格隔开,打上完全路径
 
./mysqlbinlog --start-date="2009-04-10 17:30:05" --stop-date="2009-04-10 17:41:28" /usr/local/mysql/data/mysql-bin.000002 |mysql -u root -p123456
或者  source /opt/004.sql
将mysql-bin.000002日志文件中从2009-04-10 17:30:05到2008-04-10 17:41:28截止的sql语句导入到mysql中

mysql的binlog的更多相关文章

  1. 【转】【MySQL】mysql 通过bin-log恢复数据方法详解

    mysql中bin-log在mysql默认状态下是没有打开的,我们要先打开mysql 开启bin-log功能,然后再通过备份的bin-log进行数据库恢复了. 具体的操作是通过mysqlbinlog这 ...

  2. mysql开启binlog

    mysql开启binlog,至于为什么要开启binlog,可以google下. ## 设置server_id,一般设置为IP server_id= ## 复制过滤:需要备份的数据库名,多个库以逗号分隔 ...

  3. MySQL的binlog数据如何查看

    binlog介绍 binlog,即二进制日志,它记录了数据库上的所有改变. 改变数据库的SQL语句执行结束时,将在binlog的末尾写入一条记录,同时通知语句解析器,语句执行完毕. binlog格式 ...

  4. 解说mysql之binlog日志以及利用binlog日志恢复数据

    众所周知,binlog日志对于mysql数据库来说是十分重要的.在数据丢失的紧急情况下,我们往往会想到用binlog日志功能进行数据恢复(定时全备份+binlog日志恢复增量数据部分),化险为夷! 废 ...

  5. mysql 利用binlog增量备份,还原实例

    mysql 利用binlog增量备份,还原实例 张映 发表于 2010-09-29 分类目录: mysql 标签:binlog, mysql, mysqldump, 增量备份 一,什么是增量备份 增量 ...

  6. 不小心删除数据--利用MySQL的binlog恢复数据

    MySQL Binary Log也就是常说的bin-log, ,是mysql执行改动产生的二进制日志文件,其主要作用有两个: * 数据回复 * 主从数据库.用于slave端执行增删改,保持与maste ...

  7. mysql通过binlog日志来恢复数据

    简介 在生产的过程中有这么一个业务场景:比如我在2016-11-19 09:30:00 通过mysqldump的方式备份了数据库,但是在2016-11-19 10:30:00的时候数据库崩溃了,如果通 ...

  8. 【MySQL】binlog缓存的问题和性能

    之前在没有备库的情况下,遇到过more than 'max_binlog_cache_size' bytes of storage 的错误,今天在主备复制的时候又遇到了这个问题 Last_SQL_Er ...

  9. SQL注入实验,PHP连接数据库,Mysql查看binlog,PreparedStatement,mysqli, PDO

    看到有人说了判断能否sql注入的方法: 简单的在参数后边加一个单引号,就可以快速判断是否可以进行SQL注入,这个百试百灵,如果有漏洞的话,一般会报错. 下面内容参考了这两篇文章 http://blog ...

  10. [转载]mysql的binlog安全删除

    [转载]mysql的binlog安全删除  理论上,应该在配置文件/etc/my.cnf中加上binlog过期时间的配置项,expire_logs_days = 10 但是如果没有加这一项,随着产生越 ...

随机推荐

  1. bootstrap学习笔记<十>(按钮组,导航)

    1)按钮组.样式:class="btn-group" <div class="btn-group"> <button class=" ...

  2. 我想有个梦想(I want have a dream)

    成东青说过:梦想是什么,梦想就是你坚持就觉得是幸福东西. 多好啊,有梦想,想想就觉得幸福的.也许你会觉得有点做作,但我真觉得是这样.没有梦想就像一个没有了灵魂的躯壳,整天浑浑噩噩,整天麻木的上班下班, ...

  3. MonkeyRunner学习(1)测试连接

    前期的环境搭建详见:http://www.cnblogs.com/peng-lan/p/5388488.html 1.打开模拟器 打开模拟器有两种方法,正常的在eclipse 中启动就不介绍了 另一种 ...

  4. 对于syncedmen类的代码分析

    对于数据在cpu与GPU之间同步的问题,caffe中用syncedMemory这个类来解 决:在GPU模式下,并且使用CUDA时,可以用CaffeMallocHost函数与CaffeFreeHost函 ...

  5. Java 13 字符串

    1 String对象不可变 每一个修改String值的方法 实际上都是创建一个全新的String对象 public class Immutable { public static String upc ...

  6. Maven项目的发布,发布到Nexus

    原文:http://blog.csdn.net/mexican_jacky/article/details/50277505 第一步: 配置信息如下: <!-- 发布项目的配置 -->  ...

  7. svn设置提交忽略某些文件或文件夹

    在svn客户端,想设置忽略提交.class文件,通过 properties > New > Other 添加一个忽略的属性,,还是不行:部分屏蔽了,部分class还是在列表中 再次参考了一 ...

  8. spring配置详解

    1.前言 公司老项目的后台,均是基于spring框架搭建,其中还用到了log4j.jar等开源架包.在新项目中,则是spring和hibernate框架均有使用,利用了hibernate框架,来实现持 ...

  9. linux套件安装过程中configure,make,make install的作用

    ./configure,make,make install都是典型的使用GNU的AUTOCONF和AUTOMAKE产生的程序的安装步骤.其中: ./configure是检测程序文件,用来检测你的安装平 ...

  10. mysql命令行登录指定编码

    mysql --default-character-set=utf8  -uroot -p mysql --default-character-set-utf8 -uroot-p