<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ru Servers Reference Blog &#187; Repair</title>
	<atom:link href="http://blog.server.ruservers.com/category/repair/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.server.ruservers.com</link>
	<description>Server Maintenance Guide</description>
	<lastBuildDate>Tue, 02 Aug 2011 20:57:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>MYSQL &#8211; How to Repair Tables</title>
		<link>http://blog.server.ruservers.com/2009/01/mysql-how-to-repair-tables/</link>
		<comments>http://blog.server.ruservers.com/2009/01/mysql-how-to-repair-tables/#comments</comments>
		<pubDate>Tue, 20 Jan 2009 19:34:27 +0000</pubDate>
		<dc:creator>Ru Servers</dc:creator>
				<category><![CDATA[How To]]></category>
		<category><![CDATA[MYSQL]]></category>
		<category><![CDATA[Repair]]></category>

		<guid isPermaLink="false">http://blog.server.ruservers.com/2009/01/mysql-how-to-repair-tables/</guid>
		<description><![CDATA[&#160; Source: http://dev.mysql.com/doc/refman/5.1/en/repair.html &#160; The discussion in this section describes how to use myisamchk on MyISAM tables (extensions .MYI and .MYD). You can also (and should, if possible) use the CHECK TABLE and REPAIR TABLE statements to check and repair MyISAM tables. See Section 12.5.2.3, “CHECK TABLE Syntax”, and Section 12.5.2.6, “REPAIR TABLE Syntax”. Symptoms [...]]]></description>
			<content:encoded><![CDATA[<p>&#160;</p>
<p>Source: <a title="http://dev.mysql.com/doc/refman/5.1/en/repair.html" href="http://dev.mysql.com/doc/refman/5.1/en/repair.html">http://dev.mysql.com/doc/refman/5.1/en/repair.html</a></p>
<p>&#160;</p>
<p>The discussion in this section describes how to use <a href="http://dev.mysql.com/doc/refman/5.1/en/myisamchk.html"><strong>myisamchk</strong></a> on <code>MyISAM</code> tables (extensions <code>.MYI</code> and <code>.MYD</code>). </p>
<p>You can also (and should, if possible) use the <a href="http://dev.mysql.com/doc/refman/5.1/en/check-table.html"><code>CHECK TABLE</code></a> and <a href="http://dev.mysql.com/doc/refman/5.1/en/repair-table.html"><code>REPAIR TABLE</code></a> statements to check and repair <code>MyISAM</code> tables. See <a href="http://dev.mysql.com/doc/refman/5.1/en/check-table.html">Section 12.5.2.3, “<code>CHECK TABLE</code> Syntax”</a>, and <a href="http://dev.mysql.com/doc/refman/5.1/en/repair-table.html">Section 12.5.2.6, “<code>REPAIR TABLE</code> Syntax”</a>. </p>
<p>Symptoms of corrupted tables include queries that abort unexpectedly and observable errors such as these: </p>
<ul>
<li>
<p><code><em><code>tbl_name</code></em>.frm</code> is locked against change </p>
</li>
<li>
<p>Can&#8217;t find file <code><em><code>tbl_name</code></em>.MYI</code> (Errcode: <em><code>nnn</code></em>) </p>
</li>
<li>
<p>Unexpected end of file </p>
</li>
<li>
<p>Record file is crashed </p>
</li>
<li>
<p>Got error <em><code>nnn</code></em> from table handler </p>
</li>
</ul>
<p>To get more information about the error, run <a href="http://dev.mysql.com/doc/refman/5.1/en/perror.html"><strong>perror</strong></a> <em><code>nnn</code></em>, where <em><code>nnn</code></em> is the error number. The following example shows how to use <a href="http://dev.mysql.com/doc/refman/5.1/en/perror.html"><strong>perror</strong></a> to find the meanings for the most common error numbers that indicate a problem with a table: </p>
<pre>shell&gt; <strong><code>perror 126 127 132 134 135 136 141 144 145</code></strong>
MySQL error code 126 = Index file is crashed
MySQL error code 127 = Record-file is crashed
MySQL error code 132 = Old database file
MySQL error code 134 = Record was already deleted (or record file crashed)
MySQL error code 135 = No more room in record file
MySQL error code 136 = No more room in index file
MySQL error code 141 = Duplicate unique key or constraint on write or update
MySQL error code 144 = Table is crashed and last repair failed
MySQL error code 145 = Table was marked as crashed and should be repaired</pre>
<p>Note that error 135 (no more room in record file) and error 136 (no more room in index file) are not errors that can be fixed by a simple repair. In this case, you must use <a href="http://dev.mysql.com/doc/refman/5.1/en/alter-table.html"><code>ALTER TABLE</code></a> to increase the <code>MAX_ROWS</code> and <code>AVG_ROW_LENGTH</code> table option values: </p>
<pre>ALTER TABLE <em><code>tbl_name</code></em> MAX_ROWS=<em><code>xxx</code></em> AVG_ROW_LENGTH=<em><code>yyy</code></em>;</pre>
<p>If you do not know the current table option values, use <a href="http://dev.mysql.com/doc/refman/5.1/en/show-create-table.html"><code>SHOW CREATE TABLE</code></a>. </p>
<p>For the other errors, you must repair your tables. <a href="http://dev.mysql.com/doc/refman/5.1/en/myisamchk.html"><strong>myisamchk</strong></a> can usually detect and fix most problems that occur. </p>
<p>The repair process involves up to four stages, described here. Before you begin, you should change location to the database directory and check the permissions of the table files. On Unix, make sure that they are readable by the user that <a href="http://dev.mysql.com/doc/refman/5.1/en/mysqld.html"><strong>mysqld</strong></a> runs as (and to you, because you need to access the files you are checking). If it turns out you need to modify files, they must also be writable by you. </p>
<p>This section is for the cases where a table check fails (such as those described in <a href="http://dev.mysql.com/doc/refman/5.1/en/check.html">Section 6.4.2, “How to Check <code>MyISAM</code> Tables for Errors”</a>), or you want to use the extended features that <a href="http://dev.mysql.com/doc/refman/5.1/en/myisamchk.html"><strong>myisamchk</strong></a> provides. </p>
<p>The options that you can use for table maintenance with <a href="http://dev.mysql.com/doc/refman/5.1/en/myisamchk.html"><strong>myisamchk</strong></a> are described in <a href="http://dev.mysql.com/doc/refman/5.1/en/myisamchk.html">Section 4.6.3, “<strong>myisamchk</strong> — MyISAM Table-Maintenance Utility”</a>. </p>
<p>If you are going to repair a table from the command line, you must first stop the <a href="http://dev.mysql.com/doc/refman/5.1/en/mysqld.html"><strong>mysqld</strong></a> server. Note that when you do <a href="http://dev.mysql.com/doc/refman/5.1/en/mysqladmin.html"><strong>mysqladmin shutdown</strong></a> on a remote server, the <a href="http://dev.mysql.com/doc/refman/5.1/en/mysqld.html"><strong>mysqld</strong></a> server is still alive for a while after <a href="http://dev.mysql.com/doc/refman/5.1/en/mysqladmin.html"><strong>mysqladmin</strong></a> returns, until all statement-processing has stopped and all index changes have been flushed to disk. </p>
<p><strong>Stage 1: Checking your tables</strong></p>
<p>Run <a href="http://dev.mysql.com/doc/refman/5.1/en/myisamchk.html"><strong>myisamchk *.MYI</strong></a> or <a href="http://dev.mysql.com/doc/refman/5.1/en/myisamchk.html"><strong>myisamchk -e *.MYI</strong></a> if you have more time. Use the <code>-s</code> (silent) option to suppress unnecessary information. </p>
<p>If the <a href="http://dev.mysql.com/doc/refman/5.1/en/mysqld.html"><strong>mysqld</strong></a> server is stopped, you should use the <code>--update-state</code> option to tell <a href="http://dev.mysql.com/doc/refman/5.1/en/myisamchk.html"><strong>myisamchk</strong></a> to mark the table as “checked.” </p>
<p>You have to repair only those tables for which <a href="http://dev.mysql.com/doc/refman/5.1/en/myisamchk.html"><strong>myisamchk</strong></a> announces an error. For such tables, proceed to Stage 2. </p>
<p>If you get unexpected errors when checking (such as <code>out of memory</code> errors), or if <a href="http://dev.mysql.com/doc/refman/5.1/en/myisamchk.html"><strong>myisamchk</strong></a> crashes, go to Stage 3. </p>
<p><strong>Stage 2: Easy safe repair</strong></p>
<p>First, try <a href="http://dev.mysql.com/doc/refman/5.1/en/myisamchk.html"><strong>myisamchk -r -q <em><code>tbl_name</code></em></strong></a> (<code>-r -q</code> means “quick recovery mode”). This attempts to repair the index file without touching the data file. If the data file contains everything that it should and the delete links point at the correct locations within the data file, this should work, and the table is fixed. Start repairing the next table. Otherwise, use the following procedure: </p>
<ol>
<li>
<p>Make a backup of the data file before continuing. </p>
</li>
<li>
<p>Use <a href="http://dev.mysql.com/doc/refman/5.1/en/myisamchk.html"><strong>myisamchk -r <em><code>tbl_name</code></em></strong></a> (<code>-r</code> means “recovery mode”). This removes incorrect rows and deleted rows from the data file and reconstructs the index file. </p>
</li>
<li>
<p>If the preceding step fails, use <a href="http://dev.mysql.com/doc/refman/5.1/en/myisamchk.html"><strong>myisamchk &#8211;safe-recover <em><code>tbl_name</code></em></strong></a>. Safe recovery mode uses an old recovery method that handles a few cases that regular recovery mode does not (but is slower). </p>
</li>
</ol>
<h5>Note</h5>
<p>If you want a repair operation to go much faster, you should set the values of the <a href="http://dev.mysql.com/doc/refman/5.1/en/server-system-variables.html#sysvar_sort_buffer_size"><code>sort_buffer_size</code></a> and <a href="http://dev.mysql.com/doc/refman/5.1/en/server-system-variables.html#sysvar_key_buffer_size"><code>key_buffer_size</code></a> variables each to about 25% of your available memory when running <a href="http://dev.mysql.com/doc/refman/5.1/en/myisamchk.html"><strong>myisamchk</strong></a>. </p>
<p>If you get unexpected errors when repairing (such as <code>out of memory</code> errors), or if <a href="http://dev.mysql.com/doc/refman/5.1/en/myisamchk.html"><strong>myisamchk</strong></a> crashes, go to Stage 3. </p>
<p><strong>Stage 3: Difficult repair</strong></p>
<p>You should reach this stage only if the first 16KB block in the index file is destroyed or contains incorrect information, or if the index file is missing. In this case, it is necessary to create a new index file. Do so as follows: </p>
<ol>
<li>
<p>Move the data file to a safe place. </p>
</li>
<li>
<p>Use the table description file to create new (empty) data and index files: </p>
<pre>shell&gt; <strong><code>mysql <em><code>db_name</code></em></code></strong>
mysql&gt; <strong><code>SET autocommit=1;</code></strong>
mysql&gt; <strong><code>TRUNCATE TABLE <em><code>tbl_name</code></em>;</code></strong>
mysql&gt; <strong><code>quit</code></strong></pre>
</li>
<li>
<p>Copy the old data file back onto the newly created data file. (Do not just move the old file back onto the new file. You want to retain a copy in case something goes wrong.) </p>
</li>
</ol>
<h5>Important</h5>
<p>If you are using replication, you should stop it prior to performing the above procedure, since it involves filesystem operations, and these are not logged by MySQL. </p>
<p>Go back to Stage 2. <a href="http://dev.mysql.com/doc/refman/5.1/en/myisamchk.html"><strong>myisamchk -r -q</strong></a> should work. (This should not be an endless loop.) </p>
<p>You can also use the <code>REPAIR TABLE <em><code>tbl_name</code></em> USE_FRM</code> SQL statement, which performs the whole procedure automatically. There is also no possibility of unwanted interaction between a utility and the server, because the server does all the work when you use <a href="http://dev.mysql.com/doc/refman/5.1/en/repair-table.html"><code>REPAIR TABLE</code></a>. See <a href="http://dev.mysql.com/doc/refman/5.1/en/repair-table.html">Section 12.5.2.6, “<code>REPAIR TABLE</code> Syntax”</a>. </p>
<p><strong>Stage 4: Very difficult repair</strong></p>
<p>You should reach this stage only if the <code>.frm</code> description file has also crashed. That should never happen, because the description file is not changed after the table is created: </p>
<ol>
<li>
<p>Restore the description file from a backup and go back to Stage 3. You can also restore the index file and go back to Stage 2. In the latter case, you should start with <a href="http://dev.mysql.com/doc/refman/5.1/en/myisamchk.html"><strong>myisamchk -r</strong></a>. </p>
</li>
<li>
<p>If you do not have a backup but know exactly how the table was created, create a copy of the table in another database. Remove the new data file, and then move the <code>.frm</code> description and <code>.MYI</code> index files from the other database to your crashed database. This gives you new description and index files, but leaves the <code>.MYD</code> data file alone. Go back to Stage 2 and attempt to reconstruct the index file. </p>
</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://blog.server.ruservers.com/2009/01/mysql-how-to-repair-tables/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

