2 /* vim: set expandtab sw=4 ts=4 sts=4: */
9 if (! defined('PHPMYADMIN')) {
14 * get master replication from server
16 $server_master_replication = $GLOBALS['dbi']->fetchResult('SHOW MASTER STATUS');
19 * set selected master server
21 if (! empty($_REQUEST['master_connection'])) {
23 * check for multi-master replication functionality
25 $server_slave_multi_replication = $GLOBALS['dbi']->fetchResult(
26 'SHOW ALL SLAVES STATUS'
28 if ($server_slave_multi_replication) {
29 $GLOBALS['dbi']->query(
30 "SET @@default_master_connection = '"
31 . PMA\libraries\Util
::sqlAddSlashes(
32 $_REQUEST['master_connection']
35 $GLOBALS['url_params']['master_connection'] = $_REQUEST['master_connection'];
40 * get slave replication from server
42 $server_slave_replication = $GLOBALS['dbi']->fetchResult('SHOW SLAVE STATUS');
47 $replication_types = array('master', 'slave');
51 * define variables for master status
53 $master_variables = array(
61 * Define variables for slave status
63 $slave_variables = array(
70 'Read_Master_Log_Pos',
73 'Relay_Master_Log_File',
77 'Replicate_Ignore_DB',
79 'Replicate_Ignore_Table',
80 'Replicate_Wild_Do_Table',
81 'Replicate_Wild_Ignore_Table',
85 'Exec_Master_Log_Pos',
96 'Seconds_Behind_Master',
99 * define important variables, which need to be watched for
100 * correct running of replication in slave mode
102 * @usedby PMA_getHtmlForReplicationStatusTable()
104 // TODO change to regexp or something, to allow for negative match.
105 // To e.g. highlight 'Last_Error'
107 $slave_variables_alerts = array(
108 'Slave_IO_Running' => 'No',
109 'Slave_SQL_Running' => 'No',
111 $slave_variables_oks = array(
112 'Slave_IO_Running' => 'Yes',
113 'Slave_SQL_Running' => 'Yes',
116 // check which replication is available and
117 // set $server_{master/slave}_status and assign values
119 // replication info is more easily passed to functions
120 $GLOBALS['replication_info'] = array();
122 foreach ($replication_types as $type) {
123 if (count($
{"server_{$type}_replication"}) > 0) {
124 $GLOBALS['replication_info'][$type]['status'] = true;
126 $GLOBALS['replication_info'][$type]['status'] = false;
128 if ($GLOBALS['replication_info'][$type]['status']) {
129 if ($type == "master") {
130 PMA_fillReplicationInfo(
131 $type, 'Do_DB', $server_master_replication[0],
135 PMA_fillReplicationInfo(
136 $type, 'Ignore_DB', $server_master_replication[0],
139 } elseif ($type == "slave") {
140 PMA_fillReplicationInfo(
141 $type, 'Do_DB', $server_slave_replication[0],
145 PMA_fillReplicationInfo(
146 $type, 'Ignore_DB', $server_slave_replication[0],
147 'Replicate_Ignore_DB'
150 PMA_fillReplicationInfo(
151 $type, 'Do_Table', $server_slave_replication[0],
155 PMA_fillReplicationInfo(
156 $type, 'Ignore_Table', $server_slave_replication[0],
157 'Replicate_Ignore_Table'
160 PMA_fillReplicationInfo(
161 $type, 'Wild_Do_Table', $server_slave_replication[0],
162 'Replicate_Wild_Do_Table'
165 PMA_fillReplicationInfo(
166 $type, 'Wild_Ignore_Table', $server_slave_replication[0],
167 'Replicate_Wild_Ignore_Table'
174 * Fill global replication_info variable.
176 * @param string $type Type: master, slave
177 * @param string $replicationInfoKey Key in replication_info variable
178 * @param array $mysqlInfo MySQL data about replication
179 * @param string $mysqlKey MySQL key
183 function PMA_fillReplicationInfo(
184 $type, $replicationInfoKey, $mysqlInfo, $mysqlKey
186 $GLOBALS['replication_info'][$type][$replicationInfoKey]
187 = empty($mysqlInfo[$mysqlKey])
191 $mysqlInfo[$mysqlKey]
194 return $GLOBALS['replication_info'][$type][$replicationInfoKey];
198 * Extracts database or table name from string
200 * @param string $string contains "dbname.tablename"
201 * @param string $what what to extract (db|table)
203 * @return string the extracted part
205 function PMA_extractDbOrTable($string, $what = 'db')
207 $list = explode(".", $string);
216 * Configures replication slave
218 * @param string $action possible values: START or STOP
219 * @param string $control default: null,
220 * possible values: SQL_THREAD or IO_THREAD or null.
221 * If it is set to null, it controls both
222 * SQL_THREAD and IO_THREAD
223 * @param mixed $link mysql link
225 * @return mixed output of DatabaseInterface::tryQuery
227 function PMA_Replication_Slave_control($action, $control = null, $link = null)
229 $action = mb_strtoupper($action);
230 $control = mb_strtoupper($control);
232 if ($action != "START" && $action != "STOP") {
235 if ($control != "SQL_THREAD" && $control != "IO_THREAD" && $control != null) {
239 return $GLOBALS['dbi']->tryQuery($action . " SLAVE " . $control . ";", $link);
243 * Changes master for replication slave
245 * @param string $user replication user on master
246 * @param string $password password for the user
247 * @param string $host master's hostname or IP
248 * @param int $port port, where mysql is running
249 * @param array $pos position of mysql replication,
250 * array should contain fields File and Position
251 * @param bool $stop shall we stop slave?
252 * @param bool $start shall we start slave?
253 * @param mixed $link mysql link
255 * @return string output of CHANGE MASTER mysql command
257 function PMA_Replication_Slave_changeMaster($user, $password, $host, $port,
258 $pos, $stop = true, $start = true, $link = null
261 PMA_Replication_Slave_control("STOP", null, $link);
264 $out = $GLOBALS['dbi']->tryQuery(
265 'CHANGE MASTER TO ' .
266 'MASTER_HOST=\'' . $host . '\',' .
267 'MASTER_PORT=' . ($port * 1) . ',' .
268 'MASTER_USER=\'' . $user . '\',' .
269 'MASTER_PASSWORD=\'' . $password . '\',' .
270 'MASTER_LOG_FILE=\'' . $pos["File"] . '\',' .
271 'MASTER_LOG_POS=' . $pos["Position"] . ';', $link
275 PMA_Replication_Slave_control("START", null, $link);
282 * This function provides connection to remote mysql server
284 * @param string $user mysql username
285 * @param string $password password for the user
286 * @param string $host mysql server's hostname or IP
287 * @param int $port mysql remote port
288 * @param string $socket path to unix socket
290 * @return mixed $link mysql link on success
292 function PMA_Replication_connectToMaster(
293 $user, $password, $host = null, $port = null, $socket = null
296 $server["host"] = $host;
297 $server["port"] = $port;
298 $server["socket"] = $socket;
300 // 5th parameter set to true means that it's an auxiliary connection
301 // and we must not go back to login page if it fails
302 return $GLOBALS['dbi']->connect($user, $password, false, $server, true);
305 * Fetches position and file of current binary log on master
307 * @param mixed $link mysql link
309 * @return array an array containing File and Position in MySQL replication
310 * on master server, useful for PMA_Replication_Slave_changeMaster
312 function PMA_Replication_Slave_binLogMaster($link = null)
314 $data = $GLOBALS['dbi']->fetchResult('SHOW MASTER STATUS', null, null, $link);
317 if (! empty($data)) {
318 $output["File"] = $data[0]["File"];
319 $output["Position"] = $data[0]["Position"];