Upgraded phpmyadmin to 4.0.4 (All Languages) - No modifications yet
[openemr.git] / phpmyadmin / libraries / replication.inc.php
blob467b3d5416fca6b685f32a56ccff54fd795b8ffe
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Replication helpers
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.
83 // To e.g. highlight 'Last_Error'
85 $slave_variables_alerts = array(
86 'Slave_IO_Running' => 'No',
87 'Slave_SQL_Running' => 'No',
89 $slave_variables_oks = array(
90 'Slave_IO_Running' => 'Yes',
91 'Slave_SQL_Running' => 'Yes',
94 // check which replication is available and
95 // set $server_{master/slave}_status and assign values
97 // replication info is more easily passed to functions
99 * @todo use $replication_info everywhere instead of the generated variable names
101 $replication_info = array();
103 foreach ($replication_types as $type) {
104 if (count(${"server_{$type}_replication"}) > 0) {
105 ${"server_{$type}_status"} = true;
106 $replication_info[$type]['status'] = true;
107 } else {
108 ${"server_{$type}_status"} = false;
109 $replication_info[$type]['status'] = false;
111 if (${"server_{$type}_status"}) {
112 if ($type == "master") {
113 ${"server_{$type}_Do_DB"} = explode(",", $server_master_replication[0]["Binlog_Do_DB"]);
114 $replication_info[$type]['Do_DB'] = ${"server_{$type}_Do_DB"};
116 ${"server_{$type}_Ignore_DB"} = explode(",", $server_master_replication[0]["Binlog_Ignore_DB"]);
117 $replication_info[$type]['Ignore_DB'] = ${"server_{$type}_Ignore_DB"};
118 } elseif ($type == "slave") {
119 ${"server_{$type}_Do_DB"} = explode(",", $server_slave_replication[0]["Replicate_Do_DB"]);
120 $replication_info[$type]['Do_DB'] = ${"server_{$type}_Do_DB"};
122 ${"server_{$type}_Ignore_DB"} = explode(",", $server_slave_replication[0]["Replicate_Ignore_DB"]);
123 $replication_info[$type]['Ignore_DB'] = ${"server_{$type}_Ignore_DB"};
125 ${"server_{$type}_Do_Table"} = explode(",", $server_slave_replication[0]["Replicate_Do_Table"]);
126 $replication_info[$type]['Do_Table'] = ${"server_{$type}_Do_Table"};
128 ${"server_{$type}_Ignore_Table"} = explode(",", $server_slave_replication[0]["Replicate_Ignore_Table"]);
129 $replication_info[$type]['Ignore_Table'] = ${"server_{$type}_Ignore_Table"};
131 ${"server_{$type}_Wild_Do_Table"} = explode(",", $server_slave_replication[0]["Replicate_Wild_Do_Table"]);
132 $replication_info[$type]['Wild_Do_Table'] = ${"server_{$type}_Wild_Do_Table"};
134 ${"server_{$type}_Wild_Ignore_Table"} = explode(",", $server_slave_replication[0]["Replicate_Wild_Ignore_Table"]);
135 $replication_info[$type]['Wild_Ignore_Table'] = ${"server_{$type}_Wild_Ignore_Table"};
141 * Extracts database or table name from string
143 * @param string $string contains "dbname.tablename"
144 * @param string $what what to extract (db|table)
146 * @return $string the extracted part
148 function PMA_extract_db_or_table($string, $what = 'db')
150 $list = explode(".", $string);
151 if ('db' == $what) {
152 return $list[0];
153 } else {
154 return $list[1];
159 * Configures replication slave
161 * @param string $action possible values: START or STOP
162 * @param string $control default: null, possible values: SQL_THREAD or IO_THREAD or null.
163 * If it is set to null, it controls both SQL_THREAD and IO_THREAD
164 * @param mixed $link mysql link
166 * @return mixed output of PMA_DBI_try_query
168 function PMA_replication_slave_control($action, $control = null, $link = null)
170 $action = strtoupper($action);
171 $control = strtoupper($control);
173 if ($action != "START" && $action != "STOP") {
174 return -1;
176 if ($control != "SQL_THREAD" && $control != "IO_THREAD" && $control != null) {
177 return -1;
180 return PMA_DBI_try_query($action . " SLAVE " . $control . ";", $link);
184 * Changes master for replication slave
186 * @param string $user replication user on master
187 * @param string $password password for the user
188 * @param string $host master's hostname or IP
189 * @param int $port port, where mysql is running
190 * @param array $pos position of mysql replication,
191 * array should contain fields File and Position
192 * @param bool $stop shall we stop slave?
193 * @param bool $start shall we start slave?
194 * @param mixed $link mysql link
196 * @return output of CHANGE MASTER mysql command
198 function PMA_replication_slave_change_master($user, $password, $host, $port,
199 $pos, $stop = true, $start = true, $link = null
201 if ($stop) {
202 PMA_replication_slave_control("STOP", null, $link);
205 $out = PMA_DBI_try_query(
206 'CHANGE MASTER TO ' .
207 'MASTER_HOST=\'' . $host . '\',' .
208 'MASTER_PORT=' . ($port * 1) . ',' .
209 'MASTER_USER=\'' . $user . '\',' .
210 'MASTER_PASSWORD=\'' . $password . '\',' .
211 'MASTER_LOG_FILE=\'' . $pos["File"] . '\',' .
212 'MASTER_LOG_POS=' . $pos["Position"] . ';', $link
215 if ($start) {
216 PMA_replication_slave_control("START", null, $link);
219 return $out;
223 * This function provides connection to remote mysql server
225 * @param string $user mysql username
226 * @param string $password password for the user
227 * @param string $host mysql server's hostname or IP
228 * @param int $port mysql remote port
229 * @param string $socket path to unix socket
231 * @return mixed $link mysql link on success
233 function PMA_replication_connect_to_master($user, $password, $host = null, $port = null, $socket = null)
235 $server = array();
236 $server["host"] = $host;
237 $server["port"] = $port;
238 $server["socket"] = $socket;
240 // 5th parameter set to true means that it's an auxiliary connection
241 // and we must not go back to login page if it fails
242 return PMA_DBI_connect($user, $password, false, $server, true);
245 * Fetches position and file of current binary log on master
247 * @param mixed $link mysql link
249 * @return array an array containing File and Position in MySQL replication
250 * on master server, useful for PMA_replication_slave_change_master
252 function PMA_replication_slave_bin_log_master($link = null)
254 $data = PMA_DBI_fetch_result('SHOW MASTER STATUS', null, null, $link);
255 $output = array();
257 if (! empty($data)) {
258 $output["File"] = $data[0]["File"];
259 $output["Position"] = $data[0]["Position"];
261 return $output;
265 * Get list of replicated databases on master server
267 * @param mixed $link mysql link
269 * @return array array of replicated databases
272 function PMA_replication_master_replicated_dbs($link = null)
274 // let's find out, which databases are replicated
275 $data = PMA_DBI_fetch_result('SHOW MASTER STATUS', null, null, $link);
277 $do_db = array();
278 $ignore_db = array();
280 if (! empty($data[0]['Binlog_Do_DB'])) {
281 $do_db = explode(',', $data[0]['Binlog_Do_DB']);
283 if (! empty($data[0]['Binlog_Ignore_DB'])) {
284 $ignore_db = explode(',', $data[0]['Binlog_Ignore_DB']);
287 $tmp_alldbs = PMA_DBI_query('SHOW DATABASES;', $link);
288 while ($tmp_row = PMA_DBI_fetch_row($tmp_alldbs)) {
289 if (PMA_is_system_schema($tmp_row[0])) {
290 continue;
292 if (count($do_db) == 0) {
293 if (array_search($tmp_row[0], $ignore_db) !== false) {
294 continue;
296 $dblist[] = $tmp_row[0];
298 } else {
299 if (array_search($tmp_row[0], $do_db) !== false) {
300 $dblist[] = $tmp_row[0];
303 } // end while
305 return $link;