依星源码资源网,依星资源网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

【好消息,好消息,好消息】VIP会员可以发表文章赚积分啦 !
查看: 46|回复: 0

还想删库跑路?看我怎么破你

[复制链接] 主动推送

1万

主题

1万

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
15657
发表于 4 天前 | 显示全部楼层 |阅读模式
还想删库跑路?看我怎么破你
  1. 1、进入数据库,查看binlog状态
  2. mysql> show variables like '%log-bin%' ;
  3. Empty set (0.00 sec)


  4. 2、修改MySQL配置文件
  5. [root@localhost bin]# vi /etc/my.cnf

  6. // 找到mysqld,在下面添加如下数据
  7. server-id = 1   # 单个节点id
  8. log-bin=/var/lib/mysql/mysql-bin   # binlog日志文件保存地址
  9. expire_logs_days = 10   # 日志过期时间

  10. 保存并退出


  11. 3. 重启MySQL
  12. service mysqld restart


  13. 4. 再次查看binlog状态
  14. mysql> show variables like '%log_bin%';
  15. +---------------------------------+--------------------------------+
  16. | Variable_name                   | Value                          |
  17. +---------------------------------+--------------------------------+
  18. | log_bin                         | ON                             |
  19. | log_bin_basename                | /var/lib/mysql/mysql-bin       |
  20. | log_bin_index                   | /var/lib/mysql/mysql-bin.index |
  21. | log_bin_trust_function_creators | OFF                            |
  22. | log_bin_use_v1_row_events       | OFF                            |
  23. | sql_log_bin                     | ON                             |
  24. +---------------------------------+--------------------------------+
  25. 6 rows in set (0.00 sec)

  26. 可以看到,binlog已经开启

  27. 5. Binlog日志包括两类文件;第一个是二进制索引文件(后缀名为.index),第二个为日志文件(后缀名为.00000*),记录数据库所有的DDL和DML(除了查询语句select)语句事件
  28. 可以查看所有binlog日志文件列表
  29. mysql> show master logs;
  30. +------------------+-----------+
  31. | Log_name         | File_size |
  32. +------------------+-----------+
  33. | mysql-bin.000001 |       154 |
  34. +------------------+-----------+
  35. 1 row in set (0.00 sec)


  36. 6. 查看最后一个binlog日志的编号名称及其最后一个操作事件pos结束点的值
  37. mysql> show master status;
  38. +------------------+----------+--------------+------------------+-------------------+
  39. | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  40. +------------------+----------+--------------+------------------+-------------------+
  41. | mysql-bin.000001 |      154 |              |                  |                   |
  42. +------------------+----------+--------------+------------------+-------------------+
  43. 1 row in set (0.01 sec)


  44. 7. 执行Flush logs ,   刷新日志,此刻开始产生一个新编号的binlog文件
  45. mysql> Flush logs;
  46. Query OK, 0 rows affected (0.01 sec)

  47. mysql> show master status;
  48. +------------------+----------+--------------+------------------+-------------------+
  49. | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  50. +------------------+----------+--------------+------------------+-------------------+
  51. | mysql-bin.000002 |      154 |              |                  |                   |
  52. +------------------+----------+--------------+------------------+-------------------+
  53. 1 row in set (0.00 sec)

  54. mysql> show master logs;
  55. +------------------+-----------+
  56. | Log_name         | File_size |
  57. +------------------+-----------+
  58. | mysql-bin.000001 |       201 |
  59. | mysql-bin.000002 |       154 |
  60. +------------------+-----------+
  61. 2 rows in set (0.00 sec)

  62. 每当mysqld服务重启时,会自动执行刷新binlog日志命令,mysqldump备份数据时加-F选项也会刷新binlog日志

  63. 8. 可以通过mysqlbinlog查看binlog日志,(cat命令无法查看)
  64. [root@localhost ~]# whereis mysqlbinlog
  65. mysqlbinlog: /usr/bin/mysqlbinlog /usr/share/man/man1/mysqlbinlog.1.gz
  66. [root@localhost mysql]# /usr/bin/mysqlbinlog  mysql-bin.000001
  67. mysqlbinlog: [ERROR] unknown variable 'default-character-set=utf8'

  68. // 此处报错是因为mysqlbinlog这个工具无法识别binlog中的配置中的default-character-set=utf8这个指令
  69. //有两个解决方案:
  70. 1.是在MySQL的配置/etc/my.cnf中将default-character-set=utf8 修改为 character-set-server = utf8,但是这需要重启MySQL服务,如果你的MySQL服务正在忙,那这样的代价会比较大。
  71. 2.用mysqlbinlog --no-defaults mysql-bin.000001 命令打开

  72. [root@localhost mysql]# /usr/bin/mysqlbinlog  --no-defaults mysql-bin.000001
  73. /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
  74. /*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
  75. DELIMITER /*!*/;
  76. # at 4
  77. #200715 19:58:14 server id 1  end_log_pos 123 CRC32 0x1c66870e  Start: binlog v 4, server v 5.7.27-log created 200715 19:58:14 at startup
  78. # Warning: this binlog is either in use or was not closed properly.
  79. ROLLBACK/*!*/;
  80. BINLOG '
  81. Vu8OXw8BAAAAdwAAAHsAAAABAAQANS43LjI3LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  82. AAAAAAAAAAAAAAAAAABW7w5fEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA
  83. AQ6HZhw=
  84. '/*!*/;
  85. # at 123
  86. #200715 19:58:14 server id 1  end_log_pos 154 CRC32 0x59a19e6a  Previous-GTIDs
  87. # [empty]
  88. SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
  89. DELIMITER ;
  90. # End of log file
  91. /*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
  92. /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;


  93. 9. 也可以在mysql中以show binlog events in 'mysql-bin.000001'; 查看
  94. mysql> show binlog events in 'mysql-bin.000001';
  95. +------------------+-----+----------------+-----------+-------------+---------------------------------------+
  96. | Log_name         | Pos | Event_type     | Server_id | End_log_pos | Info                                  |
  97. +------------------+-----+----------------+-----------+-------------+---------------------------------------+
  98. | mysql-bin.000001 |   4 | Format_desc    |         1 |         123 | Server ver: 5.7.27-log, Binlog ver: 4 |
  99. | mysql-bin.000001 | 123 | Previous_gtids |         1 |         154 |                                       |
  100. +------------------+-----+----------------+-----------+-------------+---------------------------------------+
  101. 2 rows in set (0.00 sec)


  102. 10. 执行下面SQL
  103. create database hello ;
  104. create table student(id int primary key auto_increment , name varchar(20) , age int );
  105. insert into
  106.     student
  107.         (name , age )
  108.     values
  109.          ('张三',18),
  110.          ('李四',20),
  111.          ('王五',19);



  112. 11. 将hello数据库备份到本地
  113. [root@localhost ~]# mysqldump -u root -p  hello > /root/hello.sql
  114. Enter password:


  115. 12. 执行flush logs 生成新的binlog日志
  116. mysql> flush  logs ;
  117. Query OK, 0 rows affected (0.01 sec)

  118. mysql> show master logs ;
  119. +------------------+-----------+
  120. | Log_name         | File_size |
  121. +------------------+-----------+
  122. | mysql-bin.000001 |       201 |
  123. | mysql-bin.000002 |       896 |
  124. | mysql-bin.000003 |       154 |
  125. +------------------+-----------+
  126. 3 rows in set (0.00 sec)

  127. mysql> show master status ;
  128. +------------------+----------+--------------+------------------+-------------------+
  129. | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  130. +------------------+----------+--------------+------------------+-------------------+
  131. | mysql-bin.000003 |      154 |              |                  |                   |
  132. +------------------+----------+--------------+------------------+-------------------+
  133. 1 row in set (0.00 sec)



  134. 14. 往student表中新增两条数据
  135. mysql> insert into student (name , age ) values ('赵六',28),('周七',25);
  136. Query OK, 2 rows affected (0.00 sec)

  137. mysql> select * from student ;
  138. +----+--------+------+
  139. | id | name   | age  |
  140. +----+--------+------+
  141. |  1 | 张三   |   18 |
  142. |  2 | 李四   |   20 |
  143. |  3 | 王五   |   19 |
  144. |  4 | 赵六   |   28 |
  145. |  5 | 周七   |   25 |
  146. +----+--------+------+
  147. 5 rows in set (0.00 sec)



  148. 15.模拟误删操作(当然也可能是删库跑路那种...),删除hello数据库
  149. mysql> drop database hello ;
  150. Query OK, 1 row affected (0.01 sec)


  151. 16. 新建hello数据库,执行之前备份的SQL文件,数据已经恢复到备份那一刻
  152. mysql> create database hello ;
  153. Query OK, 1 row affected (0.00 sec)

  154. mysql> use hello ;
  155. Database changed
  156. mysql> source /root/hello.sql
  157. Query OK, 0 rows affected (0.00 sec)
  158. mysql> select * from student ;
  159. +----+--------+------+
  160. | id | name   | age  |
  161. +----+--------+------+
  162. |  1 | 张三   |   18 |
  163. |  2 | 李四   |   20 |
  164. |  3 | 王五   |   19 |
  165. +----+--------+------+
  166. 3 rows in set (0.00 sec)



  167. 17. 执行flush logs 生成新的binlog日志,方便操作之前的binlog日志
  168. mysql> flush logs
  169.     -> ;
  170. Query OK, 0 rows affected (0.00 sec)

  171. mysql> show master logs ;
  172. +------------------+-----------+
  173. | Log_name         | File_size |
  174. +------------------+-----------+
  175. | mysql-bin.000001 |       201 |
  176. | mysql-bin.000002 |       896 |
  177. | mysql-bin.000003 |      1347 |
  178. | mysql-bin.000004 |      154 |
  179. +------------------+-----------+
  180. 5 rows in set (0.00 sec)

  181. mysql> show master status ;
  182. +------------------+----------+--------------+------------------+-------------------+
  183. | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  184. +------------------+----------+--------------+------------------+-------------------+
  185. | mysql-bin.000004 |      154 |              |                  |                   |
  186. +------------------+----------+--------------+------------------+-------------------+
  187. 1 row in set (0.00 sec)



  188. 18. 查看日志4:show binlog events  in 'mysql-bin.000003' ;
  189. mysql> show binlog events  in 'mysql-bin.000003' ;
  190. +------------------+-----+----------------+-----------+-------------+---------------------------------------+
  191. | Log_name         | Pos | Event_type     | Server_id | End_log_pos | Info                                  |
  192. +------------------+-----+----------------+-----------+-------------+---------------------------------------+
  193. | mysql-bin.000003 |   4 | Format_desc    |         1 |         123 | Server ver: 5.7.27-log, Binlog ver: 4 |
  194. | mysql-bin.000003 | 123 | Previous_gtids |         1 |         154 |                                       |
  195. | mysql-bin.000003 | 154 | Anonymous_Gtid |         1 |         219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'  |
  196. | mysql-bin.000003 | 219 | Query          |         1 |         292 | BEGIN                                 |
  197. | mysql-bin.000003 | 292 | Table_map      |         1 |         347 | table_id: 113 (hello.student)         |
  198. | mysql-bin.000003 | 347 | Write_rows     |         1 |         414 | table_id: 113 flags: STMT_END_F       |
  199. | mysql-bin.000003 | 414 | Xid            |         1 |         445 | COMMIT /* xid=242 */                  |
  200. | mysql-bin.000003| 445 | Anonymous_Gtid |         1 |         510 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'  |
  201. | mysql-bin.000003| 510 | Query          |         1 |         605 | drop database hello                   |
  202. | mysql-bin.000003 | 605 | Anonymous_Gtid |         1 |         670 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'  |
  203. | mysql-bin.000003| 670 | Query          |         1 |         767 | create database hello                 |
  204. | mysql-bin.000003| 767 | Rotate         |         1 |         814 | mysql-bin.000007;pos=4                |
  205. +------------------+-----+----------------+-----------+-------------+---------------------------------------+


  206. 可以看出,在292开启事务,414开始写入内容,445提交事务

  207. 19. 在mysql外部执行数据恢复指令
  208. [root@localhost mysql]# /usr/bin/mysqlbinlog  --no-defaults --start-position=292 --stop-position=445 --database=hello  /var/lib/mysql/mysql-bin.000003 | /usr/bin/mysql -uroot -pitheima -v hello
  209. mysql: [Warning] Using a password on the command line interface can be insecure.
  210. --------------
  211. /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/
  212. --------------

  213. --------------
  214. /*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/
  215. --------------

  216. --------------
  217. BINLOG '
  218. YP0OXw8BAAAAdwAAAHsAAAAAAAQANS43LjI3LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  219. AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA
  220. Af0bDWo=
  221. '
  222. --------------

  223. --------------
  224. BINLOG '
  225. gP0OXxMBAAAANwAAAFsBAAAAAHEAAAAAAAEABWhlbGxvAAdzdHVkZW50AAMDDwMCPAAGrW1P0A==
  226. gP0OXx4BAAAAQwAAAJ4BAAAAAHEAAAAAAAEAAgAD//gGAAAABui1teWFrRwAAAD4BwAAAAblkajk
  227. uIMZAAAAZ7sssw==
  228. '
  229. --------------

  230. --------------
  231. COMMIT
  232. --------------

  233. --------------
  234. SET @@SESSION.GTID_NEXT= 'AUTOMATIC'
  235. --------------

  236. --------------
  237. /*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/
  238. --------------

  239. --------------
  240. /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/
  241. --------------

  242. [root@localhost mysql]#



  243. 20.mysql查看student表数据,搞定!
  244. mysql> select * from student ;
  245. +----+--------+------+
  246. | id | name   | age  |
  247. +----+--------+------+
  248. |  1 | 张三   |   18 |
  249. |  2 | 李四   |   20 |
  250. |  3 | 王五   |   19 |
  251. |  6 | 赵六   |   28 |
  252. |  7 | 周七   |   25 |
  253. +----+--------+------+
  254. 5 rows in set (0.00 sec)
复制代码


扫码关注微信公众号,及时获取最新资源信息!下载附件优惠VIP会员6折;永久VIP4折
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

免责声明:
1、本站提供的所有资源仅供参考学习使用,版权归原著所有,禁止下载本站资源参与商业和非法行为,请在24小时之内自行删除!
2、本站所有内容均由互联网收集整理、网友上传,并且以计算机技术研究交流为目的,仅供大家参考、学习,请勿任何商业目的与商业用途。
3、若您需要商业运营或用于其他商业活动,请您购买正版授权并合法使用。
4、论坛的所有内容都不保证其准确性,完整性,有效性,由于源码具有复制性,一经售出,概不退换。阅读本站内容因误导等因素而造成的损失本站不承担连带责任。
5、用户使用本网站必须遵守适用的法律法规,对于用户违法使用本站非法运营而引起的一切责任,由用户自行承担
6、本站所有资源来自互联网转载,版权归原著所有,用户访问和使用本站的条件是必须接受本站“免责声明”,如果不遵守,请勿访问或使用本网站
7、本站使用者因为违反本声明的规定而触犯中华人民共和国法律的,一切后果自己负责,本站不承担任何责任。
8、凡以任何方式登陆本网站或直接、间接使用本网站资料者,视为自愿接受本网站声明的约束。
9、本站以《2013 中华人民共和国计算机软件保护条例》第二章 “软件著作权” 第十七条为原则:为了学习和研究软件内含的设计思想和原理,通过安装、显示、传输或者存储软件等方式使用软件的,可以不经软件著作权人许可,不向其支付报酬。若有学员需要商用本站资源,请务必联系版权方购买正版授权!
10、本网站如无意中侵犯了某个企业或个人的知识产权,请来信【站长信箱312337667@qq.com】告之,本站将立即删除。
郑重声明:
本站所有资源仅供用户本地电脑学习源代码的内含设计思想和原理,禁止任何其他用途!
本站所有资源、教程来自互联网转载,仅供学习交流,不得商业运营资源,不确保资源完整性,图片和资源仅供参考,不提供任何技术服务。
本站资源仅供本地编辑研究学习参考,禁止未经资源商正版授权参与任何商业行为,违法行为!如需商业请购买各资源商正版授权
本站仅收集资源,提供用户自学研究使用,本站不存在私自接受协助用户架设游戏或资源,非法运营资源行为。
 
在线客服
点击这里给我发消息 点击这里给我发消息 点击这里给我发消息
售前咨询热线
312337667

微信扫一扫,私享最新原创实用干货

QQ|免责声明|小黑屋|依星资源网 ( 鲁ICP备2021043233号-3 )|网站地图

GMT+8, 2025-4-19 08:09

Powered by Net188.com X3.4

邮箱:312337667@qq.com 客服QQ:312337667(工作时间:9:00~21:00)

快速回复 返回顶部 返回列表