PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/admin/cli/mysql_compressed_rows.php

http://github.com/moodle/moodle
PHP | 202 lines | 146 code | 33 blank | 23 comment | 28 complexity | 491db49fed8ae1475e03994555c6e358 MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * MySQL table row compression tool tool.
  18. *
  19. * @package core
  20. * @copyright 2014 Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. * @author Petr Skoda <petr.skoda@totaralms.com>
  23. */
  24. define('CLI_SCRIPT', true);
  25. require(__DIR__.'/../../config.php');
  26. require_once($CFG->libdir . '/clilib.php');
  27. if ($DB->get_dbfamily() !== 'mysql') {
  28. cli_error('This script is used for MySQL databases only.');
  29. }
  30. $engine = strtolower($DB->get_dbengine());
  31. if ($engine !== 'innodb' and $engine !== 'xtradb') {
  32. cli_error('This script is for MySQL servers using InnoDB or XtraDB engines only.');
  33. }
  34. list($options, $unrecognized) = cli_get_params(
  35. array('help' => false, 'info' => false, 'list' => false, 'fix' => false, 'showsql' => false),
  36. array('h' => 'help', 'i' => 'info', 'l' => 'list', 'f' => 'fix', 's' => 'showsql')
  37. );
  38. if ($unrecognized) {
  39. $unrecognized = implode("\n ", $unrecognized);
  40. cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
  41. }
  42. $help =
  43. "Script for detection of row size problems in MySQL InnoDB tables.
  44. By default InnoDB storage table is using legacy Antelope file format
  45. which has major restriction on database row size.
  46. Use this script to detect and fix database tables with potential data
  47. overflow problems.
  48. Options:
  49. -i, --info Show database information
  50. -l, --list List problematic tables
  51. -f, --fix Attempt to fix all tables (requires SUPER privilege)
  52. -s, --showsql Print SQL statements for fixing of tables
  53. -h, --help Print out this help
  54. Example:
  55. \$ sudo -u www-data /usr/bin/php admin/cli/mysql_compressed_rows.php -l
  56. ";
  57. /** @var mysql_sql_generator $generator */
  58. $generator = $DB->get_manager()->generator;
  59. $info = $DB->get_server_info();
  60. $filepertable = $DB->get_record_sql("SHOW VARIABLES LIKE 'innodb_file_per_table'");
  61. $filepertable = $filepertable ? $filepertable->value : '';
  62. $fileformat = $DB->get_record_sql("SHOW VARIABLES LIKE 'innodb_file_format'");
  63. $fileformat = $fileformat ? $fileformat->value : '';
  64. $prefix = $DB->get_prefix();
  65. $database = $CFG->dbname;
  66. if (!empty($options['info'])) {
  67. echo "Database version: " . $info['description'] . "\n";
  68. echo "Database name: $database\n";
  69. echo "Database engine: " . $DB->get_dbengine() . "\n";
  70. echo "innodb_file_per_table: $filepertable\n";
  71. echo "innodb_file_format: $fileformat\n";
  72. exit(0);
  73. } else if (!empty($options['list'])) {
  74. $problem = false;
  75. foreach ($DB->get_tables(false) as $table) {
  76. $columns = $DB->get_columns($table, false);
  77. $size = $generator->guess_antelope_row_size($columns);
  78. $format = $DB->get_row_format($table);
  79. if ($size <= $generator::ANTELOPE_MAX_ROW_SIZE) {
  80. continue;
  81. }
  82. echo str_pad($prefix . $table, 32, ' ', STR_PAD_RIGHT);
  83. echo str_pad($format, 11, ' ', STR_PAD_RIGHT);
  84. if ($format === 'Compact' or $format === 'Redundant') {
  85. $problem = true;
  86. echo " (needs fixing)\n";
  87. } else if ($format !== 'Compressed' and $format !== 'Dynamic') {
  88. echo " (unknown)\n";
  89. } else {
  90. echo "\n";
  91. }
  92. }
  93. if ($problem) {
  94. exit(1);
  95. }
  96. exit(0);
  97. } else if (!empty($options['fix'])) {
  98. $fixtables = array();
  99. foreach ($DB->get_tables(false) as $table) {
  100. $columns = $DB->get_columns($table, false);
  101. $size = $generator->guess_antelope_row_size($columns);
  102. $format = $DB->get_row_format($table);
  103. if ($size <= $generator::ANTELOPE_MAX_ROW_SIZE) {
  104. continue;
  105. }
  106. if ($format === 'Compact' or $format === 'Redundant') {
  107. $fixtables[$table] = $table;
  108. }
  109. }
  110. if (!$fixtables) {
  111. echo "No changes necessary\n";
  112. exit(0);
  113. }
  114. if ($filepertable !== 'ON') {
  115. try {
  116. $DB->execute("SET GLOBAL innodb_file_per_table=1");
  117. } catch (dml_exception $e) {
  118. echo "Cannot enable GLOBAL innodb_file_per_table setting, use --showsql option and execute the statements manually.";
  119. throw $e;
  120. }
  121. }
  122. if ($fileformat !== 'Barracuda') {
  123. try {
  124. $DB->execute("SET GLOBAL innodb_file_format=Barracuda");
  125. } catch (dml_exception $e) {
  126. echo "Cannot change GLOBAL innodb_file_format setting, use --showsql option and execute the statements manually.";
  127. throw $e;
  128. }
  129. }
  130. if (!$DB->is_compressed_row_format_supported(false)) {
  131. echo "MySQL server is not compatible with compressed row format.";
  132. exit(1);
  133. }
  134. foreach ($fixtables as $table) {
  135. $DB->change_database_structure("ALTER TABLE {$prefix}$table ROW_FORMAT=Compressed");
  136. echo str_pad($prefix . $table, 32, ' ', STR_PAD_RIGHT) . " ... Compressed\n";
  137. }
  138. exit(0);
  139. } else if (!empty($options['showsql'])) {
  140. $fixtables = array();
  141. foreach ($DB->get_tables(false) as $table) {
  142. $columns = $DB->get_columns($table, false);
  143. $size = $generator->guess_antelope_row_size($columns);
  144. $format = $DB->get_row_format($table);
  145. if ($size <= $generator::ANTELOPE_MAX_ROW_SIZE) {
  146. continue;
  147. }
  148. if ($format === 'Compact' or $format === 'Redundant') {
  149. $fixtables[$table] = $table;
  150. }
  151. }
  152. if (!$fixtables) {
  153. echo "No changes necessary\n";
  154. exit(0);
  155. }
  156. echo "Copy the following SQL statements and execute them using account with SUPER privilege:\n\n";
  157. echo "USE $database;\n";
  158. echo "SET SESSION sql_mode=STRICT_ALL_TABLES;\n";
  159. echo "SET GLOBAL innodb_file_per_table=1;\n";
  160. echo "SET GLOBAL innodb_file_format=Barracuda;\n";
  161. foreach ($fixtables as $table) {
  162. echo "ALTER TABLE {$prefix}$table ROW_FORMAT=Compressed;\n";
  163. }
  164. echo "\n";
  165. exit(0);
  166. } else {
  167. echo $help;
  168. die;
  169. }