mysql有4种不同的日志,分别是二进制日志,查询日志,慢查询日志和错误日志,这些日记记录着数据库工作的方方面面,可以帮助我们了解数据库的不同方面的踪迹,下面介绍二进制日志的作用和使用方法。
1.二进制日志(bin-log日志)用途
bin-log日志的作用和使用,bin-log日志记录了所有的DDL和DML的语句,但不包括查询的语句,语句以事件的方式保存,描述了数据的更改过程,此日志对发生灾难时数据恢复起到了极为重要的作用。
2.二进制日志(bin-log日志)操作
首先需要确定Mysql二进制日志是否开启:
1).可以通过Mysql配置文件my.cnf来确认(Mysql默认开启二进制日志记录):
# Replication Master Server (default)
# binary logging is required for replication
log-bin=mysql-bin
2).也可以通过在mysql命令行模式中查看bin-log是否开启:
mysql> show variables like "%bin%";
+-----------------------------------------+----------------------+
| Variable_name | Value |
+-----------------------------------------+----------------------+
| binlog_cache_size | 32768 |
| binlog_direct_non_transactional_updates | OFF |
| binlog_format | MIXED |
| binlog_stmt_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 | 1073741824 |
| max_binlog_stmt_cache_size | 18446744073709547520 |
| sql_log_bin | ON |
| sync_binlog | 0 |
+-----------------------------------------+----------------------+
12 rows in set (0.00 sec)
mysql> show variables like "log_bin";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin | ON |
+---------------+-------+
1 row in set (0.00 sec)
接下来查看所有的二进制日志记录文件列表(默认二进制日志文件mysql-bin.XXXXXX存放在mysql/data/目录下,可以直接去data目录下查看):
mysql> show binary logs;
+------------------+------------+
| Log_name | File_size |
+------------------+------------+
| mysql-bin.000001 | 107 |
| mysql-bin.000002 | 107 |
| mysql-bin.000003 | 107 |
| mysql-bin.000004 | 444 |
| mysql-bin.000005 | 150 |
| mysql-bin.000006 | 43875 |
| mysql-bin.000007 | 150 |
| mysql-bin.000008 | 107 |
| mysql-bin.000009 | 83748 |
| mysql-bin.000010 | 126 |
| mysql-bin.000011 | 126 |
...
3.二进制日志(bin-log日志)查看
(1)使用mysqlbinlog查看mysql-bin.XXXXXX二进制日志,根据需要去搜索指定的日志:
bin/mysqlbinlog mysql-bin.000001 |grep "xxxxxx"
(2)按日期查找保存结果:
bin\mysqlbinlog.exe --start-datetime="2013-07-00 23:30:00" --stop-datetime="2013-07-05 23:30:00" --result-file="c:\my.sql" ../data/mysql-bin.000025
4.数据恢复
(1).首先将数据存到my.sql
bin\mysqlbinlog.exe --stop-date="2011-10-30 23:30:00" --result-file="c:\my.sql" ../data/mysql-bin.000025
(2).然后开始还原数据
bin\mysql> source c:/my.sql