MySQL二进制日志总结

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL二进制日志总结相关的知识,希望对你有一定的参考价值。

二进制日志简单介绍

 

mysql的二进制日志(binary log)是一个二进制文件,主要用于记录修改数据或有可能引起数据变更的MySQL语句。二进制日志(binary log)中记录了对MySQL数据库执行更改的所有操作,并且记录了语句发生时间、执行时长、操作数据等其它额外信息,但是它不记录SELECT、SHOW等那些不修改数据的SQL语句。二进制日志(binary log)主要用于数据库恢复和主从复制,以及审计(audit)操作。

 

官方文档关于二进制日志(binary log)的介绍如下:

The binary log contains “events” that describe database changes such as table creation operations or changes to table data. It also contains events for statements that potentially could have made changes (for example, a DELETE which matched no rows), unless row-based logging is used. The binary log also contains information about how long each statement took that updated data. The binary log has two important purposes:

· For replication, the binary log on a master replication server provides a record of the data changes to be sent to slave servers. The master server sends the events contained in its binary log to its slaves, which execute those events to make the same data changes that were made on the master. See Section 17.2, “Replication Implementation”.

· Certain data recovery operations require use of the binary log. After a backup has been restored, the events in the binary log that were recorded after the backup was made are re-executed. These events bring databases up to date from the point of the backup. See Section 7.5, “Point-in-Time (Incremental) Recovery Using the Binary Log”.

The binary log is not used for statements such as SELECT or SHOW that do not modify data. To log all statements (for example, to identify a problem query), use the general query log. See Section 5.4.3, “The General Query Log”.

Running a server with binary logging enabled makes performance slightly slower. However, the benefits of the binary log in enabling you to set up replication and for restore operations generally outweigh this minor performance decrement.

 

二进制日志状态查看

 

系统变量log_bin的值为OFF表示没有开启二进制日志(binary log)。ON表示开启了二进制日志(binary log)

mysql> show variables like \'log_bin\';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin       | ON    |
+---------------+-------+
1 row in set (0.00 sec)
 
mysql> 

 

查看当前服务器所有的二进制日志文件

mysql> show binary logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000004 | 478421400 |
| mysql-bin.000005 |      9653 |
| mysql-bin.000006 | 340631484 |
+------------------+-----------+
3 rows in set (0.00 sec)

 

当然你还可以使用下面命令查看

mysql> show master logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000004 | 478421400 |
| mysql-bin.000005 |      9653 |
| mysql-bin.000006 |  340631484 |
+------------------+-----------+
3 rows in set (0.00 sec)
 
mysql> 

 

查看当前二进制日志文件状态

mysql> show master status;
+------------------+-----------+--------------+------------------+-------------------+
| File             | Position  | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+-----------+--------------+------------------+-------------------+
| mysql-bin.000006 | 373655406 |              |                  |                   |
+------------------+-----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
 
mysql> 

clip_image001

 

二进制日志开启方法

 

查看系统变量log_bin,如果其值为OFF,表示没有开启二进制日志(binary log),如果需要开启二进制日志,则必须在my.cnf中[mysqld]下面添加log-bin [=DIR\\[filename]] ,DIR参数指定二进制文件的存储路径;filename参数指定二级制文件的文件名。 其中filename可以任意指定,但最好有一定规范。系统变量log_bin是静态参数,不能动态修改的(因为它不是Dynamic Variable)。如下所示:

mysql> show variables like \'log_bin\';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin       | OFF   |
+---------------+-------+
1 row in set (0.00 sec)
 
mysql> set global log_bin=mysql_bin;
ERROR 1238 (HY000): Variable \'log_bin\' is a read only variable
mysql> 

 

1:修改my.cnf,在[mysqld]下面增加log_bin=mysql_bin_log,重启MySQL后,你就会发现log_bin变为了ON,二进制日志(binary log)默认放在数据目录下(系统变量datadir下),如下所示:

mysql> show variables like \'log_bin\';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin       | ON    |
+---------------+-------+
1 row in set (0.00 sec)
 
mysql> show variables like \'datadir\';
+---------------+-----------------+
| Variable_name | Value           |
+---------------+-----------------+
| datadir       | /var/lib/mysql/ |
+---------------+-----------------+
1 row in set (0.00 sec)
 
