Translation update done using Pootle.
[phpmyadmin/tyronm.git] / libraries / replication.inc.php
blob857f4dc8dafe10131a1abf7d5e61edfd7b133068
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
5 * @package phpMyAdmin
6 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
13 * get master replication from server
15 $server_master_replication = PMA_DBI_fetch_result('SHOW MASTER STATUS');
17 /**
18 * get slave replication from server
20 $server_slave_replication = PMA_DBI_fetch_result('SHOW SLAVE STATUS');
22 /**
23 * replication types
25 $replication_types = array('master', 'slave');
28 /**
29 * define variables for master status
31 $master_variables = array(
32 'File',
33 'Position',
34 'Binlog_Do_DB',
35 'Binlog_Ignore_DB',
38 /**
39 * Define variables for slave status
41 $slave_variables = array(
42 'Slave_IO_State',
43 'Master_Host',
44 'Master_User',
45 'Master_Port',
46 'Connect_Retry',
47 'Master_Log_File',
48 'Read_Master_Log_Pos',
49 'Relay_Log_File',
50 'Relay_Log_Pos',
51 'Relay_Master_Log_File',
52 'Slave_IO_Running',
53 'Slave_SQL_Running',
54 'Replicate_Do_DB',
55 'Replicate_Ignore_DB',
56 'Replicate_Do_Table',
57 'Replicate_Ignore_Table',
58 'Replicate_Wild_Do_Table',
59 'Replicate_Wild_Ignore_Table',
60 'Last_Errno',
61 'Last_Error',
62 'Skip_Counter',
63 'Exec_Master_Log_Pos',
64 'Relay_Log_Space',
65 'Until_Condition',
66 'Until_Log_File',
67 'Until_Log_Pos',
68 'Master_SSL_Allowed',
69 'Master_SSL_CA_File',
70 'Master_SSL_CA_Path',
71 'Master_SSL_Cert',
72 'Master_SSL_Cipher',
73 'Master_SSL_Key',
74 'Seconds_Behind_Master',
76 /**
77 * define important variables, which need to be watched for correct running of replication in slave mode
79 * @usedby PMA_replication_print_status_table()
81 // TODO change to regexp or something, to allow for negative match. To e.g. highlight 'Last_Error'
83 $slave_variables_alerts = array(
84 'Slave_IO_Running' => 'No',
85 'Slave_SQL_Running' => 'No',
87 $slave_variables_oks = array(
88 'Slave_IO_Running' => 'Yes',
89 'Slave_SQL_Running' => 'Yes',
92 // check which replication is available and set $server_{master/slave}_status and assign values
94 // replication info is more easily passed to functions
96 * @todo use $replication_info everywhere instead of the generated variable names
98 $replication_info = array();
100 foreach ($replication_types as $type) {
101 if (count(${"server_{$type}_replication"}) > 0) {
102 ${"server_{$type}_status"} = true;
103 $replication_info[$type]['status'] = true;
104 } else {
105 ${"server_{$type}_status"} = false;
106 $replication_info[$type]['status'] = false;
108 if (${"server_{$type}_status"}) {
109 if ($type == "master") {
110 ${"server_{$type}_Do_DB"} = explode(",", $server_master_replication[0]["Binlog_Do_DB"]);
111 $replication_info[$type]['Do_DB'] = ${"server_{$type}_Do_DB"};
113 ${"server_{$type}_Ignore_DB"} = explode(",", $server_master_replication[0]["Binlog_Ignore_DB"]);
114 $replication_info[$type]['Ignore_DB'] = ${"server_{$type}_Ignore_DB"};
115 } elseif ($type == "slave") {
116 ${"server_{$type}_Do_DB"} = explode(",", $server_slave_replication[0]["Replicate_Do_DB"]);
117 $replication_info[$type]['Do_DB'] = ${"server_{$type}_Do_DB"};
119 ${"server_{$type}_Ignore_DB"} = explode(",", $server_slave_replication[0]["Replicate_Ignore_DB"]);
120 $replication_info[$type]['Ignore_DB'] = ${"server_{$type}_Ignore_DB"};
122 ${"server_{$type}_Do_Table"} = explode(",", $server_slave_replication[0]["Replicate_Do_Table"]);
123 $replication_info[$type]['Do_Table'] = ${"server_{$type}_Do_Table"};
125 ${"server_{$type}_Ignore_Table"} = explode(",", $server_slave_replication[0]["Replicate_Ignore_Table"]);
126 $replication_info[$type]['Ignore_Table'] = ${"server_{$type}_Ignore_Table"};
128 ${"server_{$type}_Wild_Do_Table"} = explode(",", $server_slave_replication[0]["Replicate_Wild_Do_Table"]);
129 $replication_info[$type]['Wild_Do_Table'] = ${"server_{$type}_Wild_Do_Table"};
131 ${"server_{$type}_Wild_Ignore_Table"} = explode(",", $server_slave_replication[0]["Replicate_Wild_Ignore_Table"]);
132 $replication_info[$type]['Wild_Ignore_Table'] = ${"server_{$type}_Wild_Ignore_Table"};
139 * @param $string contains "dbname.tablename"
140 * @param $what what to extract (db|table)
141 * @return $string the extracted part
143 function PMA_extract_db_or_table($string, $what = 'db')
145 $list = explode(".", $string);
146 if ('db' == $what) {
147 return $list[0];
148 } else {
149 return $list[1];
153 * @param string $action possible values: START or STOP
154 * @param string $control default: null, possible values: SQL_THREAD or IO_THREAD or null. If it is set to null, it controls both SQL_THREAD and IO_THREAD
155 * @param mixed $link mysql link
157 * @return mixed output of PMA_DBI_try_query
159 function PMA_replication_slave_control($action, $control = null, $link = null)
161 $action = strtoupper($action);
162 $control = strtoupper($control);
164 if ($action != "START" && $action != "STOP") {
165 return -1;
167 if ($control != "SQL_THREAD" && $control != "IO_THREAD" && $control != null) {
168 return -1;
171 return PMA_DBI_try_query($action . " SLAVE " . $control . ";", $link);
174 * @param string $user replication user on master
175 * @param string $password password for the user
176 * @param string $host master's hostname or IP
177 * @param int $port port, where mysql is running
178 * @param array $pos position of mysql replication, array should contain fields File and Position
179 * @param bool $stop shall we stop slave?
180 * @param bool $start shall we start slave?
181 * @param mixed $link mysql link
183 * @return output of CHANGE MASTER mysql command
185 function PMA_replication_slave_change_master($user, $password, $host, $port, $pos, $stop = true, $start = true, $link = null)
187 if ($stop) {
188 PMA_replication_slave_control("STOP", null, $link);
191 $out = PMA_DBI_try_query('CHANGE MASTER TO ' .
192 'MASTER_HOST=\'' . $host . '\',' .
193 'MASTER_PORT=' . ($port * 1) . ',' .
194 'MASTER_USER=\'' . $user . '\',' .
195 'MASTER_PASSWORD=\'' . $password . '\',' .
196 'MASTER_LOG_FILE=\'' . $pos["File"] . '\',' .
197 'MASTER_LOG_POS=' . $pos["Position"] . ';', $link);
199 if ($start) {
200 PMA_replication_slave_control("START", null, $link);
203 return $out;
207 * This function provides connection to remote mysql server
209 * @param string $user mysql username
210 * @param string $password password for the user
211 * @param string $host mysql server's hostname or IP
212 * @param int $port mysql remote port
213 * @param string $socket path to unix socket
215 * @return mixed $link mysql link on success
217 function PMA_replication_connect_to_master($user, $password, $host = null, $port = null, $socket = null)
219 $server = array();
220 $server["host"] = $host;
221 $server["port"] = $port;
222 $server["socket"] = $socket;
224 // 5th parameter set to true means that it's an auxiliary connection
225 // and we must not go back to login page if it fails
226 return PMA_DBI_connect($user, $password, false, $server, true);
229 * @param mixed $link mysql link
231 * @return array - containing File and Position in MySQL replication on master server, useful for PMA_replication_slave_change_master
233 function PMA_replication_slave_bin_log_master($link = null)
235 $data = PMA_DBI_fetch_result('SHOW MASTER STATUS', null, null, $link);
236 $output = array();
238 if (! empty($data)) {
239 $output["File"] = $data[0]["File"];
240 $output["Position"] = $data[0]["Position"];
242 return $output;
246 * Get list of replicated databases on master server
248 * @param mixed $link mysql link
250 * @return array array of replicated databases
253 function PMA_replication_master_replicated_dbs($link = null)
255 $data = PMA_DBI_fetch_result('SHOW MASTER STATUS', null, null, $link); // let's find out, which databases are replicated
257 $do_db = array();
258 $ignore_db = array();
260 if (! empty($data[0]['Binlog_Do_DB'])) {
261 $do_db = explode(',', $data[0]['Binlog_Do_DB']);
263 if (! empty($data[0]['Binlog_Ignore_DB'])) {
264 $ignore_db = explode(',', $data[0]['Binlog_Ignore_DB']);
267 $tmp_alldbs = PMA_DBI_query('SHOW DATABASES;', $link);
268 while ($tmp_row = PMA_DBI_fetch_row($tmp_alldbs)) {
269 if ($tmp_row[0] == 'information_schema')
270 continue;
271 if (count($do_db) == 0) {
272 if (array_search($tmp_row[0], $ignore_db) !== false) {
273 continue;
275 $dblist[] = $tmp_row[0];
277 } else {
278 if (array_search($tmp_row[0], $do_db) !== false) {
279 $dblist[] = $tmp_row[0];
282 } // end while
284 return $link;
287 * This function provides synchronization of structure and data between two mysql servers.
289 * @todo improve code sharing between the function and synchronization
291 * @param string $db name of database, which should be synchronized
292 * @param mixed $src_link link of source server, note: if the server is current PMA server, use null
293 * @param mixed $trg_link link of target server, note: if the server is current PMA server, use null
294 * @param bool $data if true, then data will be copied as well
297 function PMA_replication_synchronize_db($db, $src_link, $trg_link, $data = true)
299 $src_db = $trg_db = $db;
301 $src_tables = PMA_DBI_get_tables($src_db, $src_link);
303 $trg_tables = PMA_DBI_get_tables($trg_db, $trg_link);
306 * initializing arrays to save table names
308 $source_tables_uncommon = array();
309 $target_tables_uncommon = array();
310 $matching_tables = array();
311 $matching_tables_num = 0;
314 * Criterion for matching tables is just their names.
315 * Finding the uncommon tables for the source database
316 * BY comparing the matching tables with all the tables in the source database
318 PMA_getMatchingTables($trg_tables, $src_tables, $matching_tables, $source_tables_uncommon);
321 * Finding the uncommon tables for the target database
322 * BY comparing the matching tables with all the tables in the target database
324 PMA_getNonMatchingTargetTables($trg_tables, $matching_tables, $target_tables_uncommon);
328 * Comparing Data In the Matching Tables
329 * It is assumed that the matching tables are structurally
330 * and typely exactly the same
332 $fields_num = array();
333 $matching_tables_fields = array();
334 $matching_tables_keys = array();
335 $insert_array = array(array(array()));
336 $update_array = array(array(array()));
337 $delete_array = array();
338 $row_count = array();
339 $uncommon_tables_fields = array();
340 $matching_tables_num = sizeof($matching_tables);
342 for ($i = 0; $i < sizeof($matching_tables); $i++) {
343 PMA_dataDiffInTables($src_db, $trg_db, $src_link, $trg_link, $matching_tables, $matching_tables_fields, $update_array, $insert_array,
344 $delete_array, $fields_num, $i, $matching_tables_keys);
346 for ($j = 0; $j < sizeof($source_tables_uncommon); $j++) {
347 PMA_dataDiffInUncommonTables($source_tables_uncommon, $src_db, $src_link, $j, $row_count);
351 * INTEGRATION OF STRUCTURE DIFFERENCE CODE
354 $source_columns = array();
355 $target_columns = array();
356 $alter_str_array = array(array());
357 $add_column_array = array(array());
358 $uncommon_columns = array();
359 $target_tables_keys = array();
360 $source_indexes = array();
361 $target_indexes = array();
362 $add_indexes_array = array();
363 $alter_indexes_array = array();
364 $remove_indexes_array = array();
365 $criteria = array('Field', 'Type', 'Null', 'Collation', 'Key', 'Default', 'Comment');
367 for ($counter = 0; $counter < $matching_tables_num; $counter++) {
368 PMA_structureDiffInTables($src_db, $trg_db, $src_link, $trg_link, $matching_tables, $source_columns,
369 $target_columns, $alter_str_array, $add_column_array, $uncommon_columns, $criteria, $target_tables_keys, $counter);
371 PMA_indexesDiffInTables($src_db, $trg_db, $src_link, $trg_link, $matching_tables, $source_indexes, $target_indexes,
372 $add_indexes_array, $alter_indexes_array,$remove_indexes_array, $counter);
376 * Generating Create Table query for all the non-matching tables present in Source but not in Target and populating tables.
378 for ($q = 0; $q < sizeof($source_tables_uncommon); $q++) {
379 if (isset($source_tables_uncommon[$q])) {
380 PMA_createTargetTables($src_db, $trg_db, $src_link, $trg_link, $source_tables_uncommon, $q, $uncommon_tables_fields, false);
382 if (isset($row_count[$q]) && $data) {
383 PMA_populateTargetTables($src_db, $trg_db, $src_link, $trg_link, $source_tables_uncommon, $q, $uncommon_tables_fields, false);