赞
踩
经常听说删库跑路这真的不只是一句玩笑话,若不小心删除了数据库,事情很严重。你一个不小心可能会给公司删没。建议研发不要直连生成环境,一般的话都会分配账号权限,生产环境的账号尽量是只读,以防你一个不经意给库或表删除。一定要备份,这很重要,这是一个血的教训。
- DROP TABLE IF EXISTS `user_misjudge`; --如果表存在则删除,然后执行如下语句重新创建。
- CREATE TABLE `user_misjudge` (
- `id` bigint(50) NOT NULL AUTO_INCREMENT,
- `attr1` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
- `attr2` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
- `attr3` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
- `attr4` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
- `attr5` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
- PRIMARY KEY (`id`) USING BTREE
- ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
声明: 当前为mysql版本5.7 当前为mysql版本5.7 当前为mysql版本5.7
SHOW VARIABLES LIKE 'LOG_BIN%';
mysql -hlocalhost -uroot -proot
SET GLOBAL log_bin = 'ON';
执行结果:
注意: 报错了! 报错了! 报错不可怕,可怕的是报错没有征兆。如果要永久修改log_bin的值,需要修改MySQL的配置文件(my.cnf或my.ini),并重启MySQL服务器使修改生效。( 只读变量,不能使用set修改,只能通过修改my.cnf或my.ini文件再重启生效 )
- # 开启BigLog用于备份和恢复数据库,以及进行数据复制等操作。
- log-bin=mysql-bin
- # 实例都必须有一个唯一的 server-id 如:server-id=1 ,以便 MySQL 集群中的各个节点能够相互识别和通信。
- server-id=1
show binary logs;
show master status;
show binlog events;
show binlog events in 'mysql-bin.000002';
注意: 上一个事件的结束位置,就是下一个事件的开始位置。如下↓↓↓
flush logs;
MySQL删除日志的方式有以下几种:
show binary logs;
purge master logs to 'mysql-bin.000001';
purge binary logs before '2023-5-29 23:59:59';
reset master;
说明:
show variables like '%expire_logs_days%';
set global expire_logs_days=7;
show databases;
- --切换到指定数据库。
- use text;
- --显示当前数据库中的所有表名。
- show tables;
- --查询表数据
- select * from user_misjudge;
说明: show binary logs; 和 show master logs; 都是显示所有可用的binlog日志文件列表。
show master logs;
注意: 可以看到我之前删除的表数据已经被记录了 ,由于之前演示删除日志,我的日志是不完整的不完整的日志是不能恢复的 。(开启日志后 重新创一个库 详情查看:3.4重新创建库) ↓↓↓
drop database text
reset master;
CREATE DATABASE `text` CHARACTER SET 'utf8' COLLATE 'utf8_general_ci'
show binlog events in 'mysql-bin.000001';
drop database text;
可以看到列表text库已经被删除
show binlog events in 'mysql-bin.000001';
mysqlbinlog --start-position=154 --stop-position=427 mysql-bin.000001 | mysql -uroot -p
以上该命令是一个从MySQL二进制日志文件中提取数据并导入到MySQL数据库的命令。具体解释如下:
说明
mysqlbinlog "mysql-bin.000001">"xj.sql"
mysqlbinlog --start-datetime="2023-06-01 11:32:34" --stop-datetime="2023-06-01 11:47:46" mysql-bin.000001 | mysql -uroot -p
- insert user_misjudge(attr1) values('CSDN臭弟弟');
- insert user_misjudge(attr1) values('这是一条数据');
drop tables user_misjudge;
mysqlbinlog --start-position=720 --stop-position=1579 mysql-bin.000001 | mysql -uroot -p
show binlog events in 'mysql-bin.000001';
可以看到 我们只恢复了表 并没有恢复数据 。为什么??? 往下继续↓↓
原因:
在导航{3.6.5、恢复表} 我们执行的语句mysqlbinlog --start-position=720 --stop-position=1579 mysql-bin.000001 | mysql -uroot -p, 事件开始位置720 ,事件结束位置1579,说明我们位置不对呗,这个位置只能恢复表不能恢复数据。(想要恢复数据应该在 事件的结束位置应该在 删除表之前的最后连接的位置才对)
- mysql> show binlog events in 'mysql-bin.000001';
- +------------------+------+----------------+-----------+-------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- | Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
- +------------------+------+----------------+-----------+-------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- | mysql-bin.000001 | 4 | Format_desc | 1 | 123 | Server ver: 5.7.28-log, Binlog ver: 4 |
- | mysql-bin.000001 | 123 | Previous_gtids | 1 | 154 | |
- | mysql-bin.000001 | 154 | Anonymous_Gtid | 1 | 219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
- | mysql-bin.000001 | 219 | Query | 1 | 362 | CREATE DATABASE `text` CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' |
- | mysql-bin.000001 | 362 | Anonymous_Gtid | 1 | 427 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
- | mysql-bin.000001 | 427 | Query | 1 | 512 | drop database text |
- | mysql-bin.000001 | 512 | Anonymous_Gtid | 1 | 577 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
- | mysql-bin.000001 | 577 | Query | 1 | 720 | CREATE DATABASE `text` CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' |
- | mysql-bin.000001 | 720 | Anonymous_Gtid | 1 | 785 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
- | mysql-bin.000001 | 785 | Query | 1 | 1532 | use `text`; CREATE TABLE `user_misjudge` ( `id` bigint(50) NOT NULL AUTO_INCREMENT, `attr1` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `attr2` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `attr3` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `attr4` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `attr5` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic |
- | mysql-bin.000001 | 1532 | Anonymous_Gtid | 1 | 1597 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
- | mysql-bin.000001 | 1597 | Query | 1 | 1669 | BEGIN |
- | mysql-bin.000001 | 1669 | Table_map | 1 | 1740 | table_id: 163 (text.user_misjudge) |
- | mysql-bin.000001 | 1740 | Write_rows | 1 | 1799 | table_id: 163 flags: STMT_END_F |
- | mysql-bin.000001 | 1799 | Xid | 1 | 1830 | COMMIT /* xid=2016 */ |
- | mysql-bin.000001 | 1830 | Anonymous_Gtid | 1 | 1895 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
- | mysql-bin.000001 | 1895 | Query | 1 | 1967 | BEGIN |
- | mysql-bin.000001 | 1967 | Table_map | 1 | 2038 | table_id: 163 (text.user_misjudge) |
- | mysql-bin.000001 | 2038 | Write_rows | 1 | 2102 | table_id: 163 flags: STMT_END_F |
- | mysql-bin.000001 | 2102 | Xid | 1 | 2133 | COMMIT /* xid=2017 */ |
- | mysql-bin.000001 | 2133 | Anonymous_Gtid | 1 | 2198 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
- | mysql-bin.000001 | 2198 | Query | 1 | 2324 | use `text`; DROP TABLE `user_misjudge` /* generated by server */ |
- | mysql-bin.000001 | 2324 | Anonymous_Gtid | 1 | 2389 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
- | mysql-bin.000001 | 2389 | Query | 1 | 3136 | use `text`; CREATE TABLE `user_misjudge` ( `id` bigint(50) NOT NULL AUTO_INCREMENT, `attr1` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `attr2` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `attr3` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `attr4` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `attr5` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic |
- +------------------+------+----------------+-----------+-------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- 24 rows in set (0.00 sec)
创建表的起始位置是720 ,在日志中删除表的结束之前最后连接的位置是2190,这样就可以恢复我们删表之前的表和两条数据。
- mysql> show binlog events in 'mysql-bin.000001';
- +------------------+------+----------------+-----------+-------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- | Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
- +------------------+------+----------------+-----------+-------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- | mysql-bin.000001 | 4 | Format_desc | 1 | 123 | Server ver: 5.7.28-log, Binlog ver: 4 |
- | mysql-bin.000001 | 123 | Previous_gtids | 1 | 154 | |
- | mysql-bin.000001 | 154 | Anonymous_Gtid | 1 | 219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
- | mysql-bin.000001 | 219 | Query | 1 | 362 | CREATE DATABASE `text` CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' |
- | mysql-bin.000001 | 362 | Anonymous_Gtid | 1 | 427 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
- | mysql-bin.000001 | 427 | Query | 1 | 512 | drop database text |
- | mysql-bin.000001 | 512 | Anonymous_Gtid | 1 | 577 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
- | mysql-bin.000001 | 577 | Query | 1 | 720 | CREATE DATABASE `text` CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' |
- | mysql-bin.000001 | 720 | Anonymous_Gtid | 1 | 785 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
- | mysql-bin.000001 | 785 | Query | 1 | 1532 | use `text`; CREATE TABLE `user_misjudge` ( `id` bigint(50) NOT NULL AUTO_INCREMENT, `attr1` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `attr2` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `attr3` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `attr4` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `attr5` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic |
- | mysql-bin.000001 | 1532 | Anonymous_Gtid | 1 | 1597 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
- | mysql-bin.000001 | 1597 | Query | 1 | 1669 | BEGIN |
- | mysql-bin.000001 | 1669 | Table_map | 1 | 1740 | table_id: 163 (text.user_misjudge) |
- | mysql-bin.000001 | 1740 | Write_rows | 1 | 1799 | table_id: 163 flags: STMT_END_F |
- | mysql-bin.000001 | 1799 | Xid | 1 | 1830 | COMMIT /* xid=2016 */ |
- | mysql-bin.000001 | 1830 | Anonymous_Gtid | 1 | 1895 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
- | mysql-bin.000001 | 1895 | Query | 1 | 1967 | BEGIN |
- | mysql-bin.000001 | 1967 | Table_map | 1 | 2038 | table_id: 163 (text.user_misjudge) |
- | mysql-bin.000001 | 2038 | Write_rows | 1 | 2102 | table_id: 163 flags: STMT_END_F |
- | mysql-bin.000001 | 2102 | Xid | 1 | 2133 | COMMIT /* xid=2017 */ |
- | mysql-bin.000001 | 2133 | Anonymous_Gtid | 1 | 2198 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
- | mysql-bin.000001 | 2198 | Query | 1 | 2324 | use `text`; DROP TABLE `user_misjudge` /* generated by server */ |
- | mysql-bin.000001 | 2324 | Anonymous_Gtid | 1 | 2389 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
- | mysql-bin.000001 | 2389 | Query | 1 | 3136 | use `text`; CREATE TABLE `user_misjudge` ( `id` bigint(50) NOT NULL AUTO_INCREMENT, `attr1` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `attr2` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `attr3` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `attr4` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `attr5` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic |
- +------------------+------+----------------+-----------+-------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- 24 rows in set (0.00 sec)
-
- mysql>
mysqlbinlog --start-position=1532 --stop-position=2189 mysql-bin.000001 | mysql -uroot -p
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。