STEP 1: BACKING UP THE DATABASES
Before you attempt to repair any database, you should back it up first. To back up all of the files from all of your databases, follow these steps:
1. Log in to your server using SSH.
2. Stop the MySQL server using the appropriate command for your Linux distribution:
- For CentOS and Fedora, type:
service mysqld stop
- For Debian and Ubuntu, type:
service mysql stop
- For CentOS and Fedora, type:
3. Type the following command:
cp -rfv /var/lib/mysql /var/lib/mysql$(date +%s)
4. Restart the MySQL server using the appropriate command for your Linux distribution:
- For CentOS and Fedora, type:
service mysqld start
- For Debian and Ubuntu, type:
service mysql start
- For CentOS and Fedora, type:
STEP 2: CHECKING AND REPAIRING A TABLE WITH MYSQLCHECK
You are prepared to begin debugging after backing up your databases. While MySQL is operating, you can check and fix databases using the mysqlcheckprogram. When you need to operate on a database without shutting down the entire MySQL service, this capability is helpful.
Additionally, mysqlcheck works on tables that use the MyISAM or InnoDB database engines.
1. As the root user, type the following command:
cd /var/lib/mysql
2. Type the following command, replacing DATABASE with the name of the database that you want to check:
mysqlcheck DATABASE
The previous command checks all of the tables in the specified database. Alternatively, to check a specific table in a database, type the following command. Replace DATABASE with the name of the database, and replace TABLE with the name of the table that you want to check:
mysqlcheck DATABASE TABLE
3. Mysqlcheck checks the specified database and tables. If a table passes the check, mysqlcheck displays OK for the table. However, if mysqlcheck reports an error for a table, type the following command to try to repair it. Replace DATABASE with the database name, and TABLE with the table name:
mysqlcheck -r DATABASE TABLE
4. If mysqlcheck cannot successfully repair the table or tables, go to the following procedure.
STEP 3: RUNNING ENGINE-SPECIFIC DIAGNOSTICS
If running mysqlcheck does not fix the problem, the next step is to run diagnostics specific to the engine used by the database table or tables. Follow the appropriate procedure below for your table’s database storage engine.
Repairing MyISAM tables with myisamchk
- Firstly, stop the MySQL server using the appropriate command for your Linux distribution:
- For CentOS and Fedora, type:
service mysqld stop
- For Debian and Ubuntu, type:
service mysql stop
myisamchk TABLE
myisamchk *.MYI
ls */*.TMDIf there are any .TMD files listed, type the following command to delete them:
rm */*.TMDThen try to run myisamchk again.
myisamchk --recover TABLE
-
- For CentOS and Fedora, type:
service mysqld start
- For Debian and Ubuntu, type:
service mysql start
- For CentOS and Fedora, type:
Running the InnoDB recovery process
- On CentOS and Fedora, the my.cnf file is located in the /etc directory.
- On Debian and Ubuntu, the my.cnf file is located in the /etc/mysql directory.
innodb_force_recovery=4
- For CentOS and Fedora, type:
service mysqld restart
- For Debian and Ubuntu, type:
service mysql restart
mysqldump --all-databases --add-drop-database --add-drop-table > databases.sql
- For CentOS and Fedora, type:
service mysqld stop
- For Debian and Ubuntu, type:
service mysql stop
cd /var/lib/mysql rm -rf DBNAME
#innodb_force_recovery=4
- For CentOS and Fedora, type:
service mysqld start
- For Debian and Ubuntu, type:
service mysql start
mysql < databases.sql