MySQL運維之記錄一次因數據庫非正常關閉致使MySQL數據庫不可用修復(一)

問題描述
因MySQL數據庫所在數據目錄磁盤出現問題致使數據庫表出現以下問題,致使表找不到對應對應表空間
數據庫版本 mysql 5.7
show tables;   -- 能查詢到表存在
desc table_name;   -- 提示表不存在
在數據庫目錄下   ibd 與 frm文件都存在

錯誤日誌

[ERROR] InnoDB: Cannot read first page of '.db_test_2/user_test.ibd' I/O error
[ERROR] Innodb: Cannot read first page in datafile: ./db_test_2/user_test.ibd, Space ID:18445744073709551615, Flags: 33. Please refer to http://dev.mysql.com/doc/refman/5.7/en/innodb-troubleshooting-datadict.html for how to resolve the issue.
[ERROR] InnoDB: Operating system error number 2 in a file operation.
[ERROR] InnoDB: The error means the system cannot find the path specified.
[ERROR] InnoDB: If you are instaling InnoDB, remember that you must create directories yourself, InnoDB desc not create them.
[ERROR] InnoDB: Cloud not find a valid tablespace file for `db_test_2/user_test`. Please refer to http://dev.mysql.com/doc/refman/5.7/en/innodb-troubleshooting-datadict.html for how to resolve the issue.
[warning] InnoDB: Ignoring tablespace `db_test_2/user_test` bacause it could not be opened

解決步驟

新庫或者新實例上操做
一、在新實例上建表
mysql> create database db_name1;
Query OK, 1 row affected (0.00 sec)

mysql> use db_name1;
Database changed
mysql> CREATE TABLE `user_test` (
  ->   `id` int(11) NOT NULL,
  ->   `name` varchar(48) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  ->   `age` int(8) NULL DEFAULT NULL,
  ->   `comment` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  ->   PRIMARY KEY (`id`) USING BTREE
  -> ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
Query OK, 0 rows affected (0.03 sec)
二、discard新建表的表空間
mysql> alter table db_name1.user_test discard tablespace;
Query OK, 0 rows affected (0.01 sec)
舊實例上操做
三、拷貝舊實例該表的ibd文件到新實例對應的位置
cp /data/mysql/data/db_test2/user_test.ibd /data/mysql/data/db_name1/
#scp /data/mysql/data/db_test2/user_test.ibd user@ip:/data/mysql/data/db_name1/
新庫或者新實例上操做
四、確保新實例下拷貝的ibd文件 權限與屬主正確
chown mysql:mysql /data/mysql/data/db_name1/user_test.ibd
五、導入拷貝過來的ibd文件
mysql> alter table db_name1.user_test import tablespace;
Query OK, 0 rows affected, 1 warning (0.01 sec)
六、檢查
mysql> select * from db_name1.user_test limit 1;
+----+--------+------+---------+
| id | name   | age | comment |
+----+--------+------+---------+
|  1 | 小一   |   22 | 學生   |
+----+--------+------+---------+
1 row in set (0.00 sec)

mysql> select count(1) from db_name1.user_test;
+----------+
| count(1) |
+----------+
|        5 |
+----------+
1 row in set (0.00 sec)
七、導出user_test 庫並拷貝備份文件至舊實例 --備份
mysqldump -uroot -p12345678 db_name1 user_test >/data/backup/user_test.sql
head -n 100 /data/backup/user_test.sql # 檢查備份文件
scp /data/backup/user_test.sql user@ip:/data/backup
舊實例上操做--恢復
八、刪除對應表的ibd文件
rm -rf /data/mysql/data/db_test_2/user_test.ibd
九、導入庫
mysql -uroot -p1234567  db_test_2 < /data/backup/user_test.sql
十、檢查
mysql> show tables;
+---------------------+
| Tables_in_db_test_2 |
+---------------------+
| user |
| user_test |
+---------------------+
2 rows in set (0.00 sec)

mysql> select * from user_test limit 1;
+----+--------+------+---------+
| id | name | age | comment |
+----+--------+------+---------+
| 1 | 小一 | 22 | 學生 |
+----+--------+------+---------+
1 row in set (0.00 sec)

mysql> select count(1) from user_test;
+----------+
| count(1) |
+----------+
| 5 |
+----------+
1 row in set (0.00 sec)

如對小編的文章感興趣,關注小編公衆號我們一塊兒學習開發運維相關知識:html


本文分享自微信公衆號 - 小偉運維開發(xiaowei-dev)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。mysql

相關文章
相關標籤/搜索