昨天爲了測試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就行了,這時候觀察,數據庫裏面的表和數據都已經恢復了