Web Hosting Knowledge Base Web Hosting Knowledge Base
  • Home
Get Started
Web Hosting Knowledge Base Web Hosting Knowledge Base
Get Started
Web Hosting Knowledge Base Web Hosting Knowledge Base
  • Home
loading
  1. Home
  2. Developer Corner
  3. Developer Corner Resources
  4. MySQL development
  5. How to repair MySQL databases and tables
Updated on June 14, 2024

Developer Corner

  • Folder icon closed Folder open iconDeveloper Corner Resources
    • MySQL development
      • How to connect to MySQL using Node.js
      • How to connect to MySQL using Microsoft .NET
      • How to manage MySQL databases, users, and tables from the command line
      • How to back up MySQL databases using AutoMySQLBackup
      • How to reset the MySQL root password
      • MySQL server runs out of memory or does not start
      • MySQL user privileges on shared servers
      • How to create and execute MySQL stored functions and procedures
      • Using MySQL views
      • Using MySQL triggers
      • Working with MySQL database engines
      • How to check MySQL database and table sizes
      • How to repair MySQL databases and tables
      • How to enable the slow query log in MySQL
      • How to rename a MySQL database
      • How to use indexes to improve MySQL query performance
      • How to restrict MySQL port access
      • How to install phpMyAdmin on managed hosting accounts
      • How to create a MySQL installation for local testing
      • How to troubleshoot MySQL definer issues
      • Using SELECT INTO OUTFILE in MySQL
      • How to determine the MySQL version
      • How to Fix the MySQL Error 1064
    • Ruby developer information
      • How to install Ruby Gems
      • How to use the Interactive Ruby Shell to troubleshoot gems
      • How to set up multiple versions of Ruby with the cPanel Ruby Selector
      • How to configure a Ruby on Rails application on a shared hosting account
    • Linux for the developer
      • How to manage the inode count
      • How to determine high disk usage locations
      • How to use screen
      • How to monitor resource usage
      • How to use rsync
      • How to use cURL
      • How to disable e-mail notifications from cron jobs
      • How to use ImageMagick
      • How to change the time zone in the Linux shell
      • Cron jobs reset to 15-minute intervals
      • How to install and configure Advanced Policy Firewall
      • How to configure a firewall using iptables
      • How to set the default text editor in Linux
      • How to determine a server's memory usage
      • How to generate and verify file checksums on Linux
      • How to install the EPEL repository on CentOS
      • How to run a cron job automatically after a server reboot
      • Conntrack sessions
      • How to install and configure UFW (Uncomplicated Firewall)
    • Version control systems
      • How to use Git
      • How to configure a Git client
      • How to configure Subversion (SVN)
      • How to set up Subversion (SVN) access for multiple users
      • How to use the Mercurial version control system
      • How to configure Concurrent Versions System (CVS)
      • How to configure and use a newer version of Git
      • '403 Forbidden' error message when you try to push to a GitHub repository
    • Client-side technologies
      • How to use the jQuery JavaScript library on your hosting account
      • How to install Ember.js on managed hosting accounts
      • How to use the markItUp JavaScript plugin
      • How to Use Flash on Your Website
      • Does Hostvento Hosting Support MP3 Hosting Files?
      • Hostvento Hosting MIDI (Musical Instrument Digital Interface) Support
      • How to use React on your hosting account
    • Application deployment information
      • How to install the Docker application deployment environment
      • How to install a LAMP stack on an Ubuntu server
    • How to add CAPTCHA protection to your web site
    • How to control search engines and web crawlers using the robots.txt file
    • How to set up URL frame forwarding
    • SQLite information
      • How to connect to SQLite from the command line
      • How to connect to SQLite using PHP
      • How to connect to SQLite using Python
    • Using Lua on Hostvento Hosting servers
    • How to determine if your account uses CageFS
    • How to set up a virtual machine with VirtualBox for local testing
    • Web testing and development
      • How to create a PostgreSQL installation for local testing
      • How to create an Apache installation for local testing
      • How to install development tools on an unmanaged server
    • How to create persistent Node.js applications on shared and reseller hosting accounts
    • How to enable Cross-Origin Resource Sharing (CORS)
    • How to configure frames with X-Frame-Options header
    • Developing ADA-compliant web sites
    • Nginx web server configuration
      • How to install the Nginx web server
    • How to install and configure the AWS Command Line Interface (CLI)
    • How to edit hosts file on Mac
    • How to disable automatic creation of the robots.txt file
    • How to use an SSL certificate in a Node.js app
  • Folder icon closed Folder open iconHow to enable and disable PHP error messages
  • Folder icon closed Folder open iconHow to enable PHP error logging
  • Folder icon closed Folder open iconHow to enable and disable the PHP register_globals directive
  • Folder icon closed Folder open iconHow to use the PHP magic quotes directive
  • Folder icon closed Folder open iconHow to change the PHP time zone setting
  • Folder icon closed Folder open iconHow to enable and disable the PHP allow_url_fopen directive
  • Folder icon closed Folder open iconPHP max_execution_time Guide
  • Folder icon closed Folder open iconHow to change the maximum upload file size for PHP scripts
  • Folder icon closed Folder open iconHow to connect to MySQL from the command line
  • Folder icon closed Folder open iconHow to connect to MySQL using PHP
  • Folder icon closed Folder open iconHow to connect to MySQL using Perl
  • Folder icon closed Folder open iconHow to import and export a MySQL database
  • Folder icon closed Folder open iconHow to set up a remote MySQL connection
  • Folder icon closed Folder open iconUsing MySQL client applications
  • Folder icon closed Folder open iconHow to convert a MySQL database to UTF-8 encoding
  • Folder icon closed Folder open iconHow to convert the time zone in MySQL
  • Folder icon closed Folder open iconHow to back up MySQL databases using cron jobs
  • Folder icon closed Folder open iconInformation schema database in MySQL
  • Folder icon closed Folder open iconHow to connect to PostgreSQL using psql
  • Folder icon closed Folder open iconHow to connect to PostgreSQL using PHP
  • Folder icon closed Folder open iconHow to connect to PostgreSQL using Perl
  • Folder icon closed Folder open iconHow to import and export a PostgreSQL database
  • Folder icon closed Folder open iconHow to set up a remote PostgreSQL connection
  • Folder icon closed Folder open iconHow to use .htaccess files
  • Folder icon closed Folder open iconHow to change the default directory index page using an .htaccess file
  • Folder icon closed Folder open iconHow to enable and disable directory index listings using an .htaccess file
  • Folder icon closed Folder open iconHow to enable data compression using the mod_deflate module
  • Folder icon closed Folder open icon'403 Forbidden' error message
  • Folder icon closed Folder open iconHow to enable pathname information in URLs using the AcceptPathInfo directive
  • Folder icon closed Folder open iconHow to troubleshoot CGI scripts
  • Folder icon closed Folder open iconWhat is PHP?
  • Folder icon closed Folder open iconPHP script basics
  • Folder icon closed Folder open iconHow to view PHP settings with the phpinfo() function
  • Folder icon closed Folder open iconControlling PHP settings with a custom php.ini file
  • Folder icon closed Folder open iconUsing php.ini directives
  • Folder icon closed Folder open iconHow to run PHP scripts from cron jobs
  • Folder icon closed Folder open iconSetting the PHP include path
  • Folder icon closed Folder open iconHow to determine if a PHP function is available
  • Folder icon closed Folder open iconHow to fix '500 Internal Server Error' while running PHP
  • Folder icon closed Folder open iconPython script basics
  • Folder icon closed Folder open iconPerl script basics
  • Folder icon closed Folder open iconHow to troubleshoot Perl scripts
  • Folder icon closed Folder open iconHow to use the shebang
  • Folder icon closed Folder open iconPostgreSQL development
    • How to connect to PostgreSQL using Python
    • How to use PostgreSQL client applications
    • How to back up PostgreSQL databases using cron jobs
    • How to manage PostgreSQL databases and users from the command line
    • How to determine the PostgreSQL and PostGIS versions
    • How to determine the size of PostgreSQL databases and tables
    • How to rename a PostgreSQL database
    • ECCN and HTS numbers for PostgreSQL
  • Folder icon closed Folder open iconApache web server configuration
    • How to change the document root directory
    • How to add or remove the www prefix in domain URLs
    • How To Use Apache Access Log & Apache Error Log
    • How to rewrite URLs in Apache with the mod_rewrite module
    • How to use Server-Side Includes (SSI)
    • How to use the mod_speling Apache module
    • How to modify HTTP headers using .htaccess files
    • How to configure caching with the mod_expires module
    • How to disable entity tags (ETags)
    • How to use keep-alive connections to improve performance
    • How to control site access based on a visitor's country
    • How to turn off caching using .htaccess
    • How to install Apache Tomcat
    • How to protect a website folders using .htaccess
  • Folder icon closed Folder open iconPHP development resources
    • How to change PHP settings and versions on managed hosting accounts
    • How to set up custom .htaccess files for PHP settings
    • Using PHP directives in custom .htaccess files
    • Using PHP to send e-mail messages
    • How to install and configure PEAR packages
    • How to troubleshoot 'String could not be parsed as XML' error message in PHP
    • How to use the Exif extension in PHP
    • How to resolve some common issues after a PHP upgrade
    • How to use a custom php.ini file for cPanel cron jobs
    • How To Use PHP Sessions
    • ionCube PHP Loader support
    • How to specify the MySQL character set in PHP
    • How to fix the 'Unable to allocate memory for pool' PHP error
    • How to optimize Symfony using APC
    • How to use the internationalization (intl) extension in PHP
    • How to install PHP composer
    • How to determine if APC is installed on a server
    • How to use the MailChimp API with PHP
    • 'Allowed memory size exhausted' error message in PHP
    • How to use ionCube Loader with different PHP versions
    • How to enable PHP opcode caching
    • How to send e-mail using PHPMailer
    • How to harden PHP 7 and earlier versions using cPanel
    • Custom PHP settings per directory with .user.ini files
    • ionCube PHP Loader unavailable on PHP 8.0
    • How to change the PHP version for a specific directory
  • Folder icon closed Folder open iconPython
    • How to use virtualenv and pip
    • How to activate a Python virtual environment from a script file
    • How to send e-mail messages using Python
    • How to configure and use a newer version of Python
    • How to install and configure Django on unmanaged servers
    • How to install and configure Django on a Linux shared hosting account
    • How to install and configure Flask on a Linux shared hosting account
    • How to install FastAPI on unmanaged servers
  • Folder icon closed Folder open iconPerl Information Overview

