每天进步一点点mysqlmysqlbinlog_MySQL
一、 简介
mysqlbinlog:用于查看服务器生成的二进制日志的工具。
二、 命令格式
mysqlbinlog 选项日志文件1 日志文件2
三、 常用参数
-d--database=name:指定数据库名称,只列出指定数据库相关操作
-o--offset=#:忽略掉日志中前N行命令
-r--result-file=name:将输出的文本格式日志输出到指定文件
-s--short-form:显示简单格式忽略掉一些信息
--set-charset=char-name:在输出为文本格式时,在文件第一行加上set names char-name,这个选项在某些情况下装载数据时非常有用。
--start-datetime=date --stop-datetime=date指定日期间各内的所有日志
--start-position=# --stop-position=#:指定位置时间间隔内的所有日志
四、 常用例子
1. 查看当前正在使用的二进制日志文件
mysql>show master status;
+--------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB |Binlog_Ignore_DB | Executed_Gtid_Set |
+--------------+----------+--------------+------------------+-------------------+
| mysql.000007 | 120 | | | |
+--------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
2. 不加任何参数,显示所有日志
[root@localhostdata]# mysqlbinlog mysql.000007
/*!50530 SET@@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!40019 SET@@session.max_insert_delayed_threads=0*/;
/*!50003 SET@OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#150717 11:01:43 server id 1 end_log_pos 120 CRC32 0xc3bad8fd Start: binlog v 4, server v 5.6.12-logcreated 150717 11:01:43
# Warning: this binlog is either in use orwas not closed properly.
BINLOG '
F3CoVQ8BAAAAdAAAAHgAAAABAAQANS42LjEyLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAAXAAEGggAAAAICAgCAAAACgoKGRkAAf3Y
usM=
'/*!*/;
# at 120
#150717 11:05:54 server id 1 end_log_pos 219 CRC32 0x79fabcbc Query thread_id=24910 exec_time=0 error_code=0
use`test2`/*!*/;
SET TIMESTAMP=1437102354/*!*/;
SET @@session.pseudo_thread_id=24910/*!*/;
SET @@session.foreign_key_checks=1,@@session.sql_auto_is_null=0, @@session.unique_checks=1,@@session.autocommit=1/*!*/;
SET @@session.sql_mode=1075838976/*!*/;
SET @@session.auto_increment_increment=1,@@session.auto_increment_offset=1/*!*/;
/*!\C utf8 *//*!*/;
SET@@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=8/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET@@session.collation_database=DEFAULT/*!*/;
createtable t6(id int)
/*!*/;
# at 219
#150717 11:06:09 server id 1 end_log_pos 300 CRC32 0xd883c511 Query thread_id=24910 exec_time=0 error_code=0
SET TIMESTAMP=1437102369/*!*/;
BEGIN
/*!*/;
# at 300
#150717 11:06:09 server id 1 end_log_pos 399 CRC32 0x4845ae22 Query thread_id=24910 exec_time=0 error_code=0
SET TIMESTAMP=1437102369/*!*/;
insertinto t6 value(1)
/*!*/;
# at 399
#150717 11:06:09 server id 1 end_log_pos 430 CRC32 0x254bd95b Xid = 75156
COMMIT/*!*/;
# at 430
#150717 11:06:12 server id 1 end_log_pos 511 CRC32 0x400701db Query thread_id=24910 exec_time=0 error_code=0
SET TIMESTAMP=1437102372/*!*/;
BEGIN
/*!*/;
# at 511
#150717 11:06:12 server id 1 end_log_pos 610 CRC32 0x4d494d54 Query thread_id=24910 exec_time=0 error_code=0
SET TIMESTAMP=1437102372/*!*/;
insertinto t6 value(2)
/*!*/;
# at 610
#150717 11:06:12 server id 1 end_log_pos 641 CRC32 0xc0160921 Xid = 75157
COMMIT/*!*/;
# at 641
#150717 11:06:14 server id 1 end_log_pos 722 CRC32 0x722e009d Query thread_id=24910 exec_time=0 error_code=0
SET TIMESTAMP=1437102374/*!*/;
BEGIN
/*!*/;
# at 722
#150717 11:06:14 server id 1 end_log_pos 821 CRC32 0x46f08ed1 Query thread_id=24910 exec_time=0 error_code=0
SET TIMESTAMP=1437102374/*!*/;
insertinto t6 value(3)
/*!*/;
# at 821
#150717 11:06:14 server id 1 end_log_pos 852 CRC32 0x6eccabd7 Xid = 75158
COMMIT/*!*/;
# at 852
#150717 11:06:36 server id 1 end_log_pos 933 CRC32 0x1d97f072 Query thread_id=24910 exec_time=0 error_code=0
SET TIMESTAMP=1437102396/*!*/;
BEGIN
/*!*/;
# at 933
#150717 11:06:36 server id 1 end_log_pos 1034 CRC32 0xca5d7197 Query thread_id=24910 exec_time=0 error_code=0
SET TIMESTAMP=1437102396/*!*/;
deletefrom t6 where id=3
/*!*/;
# at 1034
#150717 11:06:36 server id 1 end_log_pos 1065 CRC32 0xc36e2552 Xid = 75159
COMMIT/*!*/;
# at 1065
#150717 11:21:20 server id 1 end_log_pos 1166 CRC32 0x937167ae Query thread_id=24910 exec_time=0 error_code=0
use `jiaowu`/*!*/;
SET TIMESTAMP=1437103280/*!*/;
createtable t7(id int)
/*!*/;
# at 1166
#150717 11:22:03 server id 1 end_log_pos 1261 CRC32 0xb31be6d1 Query thread_id=24910 exec_time=0 error_code=0
SET TIMESTAMP=1437103323/*!*/;
truncatetable t7
/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SETCOMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET@@SESSION.PSEUDO_SLAVE_MODE=0*/;
1. 做一些操作
mysql>create table t6(id int);
Query OK, 0 rows affected (0.02 sec)
mysql>insert into t6 value(1);
Query OK, 1 row affected (0.00 sec)
mysql>insert into t6 value(2);
Query OK, 1 row affected (0.01 sec)
mysql>insert into t6 value(3);
Query OK, 1 row affected (0.01 sec)
mysql>delete from t6 where id=3;
Query OK, 1 row affected (0.00 sec)
2. 只看jiaowu库中的信息
[root@localhostdata]# mysqlbinlog mysql.000007 -d jiaowu
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!40019 SET@@session.max_insert_delayed_threads=0*/;
/*!50003 SET@OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#150717 11:01:43 server id 1 end_log_pos 120 CRC32 0xc3bad8fd Start: binlog v 4, server v 5.6.12-logcreated 150717 11:01:43
# Warning: this binlog is either in use orwas not closed properly.
BINLOG '
F3CoVQ8BAAAAdAAAAHgAAAABAAQANS42LjEyLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAAXAAEGggAAAAICAgCAAAACgoKGRkAAf3Y
usM=
'/*!*/;
# at 120
# at 219
#150717 11:06:09 server id 1 end_log_pos 300 CRC32 0xd883c511 Query thread_id=24910 exec_time=0 error_code=0
SET TIMESTAMP=1437102369/*!*/;
SET @@session.pseudo_thread_id=24910/*!*/;
SET @@session.foreign_key_checks=1,@@session.sql_auto_is_null=0, @@session.unique_checks=1,@@session.autocommit=1/*!*/;
SET @@session.sql_mode=1075838976/*!*/;
SET @@session.auto_increment_increment=1,@@session.auto_increment_offset=1/*!*/;
/*!\C utf8 *//*!*/;
SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=8/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET@@session.collation_database=DEFAULT/*!*/;
BEGIN
/*!*/;
# at 300
# at 399
#150717 11:06:09 server id 1 end_log_pos 430 CRC32 0x254bd95b Xid = 75156
COMMIT/*!*/;
# at 430
#150717 11:06:12 server id 1 end_log_pos 511 CRC32 0x400701db Query thread_id=24910 exec_time=0 error_code=0
SET TIMESTAMP=1437102372/*!*/;
BEGIN
/*!*/;
# at 511
# at 610
#150717 11:06:12 server id 1 end_log_pos 641 CRC32 0xc0160921 Xid = 75157
COMMIT/*!*/;
# at 641
#150717 11:06:14 server id 1 end_log_pos 722 CRC32 0x722e009d Query thread_id=24910 exec_time=0 error_code=0
SET TIMESTAMP=1437102374/*!*/;
BEGIN
/*!*/;
# at 722
# at 821
#150717 11:06:14 server id 1 end_log_pos 852 CRC32 0x6eccabd7 Xid = 75158
COMMIT/*!*/;
# at 852
#150717 11:06:36 server id 1 end_log_pos 933 CRC32 0x1d97f072 Query thread_id=24910 exec_time=0 error_code=0
SET TIMESTAMP=1437102396/*!*/;
BEGIN
/*!*/;
# at 933
# at 1034
#150717 11:06:36 server id 1 end_log_pos 1065 CRC32 0xc36e2552 Xid = 75159
COMMIT/*!*/;
# at 1065
#150717 11:21:20 server id 1 end_log_pos 1166 CRC32 0x937167ae Query thread_id=24910 exec_time=0 error_code=0
use `jiaowu`/*!*/;
SET TIMESTAMP=1437103280/*!*/;
createtable t7(id int)
/*!*/;
# at 1166
#150717 11:22:03 server id 1 end_log_pos 1261 CRC32 0xb31be6d1 Query thread_id=24910 exec_time=0 error_code=0
SET TIMESTAMP=1437103323/*!*/;
truncatetable t7
/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SETCOMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET@@SESSION.PSEUDO_SLAVE_MODE=0*/;
3. 忽略前三个操作
[root@localhostdata]# mysqlbinlog mysql.000007 -o 3
/*!50530 SET@@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET@OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#150717 11:01:43 server id 1 end_log_pos 120 CRC32 0xc3bad8fd Start: binlog v 4, server v 5.6.12-logcreated 150717 11:01:43
# Warning: this binlog is either in use orwas not closed properly.
BINLOG '
F3CoVQ8BAAAAdAAAAHgAAAABAAQANS42LjEyLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAAXAAEGggAAAAICAgCAAAACgoKGRkAAf3Y
usM=
'/*!*/;
# at 300
#150717 11:06:09 server id 1 end_log_pos 399 CRC32 0x4845ae22 Query thread_id=24910 exec_time=0 error_code=0
use`test2`/*!*/;
SET TIMESTAMP=1437102369/*!*/;
SET @@session.pseudo_thread_id=24910/*!*/;
SET @@session.foreign_key_checks=1,@@session.sql_auto_is_null=0, @@session.unique_checks=1,@@session.autocommit=1/*!*/;
SET @@session.sql_mode=1075838976/*!*/;
SET @@session.auto_increment_increment=1,@@session.auto_increment_offset=1/*!*/;
/*!\C utf8 *//*!*/;
SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=8/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET@@session.collation_database=DEFAULT/*!*/;
insertinto t6 value(1)
/*!*/;
# at 399
#150717 11:06:09 server id 1 end_log_pos 430 CRC32 0x254bd95b Xid = 75156
COMMIT/*!*/;
# at 430
#150717 11:06:12 server id 1 end_log_pos 511 CRC32 0x400701db Query thread_id=24910 exec_time=0 error_code=0
SET TIMESTAMP=1437102372/*!*/;
BEGIN
/*!*/;
# at 511
#150717 11:06:12 server id 1 end_log_pos 610 CRC32 0x4d494d54 Query thread_id=24910 exec_time=0 error_code=0
SET TIMESTAMP=1437102372/*!*/;
insertinto t6 value(2)
/*!*/;
# at 610
#150717 11:06:12 server id 1 end_log_pos 641 CRC32 0xc0160921 Xid = 75157
COMMIT/*!*/;
# at 641
#150717 11:06:14 server id 1 end_log_pos 722 CRC32 0x722e009d Query thread_id=24910 exec_time=0 error_code=0
SET TIMESTAMP=1437102374/*!*/;
BEGIN
/*!*/;
# at 722
#150717 11:06:14 server id 1 end_log_pos 821 CRC32 0x46f08ed1 Query thread_id=24910 exec_time=0 error_code=0
SET TIMESTAMP=1437102374/*!*/;
insertinto t6 value(3)
/*!*/;
# at 821
#150717 11:06:14 server id 1 end_log_pos 852 CRC32 0x6eccabd7 Xid = 75158
COMMIT/*!*/;
# at 852
#150717 11:06:36 server id 1 end_log_pos 933 CRC32 0x1d97f072 Query thread_id=24910 exec_time=0 error_code=0
SET TIMESTAMP=1437102396/*!*/;
BEGIN
/*!*/;
# at 933
#150717 11:06:36 server id 1 end_log_pos 1034 CRC32 0xca5d7197 Query thread_id=24910 exec_time=0 error_code=0
SET TIMESTAMP=1437102396/*!*/;
deletefrom t6 where id=3
/*!*/;
# at 1034
#150717 11:06:36 server id 1 end_log_pos 1065 CRC32 0xc36e2552 Xid = 75159
COMMIT/*!*/;
# at 1065
#150717 11:21:20 server id 1 end_log_pos 1166 CRC32 0x937167ae Query thread_id=24910 exec_time=0 error_code=0
use `jiaowu`/*!*/;
SET TIMESTAMP=1437103280/*!*/;
createtable t7(id int)
/*!*/;
# at 1166
#150717 11:22:03 server id 1 end_log_pos 1261 CRC32 0xb31be6d1 Query thread_id=24910 exec_time=0 error_code=0
SET TIMESTAMP=1437103323/*!*/;
truncatetable t7
/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SETCOMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
4. 将输出结果导出到文件
[root@localhostdata]# mysqlbinlog mysql.000007 -o 3 -r /root/test.sql
5. 将指定时间中的内容简单显示
[root@localhostdata]# mysqlbinlog mysql.000007 --start-datetime='2015/07/17 11:06:14'--stop-datetime='2015/07/17 11:12:20'
/*!50530 SET@@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!40019 SET@@session.max_insert_delayed_threads=0*/;
/*!50003 SET@OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#150717 11:01:43 server id 1 end_log_pos 120 CRC32 0xc3bad8fd Start: binlog v 4, server v 5.6.12-logcreated 150717 11:01:43
# Warning: this binlog is either in use orwas not closed properly.
BINLOG '
F3CoVQ8BAAAAdAAAAHgAAAABAAQANS42LjEyLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAAXAAEGggAAAAICAgCAAAACgoKGRkAAf3Y
usM=
'/*!*/;
# at 641
#150717 11:06:14 server id 1 end_log_pos 722CRC32 0x722e009d Query thread_id=24910 exec_time=0 error_code=0
SET TIMESTAMP=1437102374/*!*/;
SET @@session.pseudo_thread_id=24910/*!*/;
SET @@session.foreign_key_checks=1,@@session.sql_auto_is_null=0, @@session.unique_checks=1,@@session.autocommit=1/*!*/;
SET @@session.sql_mode=1075838976/*!*/;
SET @@session.auto_increment_increment=1,@@session.auto_increment_offset=1/*!*/;
/*!\C utf8 *//*!*/;
SET@@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=8/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET@@session.collation_database=DEFAULT/*!*/;
BEGIN
/*!*/;
# at 722
#150717 11:06:14 server id 1 end_log_pos 821 CRC32 0x46f08ed1 Query thread_id=24910 exec_time=0 error_code=0
use `test2`/*!*/;
SET TIMESTAMP=1437102374/*!*/;
insert into t6 value(3)
/*!*/;
# at 821
#150717 11:06:14 server id 1 end_log_pos 852 CRC32 0x6eccabd7 Xid = 75158
COMMIT/*!*/;
# at 852
#150717 11:06:36 server id 1 end_log_pos 933 CRC32 0x1d97f072 Query thread_id=24910 exec_time=0 error_code=0
SET TIMESTAMP=1437102396/*!*/;
BEGIN
/*!*/;
# at 933
#150717 11:06:36 server id 1 end_log_pos 1034 CRC32 0xca5d7197 Query thread_id=24910 exec_time=0 error_code=0
SET TIMESTAMP=1437102396/*!*/;
delete from t6 where id=3
/*!*/;
# at 1034
#150717 11:06:36 server id 1 end_log_pos 1065CRC32 0xc36e2552 Xid = 75159
COMMIT/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SETCOMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET@@SESSION.PSEUDO_SLAVE_MODE=0*/;=0*/;
注意:--start-datetime包含所选择时间,--stop-datatime不包含所选时间,如果只写前者则表明到日志最后结束,如果只写后者则表明从日志最开始时间开始

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

The difference between clustered index and non-clustered index is: 1. Clustered index stores data rows in the index structure, which is suitable for querying by primary key and range. 2. The non-clustered index stores index key values and pointers to data rows, and is suitable for non-primary key column queries.

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

MySQL and MariaDB can coexist, but need to be configured with caution. The key is to allocate different port numbers and data directories to each database, and adjust parameters such as memory allocation and cache size. Connection pooling, application configuration, and version differences also need to be considered and need to be carefully tested and planned to avoid pitfalls. Running two databases simultaneously can cause performance problems in situations where resources are limited.

MySQL supports four index types: B-Tree, Hash, Full-text, and Spatial. 1.B-Tree index is suitable for equal value search, range query and sorting. 2. Hash index is suitable for equal value searches, but does not support range query and sorting. 3. Full-text index is used for full-text search and is suitable for processing large amounts of text data. 4. Spatial index is used for geospatial data query and is suitable for GIS applications.
