【轉載】MySQL 誤刪ibdata、ib_logfile恢復案例

一、前言

InnoDB 有兩塊非常重要的日誌,一個是undo log,另外一個是redo log,前者用來保證事務的原子性以及InnoDB的MVCC,後者用來保證事務的持久性。

由於刪除了這兩個log,數據庫又重啓了,因此就需要一些其他辦法來恢復數據庫

二、mysqlfrm工具安裝

要恢復數據,我們需要用到mysqlfrm工具, 需要安裝MySQL Utilities包,這裏可以採用yum等形式來安裝,如下:

yum install  mysql-utilities.noarch

其他安裝方式詳見:MySQL管理工具MySQL Utilities 安裝

三、開始數據恢復

由於影響的數據庫比較多,大概40多個庫,表大概有2500多個,手動操作很不現實,因此中間需要一些腳本代替體力勞動

3.1  用frm工具批量提取建表語句

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建表語句,應該如下所示:

# Spawning server with --user=root.
# Starting the spawned server on port 3336 ... done.
# Reading .frm files
#
# Reading the @0024_@0024Inception_backup_information@[email protected] file.
#
# CREATE statement for /home/mysql/data/mysql_3306/data/10_0_0_5_3306_data/@0024_@0024Inception_backup_information@[email protected]:
#

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建表語句後面加上分號

3.2  批量生成建庫語句

這裏的命令是批量生成建庫語句,並創建數據庫

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

3.3  導入建表語句

mysql> source /home/mysql/data/mysql_3306/test/da_frm.sql

這裏導入進去之後,可以檢查一下,表是否創建成功

3.4  刪除新建表的獨立表空間

這裏我們因爲是多個庫的,因此也需要一個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的物理庫文件,獨立表空間應該已經被刪除了(最好在乾淨的環境做)

3.5  批量複製表ibd文件

for n in `ls -d 10.*`;do cp ~/data/mysql_3306/data/$n/*.ibd ./$n/;done

這裏注意文件權限是否正確

chown  mysql:mysql *.ibd
chmod 660 *.ibd

3.6  批量導入表空間

首先生成導入語句

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

 

 

轉載:https://www.lmlphp.com/user/102/article/item/20349/

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章