2 /* vim: set expandtab sw=4 ts=4 sts=4: */
9 if (! defined('PHPMYADMIN')) {
14 * get master replication from server
16 $server_master_replication = PMA_DBI_fetch_result('SHOW MASTER STATUS');
19 * get slave replication from server
21 $server_slave_replication = PMA_DBI_fetch_result('SHOW SLAVE STATUS');
26 $replication_types = array('master', 'slave');
30 * define variables for master status
32 $master_variables = array(
40 * Define variables for slave status
42 $slave_variables = array(
49 'Read_Master_Log_Pos',
52 'Relay_Master_Log_File',
56 'Replicate_Ignore_DB',
58 'Replicate_Ignore_Table',
59 'Replicate_Wild_Do_Table',
60 'Replicate_Wild_Ignore_Table',
64 'Exec_Master_Log_Pos',
75 'Seconds_Behind_Master',
78 * define important variables, which need to be watched for correct running of replication in slave mode
80 * @usedby PMA_replication_print_status_table()
82 // TODO change to regexp or something, to allow for negative match. To e.g. highlight 'Last_Error'
84 $slave_variables_alerts = array(
85 'Slave_IO_Running' => 'No',
86 'Slave_SQL_Running' => 'No',
88 $slave_variables_oks = array(
89 'Slave_IO_Running' => 'Yes',
90 'Slave_SQL_Running' => 'Yes',
93 // check which replication is available and set $server_{master/slave}_status and assign values
94 foreach ($replication_types as $type) {
95 if (count($
{"server_{$type}_replication"}) > 0) {
96 $
{"server_{$type}_status"} = true;
98 $
{"server_{$type}_status"} = false;
100 if ($
{"server_{$type}_status"}) {
101 if ($type == "master") {
102 $
{"server_{$type}_Do_DB"} = explode(",", $server_master_replication[0]["Binlog_Do_DB"]);
103 $
{"server_{$type}_Ignore_DB"} = explode(",", $server_master_replication[0]["Binlog_Ignore_DB"]);
104 } elseif ($type == "slave") {
105 $
{"server_{$type}_Do_DB"} = explode(",", $server_slave_replication[0]["Replicate_Do_DB"]);
106 $
{"server_{$type}_Ignore_DB"} = explode(",", $server_slave_replication[0]["Replicate_Ignore_DB"]);
108 $
{"server_{$type}_Do_Table"} = explode(",", $server_slave_replication[0]["Replicate_Do_Table"]);
109 $
{"server_{$type}_Ignore_Table"} = explode(",", $server_slave_replication[0]["Replicate_Ignore_Table"]);
111 $
{"server_{$type}_Wild_Do_Table"} = explode(",", $server_slave_replication[0]["Replicate_Wild_Do_Table"]);
112 $
{"server_{$type}_Wild_Ignore_Table"} = explode(",", $server_slave_replication[0]["Replicate_Wild_Ignore_Table"]);
123 function PMA_replication_strout($string, $table = false) {
124 $list = explode(".", $string);
126 return $list[(int)$table];
129 * @param String $action - possible values: START or STOP
130 * @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
131 * @param mixed $link - mysql link
133 * @return mixed output of PMA_DBI_try_query
135 function PMA_replication_slave_control($action, $control = null, $link = null) {
136 $action = strtoupper($action);
137 $control = strtoupper($control);
139 if ($action != "START" && $action != "STOP") {
142 if ($control != "SQL_THREAD" && $control != "IO_THREAD" && $control != null) {
146 return PMA_DBI_try_query($action . " SLAVE " . $control . ";", $link);
149 * @param String $user - replication user on master
150 * @param String $password - password for the user
151 * @param String $host - master's hostname or IP
152 * @param int $port - port, where mysql is running
153 * @param array $pos - position of mysql replication, array should contain fields File and Position
154 * @param boolean $stop - shall we stop slave?
155 * @param boolean $start - shall we start slave?
156 * @param mixed $link - mysql link
158 * @return output of CHANGE MASTER mysql command
160 function PMA_replication_slave_change_master($user, $password, $host, $port, $pos, $stop = true, $start = true, $link = null) {
162 PMA_replication_slave_control("STOP", null, $link);
165 $out = PMA_DBI_try_query('CHANGE MASTER TO ' .
166 'MASTER_HOST=\'' . $host . '\',' .
167 'MASTER_PORT=' . ($port * 1) . ',' .
168 'MASTER_USER=\'' . $user . '\',' .
169 'MASTER_PASSWORD=\'' . $password . '\',' .
170 'MASTER_LOG_FILE=\'' . $pos["File"] . '\',' .
171 'MASTER_LOG_POS=' . $pos["Position"] . ';', $link);
174 PMA_replication_slave_control("START", null, $link);
181 * This function provides connection to remote mysql server
183 * @param String $user - mysql username
184 * @param String $password - password for the user
185 * @param String $host - mysql server's hostname or IP
186 * @param int $port - mysql remote port
187 * @param String $socket - path to unix socket
189 * @return mixed $link mysql link on success
191 function PMA_replication_connect_to_master($user, $password, $host = null, $port = null, $socket = null) {
193 $server["host"] = $host;
194 $server["port"] = $port;
195 $server["socket"] = $socket;
197 // 5th parameter set to true means that it's an auxiliary connection
198 // and we must not go back to login page if it fails
199 return PMA_DBI_connect($user, $password, false, $server, true);
202 * @param $link - mysql link
204 * @return array - containing File and Position in MySQL replication on master server, useful for PMA_replication_slave_change_master
206 function PMA_replication_slave_bin_log_master($link = null) {
207 $data = PMA_DBI_fetch_result('SHOW MASTER STATUS', null, null, $link);
210 if (! empty($data)) {
211 $output["File"] = $data[0]["File"];
212 $output["Position"] = $data[0]["Position"];
218 * Get list of replicated databases on master server
220 * @param mixed mysql link
222 * @return array array of replicated databases
225 function PMA_replication_master_replicated_dbs($link = null) {
226 $data = PMA_DBI_fetch_result('SHOW MASTER STATUS', null, null, $link); // let's find out, which databases are replicated
229 $ignore_db = array();
231 if (! empty($data[0]['Binlog_Do_DB'])) {
232 $do_db = explode(',', $data[0]['Binlog_Do_DB']);
234 if (! empty($data[0]['Binlog_Ignore_DB'])) {
235 $ignore_db = explode(',', $data[0]['Binlog_Ignore_DB']);
238 $tmp_alldbs = PMA_DBI_query('SHOW DATABASES;', $link);
239 while ($tmp_row = PMA_DBI_fetch_row($tmp_alldbs)) {
240 if ($tmp_row[0] == 'information_schema')
242 if (count($do_db) == 0) {
243 if (array_search($tmp_row[0], $ignore_db) !== false) {
246 $dblist[] = $tmp_row[0];
249 if (array_search($tmp_row[0], $do_db) !== false) {
250 $dblist[] = $tmp_row[0];
258 * This function provides synchronization of structure and data between two mysql servers.
259 * TODO: improve code sharing between the function and synchronization
261 * @param String $db - name of database, which should be synchronized
262 * @param mixed $src_link - link of source server, note: if the server is current PMA server, use null
263 * @param mixed $trg_link - link of target server, note: if the server is current PMA server, use null
264 * @param boolean $data - if true, then data will be copied as well
267 function PMA_replication_synchronize_db($db, $src_link, $trg_link, $data = true) {
268 $src_db = $trg_db = $db;
270 $src_connection = PMA_DBI_select_db($src_db, $src_link);
271 $trg_connection = PMA_DBI_select_db($trg_db, $trg_link);
273 $src_tables = PMA_DBI_get_tables($src_db, $src_link);
274 $source_tables_num = sizeof($src_tables);
276 $trg_tables = PMA_DBI_get_tables($trg_db, $trg_link);
277 $target_tables_num = sizeof($trg_tables);
280 * initializing arrays to save table names
282 $unmatched_num_src = 0;
283 $source_tables_uncommon = array();
284 $unmatched_num_trg = 0;
285 $target_tables_uncommon = array();
286 $matching_tables = array();
287 $matching_tables_num = 0;
290 * Criterion for matching tables is just their names.
291 * Finding the uncommon tables for the source database
292 * BY comparing the matching tables with all the tables in the source database
294 PMA_getMatchingTables($trg_tables, $src_tables, $matching_tables, $source_tables_uncommon);
297 * Finding the uncommon tables for the target database
298 * BY comparing the matching tables with all the tables in the target database
300 PMA_getNonMatchingTargetTables($trg_tables, $matching_tables, $target_tables_uncommon);
304 * Comparing Data In the Matching Tables
305 * It is assumed that the matching tables are structurally
306 * and typely exactly the same
308 $fields_num = array();
309 $matching_tables_fields = array();
310 $matching_tables_keys = array();
311 $insert_array = array(array(array()));
312 $update_array = array(array(array()));
313 $delete_array = array();
314 $row_count = array();
315 $uncommon_tables_fields = array();
316 $matching_tables_num = sizeof($matching_tables);
318 for ($i = 0; $i < sizeof($matching_tables); $i++
) {
319 PMA_dataDiffInTables($src_db, $trg_db, $src_link, $trg_link, $matching_tables, $matching_tables_fields, $update_array, $insert_array,
320 $delete_array, $fields_num, $i, $matching_tables_keys);
322 for ($j = 0; $j < sizeof($source_tables_uncommon); $j++
) {
323 PMA_dataDiffInUncommonTables($source_tables_uncommon, $src_db, $src_link, $j, $row_count);
327 * INTEGRATION OF STRUCTURE DIFFERENCE CODE
330 $source_columns = array();
331 $target_columns = array();
332 $alter_str_array = array(array());
333 $add_column_array = array(array());
334 $uncommon_columns = array();
335 $target_tables_keys = array();
336 $source_indexes = array();
337 $target_indexes = array();
338 $add_indexes_array = array();
339 $remove_indexes_array = array();
340 $criteria = array('Field', 'Type', 'Null', 'Collation', 'Key', 'Default', 'Comment');
342 for ($counter = 0; $counter < $matching_tables_num; $counter++
) {
343 PMA_structureDiffInTables($src_db, $trg_db, $src_link, $trg_link, $matching_tables, $source_columns,
344 $target_columns, $alter_str_array, $add_column_array, $uncommon_columns, $criteria, $target_tables_keys, $counter);
346 PMA_indexesDiffInTables($src_db, $trg_db, $src_link, $trg_link, $matching_tables, $source_indexes, $target_indexes,
347 $add_indexes_array, $alter_indexes_array,$remove_indexes_array, $counter);
350 $matching_table_data_diff = array();
351 $matching_table_structure_diff = array();
352 $uncommon_table_structure_diff = array();
353 $uncommon_table_data_diff = array();
354 $uncommon_tables = $source_tables_uncommon;
357 * Generating Create Table query for all the non-matching tables present in Source but not in Target and populating tables.
359 for($q = 0; $q < sizeof($source_tables_uncommon); $q++
) {
360 if (isset($uncommon_tables[$q])) {
361 PMA_createTargetTables($src_db, $trg_db, $src_link, $trg_link, $source_tables_uncommon, $q, $uncommon_tables_fields, false);
363 if (isset($row_count[$q]) && $data) {
364 PMA_populateTargetTables($src_db, $trg_db, $src_link, $trg_link, $source_tables_uncommon, $q, $uncommon_tables_fields, false);