當(dāng)數(shù)據(jù)庫的磁盤滿了,需要drop一些比較大的表表鳍,但是DROP TABLE
不管用,因?yàn)閿?shù)據(jù)庫不能寫入。
背景
Database: my_db
Table name: my_tbl
Table size: 300GB+
保存了show create table my_tbl
的結(jié)果
刪除了對應(yīng)的my_tbl.ibd
旗唁,my_tbl.frm
文件
刪除之后,想重新把table建上痹束,可能會(huì)遇見以下錯(cuò)誤
ERROR 1813 (HY000): Tablespace '`XXX`.`XXXXXX`' exists.
嘗試
ALTER TABLESPACE [tablespace_name] DROP DATAFILE [file_name]
:不管用检疫。
解決方法:
- 創(chuàng)建一個(gè)新數(shù)據(jù)庫
my_db_2
,一定是要在同一個(gè)MySQL的server上祷嘶。 - 在新db中屎媳,運(yùn)行保存下來的create table語句,此方法可以在當(dāng)前數(shù)據(jù)庫文件里生成兩個(gè)對應(yīng)的文件
my_tbl.ibd
论巍,my_tbl.frm
- 把
my_tbl.frm
文件拷貝到舊的數(shù)據(jù)庫(my_db
)目錄下 chown mysql:mysql my_tbl.frm
- 在舊db(
my_db
)中烛谊,運(yùn)行DROP TABLE my_tbl
,此時(shí)就能把表刪除环壤,然后再用保存的語句創(chuàng)建同樣的表晒来。
Reference:
https://stackoverflow.com/a/12086798/5755004
InnoDB: You can drop the orphaned table inside InnoDB by
InnoDB: creating an InnoDB table with the same name in another
InnoDB: database and copying the .frm file to the current database.
InnoDB: Then MySQL thinks the table exists, and DROP TABLE will
InnoDB: succeed.