<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h3 class="title">
      <a name="myisam-crash-recovery">
      </a>
      9.6.1 Using myisamchk for Crash Recovery
     </h3>
    </div>
   </div>
  </div>
  <a class="indexterm" name="idm46045230673632">
  </a>
  <a class="indexterm" name="idm46045230672176">
  </a>
  <a class="indexterm" name="idm46045230670688">
  </a>
  <a class="indexterm" name="idm46045230669616">
  </a>
  <p>
   This section describes how to check for and deal with data
        corruption in MySQL databases. If your tables become corrupted
        frequently, you should try to find the reason why. See
   <a class="xref" href="crashing.html" title="B.3.3.3 What to Do If MySQL Keeps Crashing">
    Section B.3.3.3, “What to Do If MySQL Keeps Crashing”
   </a>
   .
  </p>
  <p>
   For an explanation of how
   <code class="literal">
    MyISAM
   </code>
   tables can
        become corrupted, see
   <a class="xref" href="myisam-table-problems.html" title="18.2.4 MyISAM Table Problems">
    Section 18.2.4, “MyISAM Table Problems”
   </a>
   .
  </p>
  <p>
   If you run
   <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
    <span class="command">
     <strong>
      mysqld
     </strong>
    </span>
   </a>
   with external locking
        disabled (which is the default), you cannot reliably use
   <a class="link" href="myisamchk.html" title="6.6.4 myisamchk — MyISAM Table-Maintenance Utility">
    <span class="command">
     <strong>
      myisamchk
     </strong>
    </span>
   </a>
   to check a table when
   <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
    <span class="command">
     <strong>
      mysqld
     </strong>
    </span>
   </a>
   is using the same table. If you can be
        certain that no one can access the tables using
   <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
    <span class="command">
     <strong>
      mysqld
     </strong>
    </span>
   </a>
   while you run
   <a class="link" href="myisamchk.html" title="6.6.4 myisamchk — MyISAM Table-Maintenance Utility">
    <span class="command">
     <strong>
      myisamchk
     </strong>
    </span>
   </a>
   , you only have to execute
   <a class="link" href="mysqladmin.html" title="6.5.2 mysqladmin — A MySQL Server Administration Program">
    <span class="command">
     <strong>
      mysqladmin flush-tables
     </strong>
    </span>
   </a>
   before you start
        checking the tables. If you cannot guarantee this, you must stop
   <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
    <span class="command">
     <strong>
      mysqld
     </strong>
    </span>
   </a>
   while you check the tables. If you run
   <a class="link" href="myisamchk.html" title="6.6.4 myisamchk — MyISAM Table-Maintenance Utility">
    <span class="command">
     <strong>
      myisamchk
     </strong>
    </span>
   </a>
   to check tables that
   <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
    <span class="command">
     <strong>
      mysqld
     </strong>
    </span>
   </a>
   is updating at the same time, you may
        get a warning that a table is corrupt even when it is not.
  </p>
  <p>
   If the server is run with external locking enabled, you can use
   <a class="link" href="myisamchk.html" title="6.6.4 myisamchk — MyISAM Table-Maintenance Utility">
    <span class="command">
     <strong>
      myisamchk
     </strong>
    </span>
   </a>
   to check tables at any time. In
        this case, if the server tries to update a table that
   <a class="link" href="myisamchk.html" title="6.6.4 myisamchk — MyISAM Table-Maintenance Utility">
    <span class="command">
     <strong>
      myisamchk
     </strong>
    </span>
   </a>
   is using, the server waits for
   <a class="link" href="myisamchk.html" title="6.6.4 myisamchk — MyISAM Table-Maintenance Utility">
    <span class="command">
     <strong>
      myisamchk
     </strong>
    </span>
   </a>
   to finish before it continues.
  </p>
  <p>
   If you use
   <a class="link" href="myisamchk.html" title="6.6.4 myisamchk — MyISAM Table-Maintenance Utility">
    <span class="command">
     <strong>
      myisamchk
     </strong>
    </span>
   </a>
   to repair or optimize
        tables, you
   <span class="emphasis">
    <em>
     must
    </em>
   </span>
   always ensure that the
   <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
    <span class="command">
     <strong>
      mysqld
     </strong>
    </span>
   </a>
   server is not using the table (this
        also applies if external locking is disabled). If you do not
        stop
   <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
    <span class="command">
     <strong>
      mysqld
     </strong>
    </span>
   </a>
   , you should at least do a
   <a class="link" href="mysqladmin.html" title="6.5.2 mysqladmin — A MySQL Server Administration Program">
    <span class="command">
     <strong>
      mysqladmin flush-tables
     </strong>
    </span>
   </a>
   before you run
   <a class="link" href="myisamchk.html" title="6.6.4 myisamchk — MyISAM Table-Maintenance Utility">
    <span class="command">
     <strong>
      myisamchk
     </strong>
    </span>
   </a>
   . Your tables
   <span class="emphasis">
    <em>
     may become
        corrupted
    </em>
   </span>
   if the server and
   <a class="link" href="myisamchk.html" title="6.6.4 myisamchk — MyISAM Table-Maintenance Utility">
    <span class="command">
     <strong>
      myisamchk
     </strong>
    </span>
   </a>
   access the tables simultaneously.
  </p>
  <p>
   When performing crash recovery, it is important to understand
        that each
   <code class="literal">
    MyISAM
   </code>
   table
   <em class="replaceable">
    <code>
     tbl_name
    </code>
   </em>
   in a database corresponds to
        the three files in the database directory shown in the following
        table.
  </p>
  <div class="informaltable">
   <table summary="The two files in the database directory that correspond to each MyISAM table.">
    <colgroup>
     <col style="width: 20%"/>
     <col style="width: 40%"/>
    </colgroup>
    <thead>
     <tr>
      <th>
       File
      </th>
      <th>
       Purpose
      </th>
     </tr>
    </thead>
    <tbody>
     <tr>
      <td>
       <code class="filename">
        <em class="replaceable">
         <code>
          tbl_name
         </code>
        </em>
        .MYD
       </code>
      </td>
      <td>
       Data file
      </td>
     </tr>
     <tr>
      <td>
       <code class="filename">
        <em class="replaceable">
         <code>
          tbl_name
         </code>
        </em>
        .MYI
       </code>
      </td>
      <td>
       Index file
      </td>
     </tr>
    </tbody>
   </table>
  </div>
  <p>
   Each of these three file types is subject to corruption in
        various ways, but problems occur most often in data files and
        index files.
  </p>
  <p>
   <a class="link" href="myisamchk.html" title="6.6.4 myisamchk — MyISAM Table-Maintenance Utility">
    <span class="command">
     <strong>
      myisamchk
     </strong>
    </span>
   </a>
   works by creating a copy of the
   <code class="filename">
    .MYD
   </code>
   data file row by row. It ends the
        repair stage by removing the old
   <code class="filename">
    .MYD
   </code>
   file
        and renaming the new file to the original file name. If you use
   <a class="link" href="myisamchk-repair-options.html#option_myisamchk_quick">
    <code class="option">
     --quick
    </code>
   </a>
   ,
   <a class="link" href="myisamchk.html" title="6.6.4 myisamchk — MyISAM Table-Maintenance Utility">
    <span class="command">
     <strong>
      myisamchk
     </strong>
    </span>
   </a>
   does not create a temporary
   <code class="filename">
    .MYD
   </code>
   file, but instead assumes that the
   <code class="filename">
    .MYD
   </code>
   file is correct and generates only a
        new index file without touching the
   <code class="filename">
    .MYD
   </code>
   file. This is safe, because
   <a class="link" href="myisamchk.html" title="6.6.4 myisamchk — MyISAM Table-Maintenance Utility">
    <span class="command">
     <strong>
      myisamchk
     </strong>
    </span>
   </a>
   automatically detects whether the
   <code class="filename">
    .MYD
   </code>
   file
        is corrupt and aborts the repair if it is. You can also specify
        the
   <a class="link" href="myisamchk-repair-options.html#option_myisamchk_quick">
    <code class="option">
     --quick
    </code>
   </a>
   option twice to
   <a class="link" href="myisamchk.html" title="6.6.4 myisamchk — MyISAM Table-Maintenance Utility">
    <span class="command">
     <strong>
      myisamchk
     </strong>
    </span>
   </a>
   . In this case,
   <a class="link" href="myisamchk.html" title="6.6.4 myisamchk — MyISAM Table-Maintenance Utility">
    <span class="command">
     <strong>
      myisamchk
     </strong>
    </span>
   </a>
   does not abort on some errors (such
        as duplicate-key errors) but instead tries to resolve them by
        modifying the
   <code class="filename">
    .MYD
   </code>
   file. Normally the use
        of two
   <a class="link" href="myisamchk-repair-options.html#option_myisamchk_quick">
    <code class="option">
     --quick
    </code>
   </a>
   options is
        useful only if you have too little free disk space to perform a
        normal repair. In this case, you should at least make a backup
        of the table before running
   <a class="link" href="myisamchk.html" title="6.6.4 myisamchk — MyISAM Table-Maintenance Utility">
    <span class="command">
     <strong>
      myisamchk
     </strong>
    </span>
   </a>
   .
  </p>
 </div>
 <br/>
</div>
