超级有用的15个mysqlbinlog命令
当你需要恢复MySQL时,也会需要使用到二进制日志文件。
mysqlbinlog 命令,以用户可视的方式展现出二进制日志中的内容。同时,也能够将其中的内容读取出来,供其他MySQL实用程序使用。
在此示例中,我们将会触及以下内容:
- 获得当前二进制日志列表
- mysqlbinlog默许行动
- 获得特定数据库条目
- 制止恢复进程产生日志
- 在输出中控制base⑹4 BINLOG
- mysqlbinlog输出调试信息
- 跳过前N个条目
- 保存输出到文件
- 从一个特定位置提取条目
- 将条目截止到一个特定的位置
- 刷新日志以清除Binlog输出
- 在输出中只显示语句
- 查看特定开始时间的条目
- 查看特定结束时间的条目
- 从远程服务器获得二进制日志
1 获得当前二进制日志列表
+———————-+———-+
| Log_name | File_size |
+————————–+————+
| mysqld-bin.000001 | 15740 |
| mysqld-bin.000002 | 3319 |
..
..
8
|
. |
ERROR 1381 (HY000): You are not using binary logging
2
|
logging |
-rw-rw—-. 1 mysql mysql 15740 Aug 28 14:57 mysqld-bin.000001
-rw-rw—-. 1 mysql mysql 3319 Aug 28 14:57 mysqld-bin.000002
..
..
5
|
. |
2 mysqlbinlog 默许行动
1
|
000001 |
ROLLBACK/*!*/;
BINLOG ‘ IeZ4WQ8BAAAAZgAAAGoAAAABAAQANS4xLjczLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAh5nhZEzgNAAgAEgAEBAQEEgAAUwAEGggAAAAICAgC ‘/*!*/;
# at 106
#170726 14:59:31 server id 1 end_log_pos 182 Query thread_id=2 exec_time=0 error_code=0
SET TIMESTAMP=1501095571/*!*/;
SET @@session.pseudo_thread_id=2/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=1, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=0/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C latin1 *//*!*/;
SET @@session.character_set_client=8,@@session.collation_connection=8,@@session.collation_server=8/*!*/;
..
..
..
# at 14191
#170726 15:20:38 server id 1 end_log_pos 14311 Query thread_id=4 exec_time=0 error_code=0SET TIMESTAMP=1501096838/*!*/;
insert into salary(name,dept) values(‘Ritu’, ‘Accounting’)/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*
22
|
OLD_COMPLETION_TYPE* |
3 获得特定数据库条目
1
|
txt |
1
|
txt |
4 制止恢复进程产生日志
1
|
000001 |
1
|
000001 |
/*!32316 SET @OLD_SQL_LOG_BIN=@@SQL_LOG_BIN, SQL_LOG_BIN=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
3
|
; |
当使用-to-last-log选项时,这个选项也会有所帮助。另外,请记住,该命令需要root权限来履行。
5 在输出中控制base⑹4 BINLOG
- never
- always
- decode-rows
- auto(默许)
1
|
000001 |
1
|
AAAAAAAAAAAAAAh5nhZEzgNAAgAEgAEBAQEEgAAUwAEGggAAAAICAgC |
1
|
000001 |
# at 106
#170726 14:59:31 server id 1 end_log_pos 182
BINLOG ‘ k+Z4WQIBAAAATAAAALYAAAAIAAIAAAAAAAAADAAAGgAAAEAAAAEAAAAAAAAAAAYDc3RkBAgACAAI AHRoZWdlZWtzdHVmZgBCRUdJTg== ‘/*!*/;
# at 182
#170726 14:59:30 server id 1 end_log_pos 291
BINLOG ‘ kuZ4WQIBAAAAbQAAACMBAAAAAAIAAAAAAAAADAAAGgAAAEAAAAEAAAAAAAAAAAYDc3RkBAgACAAI AHRoZWdlZWtzdHVmZgBJTlNFUlQgSU5UTyB0IFZBTFVFUygxLCAnYXBwbGUnLCBOVUxMKQ== ‘/*!*/;
# at 291
#170726 14:59:30 server id 1 end_log_pos 422
BINLOG ‘ kuZ4WQIBAAAAgwAAAKYBAAAAAAIAAAAAAAAADAAAGgAAAEAAAAEAAAAAAAAAAAYDc3RkBAgACAAI AHRoZWdlZWtzdHVmZgBVUERBVEUgdCBTRVQgbmFtZSA9ICdwZWFyJywgZGF0ZSA9ICcyMDA5LTAx LTAxJyBXSEVSRSBpZCA9IDE=
10
|
= |
1
|
000001 |
$ mysqlbinlog mysqld-bin.000001
2
|
000001 |
6 mysqlbinlog输出调试信息
1
|
000001 |
User time 0.00, System time 0.00
Maximum resident set size 2848, Integral resident set size 0
Non-physical pagefaults 863, Physical pagefaults 0, Swaps 0
Blocks in 0 out 48, Messages in 0 out 0, Signals 0
Voluntary context switches 1, Involuntary context switches 2
6
|
2 |
7 跳过前N个条目
下面将跳过指定的mysql bin日志中的前10个条目。
1
|
000001 |
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/; .. ..
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
6
|
; |
8 保存输出到文件
1
|
log |
1
|
000001 |
1
|
000001 |
9 从一个特定位置提取条目
SET TIMESTAMP=1501097894/*!*/;
insert into salary values(400,’Nisha’,’Marketing’,9500)
/*!*/;
# at 15028
#170726 15:38:14 server id 1 end_log_pos 15146 Query thread_id=5 exec_time=0 error_code=0
SET TIMESTAMP=1501097894/*!*/;
insert into salary values(500,’Randy’,’Technology’,6000)
8
|
) |
1
|
15028.out |
1
|
out |
10 将条目截止到一个特定的位置
1
|
15028.out |
11 刷新日志以清除Binlog输出
1
|
out |
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
.. ..
# Warning: this binlog is either in use or was not closed properly.
..
.. .. BINLOG ‘ IeZ4WQ8BAAAAZgAAAGoAAAABAAQANS4xLjczLWxvZwAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAh5nhZEzgNAAgAEgAEBAQEEgAAUwAEGggAAAAICAgC
7
|
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAh5nhZEzgNAAgAEgAEBAQEEgAAUwAEGggAAAAICAgC |
1
|
; |
12 在输出中只显示语句
也能够使用 –short-form 选项,效果相同。
$ mysqlbinlog –short-form mysqld-bin.000001
3
|
000001 |
insert into employee values(400,’Nisha’,’Marketing’,9500)/*!*/;
SET TIMESTAMP=1501096106/*!*/;
insert into employee values(500,’Randy’,’Technology’,6000)
..
..
6
|
. |
#170726 15:08:26 server id 1 end_log_pos 1329 Query thread_id=3 exec_time=0 error_code=0
2
|
#170726 15:08:26 server id 1 end_log_pos 1329 Query thread_id=3 exec_time=0 error_code=0 |
13 查看特定开始时间的条目
1
|
000001 |
14 查看特定结束时间的条目
1
|
000001 |
15 从远程服务器获得二进制日志
此处使用-R选项。-R选项与-read-from-remote-server相同。
1
|
000001 |
- -R 选项唆使mysqlbinlog命令从远程服务器读取日志文件
- -h 指定远程服务器的ip地址
- -p 将提示输入密码。默许情况下,它将使用“root”作为用户名。也能够使用 -u 选项指定用户名。
- mysqld-bin.000001 这是在这里读到的远程服务器的二进制日志文件的名称。
下面命令与上面的命令完全相同:
1
|
000001 |
mysqlbinlog: File ‘mysqld-bin.000001’ not found (Errcode: 2)
2
|
) |
ERROR: Failed on connect: Host ‘216.172.166.27’ is not allowed to connect
to this MySQL server
3
|
server |
ERROR: Failed on connect: Access denied for user ‘root’@’216.172.166.27’ (using password: YES)
2
|
) |
1
|
> |
文章来源:丸子建站
文章标题:超级有用的15个mysqlbinlog命令
https://www.wanzijz.com/view/63602.html