注意:假如误删除 ibdata文件 ,此时千万别把mysqld进程杀死,否则没法挽救。
1.模拟删除ibdata数据文件和重做日志文件:
[root@hcdb0 data]# ll
total 421944
-rw-r-----. 1 mysql mysql 56 Sep 12 20:57 auto.cnf
-rw-r-----. 1 mysql mysql 11937 Sep 12 23:37 error.log
-rw-r-----. 1 mysql mysql 413 Sep 12 20:57 ib_buffer_pool
-rw-r-----. 1 mysql mysql 104857600 Sep 12 23:32 ibdata1
-rw-r-----. 1 mysql mysql 104857600 Sep 12 23:32 ib_logfile0
-rw-r-----. 1 mysql mysql 104857600 Sep 12 20:57 ib_logfile1
-rw-r-----. 1 mysql mysql 104857600 Sep 12 20:57 ib_logfile2
-rw-r-----. 1 mysql mysql 12582912 Sep 12 23:31 ibtmp1
-rw-r-----. 1 mysql mysql 3838 Sep 13 00:30 innodb_status.10225
drwxr-x---. 2 mysql mysql 4096 Sep 12 23:32 liangdb
drwxr-x---. 2 mysql mysql 4096 Sep 12 20:57 mysql
-rw-r-----. 1 mysql mysql 6 Sep 12 20:57 mysql.pid
drwxr-x---. 2 mysql mysql 4096 Sep 12 20:57 performance_schema
-rw-r-----. 1 mysql mysql 377 Sep 12 20:57 slow.log
drwxr-x---. 2 mysql mysql 12288 Sep 12 20:57 sys
[root@hcdb0 data]# rm -rf ib*
[root@hcdb0 data]# ll
total 52
-rw-r-----. 1 mysql mysql 56 Sep 12 20:57 auto.cnf
-rw-r-----. 1 mysql mysql 11937 Sep 12 23:37 error.log
-rw-r-----. 1 mysql mysql 3838 Sep 13 00:30 innodb_status.10225
drwxr-x---. 2 mysql mysql 4096 Sep 12 23:32 liangdb
drwxr-x---. 2 mysql mysql 4096 Sep 12 20:57 mysql
-rw-r-----. 1 mysql mysql 6 Sep 12 20:57 mysql.pid
drwxr-x---. 2 mysql mysql 4096 Sep 12 20:57 performance_schema
-rw-r-----. 1 mysql mysql 377 Sep 12 20:57 slow.log
drwxr-x---. 2 mysql mysql 12288 Sep 12 20:57 sys
2.找到mysqld的进程id:
ps -ef|grep mysql
或者 netstat -tnlp|grep mysqld
tcp 0 0 :::3306 :::* LISTEN 10225/mysqld
这里是 10225
3.找到要恢复的句柄文件
ll /proc/10225/fd grep 'ib_|ibdata'
[root@hcdb0 data]# ll /proc/10225/fd | egrep 'ib_|ibdata'
lrwx------. 1 root root 64 Sep 13 00:28 10 -> /data/mysql/3306/data/ib_logfile1 (deleted)
lrwx------. 1 root root 64 Sep 13 00:28 11 -> /data/mysql/3306/data/ib_logfile2 (deleted)
lrwx------. 1 root root 64 Sep 13 00:28 5 -> /data/mysql/3306/data/ibdata1 (deleted)
lrwx------. 1 root root 64 Sep 13 00:28 9 -> /data/mysql/3306/data/ib_logfile0 (deleted)
4.此时把前端业务关闭,或者执行
mysql> flush tables with read lock;
上面语句,让数据库没有写入操作,以便能够进行恢复操作。
5.验证数据库没有写入操作
(1)让脏页尽快刷入到磁盘:
mysql> set global innodb_max_dirty_pages_pct=0;
(2)观察binlog日志写入情况,确保File 和 Position 的值没有变化。
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 3075 | | | |
+------------------+----------+--------------+------------------+-------------------+
(3)最后查看InnoDB状态信息,确保脏页已经刷入磁盘
mysql> show engine innodb status\G;
------------
TRANSACTIONS
------------
Trx id counter 1319
Purge done for trx's n:o < 1315 undo n:o < 0 state: running but idle
### 确保后台Purge进程把undo log全部清除掉,事务ID要一致。
-------------------------------------
INSERT BUFFER AND ADAPTIVE HASH INDEX
-------------------------------------
Ibuf: size 1, free list len 0, seg size 2, 0 merges
### insert buffer合并插入缓存等于1
---
LOG
---
Log sequence number 2553422
Log flushed up to 2553422
Pages flushed up to 2553422
Last checkpoint at 2553413
### 确保这3个值不在变化
----------------------
BUFFER POOL AND MEMORY
----------------------
Total large memory allocated 107380736
Dictionary memory allocated 352938
Buffer pool size 6400
Free buffers 6075
Database pages 325
Old database pages 0
Modified db pages 0
### 确保脏页数量(Modified db pages)为0
--------------
ROW OPERATIONS
--------------
0 queries inside InnoDB, 0 queries in queue
0 read views open inside InnoDB
Process ID=10225, Main thread ID=140654206252800, state: sleeping
Number of rows inserted 11, updated 0, deleted 0, read 17
0.00 inserts/s, 0.00 updates/s, 0.00 deletes/s, 0.00 reads/s
### 确保插入、更新、删除为0
6.上面确认后,就可以进行恢复了,
[root@hcdb0 data]# ll /proc/10225/fd | egrep 'ib_|ibdata'
lrwx------. 1 root root 64 Sep 13 00:28 10 -> /data/mysql/3306/data/ib_logfile1 (deleted)
lrwx------. 1 root root 64 Sep 13 00:28 11 -> /data/mysql/3306/data/ib_logfile2 (deleted)
lrwx------. 1 root root 64 Sep 13 00:28 5 -> /data/mysql/3306/data/ibdata1 (deleted)
lrwx------. 1 root root 64 Sep 13 00:28 9 -> /data/mysql/3306/data/ib_logfile0 (deleted)
把这些文件复制到原来目录下
cp /proc/10225/fd/10 /data/mysql/3306/data/ib_logfile1
cp /proc/10225/fd/11 /data/mysql/3306/data/ib_logfile2
cp /proc/10225/fd/5 /data/mysql/3306/data/ibdata1
cp /proc/10225/fd/9 /data/mysql/3306/data/ib_logfile0
然后修改用户属性
chown mysql:mysql /data/mysql/3306/data/ib*
最后重启MySQL即可