user表被delete
如果有权限的同学误执行了情况mysql.user表内容的情况,如果是delete的方式还是相对容易恢复的(binlog存在的情况)
1.1 模拟误删除
/* 当前user表的内容 */
mysql> select user,host from mysql.user;
+---------------+--------------+
| user | host |
+---------------+--------------+
| repl | 192.168.28.% |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+---------------+--------------+
4 rows in set (0.00 sec)
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2020-04-16 06:37:07 |
+---------------------+
1 row in set (0.00 sec)
现在执行误删除
mysql> delete from mysql.user;
Query OK, 4 rows affected (0.01 sec)
mysql>
1.2 恢复
当前情况下,如开启了binlog(生产环境一般都是开启了的),则可以从binlog恢复
注意如果有全部及其之后的binlog 则可以通过恢复备份并追加binlog的方式恢复数据(后续其他文章再专题介绍),本文基于无全备,仅有最近的日志情况下恢复(主要是为了使用binlog2sql工具)
1.2.1 先恢复root账号
因为删除后,其他用户无法重新连接数据库了,需要紧急恢复root账号,再做后续其他账号的恢复,恢复步骤为:
修改为跳过授权的模式,即在配置文件my.cnf文件中添加 skip-grant-tables
重启数据库
登录数据库,添加root@'localhost'账号(可以从相同版本数据库中导出一个root账号的sql语句恢复
/usr/local/mysql5.7/bin/mysqldump -uroot -p'123456' -t --socket=/data/mysql3307/tmp/mysql.sock mysql user --where " 1=1 and user='root' and host='localhost'" >1.sql
恢复数据
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> INSERT INTO `user` VALUES ('localhost','root','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','','','','',0,0,0,0,'mysql_native_password',password('123456'),'N',now(),NULL,'N');
Query OK, 1 row affected (0.01 sec)
改为授权模式:将配置文件里的 skip-grant-tables 注释或删除
再次重启数据库即可用刚恢复的root账号登录了
mysql> select user,host from mysql.user;+------+-----------+| user | host |+------+-----------+| root | localhost |+------+-----------+1 row in set (0.00 sec)
注:如果清空数据或后续说的删除了user表后没有退出当前会话,就无需之前的修改参数及重启数据库了,可以直接先从其他实例中先恢复root账号。
1.2.2 安装binlog2sql
binlog2sql依赖python2.7及以上版本,且依赖包通过pip安装,对于python的升级及pip的安装可参考历史文章
yum install -y git wgetgit clone https://github.com/danfengcao/binlog2sql.git && cd binlog2sqlpip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
1.2.3 使用binlog2sql工具恢复数据
使用binlog2sql的闪回工具将删除解析处理生成一个insert的sql脚本
python binlog2sql.py --flashback -h localhost -u root -p 123456 -d mysql -t user --start-file='mysql-bin.000002' --start-datetime='2020-04-16 06:37:07' --stop-datetime='2020-04-16 06:40:00' >mysql.sql
将数据导入mysql.user表即可。
注:关于binlog2log的其他用法请参考https://github.com/danfengcao/binlog2sql
02
user表被drop
2.1 模拟user 表被drop
mysql> drop table mysql.user;Query OK, 0 rows affected (0.01 sec)
2.2 恢复
2.2.1 恢复表结构
表结构的恢复比较简单,可以从其他相同版本的数据库里复制user表的建表语句,然后导入即可
CREATE TABLE `user` ( `Host` char(60) COLLATE utf8_bin NOT NULL DEFAULT '', `User` char(32) COLLATE utf8_bin NOT NULL DEFAULT '', `Select_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Insert_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Update_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Delete_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Create_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Drop_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Reload_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Shutdown_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Process_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `File_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Grant_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `References_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Index_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Alter_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Show_db_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Super_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Create_tmp_table_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Lock_tables_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Execute_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Repl_slave_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Repl_client_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Create_view_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Show_view_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Create_routine_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Alter_routine_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Create_user_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Event_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Trigger_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Create_tablespace_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `ssl_type` enum('','ANY','X509','SPECIFIED') CHARACTER SET utf8 NOT NULL DEFAULT '', `ssl_cipher` blob NOT NULL, `x509_issuer` blob NOT NULL, `x509_subject` blob NOT NULL, `max_questions` int(11) unsigned NOT NULL DEFAULT '0', `max_updates` int(11) unsigned NOT NULL DEFAULT '0', `max_connections` int(11) unsigned NOT NULL DEFAULT '0', `max_user_connections` int(11) unsigned NOT NULL DEFAULT '0', `plugin` char(64) COLLATE utf8_bin NOT NULL DEFAULT 'mysql_native_password', `authentication_string` text COLLATE utf8_bin, `password_expired` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `password_last_changed` timestamp NULL DEFAULT NULL, `password_lifetime` smallint(5) unsigned DEFAULT NULL, `account_locked` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', PRIMARY KEY (`Host`,`User`)) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Users and global privileges'
2.2.2 恢复数据
drop的方式删除数据后无法直接通过之前的binlog闪回恢复了,需要依赖于全备来恢复,关于备份恢复可以参考以下文章来恢复,此处不再赘述
03
小结
对于删除表后的恢复其实不止以上这些方式,另外还可以通过操作系统级别进行恢复,但各种恢复方式均和实际场景有关,希望大家也自行探索或多参与技术交流,提高自己的技能水平。
想了解更多内容或参与技术交流可以关注微信公众号 数据库干货铺 入群沟通。