in Synchronize, the connection problems detection no longer worked
[phpmyadmin/madhuracj.git] / libraries / replication.inc.php
blobaa060586bc99fad692051181998c85ddb094bcab
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
5 * @version $Id$
6 * @package phpMyAdmin
7 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /**
14 * get master replication from server
16 $server_master_replication = PMA_DBI_fetch_result('SHOW MASTER STATUS');
18 /**
19 * get slave replication from server
21 $server_slave_replication = PMA_DBI_fetch_result('SHOW SLAVE STATUS');
23 /**
24 * replication types
26 $replication_types = array('master', 'slave');
29 /**
30 * define variables for master status
32 $master_variables = array(
33 'File',
34 'Position',
35 'Binlog_Do_DB',
36 'Binlog_Ignore_DB',
39 /**
40 * Define variables for slave status
42 $slave_variables = array(
43 'Slave_IO_State',
44 'Master_Host',
45 'Master_User',
46 'Master_Port',
47 'Connect_Retry',
48 'Master_Log_File',
49 'Read_Master_Log_Pos',
50 'Relay_Log_File',
51 'Relay_Log_Pos',
52 'Relay_Master_Log_File',
53 'Slave_IO_Running',
54 'Slave_SQL_Running',
55 'Replicate_Do_DB',
56 'Replicate_Ignore_DB',
57 'Replicate_Do_Table',
58 'Replicate_Ignore_Table',
59 'Replicate_Wild_Do_Table',
60 'Replicate_Wild_Ignore_Table',
61 'Last_Errno',
62 'Last_Error',
63 'Skip_Counter',
64 'Exec_Master_Log_Pos',
65 'Relay_Log_Space',
66 'Until_Condition',
67 'Until_Log_File',
68 'Until_Log_Pos',
69 'Master_SSL_Allowed',
70 'Master_SSL_CA_File',
71 'Master_SSL_CA_Path',
72 'Master_SSL_Cert',
73 'Master_SSL_Cipher',
74 'Master_SSL_Key',
75 'Seconds_Behind_Master',
77 /**
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 $serverid = time();
95 // check which replication is available and set $server_{master/slave}_status and assign values
96 foreach ($replication_types as $type) {
97 if (count(${"server_{$type}_replication"}) > 0)
98 ${"server_{$type}_status"} = true;
99 else
100 ${"server_{$type}_status"} = false;
102 if (${"server_{$type}_status"}) {
103 if ($type == "master") {
104 ${"server_{$type}_Do_DB"} = explode (",", $server_master_replication[0]["Binlog_Do_DB"]);
105 ${"server_{$type}_Ignore_DB"} = explode (",", $server_master_replication[0]["Binlog_Ignore_DB"]);
106 } elseif ($type == "slave") {
107 ${"server_{$type}_Do_DB"} = explode (",", $server_slave_replication[0]["Replicate_Do_DB"]);
108 ${"server_{$type}_Ignore_DB"} = explode (",", $server_slave_replication[0]["Replicate_Ignore_DB"]);
110 ${"server_{$type}_Do_Table"} = explode (",", $server_slave_replication[0]["Replicate_Do_Table"]);
111 ${"server_{$type}_Ignore_Table"} = explode (",", $server_slave_replication[0]["Replicate_Ignore_Table"]);
113 ${"server_{$type}_Wild_Do_Table"} = explode (",", $server_slave_replication[0]["Replicate_Wild_Do_Table"]);
114 ${"server_{$type}_Wild_Ignore_Table"} = explode (",", $server_slave_replication[0]["Replicate_Wild_Ignore_Table"]);
121 * @param $string -
122 * @param $table -
123 * @return
125 function PMA_replication_strout($string, $table = false) {
126 $list = explode(".", $string);
128 return $list[(int)$table];
131 * @param String $action - possible values: START or STOP
132 * @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
133 * @param mixed $link - mysql link
135 * @return mixed output of PMA_DBI_try_query
137 function PMA_replication_slave_control ($action, $control = null, $link = null)
139 $action = strtoupper($action);
140 $control = strtoupper($control);
143 if ($action != "START" && $action != "STOP") {
144 return -1;
146 if ($control != "SQL_THREAD" && $control != "IO_THREAD" && $control != null) {
147 return -1;
150 return PMA_DBI_try_query($action." SLAVE ".$control.";", $link);
153 * @param String $user - replication user on master
154 * @param String $password - password for the user
155 * @param String $host - master's hostname or IP
156 * @param int $port - port, where mysql is running
157 * @param array $pos - position of mysql replication, array should contain fields File and Position
158 * @param boolean $stop - shall we stop slave?
159 * @param boolean $start - shall we start slave?
160 * @param mixed $link - mysql link
162 * @return output of CHANGE MASTER mysql command
164 function PMA_replication_slave_change_master($user, $password, $host, $port, $pos, $stop = true, $start = true, $link = null)
166 if ($stop) {
167 PMA_replication_slave_control("STOP", null, $link);
170 $out = PMA_DBI_try_query('CHANGE MASTER TO '.
171 'MASTER_HOST=\''.$host.'\','.
172 'MASTER_PORT='.($port*1).','.
173 'MASTER_USER=\''.$user.'\','.
174 'MASTER_PASSWORD=\''.$password.'\','.
175 'MASTER_LOG_FILE=\''.$pos["File"].'\','.
176 'MASTER_LOG_POS='.$pos["Position"].';', $link);
178 if ($start) {
179 PMA_replication_slave_control("START", null, $link);
182 return $out;
186 * This function provides connection to remote mysql server
188 * @param String $user - mysql username
189 * @param String $password - password for the user
190 * @param String $host - mysql server's hostname or IP
191 * @param int $port - mysql remote port
192 * @param String $socket - path to unix socket
194 * @return mixed $link mysql link on success
196 function PMA_replication_connect_to_master($user, $password, $host = null, $port = null, $socket = null)
198 $server = array();
199 $server["host"] = $host;
200 $server["port"] = $port;
201 $server["socket"] = $socket;
203 return PMA_DBI_connect($user, $password, false, $server);
206 * @param $link - mysql link
208 * @return array - containing File and Position in MySQL replication on master server, useful for PMA_replication_slave_change_master
210 function PMA_replication_slave_bin_log_master($link = null)
212 $data = PMA_DBI_fetch_result('SHOW MASTER STATUS', null, null, $link);
213 $output = array();
215 if (!empty($data)) {
216 $output["File"] = $data[0]["File"];
217 $output["Position"] = $data[0]["Position"];
219 return $output;
223 * Get list of replicated databases on master server
225 * @param mixed mysql link
227 * @return array array of replicated databases
230 function PMA_replication_master_replicated_dbs($link = null)
232 $data = PMA_DBI_fetch_result('SHOW MASTER STATUS', null, null, $link); // let's find out, which databases are replicated
234 $do_db = array();
235 $ignore_db = array();
237 if (!empty($data[0]['Binlog_Do_DB'])) {
238 $do_db = explode(',', $data[0]['Binlog_Do_DB']);
240 if (!empty($data[0]['Binlog_Ignore_DB'])) {
241 $ignore_db = explode(',', $data[0]['Binlog_Ignore_DB']);
244 $tmp_alldbs = PMA_DBI_query('SHOW DATABASES;', $link);
245 while ($tmp_row = PMA_DBI_fetch_row($tmp_alldbs)) {
246 if ($tmp_row[0] == 'information_schema')
247 continue;
248 if (count($do_db) == 0) {
249 if (array_search($tmp_row[0], $ignore_db) !== false) {
250 continue;
252 $dblist[] = $tmp_row[0];
254 } else {
255 if (array_search($tmp_row[0], $do_db) !== false) {
256 $dblist[] = $tmp_row[0];
259 } // end while
261 return $link;
264 * This function provides synchronization of structure and data between two mysql servers.
265 * TODO: improve code sharing between the function and synchronization
267 * @param String $db - name of database, which should be synchronized
268 * @param mixed $src_link - link of source server, note: if the server is current PMA server, use null
269 * @param mixed $trg_link - link of target server, note: if the server is current PMA server, use null
270 * @param boolean $data - if true, then data will be copied as well
273 function PMA_replication_synchronize_db($db, $src_link, $trg_link, $data = true)
275 $src_db = $trg_db = $db;
277 $src_connection = PMA_DBI_select_db($src_db, $src_link);
278 $trg_connection = PMA_DBI_select_db($trg_db, $trg_link);
280 $src_tables = PMA_DBI_get_tables($src_db, $src_link);
281 $source_tables_num = sizeof($src_tables);
283 $trg_tables = PMA_DBI_get_tables($trg_db, $trg_link);
284 $target_tables_num = sizeof($trg_tables);
287 * initializing arrays to save table names
289 $unmatched_num_src = 0;
290 $source_tables_uncommon = array();
291 $unmatched_num_trg = 0;
292 $target_tables_uncommon = array();
293 $matching_tables = array();
294 $matching_tables_num = 0;
297 * Criterion for matching tables is just their names.
298 * Finding the uncommon tables for the source database
299 * BY comparing the matching tables with all the tables in the source database
301 PMA_getMatchingTables($trg_tables, $src_tables, $matching_tables, $source_tables_uncommon);
304 * Finding the uncommon tables for the target database
305 * BY comparing the matching tables with all the tables in the target database
307 PMA_getNonMatchingTargetTables($trg_tables, $matching_tables, $target_tables_uncommon);
311 * ********************************************* Comparing Data In the Matching Tables ******************************************
312 * *********************It is assumed that the matching tables are structurally and typely exactly the same *********************
314 $fields_num = array();
315 $matching_tables_fields = array();
316 $matching_tables_keys = array();
317 $insert_array = array(array(array()));
318 $update_array = array(array(array()));
319 $delete_array = array();
320 $row_count = array();
321 $uncommon_tables_fields = array();
322 $matching_tables_num = sizeof($matching_tables);
324 for ($i=0; $i< sizeof($matching_tables); $i++)
326 PMA_dataDiffInTables($src_db, $trg_db, $src_link, $trg_link, $matching_tables, $matching_tables_fields, $update_array, $insert_array,
327 $delete_array, $fields_num, $i, $matching_tables_keys);
329 for ($j=0; $j< sizeof($source_tables_uncommon); $j++)
331 PMA_dataDiffInUncommonTables($source_tables_uncommon, $src_db, $src_link, $j, $row_count);
335 * INTEGRATION OF STRUCTURE DIFFERENCE CODE
338 $source_columns = array();
339 $target_columns = array();
340 $alter_str_array = array(array());
341 $add_column_array = array(array());
342 $uncommon_columns = array();
343 $target_tables_keys = array();
344 $source_indexes = array();
345 $target_indexes = array();
346 $add_indexes_array = array();
347 $remove_indexes_array = array();
348 $criteria = array('Field', 'Type', 'Null', 'Collation', 'Key', 'Default', 'Comment');
350 for ($counter = 0; $counter < $matching_tables_num; $counter++)
352 // PMA_structureDiffInTables($src_db, $trg_db, $src_link, $trg_link, $matching_tables, $source_columns,
353 // $target_columns, $alter_str_array, $add_column_array, $uncommon_columns, $criteria, $matching_tables_keys,
354 // $target_tables_keys, $matching_tables_fields, $counter);
356 // PMA_indexesDiffInTables($src_db, $trg_db, $src_link, $trg_link, $matching_tables, $source_indexes, $target_indexes,
357 // $add_indexes_array, $remove_indexes_array, $counter);
358 PMA_structureDiffInTables($src_db, $trg_db, $src_link, $trg_link, $matching_tables, $source_columns,
359 $target_columns, $alter_str_array, $add_column_array, $uncommon_columns, $criteria, $target_tables_keys, $counter);
361 PMA_indexesDiffInTables($src_db, $trg_db, $src_link, $trg_link, $matching_tables, $source_indexes, $target_indexes,
362 $add_indexes_array, $alter_indexes_array,$remove_indexes_array, $counter);
365 $matching_table_data_diff = array();
366 $matching_table_structure_diff = array();
367 $uncommon_table_structure_diff = array();
368 $uncommon_table_data_diff = array();
369 $uncommon_tables = $source_tables_uncommon;
372 * Generating Create Table query for all the non-matching tables present in Source but not in Target and populating tables.
374 for($q = 0; $q < sizeof($source_tables_uncommon); $q++)
376 if (isset($uncommon_tables[$q])) {
377 PMA_createTargetTables($src_db, $trg_db, $src_link, $trg_link, $source_tables_uncommon, $q, $uncommon_tables_fields, false);
379 if (isset($row_count[$q]) && $data) {
380 PMA_populateTargetTables($src_db, $trg_db, $src_link, $trg_link, $source_tables_uncommon, $q, $uncommon_tables_fields, false);