Error: Tablespace for table xxx exists. Please DISCARD the tablespace before IMPORT

A little late here but generally I’ve seen this problem occur when you get a ‘tablespace full’ error when running in a ‘innodb_file_per_table’ mode. Without going into too much detail (more here), the database server’s tablespace is defined by the innodb_data_file_path setting and by default is rather small. Even made larger, the ‘tablespace full’ can still occur with larger queries and such (lots of non-table ‘stuff’ is stored in there, undo logs, caches, etc…).

Anyways, I found that if you look in the OS directory where the files-per-table are stored, /var/lib/mysql by default on OSX, /usr/local/var/mysql with homebrew iirc, you’ll find an orphaned tablename.ibd file without it’s normal companion tablename.frm file. If you move that .ibd file to a safe temporary location (just to be safe) that should fix the problem.

$ ls /var/lib/mysql

table1.frm
table1.idb
table2.frm
table2.idb
table3.idb <- problem table, no table3.frm
table4.frm
table4.idb

$ mkdir /tmp/mysql_orphans
$ mv /var/lib/mysql/table3.ibd /tmp/mysql_orphans/

One caveat though, make sure what ever is causing the problem originally, e.g. long running query, locked table, etc… has been cleared. Otherwise you just end up with another orphaned .ibd file when you try a second time.

Leave a Comment