Translated using Weblate.
[phpmyadmin.git] / server_replication.php
blobdc209d232bbf481d80da32d422119f58e0774f44
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
5 * @package PhpMyAdmin
6 */
8 /**
11 require_once './libraries/common.inc.php';
13 /**
14 * Does the common work
16 $GLOBALS['js_include'][] = 'server_privileges.js';
17 $GLOBALS['js_include'][] = 'replication.js';
19 require './libraries/server_common.inc.php';
20 require './libraries/replication.inc.php';
21 require './libraries/replication_gui.lib.php';
22 require_once './libraries/server_synchronize.lib.php';
24 /**
25 * Checks if the user is allowed to do what he tries to...
27 if (! $is_superuser) {
28 include './libraries/server_links.inc.php';
29 echo '<h2>' . "\n"
30 . PMA_getIcon('s_replication.png')
31 . __('Replication') . "\n"
32 . '</h2>' . "\n";
33 PMA_Message::error(__('No Privileges'))->display();
34 include './libraries/footer.inc.php';
37 /**
38 * Sets globals from $_REQUEST
40 $request_params = array(
41 'hostname',
42 'mr_adduser',
43 'mr_configure',
44 'pma_pw',
45 'port',
46 'repl_clear_scr',
47 'repl_data',
48 'sl_configure',
49 'sl_sync',
50 'slave_changemaster',
51 'sr_skip_errors_count',
52 'sr_slave_action',
53 'sr_slave_control_parm',
54 'sr_slave_server_control',
55 'sr_slave_skip_error',
56 'sr_take_action',
57 'url_params',
58 'username'
61 foreach ($request_params as $one_request_param) {
62 if (isset($_REQUEST[$one_request_param])) {
63 $GLOBALS[$one_request_param] = $_REQUEST[$one_request_param];
67 /**
68 * Handling control requests
70 if (isset($GLOBALS['sr_take_action'])) {
71 $refresh = false;
72 if (isset($GLOBALS['slave_changemaster'])) {
73 $_SESSION['replication']['m_username'] = $sr['username'] = PMA_sqlAddSlashes($GLOBALS['username']);
74 $_SESSION['replication']['m_password'] = $sr['pma_pw'] = PMA_sqlAddSlashes($GLOBALS['pma_pw']);
75 $_SESSION['replication']['m_hostname'] = $sr['hostname'] = PMA_sqlAddSlashes($GLOBALS['hostname']);
76 $_SESSION['replication']['m_port'] = $sr['port'] = PMA_sqlAddSlashes($GLOBALS['port']);
77 $_SESSION['replication']['m_correct'] = '';
78 $_SESSION['replication']['sr_action_status'] = 'error';
79 $_SESSION['replication']['sr_action_info'] = __('Unknown error');
81 // Attempt to connect to the new master server
82 $link_to_master = PMA_replication_connect_to_master($sr['username'], $sr['pma_pw'], $sr['hostname'], $sr['port']);
84 if (! $link_to_master) {
85 $_SESSION['replication']['sr_action_status'] = 'error';
86 $_SESSION['replication']['sr_action_info'] = sprintf(__('Unable to connect to master %s.'), htmlspecialchars($sr['hostname']));
87 } else {
88 // Read the current master position
89 $position = PMA_replication_slave_bin_log_master($link_to_master);
91 if (empty($position)) {
92 $_SESSION['replication']['sr_action_status'] = 'error';
93 $_SESSION['replication']['sr_action_info'] = __('Unable to read master log position. Possible privilege problem on master.');
94 } else {
95 $_SESSION['replication']['m_correct'] = true;
97 if (! PMA_replication_slave_change_master($sr['username'], $sr['pma_pw'], $sr['hostname'], $sr['port'], $position, true, false)) {
98 $_SESSION['replication']['sr_action_status'] = 'error';
99 $_SESSION['replication']['sr_action_info'] = __('Unable to change master');
100 } else {
101 $_SESSION['replication']['sr_action_status'] = 'success';
102 $_SESSION['replication']['sr_action_info'] = sprintf(__('Master server changed successfully to %s'), htmlspecialchars($sr['hostname']));
106 } elseif (isset($GLOBALS['sr_slave_server_control'])) {
107 if ($GLOBALS['sr_slave_action'] == 'reset') {
108 PMA_replication_slave_control("STOP");
109 PMA_DBI_try_query("RESET SLAVE;");
110 PMA_replication_slave_control("START");
111 } else {
112 PMA_replication_slave_control($GLOBALS['sr_slave_action'], $GLOBALS['sr_slave_control_parm']);
114 $refresh = true;
116 } elseif (isset($GLOBALS['sr_slave_skip_error'])) {
117 $count = 1;
118 if (isset($GLOBALS['sr_skip_errors_count'])) {
119 $count = $GLOBALS['sr_skip_errors_count'] * 1;
121 PMA_replication_slave_control("STOP");
122 PMA_DBI_try_query("SET GLOBAL SQL_SLAVE_SKIP_COUNTER = ".$count.";");
123 PMA_replication_slave_control("START");
125 } elseif (isset($GLOBALS['sl_sync'])) {
126 // TODO username, host and port could be read from 'show slave status',
127 // when asked for a password this might work in more situations then just after changing master (where the master password is stored in session)
128 $src_link = PMA_replication_connect_to_master($_SESSION['replication']['m_username'], $_SESSION['replication']['m_password'], $_SESSION['replication']['m_hostname'], $_SESSION['replication']['m_port']);
129 $trg_link = null; // using null to indicate the current PMA server
131 $data = PMA_DBI_fetch_result('SHOW MASTER STATUS', null, null, $src_link); // let's find out, which databases are replicated
133 $do_db = array();
134 $ignore_db = array();
135 $dblist = array();
137 if (! empty($data[0]['Binlog_Do_DB'])) {
138 $do_db = explode(',', $data[0]['Binlog_Do_DB']);
140 if (! empty($data[0]['Binlog_Ignore_DB'])) {
141 $ignore_db = explode(',', $data[0]['Binlog_Ignore_DB']);
144 $tmp_alldbs = PMA_DBI_query('SHOW DATABASES;', $src_link);
145 while ($tmp_row = PMA_DBI_fetch_row($tmp_alldbs)) {
146 if (PMA_is_system_schema($tmp_row[0])) {
147 continue;
149 if (count($do_db) == 0) {
150 if (array_search($tmp_row[0], $ignore_db) !== false) {
151 continue;
153 $dblist[] = $tmp_row[0];
155 PMA_DBI_query('CREATE DATABASE IF NOT EXISTS '.PMA_backquote($tmp_row[0]), $trg_link);
156 } else {
157 if (array_search($tmp_row[0], $do_db) !== false) {
158 $dblist[] = $tmp_row[0];
159 PMA_DBI_query('CREATE DATABASE IF NOT EXISTS '.PMA_backquote($tmp_row[0]), $trg_link);
162 } // end while
164 unset($do_db, $ignore_db, $data);
166 if (isset($GLOBALS['repl_data'])) {
167 $include_data = true;
168 } else {
169 $include_data = false;
171 foreach ($dblist as $db) {
172 PMA_replication_synchronize_db($db, $src_link, $trg_link, $include_data);
174 // TODO some form of user feedback error/success would be nice
175 // What happens if $dblist is empty?
176 // or sync failed?
179 if ($refresh) {
180 Header("Location: ". PMA_generate_common_url($GLOBALS['url_params']));
182 unset($refresh);
185 * Displays the links
187 require './libraries/server_links.inc.php';
189 echo '<div id="replication">';
190 echo ' <h2>';
191 echo ' ' . PMA_getImage('s_replication.png');
192 echo __('Replication');
193 echo ' </h2>';
195 // Display error messages
196 if (isset($_SESSION['replication']['sr_action_status']) && isset($_SESSION['replication']['sr_action_info'])) {
197 if ($_SESSION['replication']['sr_action_status'] == 'error') {
198 PMA_Message::error($_SESSION['replication']['sr_action_info'])->display();
199 $_SESSION['replication']['sr_action_status'] = 'unknown';
200 } elseif ($_SESSION['replication']['sr_action_status'] == 'success') {
201 PMA_Message::success($_SESSION['replication']['sr_action_info'])->display();
202 $_SESSION['replication']['sr_action_status'] = 'unknown';
206 if ($server_master_status) {
207 if (! isset($GLOBALS['repl_clear_scr'])) {
208 echo '<fieldset>';
209 echo '<legend>' . __('Master replication') . '</legend>';
210 echo __('This server is configured as master in a replication process.');
211 echo '<ul>';
212 echo ' <li><a href="#" id="master_status_href">' . __('Show master status') . '</a></li>';
213 PMA_replication_print_status_table('master', true, false);
215 echo ' <li><a href="#" id="master_slaves_href">' . __('Show connected slaves') . '</a></li>';
216 PMA_replication_print_slaves_table(true);
218 $_url_params = $GLOBALS['url_params'];
219 $_url_params['mr_adduser'] = true;
220 $_url_params['repl_clear_scr'] = true;
222 echo ' <li><a href="' . PMA_generate_common_url($_url_params) . '" id="master_addslaveuser_href">' . __('Add slave replication user') . '</a></li>';
225 // Display 'Add replication slave user' form
226 if (isset($GLOBALS['mr_adduser'])) {
227 PMA_replication_gui_master_addslaveuser();
228 } elseif (! isset($GLOBALS['repl_clear_scr'])) {
229 echo "</ul>";
230 echo "</fieldset>";
232 } elseif (! isset($GLOBALS['mr_configure']) && ! isset($GLOBALS['repl_clear_scr'])) {
233 $_url_params = $GLOBALS['url_params'];
234 $_url_params['mr_configure'] = true;
236 echo '<fieldset>';
237 echo '<legend>' . __('Master replication') . '</legend>';
238 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));
239 echo '</fieldset>';
242 if (isset($GLOBALS['mr_configure'])) {
243 // Render the 'Master configuration' section
244 echo '<fieldset>';
245 echo '<legend>' . __('Master configuration') . '</legend>';
246 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 />';
248 echo '<select name="db_type" id="db_type">';
249 echo '<option value="all">' . __('Replicate all databases; Ignore:') . '</option>';
250 echo '<option value="ign">' . __('Ignore all databases; Replicate:') . '</option>';
251 echo '</select>';
252 echo '<br /><br />';
253 echo __('Please select databases:') . '<br />';
254 echo PMA_replication_db_multibox();
255 echo '<br /><br />';
256 echo __('Now, add the following lines at the end of [mysqld] section in your my.cnf and please restart the MySQL server afterwards.') . '<br />';
257 echo '<pre id="rep"></pre>';
258 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');
259 echo '</fieldset>';
260 echo '<fieldset class="tblFooters">';
261 echo ' <form method="post" action="server_replication.php" >';
262 echo PMA_generate_common_hidden_inputs('', '');
263 echo ' <input type="submit" value="' . __('Go') . '" id="goButton" />';
264 echo ' </form>';
265 echo '</fieldset>';
267 include './libraries/footer.inc.php';
268 exit;
271 echo '</div>';
273 if (! isset($GLOBALS['repl_clear_scr'])) {
274 // Render the 'Slave configuration' section
275 echo '<fieldset>';
276 echo '<legend>' . __('Slave replication') . '</legend>';
277 if ($server_slave_status) {
278 echo '<div id="slave_configuration_gui">';
280 $_url_params = $GLOBALS['url_params'];
281 $_url_params['sr_take_action'] = true;
282 $_url_params['sr_slave_server_control'] = true;
284 if ($server_slave_replication[0]['Slave_IO_Running'] == 'No') {
285 $_url_params['sr_slave_action'] = 'start';
286 } else {
287 $_url_params['sr_slave_action'] = 'stop';
290 $_url_params['sr_slave_control_parm'] = 'IO_THREAD';
291 $slave_control_io_link = PMA_generate_common_url($_url_params);
293 if ($server_slave_replication[0]['Slave_SQL_Running'] == 'No') {
294 $_url_params['sr_slave_action'] = 'start';
295 } else {
296 $_url_params['sr_slave_action'] = 'stop';
299 $_url_params['sr_slave_control_parm'] = 'SQL_THREAD';
300 $slave_control_sql_link = PMA_generate_common_url($_url_params);
302 if ($server_slave_replication[0]['Slave_IO_Running'] == 'No'
303 || $server_slave_replication[0]['Slave_SQL_Running'] == 'No'
305 $_url_params['sr_slave_action'] = 'start';
306 } else {
307 $_url_params['sr_slave_action'] = 'stop';
310 $_url_params['sr_slave_control_parm'] = null;
311 $slave_control_full_link = PMA_generate_common_url($_url_params);
313 $_url_params['sr_slave_action'] = 'reset';
314 $slave_control_reset_link = PMA_generate_common_url($_url_params);
316 $_url_params = $GLOBALS['url_params'];
317 $_url_params['sr_slave_skip_error'] = true;
318 $slave_skip_error_link = PMA_generate_common_url($_url_params);
320 if ($server_slave_replication[0]['Slave_SQL_Running'] == 'No') {
321 PMA_Message::error(__('Slave SQL Thread not running!'))->display();
323 if ($server_slave_replication[0]['Slave_IO_Running'] == 'No') {
324 PMA_Message::error(__('Slave IO Thread not running!'))->display();
327 $_url_params = $GLOBALS['url_params'];
328 $_url_params['sl_configure'] = true;
329 $_url_params['repl_clear_scr'] = true;
331 $reconfiguremaster_link = PMA_generate_common_url($_url_params);
333 echo __('Server is configured as slave in a replication process. Would you like to:');
334 echo '<br />';
335 echo '<ul>';
336 echo ' <li><a href="#" id="slave_status_href">' . __('See slave status table') . '</a></li>';
337 echo PMA_replication_print_status_table('slave', true, false);
338 if (isset($_SESSION['replication']['m_correct']) && $_SESSION['replication']['m_correct'] == true) {
339 echo ' <li><a href="#" id="slave_synchronization_href">' . __('Synchronize databases with master') . '</a></li>';
340 echo ' <div id="slave_synchronization_gui" style="display: none">';
341 echo ' <form method="post" action="server_replication.php">';
342 echo PMA_generate_common_hidden_inputs('', '');
343 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
344 echo ' <input type="checkbox" name="repl_data" value="1" checked="checked" /> ' . __('Data') .' <br />';
345 echo ' <input type="hidden" name="sr_take_action" value="1" />';
346 echo ' <input type="submit" name="sl_sync" value="' . __('Go') . '" />';
347 echo ' </form>';
348 echo ' </div>';
350 echo ' <li><a href="#" id="slave_control_href">' . __('Control slave:') . '</a>';
351 echo ' <div id="slave_control_gui" style="display: none">';
352 echo ' <ul>';
353 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>';
354 echo ' <li><a href="'. $slave_control_reset_link . '">' . __('Reset slave') . '</a></li>';
355 if ($server_slave_replication[0]['Slave_SQL_Running'] == 'No') {
356 echo ' <li><a href="' . $slave_control_sql_link . '">' . __('Start SQL Thread only') . '</a></li>';
357 } else {
358 echo ' <li><a href="' . $slave_control_sql_link . '">' . __('Stop SQL Thread only') . '</a></li>';
360 if ($server_slave_replication[0]['Slave_IO_Running'] == 'No') {
361 echo ' <li><a href="' . $slave_control_io_link . '">' . __('Start IO Thread only') . '</a></li>';
362 } else {
363 echo ' <li><a href="' . $slave_control_io_link . '">' . __('Stop IO Thread only') . '</a></li>';
365 echo ' </ul>';
366 echo ' </div>';
367 echo ' </li>';
368 echo ' <li><a href="#" id="slave_errormanagement_href">' . __('Error management:') . '</a>';
369 echo ' <div id="slave_errormanagement_gui" style="display: none">';
370 PMA_Message::error(__('Skipping errors might lead into unsynchronized master and slave!'))->display();
371 echo ' <ul>';
372 echo ' <li><a href="' . $slave_skip_error_link . '">' . __('Skip current error') . '</a></li>';
373 echo ' <li>' . __('Skip next');
374 echo ' <form method="post" action="server_replication.php">';
375 echo PMA_generate_common_hidden_inputs('', '');
376 echo ' <input type="text" name="sr_skip_errors_count" value="1" style="width: 30px" />' . __('errors.');
377 echo ' <input type="submit" name="sr_slave_skip_error" value="' . __('Go') . '" />';
378 echo ' <input type="hidden" name="sr_take_action" value="1" />';
379 echo ' </form></li>';
380 echo ' </ul>';
381 echo ' </div>';
382 echo ' </li>';
383 echo ' <li><a href="' . $reconfiguremaster_link . '">' . __('Change or reconfigure master server') . '</a></li>';
384 echo '</ul>';
386 } elseif (! isset($GLOBALS['sl_configure'])) {
387 $_url_params = $GLOBALS['url_params'];
388 $_url_params['sl_configure'] = true;
389 $_url_params['repl_clear_scr'] = true;
391 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));
393 echo '</div>';
394 echo '</fieldset>';
396 if (isset($GLOBALS['sl_configure'])) {
397 PMA_replication_gui_changemaster("slave_changemaster");
399 require './libraries/footer.inc.php';