mysql命令之mysqldump详解

一,功能

mysqldump:用来解析binlog日志。

binlog日志:用来记录对数据库进行有更新(或潜在更新,如delete语句,删除0条记录)的事件,不记录select,show等查询语句
形如以下log-files:

 mysql_bin.000001  
 mysql_bin.000002
 mysql_bin.000003
 mysql_bin.000004

二,用法

 mysqlbinlog [options] log-files
[root@www data]# mysqlbinlog mysql_bin.000027
/*!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


#180208 14:39:15 server id 1  end_log_pos 107   Start: binlog v 4, server v 5.5.32-log created 180208 14:39:15
# Warning: this binlog is either in use or was not closed properly.
BINLOG '
k/B7Wg8BAAAAZwAAAGsAAAABAAQANS41LjMyLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAAVAAEGggAAAAICAgCAA==
'/*!*/;
------------------------------------------------------------------

**这是最初的binlog日志,没有写入任何数据,以后对数据库的更新会从这里写入**

-------------------------------------------------------------------
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;

三, 常用options

为方便介绍,先创建数据库
登录:

[root@www data]# mysql -uroot -pgzn

创建数据库

mysql> create database abc;
Query OK, 1 row affected (0.00 sec)

mysql> create database xyz;
Query OK, 1 row affected (0.00 sec)

再次查看

 mysqlbinlog [options] log-files
[root@www data]# mysqlbinlog mysql_bin.000027
/*!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
#180208 14:39:15 server id 1  end_log_pos 107   Start: binlog v 4, server v 5.5.32-log created 180208 14:39:15
# Warning: this binlog is either in use or was not closed properly.
BINLOG '
k/B7Wg8BAAAAZwAAAGsAAAABAAQANS41LjMyLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAAVAAEGggAAAAICAgCAA==
'/*!*/;
——————————————————————————————————————————————————————————————————-
以上不用看,每个刚创建的binlog日志都有
                    《事件create database abc》
# at 107  (可以理解为create database abc在binlog文件中的起始位置)
#180208 14:49:16(时间:18年2月8号) 
server id 1 
end_log_pos 188 (可以理解为事件create database abc 在文件中的结束位置,同时也是下一事件的起始位置,  
Query   thread_id=3     exec_time=0     error_code=0
SET TIMESTAMP=1518072556/*!*/;

*************************************************************************************************
关于mysql数据库的若干信息,有的可以在my.cnf中配置,是全局数据库的设置
SET @@session.pseudo_thread_id=3/*!*/;                                                          *  
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0,                               *
@@session.unique_checks=1, @@session.autocommit=1/*!*/;                                         *
SET @@session.sql_mode=0/*!*/;                                                                  *
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=33/*!*/;                                                             *                            
SET @@session.lc_time_names=0/*!*/;                                                             *
SET @@session.collation_database=DEFAULT/*!*/;                                                  *
*************************************************************************************************

create database abc

/*!*/;
               《事件create databade xyz》
# at 188
#180208 14:49:27 server id 1  end_log_pos 269   Query   thread_id=3     exec_time=0     error_code=0
SET TIMESTAMP=1518072567/*!*/;

create database xyz

/*!*/;
以下不用看
————————————————————————————————————————————————————————————————————
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;

-d db_name : 从binlog日志中提取和指定库相关的sql语句

[root@www data]# mysqlbinlog -d abc mysql_bin.000027    
/*!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
#180208 14:39:15 server id 1  end_log_pos 107   Start: binlog v 4, server v 5.5.32-log created 180208 14:39:15
# Warning: this binlog is either in use or was not closed properly.
BINLOG '
k/B7Wg8BAAAAZwAAAGsAAAABAAQANS41LjMyLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAAVAAEGggAAAAICAgCAA==
'/*!*/;
# at 107
#180208 14:49:16 server id 1  end_log_pos 188   Query   thread_id=3     exec_time=0     error_code=0
SET TIMESTAMP=1518072556/*!*/;
SET @@session.pseudo_thread_id=3/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=0/*!*/;
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=33/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
————————————————————-----------------
create database abc     看这里
————————————————————------------------
/*!*/;
# at 188
————————————————————----------------
关于 数据库xyz的语句没有提取   和这里
——————————————————————---------------
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;

指定时间提取sql,
–start-datetime=’time’;
–stop-datatime=’time’ ;
time格式:2018-02-08 14:49:16

指定位置提取sql,
–start-position= pos ;
–stop-position= pos ;

猜你喜欢

转载自blog.csdn.net/gao_zhennan/article/details/79290929