1. 环境
CentOS release 6.4 MySQL 5.6.14
innodb_file_per_table = ON
2. 问题
手工删除mysql.innodb_index_stats表文件
cd /usr/local/mysql/data/mysql
rm -f innodb_index_stats.*
use mysql;
show tables;
已经没有innodb_index_stats表
建表:
CREATE TABLE `innodb_index_stats` (
`database_name` varchar(64) COLLATE utf8_bin NOT NULL,
`table_name` varchar(64) COLLATE utf8_bin NOT NULL,
`index_name` varchar(64) COLLATE utf8_bin NOT NULL,
`last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`stat_name` varchar(64) COLLATE utf8_bin NOT NULL,
`stat_value` bigint(20) unsigned NOT NULL,
`sample_size` bigint(20) unsigned DEFAULT NULL,
`stat_description` varchar(1024) COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`database_name`,`table_name`,`index_name`,`stat_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin STATS_PERSISTENT=0;
显示错误:
ERROR 1050 (42S01): Table 'innodb_index_stats' already exists
删表:
drop table innodb_index_stats;
显示错误:
ERROR 1051 (42S02): Unknown table 'mysql.innodb_index_stats'
此时,建表建不上,报表已经存在,删表删不了,报不知道的表。
3. 分析
先看一下.err文件里写了些什么
InnoDB: Error: table mysql.innodb_index_stats already exists in InnoDB internal
InnoDB: data dictionary. Have you deleted the .frm file
InnoDB: and not used DROP TABLE? Have you used DROP DATABASE
InnoDB: for InnoDB tables in MySQL version <= 3.23.43?
InnoDB: See the Restrictions section of the InnoDB manual.
InnoDB: You can drop the orphaned table inside InnoDB by
InnoDB: creating an InnoDB table with the same name in another
InnoDB: database and moving the .frm file to the current database.
InnoDB: Then MySQL thinks the table exists, and DROP TABLE will
InnoDB: succeed.
写的挺清楚的,我确实是删除了.frm文件而没有使用drop table命令。建表错误是因为InnoDB内部数据字典(ibdata1文件)里已经有了mysql.innodb_index_stats表,删表错 误是因为没有.frm文件。连解决方案也写了,照着做就是。
4. 解决
CREATE SCHEMA recovery;
use recovery
CREATE TABLE innodb_index_stats (id INT) ENGINE = InnoDB;
cp $datadir/recovery/innodb_index_stats.frm $datadir/mysql/
use mysql
DROP TABLE innodb_index_stats;
CREATE TABLE `innodb_index_stats` (
`database_name` varchar(64) COLLATE utf8_bin NOT NULL,
`table_name` varchar(64) COLLATE utf8_bin NOT NULL,
`index_name` varchar(64) COLLATE utf8_bin NOT NULL,
`last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`stat_name` varchar(64) COLLATE utf8_bin NOT NULL,
`stat_value` bigint(20) unsigned NOT NULL,
`sample_size` bigint(20) unsigned DEFAULT NULL,
`stat_description` varchar(1024) COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`database_name`,`table_name`,`index_name`,`stat_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin STATS_PERSISTENT=0;
DROP SCHEMA recovery;
这时表已建好,如果有备份就可以恢复数据了。
5. 警告
这么做之前最好先备份数据库,而且这种方式不会恢复数据,建表语句也是得有的,它只能作为一种补救的办法。
--转自