mysql数据库mysqlbinlog二进制日志文件挖掘

点击(此处)折叠或打开

阜平网站建设公司成都创新互联,阜平网站设计制作,有大型网站制作公司丰富经验。已为阜平上千家提供企业网站建设服务。企业网站搭建\成都外贸网站建设公司要多少钱,请找那个售后服务好的阜平做网站的公司定做!

  1. 1.查看MySQL数据库是否开启二进制日志log_bin的value值为ON为开启

  2. mysql> show variables like 'log_%';
  3. +----------------------------------------+-------------------------------------+
  4. | Variable_name | Value |
  5. +----------------------------------------+-------------------------------------+
  6. | log_bin | ON |
  7. | log_bin_basename | /data/db/mysql/3306/mysql-bin |
  8. | log_bin_index | /data/db/mysql/3306/mysql-bin.index |
  9. | log_bin_trust_function_creators | OFF |
  10. | log_bin_use_v1_row_events | OFF |
  11. | log_builtin_as_identified_by_password | OFF |
  12. | log_error | /data/db/mysql/3306/mariadb.log |
  13. | log_error_verbosity | 3 |
  14. | log_output | FILE |
  15. | log_queries_not_using_indexes | OFF |
  16. | log_slave_updates | OFF |
  17. | log_slow_admin_statements | OFF |
  18. | log_slow_slave_statements | OFF |
  19. | log_statements_unsafe_for_binlog | ON |
  20. | log_syslog | OFF |
  21. | log_syslog_facility | daemon |
  22. | log_syslog_include_pid | ON |
  23. | log_syslog_tag | |
  24. | log_throttle_queries_not_using_indexes | 0 |
  25. | log_timestamps | UTC |
  26. | log_warnings | 2 |
  27. +----------------------------------------+-------------------------------------+
  28. 21 rows in set (0.01 sec)

  29. 2.查看时间

  30. mysql> select now();
  31. +---------------------+
  32. | now() |
  33. +---------------------+
  34. | 2017-10-20 19:26:55 |
  35. +---------------------+
  36. 1 row in set (0.00 sec)

  37. 3.查看bin日志文件

  38. mysql> show master logs;
  39. +------------------+-----------+
  40. | Log_name | File_size |
  41. +------------------+-----------+
  42. | mysql-bin.000044 | 870441798 |
  43. +------------------+-----------+
  44. 1 rows in set (0.00 sec)

  45. 4.创建测试表插入数据
  46. mysql> create table t(id int,name varchar(10));
  47. Query OK, 0 rows affected (0.26 sec)
  48. mysql> select * from t;
  49. Empty set (0.00 sec)

  50. mysql> insert into t(id,name)values (1,'a');
  51. Query OK, 1 row affected (0.00 sec)

  52. mysql> insert into t(id,name)values (1,'a');
  53. Query OK, 1 row affected (0.01 sec)

  54. mysql> insert into t(id,name)values (2,'b');
  55. Query OK, 1 row affected (0.00 sec)

  56. mysql> insert into t(id,name)values (2,'b');
  57. Query OK, 1 row affected (0.00 sec)

  58. mysql> insert into t(id,name)values (3,'c');
  59. Query OK, 1 row affected (0.00 sec)

  60. mysql> insert into t(id,name)values (3,'c');
  61. Query OK, 1 row affected (0.01 sec)

  62. mysql> select * from t;
  63. +------+------+
  64. | id | name |
  65. +------+------+
  66. | 1 | a |
  67. | 1 | a |
  68. | 2 | b |
  69. | 2 | b |
  70. | 3 | c |
  71. | 3 | c |
  72. +------+------+
  73. 6 rows in set (0.00 sec)

  74. mysql> insert into t select * from t;
  75. Query OK, 6 rows affected (0.00 sec)
  76. Records: 6 Duplicates: 0 Warnings: 0

  77. mysql> select * from t;
  78. +------+------+
  79. | id | name |
  80. +------+------+
  81. | 1 | a |
  82. | 1 | a |
  83. | 2 | b |
  84. | 2 | b |
  85. | 3 | c |
  86. | 3 | c |
  87. | 1 | a |
  88. | 1 | a |
  89. | 2 | b |
  90. | 2 | b |
  91. | 3 | c |
  92. | 3 | c |
  93. +------+------+
  94. 12 rows in set (0.00 sec)



  95. 5.删除数据
  96. mysql> select now();
  97. +---------------------+
  98. | now() |
  99. +---------------------+
  100. | 2017-10-20 19:27:46 |
  101. +---------------------+
  102. 1 row in set (0.00 sec)

  103. mysql> delete from t;
  104. Query OK, 12 rows affected (0.01 sec)


  105. mysql> flush logs;
  106. Query OK, 0 rows affected (0.02 sec)

  107. mysql> show master logs;
  108. +------------------+-----------+
  109. | Log_name | File_size |
  110. +------------------+-----------+
  111. | mysql-bin.000044 | 870441798 |
  112. | mysql-bin.000045 | 154 |
  113. | mysql-bin.000046 | 2690 |
  114. | mysql-bin.000047 | 448 |
  115. +------------------+-----------+
  116. 4 rows in set (0.00 sec)

  117. 刷新日志后会看到有三个二进制bin文件生成

  118. 6.提取bin文件中的sql(基于时间的数据恢复)
  119. [root@msp binlog]# ls
  120. bak.sql bin.sql test.sql
  121. [root@msp binlog]# mysqlbinlog --start-datetime="2017-10-20 19:26:55" --stop-datetime="2017-10-20 19:27:46" /data/db/mysql/3306/mysql-bin.000045 >/root/binlog/t.sql
  122. [root@msp binlog]# ls
  123. bak.sql bin.sql test.sql t.sql


  124. 7.进行数据恢复

  125. mysql> source /root/binlog/t.sql;
  126. Query OK, 0 rows affected (0.00 sec)

  127. Query OK, 0 rows affected (0.00 sec)

  128. Query OK, 0 rows affected (0.00 sec)

  129. Query OK, 0 rows affected (0.00 sec)

  130. Query OK, 0 rows affected (0.00 sec)

  131. Query OK, 0 rows affected (0.00 sec)

  132. Query OK, 0 rows affected (0.00 sec)

  133. Query OK, 0 rows affected (0.00 sec)

  134. Query OK, 0 rows affected (0.00 sec)

  135. Query OK, 0 rows affected (0.00 sec)

  136. Query OK, 0 rows affected (0.00 sec)

  137. Charset changed
  138. Query OK, 0 rows affected (0.00 sec)

  139. Query OK, 0 rows affected (0.00 sec)

  140. Query OK, 0 rows affected (0.00 sec)

  141. Query OK, 0 rows affected, 1 warning (0.00 sec)

  142. Query OK, 0 rows affected (0.00 sec)

  143. Query OK, 0 rows affected (0.00 sec)

  144. Query OK, 0 rows affected (0.02 sec)

  145. Query OK, 0 rows affected (0.00 sec)

  146. Query OK, 0 rows affected (0.00 sec)

  147. Query OK, 0 rows affected (0.00 sec)

  148. Query OK, 0 rows affected (0.00 sec)

  149. Query OK, 0 rows affected (0.00 sec)

  150. Query OK, 0 rows affected (0.00 sec)

  151. Query OK, 0 rows affected (0.00 sec)

  152. Query OK, 0 rows affected (0.00 sec)

  153. Query OK, 0 rows affected (0.00 sec)

  154. [root@msp binlog]# rm -f t.sql
  155. [root@msp binlog]# ls
  156. bak.sql bin.sql test.sql
  157. [root@msp binlog]# mysqlbinlog --start-datetime="2017-10-20 19:26:55" --stop-datetime="2017-10-20 19:27:46" /data/db/mysql/3306/mysql-bin.000046 >>/root/binlog/t.sql
  158. [root@msp binlog]# ls
  159. bak.sql bin.sql test.sql t.sql
  160. [root@msp binlog]# ll
  161. total 2715356
  162. -rw-r--r--. 1 root root 6834 Oct 19 17:28 bak.sql
  163. -rw-r--r--. 1 root root 2780497429 Oct 19 17:21 bin.sql
  164. -rw-r--r--. 1 root root 9193 Oct 19 17:36 test.sql
  165. -rw-r--r--. 1 root root 2112 Oct 20 19:44 t.sql
  166. [root@msp binlog]# mysqlbinlog --start-datetime="2017-10-20 19:26:55" --stop-datetime="2017-10-20 19:27:46" /data/db/mysql/3306/mysql-bin.000047 >>/root/binlog/t.sql
  167. [root@msp binlog]# ll
  168. total 2715356
  169. -rw-r--r--. 1 root root 6834 Oct 19 17:28 bak.sql
  170. -rw-r--r--. 1 root root 2780497429 Oct 19 17:21 bin.sql
  171. -rw-r--r--. 1 root root 9193 Oct 19 17:36 test.sql
  172. -rw-r--r--. 1 root root 2448 Oct 20 19:44 t.sql

  173. mysql> source /root/binlog/t.sql;
  174. Query OK, 0 rows affected (0.00 sec)

  175. Query OK, 0 rows affected (0.00 sec)

  176. Query OK, 0 rows affected (0.00 sec)

  177. Query OK, 0 rows affected (0.00 sec)

  178. Query OK, 0 rows affected (0.00 sec)

  179. Query OK, 0 rows affected (0.00 sec)

  180. Query OK, 0 rows affected (0.00 sec)

  181. Query OK, 0 rows affected (0.00 sec)

  182. Query OK, 0 rows affected (0.00 sec)

  183. Query OK, 0 rows affected (0.00 sec)

  184. Query OK, 0 rows affected (0.00 sec)

  185. Charset changed
  186. Query OK, 0 rows affected (0.00 sec)

  187. Query OK, 0 rows affected (0.00 sec)

  188. Query OK, 0 rows affected (0.00 sec)

  189. Query OK, 0 rows affected, 1 warning (0.00 sec)

  190. Query OK, 0 rows affected (0.00 sec)

  191. Query OK, 0 rows affected (0.00 sec)

  192. Query OK, 0 rows affected (0.00 sec)

  193. Query OK, 0 rows affected (0.00 sec)

  194. Query OK, 0 rows affected (0.00 sec)

  195. Query OK, 0 rows affected (0.00 sec)

  196. Query OK, 0 rows affected (0.00 sec)

  197. Query OK, 0 rows affected (0.00 sec)

  198. Query OK, 0 rows affected (0.00 sec)

  199. Query OK, 0 rows affected (0.00 sec)

  200. Query OK, 0 rows affected, 1 warning (0.00 sec)
  201.  
  202. mysql> select * from t;
  203. +------+------+
  204. | id | name |
  205. +------+------+
  206. | 1 | a |
  207. | 1 | a |
  208. | 2 | b |
  209. | 2 | b |
  210. | 3 | c |
  211. | 3 | c |
  212. | 1 | a |
  213. | 1 | a |
  214. | 2 | b |
  215. | 2 | b |
  216. | 3 | c |
  217. | 3 | c |
  218. +------+------+
  219. 12 rows in set (0.00 sec)

  220. 此时数据已经全部恢复到数据删除之前!!


名称栏目:mysql数据库mysqlbinlog二进制日志文件挖掘
网站URL:http://pwwzsj.com/article/jecogi.html