企业案例二

在没有备份数据的情况下,突然断电导致表损坏,打不开数据库。

1.断电 表损坏

#1.打包某个独立表空间
[root@db01 /usr/local/mysql/data]# tar zcf world.tar.gz world

#2.将打包的文件传输到一台数据库
[root@db01 /usr/local/mysql/data]# scp world.tar.gz 172.16.1.52:/tmp/

#3.将新数据库的文件解压到新数据库的数据目录下
[root@db02 ~]# tar xf /tmp/world.tar.gz -C /service/mysql/data/

#4.新数据库查看数据
mysql> use world;
mysql> show tables;
+-----------------+
| Tables_in_world |
+-----------------+
| city            |
| country         |
| countrylanguage |
| jixiao          |
+-----------------+
4 rows in set (0.00 sec)

#5.操作表数据
mysql> select * from city;
ERROR 1146 (42S02): Table 'world.city' doesn't exist

2.解决数据库表损坏的问题

#1.找一台新的数据库重新建新表
CREATE TABLE `city_new` (
  `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 `index_key` (`Name`),
  KEY `idx_key` (`ID`),
  KEY `population_key` (`Population`),
  KEY `District_key` (`District`)
) ENGINE=InnoDB AUTO_INCREMENT=4080 DEFAULT CHARSET=latin1;

#2.数据库操作city_new清除自己的表空间
mysql> alter table city_new discard tablespace;

#3.物理拷贝city的数据文件
[root@db02 /service/mysql/data/world]# cp city.ibd city_new.ibd
[root@db02 /service/mysql/data/world]# chown -R mysql.mysql city_new.ibd

#4.city_new读取自己的表空间数据
mysql> alter table city_new import tablespace;

#5.数据查询
mysql> select * from city_new;

#6.删除损坏的表
mysql> drop table city;
ERROR 1051 (42S02): Unknown table 'world.city'        #只是说不认识,没说不能删除

[root@db02 /service/mysql/data/world]# rm city.ibd        #物理删除表数据

#7.修改表名
mysql> alter table city_new rename city;

3.恢复业务

1.开发修改数据库连接信息
2.将数据重新导出再导入旧机器
原文地址:https://www.cnblogs.com/chenlifan/p/13907359.html