mysql> show variables like \'%log_bin%\';
+---------------------------------+------------------------------------+
| Variable_name                   | Value                              |
+---------------------------------+------------------------------------+
| log_bin                         | ON                                 |
| log_bin_basename                | /var/lib/mysql/mysql_bin_log       |
| log_bin_index                   | /var/lib/mysql/mysql_bin_log.index |
| log_bin_trust_function_creators | OFF                                |
| log_bin_use_v1_row_events       | OFF                                |
| sql_log_bin                     | ON                                 |
+---------------------------------+------------------------------------+
6 rows in set (0.00 sec)
 
mysql> 

 

2:如果在my.cnf里面只设置log_bin,但是不指定file_name,然后重启数据库。你会发现二进制日志文件名称为${hostname}-bin 这样的格式。如下所示:

[mysqld]

log_bin

 

mysql> show variables like \'%log_bin%\';
+---------------------------------+------------------------------------+
| Variable_name                   | Value                              |
+---------------------------------+------------------------------------+
| log_bin                         | ON                                 |
| log_bin_basename                | /var/lib/mysql/DB-Server-bin       |
| log_bin_index                   | /var/lib/mysql/DB-Server-bin.index |
| log_bin_trust_function_creators | OFF                                |
| log_bin_use_v1_row_events       | OFF                                |
| sql_log_bin                     | ON                                 |
+---------------------------------+------------------------------------+
6 rows in set (0.00 sec)
 
mysql> 

 

 

3:当然你可以可以指定二进制日志的路径位置,如下所示:

log_bin=/mysql/bin_log/mysql_binlog

clip_image002

 

二进制日志切换方法

 

使用命令flush logs切换二进制日志,如下所示:

mysql> show master status;
+----------------------+----------+--------------+------------------+-------------------+
| File                 | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------------+----------+--------------+------------------+-------------------+
| DB-Server-bin.000002 |      120 |              |                  |                   |
+----------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
 
mysql> flush logs;
Query OK, 0 rows affected (0.03 sec)
 
mysql> show master status
    -> ;
+----------------------+----------+--------------+------------------+-------------------+
| File                 | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------------+----------+--------------+------------------+-------------------+
| DB-Server-bin.000003 |      120 |              |                  |                   |
+----------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
 
mysql> 

clip_image003

 

请注意,每次重启MySQL服务也会生成一个新的二进制日志文件,相当于二进制日志切换。切换二进制日志时,你会看到这些number会不断递增。另外,除了这些二进制日志文件外,你会看到还生成了一个DB-Server-bin.index的文件,这个文件中存储所有二进制日志文件的清单又称为二进制文件的索引。

[root@DB-Server mysql]# ls -lrt DB-Server-bin*
-rw-rw---- 1 mysql mysql 143 Mar 22 10:55 DB-Server-bin.000001
-rw-rw---- 1 mysql mysql 171 Mar 22 11:20 DB-Server-bin.000002
-rw-rw---- 1 mysql mysql 171 Mar 22 11:23 DB-Server-bin.000003
-rw-rw---- 1 mysql mysql 171 Mar 22 11:24 DB-Server-bin.000004
-rw-rw---- 1 mysql mysql 138 Mar 22 11:24 DB-Server-bin.index
-rw-rw---- 1 mysql mysql 120 Mar 22 11:24 DB-Server-bin.000006
-rw-rw---- 1 mysql mysql 171 Mar 22 11:24 DB-Server-bin.000005
[root@DB-Server mysql]# more DB-Server-bin.index 
./DB-Server-bin.000001
./DB-Server-bin.000002
./DB-Server-bin.000003
./DB-Server-bin.000004
./DB-Server-bin.000005
./DB-Server-bin.000006
[root@DB-Server mysql]# 

clip_image004

<

以上是关于MySQL二进制日志总结的主要内容,如果未能解决你的问题,请参考以下文章

MySQL二进制日志(binary log)总结

Mysql数据库之Binlog日志使用总结

linux学习第十周总结

Mysql数据库之Binlog日志使用总结

(转)Mysql数据库之Binlog日志使用总结

mysql 点总结