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 |
...
最后使用mysqlbinlog查看mysql-bin.XXXXXX二进制日志,根据需要去搜索指定的日志:
zhanhailiang@linux-06bq:/usr/local/services/mysql/data> ../bin/mysqlbinlog mysql-bin.000001 |grep "xxxxxx"
--转自