I accidentally dropped a MySQL database on my server. Are there any ways to recover a dropped database?
- Anybody can ask a question
- Anybody can answer
- The best answers are voted up and rise to the top
There's no easy way to break this to you. It doesn't matter if you dropped a db via phpmyadmin or the commandline. It is gone.
Human error is one reason to have a good backup regimen.
I'm not religious but I'll say a prayer hoping it wasn't anything too important.
If you act fast there is a high chance to get your database back. The chance is higher for InnoDB, for MyISAM it's non-zero, but close.
The matter is when MySQL executes DROP TABLE or DROP DATABASE (which is essentially the same) InnoDB doesn't wipe the data out. Pages with the data are still on disk.
Depending on innodb_file_per_table setting the recovery process differs. If innodb_file_per_table is OFF (default up until 5.5) then the dropped table remains in ibdata1. If innodb_file_per_table is ON (default as of 5.5) then the dropped table was in the respective .ibd file. MySQL removes this file when drops the table.
The very first thing to do is to stop any possible writes so your table isn't overwritten. If innodb_file_per_table is OFF it's enough to stop MySQL (kill -9 is even better, but make sure you kill safe_mysqld first). If innodb_file_per_table is ON then umount partition where MySQL stores its data. If the datadir is on the root partition I recommend to shut down server or at least take an image of the disk. Let me repeat, the goal is to prevent overwriting dropped table by MySQL or operating system.
There is a tool that allows to work with InnoDB pages at low level, TwinDB data recovery toolkit. I will use it to illustrate undrop recovery.
You need to take the media with dropped table (either ibdata1 or disk image) and find InnoDB pages on it. stream_parser tool from the toolkit does it.
It will scan the file, find the InnoDB pages and sort them by type and index_id. index_id is an identifier that InnoDB uses to refer to an index. A table is stored in index PRIMARY. To find what index_id is your dropped table you need to recover InnoDB dictionary.
The InnoDB dictionary is stored in ibdat1 file. You need to scan ibdata1 file the same way as above:
Now you need to get records from the InnoDB dictionary tables SYS_TABLES and SYS_INDEXES(let's say your table is sakila.actor):
158 is table_id, remember it.
So, index_id of your dropped table(sakila.actor) is 376.
Now you can fetch records of the dropped table from InnoDB index_id 376. You need to have the table structure of the dropped table, exactly CREATE TABLE statement which the table was created with. Where you can get it? Either from old backup, or from elsewhere. It's also possible to recover the structure from the InnoDB dictionary, but I won't cover it in this answer. Let's just assume you have it.
c_parser outputs records as tab-separated dump to stdout. The dump can be loaded with LOAD DATA command. c_parser prints it to stderr.
See more details in posts:
Depends on your setup. It is possible to restore if you setup your system correctly. If you have a backup you can restore it. and then apply the binary logs up to the point just before you dropped the table.
I suggest you do this on another server, once you have the table restored you can use mysqldump to extract it and import back to your production server. This will not be a fast restore, but you can recover the data.
If you don't know what you are doing I would suggest openning a support contract with one of the mysql consulting companies (pythian, percona, palamino are probably the best) and have them help you with this.
I wish you the best of luck