How to repair MySQL databases and tables

Estimated reading: 7 minutes 264 views

This article describes how to repair MySQL tables and databases. As a database’s tables grow, errors may occur from time to time. When they do, MySQL includes several tools that you can use to check and repair database tables. To do this, follow the procedures below in the order in which they appear.

This article only applies to products listed in the Article Details sidebar. You must have root access to the server to follow these procedures.

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:Copyservice mysqld stop
    • For Debian and Ubuntu, type:
      Copyservice mysql stop
  3. Type the following command:
    Copycp -rfv /var/lib/mysql /var/lib/mysql$(date +%s)This command copies all of the files from all of your databases to a directory name based on the current time (more precisely, the number of seconds elapsed since January 1, 1970). This ensures that each database backup is stored in a directory that has a unique name. For added protection, you can (and should) back up the database files to a remote location not on the server.
  4. Restart the MySQL server using the appropriate command for your Linux distribution:
    • For CentOS and Fedora, type:Copyservice mysqld start
    • For Debian and Ubuntu, type:
      Copyservice mysql start

Step 2: Running mysqlcheck

After you back up your databases, you are ready to start troubleshooting. The mysqlcheck program enables you to check and repair databases while MySQL is running. This feature is useful when you want to work on a database without stopping the entire MySQL service.

