PageRenderTime 744ms CodeModel.GetById 34ms RepoModel.GetById 2ms app.codeStats 0ms

/phpmyadmin/server_replication.php

https://bitbucket.org/adarshj/convenient_website
PHP | 370 lines | 292 code | 47 blank | 31 comment | 65 complexity | c1d6a01532a23f2ee207ca45c78f9e98 MD5 | raw file
Possible License(s): Apache-2.0, MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-2-Clause, GPL-2.0, LGPL-3.0
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. *
  5. * @package PhpMyAdmin
  6. */
  7. /**
  8. *
  9. */
  10. require_once './libraries/common.inc.php';
  11. /**
  12. * Does the common work
  13. */
  14. $GLOBALS['js_include'][] = 'server_privileges.js';
  15. $GLOBALS['js_include'][] = 'replication.js';
  16. require './libraries/server_common.inc.php';
  17. require './libraries/replication.inc.php';
  18. require './libraries/replication_gui.lib.php';
  19. require_once './libraries/server_synchronize.lib.php';
  20. /**
  21. * Checks if the user is allowed to do what he tries to...
  22. */
  23. if (! $is_superuser) {
  24. include './libraries/server_links.inc.php';
  25. echo '<h2>' . "\n"
  26. . PMA_getIcon('s_replication.png')
  27. . __('Replication') . "\n"
  28. . '</h2>' . "\n";
  29. PMA_Message::error(__('No Privileges'))->display();
  30. include './libraries/footer.inc.php';
  31. }
  32. /**
  33. * Handling control requests
  34. */
  35. if (isset($GLOBALS['sr_take_action'])) {
  36. $refresh = false;
  37. if (isset($GLOBALS['slave_changemaster'])) {
  38. $_SESSION['replication']['m_username'] = $sr['username'] = PMA_sqlAddSlashes($GLOBALS['username']);
  39. $_SESSION['replication']['m_password'] = $sr['pma_pw'] = PMA_sqlAddSlashes($GLOBALS['pma_pw']);
  40. $_SESSION['replication']['m_hostname'] = $sr['hostname'] = PMA_sqlAddSlashes($GLOBALS['hostname']);
  41. $_SESSION['replication']['m_port'] = $sr['port'] = PMA_sqlAddSlashes($GLOBALS['port']);
  42. $_SESSION['replication']['m_correct'] = '';
  43. $_SESSION['replication']['sr_action_status'] = 'error';
  44. $_SESSION['replication']['sr_action_info'] = __('Unknown error');
  45. // Attempt to connect to the new master server
  46. $link_to_master = PMA_replication_connect_to_master($sr['username'], $sr['pma_pw'], $sr['hostname'], $sr['port']);
  47. if (! $link_to_master) {
  48. $_SESSION['replication']['sr_action_status'] = 'error';
  49. $_SESSION['replication']['sr_action_info'] = sprintf(__('Unable to connect to master %s.'), htmlspecialchars($sr['hostname']));
  50. } else {
  51. // Read the current master position
  52. $position = PMA_replication_slave_bin_log_master($link_to_master);
  53. if (empty($position)) {
  54. $_SESSION['replication']['sr_action_status'] = 'error';
  55. $_SESSION['replication']['sr_action_info'] = __('Unable to read master log position. Possible privilege problem on master.');
  56. } else {
  57. $_SESSION['replication']['m_correct'] = true;
  58. if (! PMA_replication_slave_change_master($sr['username'], $sr['pma_pw'], $sr['hostname'], $sr['port'], $position, true, false)) {
  59. $_SESSION['replication']['sr_action_status'] = 'error';
  60. $_SESSION['replication']['sr_action_info'] = __('Unable to change master');
  61. } else {
  62. $_SESSION['replication']['sr_action_status'] = 'success';
  63. $_SESSION['replication']['sr_action_info'] = sprintf(__('Master server changed successfully to %s'), htmlspecialchars($sr['hostname']));
  64. }
  65. }
  66. }
  67. } elseif (isset($GLOBALS['sr_slave_server_control'])) {
  68. if ($GLOBALS['sr_slave_action'] == 'reset') {
  69. PMA_replication_slave_control("STOP");
  70. PMA_DBI_try_query("RESET SLAVE;");
  71. PMA_replication_slave_control("START");
  72. } else {
  73. PMA_replication_slave_control($GLOBALS['sr_slave_action'], $GLOBALS['sr_slave_control_parm']);
  74. }
  75. $refresh = true;
  76. } elseif (isset($GLOBALS['sr_slave_skip_error'])) {
  77. $count = 1;
  78. if (isset($GLOBALS['sr_skip_errors_count'])) {
  79. $count = $GLOBALS['sr_skip_errors_count'] * 1;
  80. }
  81. PMA_replication_slave_control("STOP");
  82. PMA_DBI_try_query("SET GLOBAL SQL_SLAVE_SKIP_COUNTER = ".$count.";");
  83. PMA_replication_slave_control("START");
  84. } elseif (isset($GLOBALS['sl_sync'])) {
  85. // TODO username, host and port could be read from 'show slave status',
  86. // when asked for a password this might work in more situations then just after changing master (where the master password is stored in session)
  87. $src_link = PMA_replication_connect_to_master($_SESSION['replication']['m_username'], $_SESSION['replication']['m_password'], $_SESSION['replication']['m_hostname'], $_SESSION['replication']['m_port']);
  88. $trg_link = null; // using null to indicate the current PMA server
  89. $data = PMA_DBI_fetch_result('SHOW MASTER STATUS', null, null, $src_link); // let's find out, which databases are replicated
  90. $do_db = array();
  91. $ignore_db = array();
  92. $dblist = array();
  93. if (! empty($data[0]['Binlog_Do_DB'])) {
  94. $do_db = explode(',', $data[0]['Binlog_Do_DB']);
  95. }
  96. if (! empty($data[0]['Binlog_Ignore_DB'])) {
  97. $ignore_db = explode(',', $data[0]['Binlog_Ignore_DB']);
  98. }
  99. $tmp_alldbs = PMA_DBI_query('SHOW DATABASES;', $src_link);
  100. while ($tmp_row = PMA_DBI_fetch_row($tmp_alldbs)) {
  101. if (PMA_is_system_schema($tmp_row[0])) {
  102. continue;
  103. }
  104. if (count($do_db) == 0) {
  105. if (array_search($tmp_row[0], $ignore_db) !== false) {
  106. continue;
  107. }
  108. $dblist[] = $tmp_row[0];
  109. PMA_DBI_query('CREATE DATABASE IF NOT EXISTS '.PMA_backquote($tmp_row[0]), $trg_link);
  110. } else {
  111. if (array_search($tmp_row[0], $do_db) !== false) {
  112. $dblist[] = $tmp_row[0];
  113. PMA_DBI_query('CREATE DATABASE IF NOT EXISTS '.PMA_backquote($tmp_row[0]), $trg_link);
  114. }
  115. }
  116. } // end while
  117. unset($do_db, $ignore_db, $data);
  118. if (isset($GLOBALS['repl_data'])) {
  119. $include_data = true;
  120. } else {
  121. $include_data = false;
  122. }
  123. foreach ($dblist as $db) {
  124. PMA_replication_synchronize_db($db, $src_link, $trg_link, $include_data);
  125. }
  126. // TODO some form of user feedback error/success would be nice
  127. // What happens if $dblist is empty?
  128. // or sync failed?
  129. }
  130. if ($refresh) {
  131. Header("Location: ". PMA_generate_common_url($GLOBALS['url_params']));
  132. }
  133. unset($refresh);
  134. }
  135. /**
  136. * Displays the links
  137. */
  138. require './libraries/server_links.inc.php';
  139. echo '<div id="replication">';
  140. echo ' <h2>';
  141. echo ' ' . PMA_getImage('s_replication.png');
  142. echo __('Replication');
  143. echo ' </h2>';
  144. // Display error messages
  145. if (isset($_SESSION['replication']['sr_action_status']) && isset($_SESSION['replication']['sr_action_info'])) {
  146. if ($_SESSION['replication']['sr_action_status'] == 'error') {
  147. PMA_Message::error($_SESSION['replication']['sr_action_info'])->display();
  148. $_SESSION['replication']['sr_action_status'] = 'unknown';
  149. } elseif ($_SESSION['replication']['sr_action_status'] == 'success') {
  150. PMA_Message::success($_SESSION['replication']['sr_action_info'])->display();
  151. $_SESSION['replication']['sr_action_status'] = 'unknown';
  152. }
  153. }
  154. if ($server_master_status) {
  155. if (! isset($GLOBALS['repl_clear_scr'])) {
  156. echo '<fieldset>';
  157. echo '<legend>' . __('Master replication') . '</legend>';
  158. echo __('This server is configured as master in a replication process.');
  159. echo '<ul>';
  160. echo ' <li><a href="#" id="master_status_href">' . __('Show master status') . '</a></li>';
  161. PMA_replication_print_status_table('master', true, false);
  162. echo ' <li><a href="#" id="master_slaves_href">' . __('Show connected slaves') . '</a></li>';
  163. PMA_replication_print_slaves_table(true);
  164. $_url_params = $GLOBALS['url_params'];
  165. $_url_params['mr_adduser'] = true;
  166. $_url_params['repl_clear_scr'] = true;
  167. echo ' <li><a href="' . PMA_generate_common_url($_url_params) . '" id="master_addslaveuser_href">' . __('Add slave replication user') . '</a></li>';
  168. }
  169. // Display 'Add replication slave user' form
  170. if (isset($GLOBALS['mr_adduser'])) {
  171. PMA_replication_gui_master_addslaveuser();
  172. } elseif (! isset($GLOBALS['repl_clear_scr'])) {
  173. echo "</ul>";
  174. echo "</fieldset>";
  175. }
  176. } elseif (! isset($GLOBALS['mr_configure']) && ! isset($GLOBALS['repl_clear_scr'])) {
  177. $_url_params = $GLOBALS['url_params'];
  178. $_url_params['mr_configure'] = true;
  179. echo '<fieldset>';
  180. echo '<legend>' . __('Master replication') . '</legend>';
  181. echo sprintf(__('This server is not configured as master in a replication process. Would you like to <a href="%s">configure</a> it?'), PMA_generate_common_url($_url_params));
  182. echo '</fieldset>';
  183. }
  184. if (isset($GLOBALS['mr_configure'])) {
  185. // Render the 'Master configuration' section
  186. echo '<fieldset>';
  187. echo '<legend>' . __('Master configuration') . '</legend>';
  188. echo __('This server is not configured as master server in a replication process. You can choose from either replicating all databases and ignoring certain (useful if you want to replicate majority of databases) or you can choose to ignore all databases by default and allow only certain databases to be replicated. Please select the mode:') . '<br /><br />';
  189. echo '<select name="db_type" id="db_type">';
  190. echo '<option value="all">' . __('Replicate all databases; Ignore:') . '</option>';
  191. echo '<option value="ign">' . __('Ignore all databases; Replicate:') . '</option>';
  192. echo '</select>';
  193. echo '<br /><br />';
  194. echo __('Please select databases:') . '<br />';
  195. echo PMA_replication_db_multibox();
  196. echo '<br /><br />';
  197. echo __('Now, add the following lines at the end of [mysqld] section in your my.cnf and please restart the MySQL server afterwards.') . '<br />';
  198. echo '<pre id="rep"></pre>';
  199. echo __('Once you restarted MySQL server, please click on Go button. Afterwards, you should see a message informing you, that this server <b>is</b> configured as master');
  200. echo '</fieldset>';
  201. echo '<fieldset class="tblFooters">';
  202. echo ' <form method="post" action="server_replication.php" >';
  203. echo PMA_generate_common_hidden_inputs('', '');
  204. echo ' <input type="submit" value="' . __('Go') . '" id="goButton" />';
  205. echo ' </form>';
  206. echo '</fieldset>';
  207. include './libraries/footer.inc.php';
  208. exit;
  209. }
  210. echo '</div>';
  211. if (! isset($GLOBALS['repl_clear_scr'])) {
  212. // Render the 'Slave configuration' section
  213. echo '<fieldset>';
  214. echo '<legend>' . __('Slave replication') . '</legend>';
  215. if ($server_slave_status) {
  216. echo '<div id="slave_configuration_gui">';
  217. $_url_params = $GLOBALS['url_params'];
  218. $_url_params['sr_take_action'] = true;
  219. $_url_params['sr_slave_server_control'] = true;
  220. if ($server_slave_replication[0]['Slave_IO_Running'] == 'No') {
  221. $_url_params['sr_slave_action'] = 'start';
  222. } else {
  223. $_url_params['sr_slave_action'] = 'stop';
  224. }
  225. $_url_params['sr_slave_control_parm'] = 'IO_THREAD';
  226. $slave_control_io_link = PMA_generate_common_url($_url_params);
  227. if ($server_slave_replication[0]['Slave_SQL_Running'] == 'No') {
  228. $_url_params['sr_slave_action'] = 'start';
  229. } else {
  230. $_url_params['sr_slave_action'] = 'stop';
  231. }
  232. $_url_params['sr_slave_control_parm'] = 'SQL_THREAD';
  233. $slave_control_sql_link = PMA_generate_common_url($_url_params);
  234. if ($server_slave_replication[0]['Slave_IO_Running'] == 'No'
  235. || $server_slave_replication[0]['Slave_SQL_Running'] == 'No'
  236. ) {
  237. $_url_params['sr_slave_action'] = 'start';
  238. } else {
  239. $_url_params['sr_slave_action'] = 'stop';
  240. }
  241. $_url_params['sr_slave_control_parm'] = null;
  242. $slave_control_full_link = PMA_generate_common_url($_url_params);
  243. $_url_params['sr_slave_action'] = 'reset';
  244. $slave_control_reset_link = PMA_generate_common_url($_url_params);
  245. $_url_params = $GLOBALS['url_params'];
  246. $_url_params['sr_slave_skip_error'] = true;
  247. $slave_skip_error_link = PMA_generate_common_url($_url_params);
  248. if ($server_slave_replication[0]['Slave_SQL_Running'] == 'No') {
  249. PMA_Message::error(__('Slave SQL Thread not running!'))->display();
  250. }
  251. if ($server_slave_replication[0]['Slave_IO_Running'] == 'No') {
  252. PMA_Message::error(__('Slave IO Thread not running!'))->display();
  253. }
  254. $_url_params = $GLOBALS['url_params'];
  255. $_url_params['sl_configure'] = true;
  256. $_url_params['repl_clear_scr'] = true;
  257. $reconfiguremaster_link = PMA_generate_common_url($_url_params);
  258. echo __('Server is configured as slave in a replication process. Would you like to:');
  259. echo '<br />';
  260. echo '<ul>';
  261. echo ' <li><a href="#" id="slave_status_href">' . __('See slave status table') . '</a></li>';
  262. echo PMA_replication_print_status_table('slave', true, false);
  263. if (isset($_SESSION['replication']['m_correct']) && $_SESSION['replication']['m_correct'] == true) {
  264. echo ' <li><a href="#" id="slave_synchronization_href">' . __('Synchronize databases with master') . '</a></li>';
  265. echo ' <div id="slave_synchronization_gui" style="display: none">';
  266. echo ' <form method="post" action="server_replication.php">';
  267. echo PMA_generate_common_hidden_inputs('', '');
  268. echo ' <input type="checkbox" name="repl_struc" value="1" checked="checked" disabled="disabled" /> ' . __('Structure') . '<br />'; // this is just for vizualization, it has no other purpose
  269. echo ' <input type="checkbox" name="repl_data" value="1" checked="checked" /> ' . __('Data') .' <br />';
  270. echo ' <input type="hidden" name="sr_take_action" value="1" />';
  271. echo ' <input type="submit" name="sl_sync" value="' . __('Go') . '" />';
  272. echo ' </form>';
  273. echo ' </div>';
  274. }
  275. echo ' <li><a href="#" id="slave_control_href">' . __('Control slave:') . '</a>';
  276. echo ' <div id="slave_control_gui" style="display: none">';
  277. echo ' <ul>';
  278. echo ' <li><a href="'. $slave_control_full_link . '">' . (($server_slave_replication[0]['Slave_IO_Running'] == 'No' || $server_slave_replication[0]['Slave_SQL_Running'] == 'No') ? __('Full start') : __('Full stop')) . ' </a></li>';
  279. echo ' <li><a href="'. $slave_control_reset_link . '">' . __('Reset slave') . '</a></li>';
  280. if ($server_slave_replication[0]['Slave_SQL_Running'] == 'No') {
  281. echo ' <li><a href="' . $slave_control_sql_link . '">' . __('Start SQL Thread only') . '</a></li>';
  282. } else {
  283. echo ' <li><a href="' . $slave_control_sql_link . '">' . __('Stop SQL Thread only') . '</a></li>';
  284. }
  285. if ($server_slave_replication[0]['Slave_IO_Running'] == 'No') {
  286. echo ' <li><a href="' . $slave_control_io_link . '">' . __('Start IO Thread only') . '</a></li>';
  287. } else {
  288. echo ' <li><a href="' . $slave_control_io_link . '">' . __('Stop IO Thread only') . '</a></li>';
  289. }
  290. echo ' </ul>';
  291. echo ' </div>';
  292. echo ' </li>';
  293. echo ' <li><a href="#" id="slave_errormanagement_href">' . __('Error management:') . '</a>';
  294. echo ' <div id="slave_errormanagement_gui" style="display: none">';
  295. PMA_Message::error(__('Skipping errors might lead into unsynchronized master and slave!'))->display();
  296. echo ' <ul>';
  297. echo ' <li><a href="' . $slave_skip_error_link . '">' . __('Skip current error') . '</a></li>';
  298. echo ' <li>' . __('Skip next');
  299. echo ' <form method="post" action="server_replication.php">';
  300. echo PMA_generate_common_hidden_inputs('', '');
  301. echo ' <input type="text" name="sr_skip_errors_count" value="1" style="width: 30px" />' . __('errors.');
  302. echo ' <input type="submit" name="sr_slave_skip_error" value="' . __('Go') . '" />';
  303. echo ' <input type="hidden" name="sr_take_action" value="1" />';
  304. echo ' </form></li>';
  305. echo ' </ul>';
  306. echo ' </div>';
  307. echo ' </li>';
  308. echo ' <li><a href="' . $reconfiguremaster_link . '">' . __('Change or reconfigure master server') . '</a></li>';
  309. echo '</ul>';
  310. } elseif (! isset($GLOBALS['sl_configure'])) {
  311. $_url_params = $GLOBALS['url_params'];
  312. $_url_params['sl_configure'] = true;
  313. $_url_params['repl_clear_scr'] = true;
  314. echo sprintf(__('This server is not configured as slave in a replication process. Would you like to <a href="%s">configure</a> it?'), PMA_generate_common_url($_url_params));
  315. }
  316. echo '</div>';
  317. echo '</fieldset>';
  318. }
  319. if (isset($GLOBALS['sl_configure'])) {
  320. PMA_replication_gui_changemaster("slave_changemaster");
  321. }
  322. require './libraries/footer.inc.php';
  323. ?>