mysql binlog使用

1.binlog日志打开方法

(查看二进制日志是否打开:

mysql> show variables like 'log_%';
+---------------------------------+--------------------------+
| Variable_name | Value |
+---------------------------------+--------------------------+
| log_bin | ON |
| log_bin_trust_function_creators | OFF |
| log_error | /var/lib/mysql/sg204.err |
| log_output | FILE |
| log_queries_not_using_indexes | OFF |
| log_slave_updates | OFF |
| log_slow_queries | OFF |
| log_warnings | 1 |
+---------------------------------+--------------------------+
8 rows in set (0.00 sec)


在my.cnf文件的[mysqld]下加上一行(windows为mysql.ini)

#vi /etc/my.cnf
[mysqld]
log-bin=/var/lib/mysql/mysql-bin-log #添加这一行就ok了=号后面的路径和名字自己定义吧

注:(用rpm包安装的MySQL是不会安装/etc/my.cnf文件的,
至于为什么没有这个文件而MySQL却也能正常启动和作用,有两个说法,
第一种说法,my.cnf只是MySQL启动时的一个参数文件,可以没有它,这时MySQL会用内置的默认参数启动,
第二种说法,MySQL在启动时自动使用/usr/share/mysql目录下的my-medium.cnf文件,这种说法仅限于rpm包安装的MySQL,
解决方法,只需要复制一个/usr/share/mysql目录下的.cnf文件到/etc目录,并改名为my.cnf即可。)

2.查看自己的binlog的名称是什么

mysql> show binary logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 7661 |
+------------------+-----------+
1 row in set (0.00 sec)


3.查看二进制日志里的操作记录


mysql> show binlog events;
+------------------+------+-------------+-----------+-------------+-----------------------------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+------------------+------+-------------+-----------+-------------+-----------------------------------------------------------+
| mysql-bin.000001 | 4 | Format_desc | 1 | 107 | Server ver: 5.5.28-log, Binlog ver: 4 |
| mysql-bin.000001 | 107 | Query | 1 | 215 | use `test`; create table person(
id int(4),
name char(8)) |
| mysql-bin.000001 | 215 | Query | 1 | 283 | BEGIN |
| mysql-bin.000001 | 283 | Query | 1 | 383 | use `test`; insert into person values(' ','jack') |
| mysql-bin.000001 | 383 | Xid | 1 | 410 | COMMIT /* xid=15 */


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

详细过程如下:
C:\Program Files\MySQL\MySQL Server 5.0\bin>mysqlbinlog --start-position=4 --sto

p-position=106 mysqlbin-log.000001 > c:\\test1.txt



或者全部导出:

C:\Program Files\MySQL\MySQL Server 5.0\bin>mysqlbinlog mysqlbin-log.000001 > c:\\test1.txt





test1.txt的文件内容:

;

;

DELIMITER ;

# at 4

#110916 9:51:06 server id 1 end_log_pos 98 Start: binlog v 4, server v 5.0.45-community-nt-log created 110916 9:51:06 at startup

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

ROLLBACK;

# at 98

#110916 10:11:21 server id 1 end_log_pos 28 Intvar

SET INSERT_ID=2;

# at 126

#110916 10:11:21 server id 1 end_log_pos 143 Query thread_id=2 exec_time=0 error_code=0

use test;

SET TIMESTAMP=1316139081;

SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=1, @@session.unique_checks=1;

SET @@session.sql_mode=1344274432;

;

SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=8;

insert into User (name,password) values('ddd','222');

DELIMITER ;

# End of log file

ROLLBACK ;

;



5. 导入结果到MYSQL中进行数据恢复。



C:\Program Files\MySQL\MySQL Server 5.0\bin>mysqlbinlog --start-position=134 --stop-position=330 mysqlbin-log.000001 | mysql -uroot -p



或者

C:\Program Files\MySQL\MySQL Server 5.0\bin>mysqlbinlog --start-position=134 --stop-position=330 mysqlbin-log.000001 >test1.txt

进入MYSQL导入

mysql> source c:\\test1.txt



还有一种办法是根据日期来恢复

C:\Program Files\MySQL\MySQL Server 5.0\bin >mysqlbinlog --start-datetime="2009-09-14 0:20:00" --stop-datetim="2009-09-15 01:25:00" /diskb/bin-logs/xxx_db-bin.000001 | mysql -u root



6、查看数据

Select * from User


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值