Additionally, mysqlcheck works on tables that use the MyISAM or InnoDB database engines.

To use mysqlcheck, follow these steps:

  1. As the root user, type the following command:Copycd /var/lib/mysql
  2. Type the following command, replacing database with the name of the database that you want to check:
    Copymysqlcheck databaseThe 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:Copymysqlcheck 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:
    Copymysqlcheck -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.

For information about how to determine which storage engine your database tables are using, please see this article.

Repairing MyISAM tables with myisamchk

If you are using the MyISAM storage engine for a table, you can run the myisamchk program to repair it. To do this, follow these steps:

The myisamchk program only works for tables that use the MyISAM storage engine. It does not work for the InnoDB engine.

  1. Stop the MySQL server using the appropriate command for your Linux distribution:
    • For CentOS and Fedora, type:Copyservice mysqld stop
    • For Debian and Ubuntu, type:
      Copyservice mysql stop
  2. Type the following command:
    Copycd /var/lib/mysql
  3. Change to the directory where the database is located. For example, if the database is named customers, type cd customers.
  4. Type the following command, replacing table with the name of the table that you want to check:
    Copymyisamchk tableTo check all of the tables in a database, type the following command:
    Copymyisamchk *.MYIIf the previous command does not work, you can try deleting temporary files that may be preventing myisamchk from running correctly. To do this, change back to the /var/lib/mysql directory, and then type the following command:Copyls */*.TMD If there are any .TMD files listed, type the following command to delete them:Copyrm */*.TMDThen try to run myisamchk again.
  5. To try to repair a table, type the following command, replacing table with the name of the table that you want to repair:
    Copymyisamchk –recover table
  6. Restart the MySQL server using the appropriate command for your Linux distribution:
    • For CentOS and Fedora, type:Copyservice mysqld start
    • For Debian and Ubuntu, type:
      Copyservice mysql start
  7. Test the repaired table or tables.
