在没有备份数据的情况下,突然断电导致表损坏,打不开数据库。
1)拷贝库目录到新库中
[root@db01 ~]# cp -r /application/mysql/data/world/ /data/3307/data/
2)启动新数据库
[root@db01 ~]# mysqld_safe –defaults-file=/data/3307/my.cnf &
3)登陆数据库查看
mysql> show databases;
4)查询表中数据
mysql> select
from city;
ERROR 1146 (42S02): Table ‘world.city‘ doesn‘t exist #当出现这种报错时,可能是表空间损坏
5)找到以前的表结构在新库中创建表
mysql> show create table world.city;
#删掉外键创建语句
CREATE TABLE city (ID int(11) NOT NULL AUTO_INCREMENT,Name char(35) NOT NULL DEFAULT ‘‘,CountryCode char(3) NOT NULL DEFAULT ‘‘,District char(20) NOT NULL DEFAULT ‘‘,Population int(11) NOT NULL DEFAULT ‘0‘,
PRIMARY KEY (ID),
KEY CountryCode (CountryCode),
KEY idx_city (Population,CountryCode),
CONSTRAINT city_ibfk_1 FOREIGN KEY (CountryCode) REFERENCES country (Code)
) ENGINE=InnoDB AUTO_INCREMENT=4080 DEFAULT CHARSET=latin1;
6)删除表空间文件
mysql> alter table city_new discard tablespaces;
7)拷贝旧表空间文件
[root@db01 world]# cp /data/3307/data/world/city.ibd /data/3307/data/world/city_new.ibd
8)授权
[root@db01 world]# chown -R mysql.mysql
9)导入表空间
mysql> alter table city_new import tablespace;