mysql binlog

1 By default, mysql is installed without binlog enabled.

 View the C:\xampp\mysql\bin\my.ini configuration file:

# binary logging - not required for slaves, but recommended
#log-bin=mysql-bin

About log-bin is a comment, we only need to open this comment to open binlog .

#add by dhpei for open the bin log
log-bin = C:/xampp/mysql/databinlog/mylog-mysql-bin

After modifying the configuration and restarting mysql , you can see the two generated files in the databinlog directory:

mylog-mysql-bin.000001
mylog-mysql-bin.index

 

2 Command to check whether binlog is enabled 

mysql> show variables like '%log_bin%';
+---------------------------------+-------------------------------------------------+
| Variable_name 						| Value  			|
+---------------------------------+-------------------------------------------------+
| log_bin      		    			| ON	 |
| log_bin_basename       			| C:\xampp\mysql\databinlog\mylog-mysql-bin       	 |
| log_bin_index  				| C:\xampp\mysql\databinlog\mylog-mysql-bin.index 		 |
| log_bin_trust_function_creators 	 	| OFF        	 |
| log_bin_use_v1_row_events        	     | OFF        	 |
| sql_log_bin    				| ON   			|
+---------------------------------+-------------------------------------------------+	
6 rows in set

You can see that binlog is enabled, and you can see the storage path of binlog .

 

3 binlog has 3 formats, namely row , statement , mixed . The three formats have their own advantages and disadvantages, and the row mode and statement mode are mainly used.

View the current binlog format of mysql

mysql> show variables like 'binlog_format';
+---------------+-----------+
| Variable_name | Value     |
+---------------+-----------+
| binlog_format | STATEMENT |
+---------------+-----------+
1 row in set

3.1 statement mode, in the statement mode, all binlog logs are of query type, you can see that the details of the executed SQL are recorded, and the type is query .

| mylog-mysql-bin.000001 | 1908 |	Intvar	| 1 |1940  | INSERT_ID=8       								|
| mylog-mysql-bin.000001 | 1940 | 	Query	| 1 | 2067 | use `rlog`; insert into ts_blog(title,content) values ('t3','c3') 			|
| mylog-mysql-bin.000001 | 2067 | 	Xid	| 1 | 2098 | COMMIT /* xid=49 */     					|
|mylog-mysql-bin.000001  | 2098 |	Query	| 1 | 2177 | BEGIN					|
| mylog-mysql-bin.000001 | 2177 |	Query	| 1 | 2303 | use `rlog`; update ts_blog set title='t3update' where title='t3'  		|
| mylog-mysql-bin.000001 | 2303 | 	Xid 	| 1 | 2334 | COMMIT /* xid=50 */       						 	|
| mylog-mysql-bin.000001 | 2334 |	Query	| 1 | 2413 | BEGIN 				 |
| mylog-mysql-bin.000001 | 2413 | 	Query	| 1 | 2529 | use `rlog`; delete from ts_blog where title='t3update'  	 |
| mylog-mysql-bin.000001 | 2529 | 	Xid	| 1 | 2560 | COMMIT /* xid=51 */      				 	 |
+------------------------+------+-------------+-----------+-------------+-------------------------------------------------------------------+

3.2row mode

Modify the format of binlog to row , you need to modify the configuration file C:\xampp\mysql\bin\my.ini that starts mysql :

#add by dhpei for open the bin log
log-bin = C:/xampp/mysql/databinlog/mylog-mysql-bin
binlog_format=row

Restart the service mysql service after modification

mysql> show variables like 'binlog_format';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | ROW   |
+---------------+-------+
1 row in set

You can see that binlog_format has been changed to ROW .

In row mode, the binlog log is as follows, you can see that only the table_id is obtained;

mysql> show binlog events in 'mylog-mysql-bin.000003';
+------------------------+-----+-------------+-----------+-------------+---------------------------------------+
| Log_name      | Pos | Event_type  | Server_id | End_log_pos | Info       |
+------------------------+-----+-------------+-----------+-------------+---------------------------------------+
| mylog-mysql-bin.000003 |   4 | Format_desc |1 |120 		 | Server ver: 5.6.16-log, Binlog ver: 4 |
| mylog-mysql-bin.000003 | 120 | Query       |1 |192 		 | BEGIN      |
| mylog-mysql-bin.000003 | 192 | Table_map   |1 |247		 | table_id: 70 (rlog.ts_blog)  |
| mylog-mysql-bin.000003 | 247 | Write_rows  |1 |294 		 | table_id: 70 flags: STMT_END_F        |
| mylog-mysql-bin.000003 | 294 | Xid|1 |325 				 | COMMIT /* xid=11 */ |
| mylog-mysql-bin.000003 | 325 | Query       |1 |397		 | BEGIN      |
| mylog-mysql-bin.000003 | 397 | Table_map   |1 |452		 | table_id: 70 (rlog.ts_blog)  |
| mylog-mysql-bin.000003 | 452 | Update_rows |1 |518 		 | table_id: 70 flags: STMT_END_F        |
| mylog-mysql-bin.000003 | 518 | Xid|1 |549 				 | COMMIT /* xid=12 */ |
| mylog-mysql-bin.000003 | 549 | Query       |1 |621 		 | BEGIN      |
| mylog-mysql-bin.000003 | 621 | Table_map   |1 |676 		 | table_id: 70 (rlog.ts_blog)  |
| mylog-mysql-bin.000003 | 676 | Delete_rows |1 |729 		 | table_id: 70 flags: STMT_END_F        |
| mylog-mysql-bin.000003 | 729 | Xid|1 |760 				 | COMMIT /* xid=13 */ |
+------------------------+-----+-------------+-----------+-------------+---------------------------------------+

 

4 View the binlog log file currently being read and written by mysql

mysql> show master status;
+------------------------+----------+--------------+------------------+-------------------+
| File                   | Position 		| Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------------+----------+--------------+------------------+-------------------+
| mylog-mysql-bin.000003  |760 			|              |              |                |
+------------------------+----------+--------------+------------------+-------------------+
1 row in set

 

5 View all binlog log files generated by the current system

mysql> show binary logs;
+------------------------+-----------+
| Log_name               | File_size |
+------------------------+-----------+
| mylog-mysql-bin.000001 |      2560 |
| mylog-mysql-bin.000002 |       120 |
| mylog-mysql-bin.000003 |       760 |
+------------------------+-----------+
3 rows in set

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324973352&siteId=291194637