2015-07-01 回答
method #1 – create work tables
1. start up clean/fresh instance of mysql with innodb_file_per_table enabled.
2. now, we need to find the table id that mysql is currently set at, as well as the table id for the table we need to recover.
note:
step 2 (2a – 2f) is simply to find the table id that is stored inside of
the .ibd file. i’ve written a php script to determine this, so using
the script can save a bunch of time. see the bottom of this page (under
“associated files”) for the exact script.
2a. create a test database:
mysql> create database test1;
mysql> use test1;
2b. issue the create table command for the table:
mysql> create table `product` (
`product_id` bigint(20) unsigned not null auto_increment,
`brand_id` int(10) unsigned default null,
`product_type_id` int(10) unsigned default null,
`group_id` int(10) unsigned default null,
`product_name` varchar(500) not null,
`default_email_id` varchar(48) default null,
`product_status` tinyint(1) not null,
`client_id` bigint(20) unsigned default null,
`last_modified_by` varchar(45) not null,
`last_modified_date` datetime not null,
primary key (`product_id`)
) engine=innodb;
2c. discard the tablespace, which will delete the newly created .ibd file:
mysql> alter table product discard tablespace;
2d. copy the pre-existing .ibd file to the datadir/test1 folder
2e. import this tablespace:
mysql> alter table product import tablespace;
this should produce the following error (at least this is most likely).
the only way it would not is if mysql’s current table id matched that of
the preexisting ibd table id. in which case, you can now dump your
table.
error 1030 (hy000): got error -1 from storage engine
2f. so, now to check the error log (manually). look for the following entry:
081010 11:47:40 innodb: error: tablespace id in file
'.test1product.ibd' is 1193, but in the innodb
innodb: data dictionary it is 1.
so, now we know the internal table id is at 1, and that of the ibd table is 1193.
3. clean up working database:
3a. manually move the ibd file from the $datadir to a safe location (as you will need this file again).
3b. drop this table.
mysql> drop table product;
note this does not re-set the internal table counter.
4. you’ll need to create the number of tables you need to increase the internal table id value.
in this case, you’d create 1191 test innodb tables (already at 1, and
need to leave 1 for the actual table, so 1193-2=1191). run below in a
loop.
for ($1=1; $i<=1191; $1++) {
create table t# (id int) engine=innodb;
}
i accomplished this via a simple php script. see the bottom of this page (under "associated files") for the exact script.
5. after these are created, go ahead and drop this database and all tables (as they are not needed).
drop db test1;
6. now, re-perform steps 2a through 2e.
mysql> create database test1;
mysql> use test1;
mysql> create table `product` ( ... ) engine=innodb;
mysql> alter table product discard tablespace;
mysql> alter table product import tablespace;
success!