一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

Mysql操作binlog二进制日志数据的例子

时间:2015-06-03 编辑:简简单单 来源:一聚教程网

系统环境:

服务器系统:CentOS 6.5 x86_64
Mysql版本 :Mysql 5.1

一、binlog介绍

1.binlog,即二进制日志,它记录了数据库上的所有改变。
2.改变数据库的SQL语句执行结束时,将在binlog的末尾写入一条记录,同时通知语句解析器,语句执行完毕
3.binlog格式
1.基于语句,无法保证所有语句都在从库执行成功,比如update…limit 1;
2.基于行,将每一次发动记为binlog中的一行,在执行一个特别复杂的update或delete操作时,基于行的格式会有优势

二、登陆到mysql查看binlog

1.只查看第一个binlog文件的内容

mysql> show binlog events;
2.查看指定binlog文件的内容

mysql> show binlog events in 'binlog.000007';
3.查看当前正在写入的binlog文件

mysql> show master status \G;
4.获取binlog文件列表

mysql> show binary logs;

三、用mysqlbinlog工具查看

1.基于开始/结束时间

[root@localhost var]# /usr/local/mysql/bin/mysqlbinlog --no-defaults --start-datetime='2014-08-04 00:00:00' --stop-datetime='2014-08-13 23:59:59' -d wordpress binlog.000007
2.基于pos值

[root@localhost var]# /usr/local/mysql/bin/mysqlbinlog --no-defaults --start-position=107 --stop-position=1000 -d wordpress binlog.000007

注意:

1.不要查看当前正在写入的binlog文件
2.不要加-force参数强制访问
3.如果binlog格式是行模式的,请回-vv参数


四、用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中

热门栏目