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.
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;
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);
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") {
176 if ($control != "SQL_THREAD" && $control != "IO_THREAD" && $control != null) {
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
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
216 PMA_replication_slave_control("START", null, $link);
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)
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);
257 if (! empty($data)) {
258 $output["File"] = $data[0]["File"];
259 $output["Position"] = $data[0]["Position"];
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);
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])) {
292 if (count($do_db) == 0) {
293 if (array_search($tmp_row[0], $ignore_db) !== false) {
296 $dblist[] = $tmp_row[0];
299 if (array_search($tmp_row[0], $do_db) !== false) {
300 $dblist[] = $tmp_row[0];