Mysql Crash Recovery
Mysql Crash Recovery
In the event of database corruption it may be necessary to manually perform database recovery. See Bug 15797 for an example of an issue with mysql that will require database recovery. In that example, a warning message like the following appeared in the mysql error log:
InnoDB: Serious error! InnoDB is trying to free page 716 InnoDB: though it is already marked as free in the tablespace! InnoDB: The tablespace free space info is corrupt. InnoDB: You may need to dump your InnoDB tables and recreate the whole InnoDB: database!
Overview of Recovery Process
- Configure mysql to start in recovery mode
- Generate SQL dumps of all relevant databases
- Remove all existing (and possibly corrupt) databases
- Re-create all databases
- Repopulate the databases with the data from the SQL dumps
- Test databases and start all ZCS services
Details of Recovery Process
1. Configure mysql to start in recovery mode
- Edit the file /opt/zimbra/conf/my.cnf and add a line like innodb_force_recovery = 1 under the [mysqld] section (Note that it may be necessary to increase the recovery level depending on the extent of the database corruption, as shown at the end of the database dump step)
- Save the file and start mysqld
mysql.server start
2. Generate SQL dumps of all databases
- Load some mysql configuration into shell variables (i.e. $mysql_socket and $mysql_root_password; note that you will use these again in step 3)
- Make a list of the existing databases
- Create a directory to hold the SQL dumps
- Generate the SQL dumps from the database list
source ~/bin/zmshutil ; zmsetvars
mysql --batch --skip-column-names -e "show databases" | grep -e mbox -e zimbra > /tmp/mysql.db.list
mkdir /tmp/mysql.sql
for db in `cat /tmp/mysql.db.list`; do ~/mysql/bin/mysqldump $db -S $mysql_socket -u root --password=$mysql_root_password > /tmp/mysql.sql/$db.sql echo "Dumped $db" done
Note: If you encounter any mysql errors while dumping the databases, start over by re-editing /opt/zimbra/conf/my.cnf, incrementing the value for innodb_force_recovery by one, and restarting mysqld. The maximum recovery level is 6. Please see MySQL's Forcing InnoDB Recovery guide for more information.
3. Remove all existing (and possibly corrupt) databases
for db in `cat /tmp/mysql.db.list` do mysql -u root --password=$mysql_root_password -e "drop database $db" echo -e "Dropped $db" done
4. Re-create all databases
- Run mysql in non-recovery mode
- Remove the innodb_force_recovery line from /opt/zimbra/conf/my.cnf
- Save the file and restart mysqld
- Re-create the databases from the database list
mysql.server restart
for db in `cat /tmp/mysql.db.list` do mysql -e "create database $db character set utf8" echo "Created $db" done
5. Repopulate the databases with the data from the SQL dumps
Import the data from the SQL dumps
for sql in /tmp/mysql.sql/* do mysql `basename $sql .sql` < $sql echo -e "Updated `basename $sql .sql` \n" done
6. Test databases and start all ZCS services
Note that this is an example query. If you know of any particular databases that were corrupt, you may want to construct other queries to verify normal access to the data.
mysql zimbra -e "select * from mailbox order by id desc limit 1"
Once you are satisfied that the databases are restored intact, start the rest of the zimbra services.
zmcontrol start
Check /opt/zimbra/log/mysql_error.log and /opt/zimbra/log/mailbox.log for database errors.