Two methods to fix mysql tables

Source: Internet
Author: User
Tags php mysql

Mysql table repair is frequently used. We will teach you two ways to fix mysql tables. If you have encountered this problem before, take a look.

Solution 1:

 
 
  1. shell> mysqlcheck -r -c -o -uuser -ppasswd db_name  

The second method to fix the mysql table is to use the following script, which is useful on the Internet. Put the script in your web root directory and then run the script from the web page.

 
 
  1. Cat repair. php
  2. Need to modify
  3. $ Dbuser = "root"; ---> modify
  4. $ Dbpw = ""; ---> modify
  5. $ Dbname = "db_name"; --> modify
  6. # $ Table = "search_profile ";
  7. Mysql_connect ($ dbhost, $ dbuser, $ dbpw );
  8. Mysql_select_db ($ dbname );
  9. If (! Get_resource_var ("register_globals ")){
  10. Foreach ($ HTTP_GET_VARS as $ key => $ val ){
  11. $ Key = $ val;
  12. }
  13. }
  14. Function checktable ($ table, $ loops = 0 ){
  15. Global $ db, $ nohtml;
  16. $ Result = mysql_query ("check table $ table ");
  17. If (! $ Nohtml ){
  18. Echo "Checking table $ table ";
  19. Echo "TableOperationTypeText ";
  20. } Else {
  21. Echo "\ n >>>>>>>>>>>>>> Checking Table $ table \ n ";
  22. Echo "---------------------------------
  23. \ N ";
  24. }
  25. $ Error = 0;
  26. While ($ r = mysql_fetch_row ($ result )){
  27. If ($ r [2] = 'error '){
  28. If ($ r [3] = "The handler for the table doesn't support check/repair "){
  29. $ R [2] = 'status ';
  30. $ R [3] = 'this table does not support check/repair/optimize ';
  31. Unset ($ bgcolor );
  32. $ Nooptimize = 1;
  33. } Else {
  34. $ Error = 1;
  35. $ Bgcolor = 'red ';
  36. Unset ($ nooptimize );
  37. }
  38. } Else {
  39. Unset ($ bgcolor );
  40. Unset ($ nooptimize );
  41. }
  42. If (! $ Nohtml ){
  43. Echo "$ r [0] $ r [1] $ r [2] $ r [3]";
  44. } Else {
  45. Echo "$ r [0] | $ r [1] | $ r [2] | $ r [3]
  46. \ N ";
  47. }
  48. }
  49. If ($ error ){
  50. If (! $ Nohtml ){
  51. Echo "Repairing table $ table ";
  52. } Else {
  53. Echo ">>>>>>>>>>>>> Repairing Table $ table
  54. \ N ";
  55. }
  56. $ Result2 = mysql_query ("repair table $ table ");
  57. If ($ result2 [3]! = 'OK ')
  58. $ Bgcolor = 'red ';
  59. Else
  60. Unset ($ bgcolor );
  61. If (! $ Nohtml ){
  62. Echo "$ result2 [0] $ result2 [1] $ result2 [2] $ result2 [3]";
  63. } Else {
  64. Echo "$ result2 [0] | $ result [1] | $ result2 [2] | $ result2 [3]
  65. \ N ";
  66. }
  67. }
  68. If ($ result2 [3] = 'OK' |! $ Error )&&! $ Nooptimize ){
  69. If (! $ Nohtml ){
  70. Echo "Optimizing table $ table ";
  71. } Else {
  72. Echo ">>>>>>>>>>>>> Optimizing Table $ table
  73. \ N ";
  74. }
  75. $ Result3 = mysql_query ("optimize table $ table ");
  76. $ Error = 0;
  77. While ($ r3 = mysql_fetch_row ($ result3 )){
  78. If ($ r3 [2] = 'error '){
  79. $ Error = 1;
  80. $ Bgcolor = 'red ';
  81. } Else {
  82. Unset ($ bgcolor );
  83. }
  84. If (! $ Nohtml ){
  85. Echo "$ r3 [0] $ r3 [1] $ r3 [2] $ r3 [3]";
  86. } Else {
  87. Echo "$ r3 [0] | $ r3 [1] | $ r3 [2] | $ r3 [3]
  88. \ N ";
  89. }
  90. }
  91. }
  92. If ($ error & $ loops ){
  93. Checktable ($ table, ($ loops-1 ));
  94. }
  95. }
  96. If ($ check ){
  97. $ Tables = mysql_query ("show tables ");
  98. If (! $ Nohtml ){
  99. Echo "";
  100. }
  101. If ($ iterations ){
  102. $ Iterations --;
  103. }
  104. While ($ table = mysql_fetch_row ($ tables )){
  105. If (substr ($ table [0],-8 )! = 'Session '){
  106. $ Answer = checktable ($ table [0], $ iterations );
  107. If (! $ Nohtml ){
  108. Echo "& nbsp ;";
  109. }
  110. Flush ();
  111. }
  112. }
  113. If (! $ Nohtml ){
  114. Echo "";
  115. }
  116. } Else {
  117. Echo "Database Table Repair Utility for Discuz! (Require MySQL 3.23)
  118. ".
  119. "This script will attempt to repair extends upted databases. While it can repair most common errors,
  120. ".
  121. "It can not repair everything. As a side benefit it will also optimize your tables.
  122. ".
  123. "This script was copyrighted by Jelsoft and modified by Crossday Studio to make it apply for Discuz !.
  124. ".
  125. "Usage:
  126. ".
  127. "Iterations = x (x is the number of times you wish for the script to attempt to repair a broken table)
  128. ".
  129. "Nohtml = 1 (Output text only no html. Useful for cronjobs and you wish to receive text only)
  130. ".
  131. "Check = 1 (without this you will get this screen and no checking)
  132. ".
  133. "Example Usage:
  134. ".
  135. "Repair. php? Html = 1 & check = 1 (most common usage, will check a forum and return html results)
  136. "." Repair. php? Check = 1 & iterations = 5 (try to repair damaged tables 5 times )".
  137. "";
  138. }
  139. ?>

MySQL table management commands

Create a temporary table in php mysql

Quick table creation in mysql

Mysql table index information

In-depth study on MySQL's data deletion from multiple tables

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.