2 /* vim: set expandtab sw=4 ts=4 sts=4: */
8 if (! defined('PHPMYADMIN')) {
13 * get master replication from server
15 $server_master_replication = PMA_DBI_fetch_result('SHOW MASTER STATUS');
18 * get slave replication from server
20 $server_slave_replication = PMA_DBI_fetch_result('SHOW SLAVE STATUS');
25 $replication_types = array('master', 'slave');
29 * define variables for master status
31 $master_variables = array(
39 * Define variables for slave status
41 $slave_variables = array(
48 'Read_Master_Log_Pos',
51 'Relay_Master_Log_File',
55 'Replicate_Ignore_DB',
57 'Replicate_Ignore_Table',
58 'Replicate_Wild_Do_Table',
59 'Replicate_Wild_Ignore_Table',
63 'Exec_Master_Log_Pos',
74 'Seconds_Behind_Master',
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;
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') {
144 $list = explode(".", $string);
152 * @param String $action - possible values: START or STOP
153 * @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
154 * @param mixed $link - mysql link
156 * @return mixed output of PMA_DBI_try_query
158 function PMA_replication_slave_control($action, $control = null, $link = null) {
159 $action = strtoupper($action);
160 $control = strtoupper($control);
162 if ($action != "START" && $action != "STOP") {
165 if ($control != "SQL_THREAD" && $control != "IO_THREAD" && $control != null) {
169 return PMA_DBI_try_query($action . " SLAVE " . $control . ";", $link);
172 * @param String $user - replication user on master
173 * @param String $password - password for the user
174 * @param String $host - master's hostname or IP
175 * @param int $port - port, where mysql is running
176 * @param array $pos - position of mysql replication, array should contain fields File and Position
177 * @param boolean $stop - shall we stop slave?
178 * @param boolean $start - shall we start slave?
179 * @param mixed $link - mysql link
181 * @return output of CHANGE MASTER mysql command
183 function PMA_replication_slave_change_master($user, $password, $host, $port, $pos, $stop = true, $start = true, $link = null) {
185 PMA_replication_slave_control("STOP", null, $link);
188 $out = PMA_DBI_try_query('CHANGE MASTER TO ' .
189 'MASTER_HOST=\'' . $host . '\',' .
190 'MASTER_PORT=' . ($port * 1) . ',' .
191 'MASTER_USER=\'' . $user . '\',' .
192 'MASTER_PASSWORD=\'' . $password . '\',' .
193 'MASTER_LOG_FILE=\'' . $pos["File"] . '\',' .
194 'MASTER_LOG_POS=' . $pos["Position"] . ';', $link);
197 PMA_replication_slave_control("START", null, $link);
204 * This function provides connection to remote mysql server
206 * @param String $user - mysql username
207 * @param String $password - password for the user
208 * @param String $host - mysql server's hostname or IP
209 * @param int $port - mysql remote port
210 * @param String $socket - path to unix socket
212 * @return mixed $link mysql link on success
214 function PMA_replication_connect_to_master($user, $password, $host = null, $port = null, $socket = null) {
216 $server["host"] = $host;
217 $server["port"] = $port;
218 $server["socket"] = $socket;
220 // 5th parameter set to true means that it's an auxiliary connection
221 // and we must not go back to login page if it fails
222 return PMA_DBI_connect($user, $password, false, $server, true);
225 * @param $link - mysql link
227 * @return array - containing File and Position in MySQL replication on master server, useful for PMA_replication_slave_change_master
229 function PMA_replication_slave_bin_log_master($link = null) {
230 $data = PMA_DBI_fetch_result('SHOW MASTER STATUS', null, null, $link);
233 if (! empty($data)) {
234 $output["File"] = $data[0]["File"];
235 $output["Position"] = $data[0]["Position"];
241 * Get list of replicated databases on master server
243 * @param mixed mysql link
245 * @return array array of replicated databases
248 function PMA_replication_master_replicated_dbs($link = null) {
249 $data = PMA_DBI_fetch_result('SHOW MASTER STATUS', null, null, $link); // let's find out, which databases are replicated
252 $ignore_db = array();
254 if (! empty($data[0]['Binlog_Do_DB'])) {
255 $do_db = explode(',', $data[0]['Binlog_Do_DB']);
257 if (! empty($data[0]['Binlog_Ignore_DB'])) {
258 $ignore_db = explode(',', $data[0]['Binlog_Ignore_DB']);
261 $tmp_alldbs = PMA_DBI_query('SHOW DATABASES;', $link);
262 while ($tmp_row = PMA_DBI_fetch_row($tmp_alldbs)) {
263 if ($tmp_row[0] == 'information_schema')
265 if (count($do_db) == 0) {
266 if (array_search($tmp_row[0], $ignore_db) !== false) {
269 $dblist[] = $tmp_row[0];
272 if (array_search($tmp_row[0], $do_db) !== false) {
273 $dblist[] = $tmp_row[0];
281 * This function provides synchronization of structure and data between two mysql servers.
282 * TODO: improve code sharing between the function and synchronization
284 * @param String $db - name of database, which should be synchronized
285 * @param mixed $src_link - link of source server, note: if the server is current PMA server, use null
286 * @param mixed $trg_link - link of target server, note: if the server is current PMA server, use null
287 * @param boolean $data - if true, then data will be copied as well
290 function PMA_replication_synchronize_db($db, $src_link, $trg_link, $data = true) {
291 $src_db = $trg_db = $db;
293 $src_connection = PMA_DBI_select_db($src_db, $src_link);
294 $trg_connection = PMA_DBI_select_db($trg_db, $trg_link);
296 $src_tables = PMA_DBI_get_tables($src_db, $src_link);
297 $source_tables_num = sizeof($src_tables);
299 $trg_tables = PMA_DBI_get_tables($trg_db, $trg_link);
300 $target_tables_num = sizeof($trg_tables);
303 * initializing arrays to save table names
305 $unmatched_num_src = 0;
306 $source_tables_uncommon = array();
307 $unmatched_num_trg = 0;
308 $target_tables_uncommon = array();
309 $matching_tables = array();
310 $matching_tables_num = 0;
313 * Criterion for matching tables is just their names.
314 * Finding the uncommon tables for the source database
315 * BY comparing the matching tables with all the tables in the source database
317 PMA_getMatchingTables($trg_tables, $src_tables, $matching_tables, $source_tables_uncommon);
320 * Finding the uncommon tables for the target database
321 * BY comparing the matching tables with all the tables in the target database
323 PMA_getNonMatchingTargetTables($trg_tables, $matching_tables, $target_tables_uncommon);
327 * Comparing Data In the Matching Tables
328 * It is assumed that the matching tables are structurally
329 * and typely exactly the same
331 $fields_num = array();
332 $matching_tables_fields = array();
333 $matching_tables_keys = array();
334 $insert_array = array(array(array()));
335 $update_array = array(array(array()));
336 $delete_array = array();
337 $row_count = array();
338 $uncommon_tables_fields = array();
339 $matching_tables_num = sizeof($matching_tables);
341 for ($i = 0; $i < sizeof($matching_tables); $i++
) {
342 PMA_dataDiffInTables($src_db, $trg_db, $src_link, $trg_link, $matching_tables, $matching_tables_fields, $update_array, $insert_array,
343 $delete_array, $fields_num, $i, $matching_tables_keys);
345 for ($j = 0; $j < sizeof($source_tables_uncommon); $j++
) {
346 PMA_dataDiffInUncommonTables($source_tables_uncommon, $src_db, $src_link, $j, $row_count);
350 * INTEGRATION OF STRUCTURE DIFFERENCE CODE
353 $source_columns = array();
354 $target_columns = array();
355 $alter_str_array = array(array());
356 $add_column_array = array(array());
357 $uncommon_columns = array();
358 $target_tables_keys = array();
359 $source_indexes = array();
360 $target_indexes = array();
361 $add_indexes_array = array();
362 $remove_indexes_array = array();
363 $criteria = array('Field', 'Type', 'Null', 'Collation', 'Key', 'Default', 'Comment');
365 for ($counter = 0; $counter < $matching_tables_num; $counter++
) {
366 PMA_structureDiffInTables($src_db, $trg_db, $src_link, $trg_link, $matching_tables, $source_columns,
367 $target_columns, $alter_str_array, $add_column_array, $uncommon_columns, $criteria, $target_tables_keys, $counter);
369 PMA_indexesDiffInTables($src_db, $trg_db, $src_link, $trg_link, $matching_tables, $source_indexes, $target_indexes,
370 $add_indexes_array, $alter_indexes_array,$remove_indexes_array, $counter);
373 $matching_table_data_diff = array();
374 $matching_table_structure_diff = array();
375 $uncommon_table_structure_diff = array();
376 $uncommon_table_data_diff = array();
377 $uncommon_tables = $source_tables_uncommon;
380 * Generating Create Table query for all the non-matching tables present in Source but not in Target and populating tables.
382 for($q = 0; $q < sizeof($source_tables_uncommon); $q++
) {
383 if (isset($uncommon_tables[$q])) {
384 PMA_createTargetTables($src_db, $trg_db, $src_link, $trg_link, $source_tables_uncommon, $q, $uncommon_tables_fields, false);
386 if (isset($row_count[$q]) && $data) {
387 PMA_populateTargetTables($src_db, $trg_db, $src_link, $trg_link, $source_tables_uncommon, $q, $uncommon_tables_fields, false);