昨天为了测试mysql数据库快速删除大库的方案,一时起意把redo和undo log也一块儿删除了,由此才有下文mysql
InnoDB 有两块很是重要的日志,一个是undo log,另一个是redo log,前者用来保证事务的原子性以及InnoDB的MVCC,后者用来保证事务的持久性。sql
因为删除了这两个log,数据库又重启了,所以就须要一些其余办法来恢复数据库数据库
要恢复数据,咱们须要用到mysqlfrm工具, 须要安装MySQL Utilities包,这里能够采用yum等形式来安装,以下:vim
yum install mysql-utilities.noarch
其余安装方式详见:MySQL管理工具MySQL Utilities 安装服务器
因为影响的数据库比较多,大概40多个库,表大概有2500多个,手动操做很不现实,所以中间须要一些脚本代替体力劳动工具
mysqlfrm 是一个恢复性质的工具,用来读取.frm文件并从该文件中找到表定义数据生成CREATE语句测试
for n in `ls -d 10.*`;do mysqlfrm --basedir=/home/mysql/mysql --port=3336 --user=root /home/mysql/data/mysql_3306/data/$n/ >> ~/data/mysql_3316/test/da_frm.sql;done
而后检查da_frm.sql文件,生成的create建表语句,应该以下所示:ui
# Spawning server with --user=root. # Starting the spawned server on port 3336 ... done. # Reading .frm files # # Reading the @0024_@0024Inception_backup_information@0024_@0024.frm file. # # CREATE statement for /home/mysql/data/mysql_3306/data/10_0_0_5_3306_data/@0024_@0024Inception_backup_information@0024_@0024.frm: # CREATE TABLE `10_0_0_5_3306_data`.`$_$Inception_backup_information$_$` ( `opid_time` varchar(50) NOT NULL DEFAULT '', `start_binlog_file` varchar(512) DEFAULT NULL, `start_binlog_pos` int(11) DEFAULT NULL, `end_binlog_file` varchar(512) DEFAULT NULL, `end_binlog_pos` int(11) DEFAULT NULL, `sql_statement` text, `host` varchar(64) DEFAULT NULL, `dbname` varchar(64) DEFAULT NULL, `tablename` varchar(64) DEFAULT NULL, `port` int(11) DEFAULT NULL, `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, `type` varchar(20) DEFAULT NULL, PRIMARY KEY (`opid_time`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 # # Reading the data_collection.frm file. # # CREATE statement for /home/mysql/data/mysql_3306/data/10_0_0_5_3306_data_mart/data_collection.frm: # CREATE TABLE `10_0_0_5_3306_data`.`collection` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `rollback_statement` mediumtext, `opid_time` varchar(50) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 # # Reading the data_log.frm file. # 。。。。。。 。。。。。。 。。。。。。
我这里的建表语句都比较规范,所以能够直接用vim或者sed工具,在create建表语句后面加上分号spa
这里的命令是批量生成建库语句,并建立数据库日志
for n in `ls -d 10.*`;do /home/mysql/mysql-5.7.21/bin/mysql -uroot -pxxxx -h127.0.0.1 -P3306 -e "create database $n;";done
mysql> source /home/mysql/data/mysql_3306/test/da_frm.sql
这里导入进去以后,能够检查一下,表是否建立成功
这里咱们由于是多个库的,所以也须要一个for循环
for n in `/home/mysql/mysql-5.7.21/bin/mysql -uroot -pxxxx -h127.0.0.1 -P3306 -e "select concat(concat('alter table ',table_schema,'.',table_name), ' discard tablespace;') from information_schema.tables where table_schema != 'test' and table_schema != 'mysql' and table_schema != 'performance_schema' and table_schema != 'information_schema' and engine ='InnoDB';"`;do echo $n >>./test.sql;done
查看test.sql文件
alter table data_mart.$_$Inception_backup_information$_$ discard tablespace; alter table data_mart.data_collection discard tablespace; alter table data_mart.data_log discard tablespace; alter table data_mart.data_schema discard tablespace; alter table data_mart.data_table discard tablespace; alter table loc_recruit.$_$Inception_backup_information$_$ discard tablespace; alter table loc_recruit.recruit_emp_info discard tablespace; alter table loc_recruit.recruit_task_info discard tablespace; alter table rainbow.$_$Inception_backup_information$_$ discard tablespace; alter table rainbow.job_deps discard tablespace; alter table rainbow.task discard tablespace; ...... ...... ......
而后在mysql里面执行
mysql> source /home/mysql/data/mysql_3306/test/test.sql
而后在服务器上查看mysql的物理库文件,独立表空间应该已经被删除了(最好在干净的环境作)
for n in `ls -d 10.*`;do cp ~/data/mysql_3306/data/$n/*.ibd ./$n/;done
这里注意文件权限是否正确
chown mysql:mysql *.ibd chmod 660 *.ibd
首先生成导入语句
alter table data_mart.$_$Inception_backup_information$_$ import tablespace; alter table data_mart.data_collection import tablespace; alter table data_mart.data_log import tablespace; alter table data_mart.data_schema import tablespace; alter table data_mart.data_table import tablespace; alter table loc_recruit.$_$Inception_backup_information$_$ import tablespace; alter table loc_recruit.recruit_emp_info import tablespace; alter table loc_recruit.recruit_task_info import tablespace; alter table rainbow.$_$Inception_backup_information$_$ import tablespace; alter table rainbow.job_deps import tablespace; alter table rainbow.task import tablespace; ...... ...... ......
在mysql上执行source就行了,这时候观察,数据库里面的表和数据都已经恢复了