Running the InnoDB recovery process

If you are using the InnoDB storage engine for a database table, you can run the InnoDB recovery process. To do this, follow these steps:

  1. Use your preferred text editor to open the my.cnf file on your server. The location of the my.cnf file depends on your Linux distribution:
    • 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.
  2. In the my.cnf file, locate the [mysqld] section.
  3. Add the following line to the [mysqld] section:
    innodb_force_recovery=4
  4. Save the changes to the my.cnf file, and then restart the MySQL server using the appropriate command for your Linux distribution:
    • For CentOS and Fedora, type:Copyservice mysqld restart
    • For Debian and Ubuntu, type:
      Copyservice mysql restart
  5. Type the following command to export all of the databases to the databases.sql file:
    Copymysqldump –all-databases –add-drop-database –add-drop-table –routines > databases.sql
  6. Start the mysql program, and then try to drop the affected database or databases using the DROP DATABASE command.
    If MySQL is unable to drop a database, you can delete it manually in step 8 below after you stop the MySQL server.
  7. Stop the MySQL server using the appropriate command for your Linux distribution:
    • For CentOS and Fedora, type:Copyservice mysqld stop
    • For Debian and Ubuntu, type:
      Copyservice mysql stop
  8. If you were unable to drop a database in step 6, type the following commands to delete it manually. Replace database with the name of the database that you want to delete:
    Copycd /var/lib/mysql rm -rf databaseMake sure you do not delete the mysql or performance_schema directories!
  9. Use your preferred text editor to open the my.cnf file on your server, and then comment out the following line in the [mysqld] section as shown:
    #innodb_force_recovery=4This disables InnoDB recovery mode.
  10. Save the changes to the my.cnf file, and then start the MySQL server using the appropriate command for your Linux distribution:
    • For CentOS and Fedora, type:Copyservice mysqld start
    • For Debian and Ubuntu, type:
      Copyservice mysql start
  11. Type the following command to restore the databases from the backup file you created in step 5:
    Copymysql < databases.sql
  12. Test the restored database.

How to optimize and customize your Drupal site

Still stuck? How can we help?

How can we help?

Was this page helpful? Yes No

Share this Doc

How to repair MySQL databases and tables

Or copy link

Clipboard Icon
CONTENTS
Leaf Illustration

© 2023 All Rights Reserved by Hostvento