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 = '" . PMA_Util
::sqlAddSlashes(
31 $_REQUEST['master_connection']
34 $GLOBALS['url_params']['master_connection'] = $_REQUEST['master_connection'];
39 * get slave replication from server
41 $server_slave_replication = $GLOBALS['dbi']->fetchResult('SHOW SLAVE STATUS');
46 $replication_types = array('master', 'slave');
50 * define variables for master status
52 $master_variables = array(
60 * Define variables for slave status
62 $slave_variables = array(
69 'Read_Master_Log_Pos',
72 'Relay_Master_Log_File',
76 'Replicate_Ignore_DB',
78 'Replicate_Ignore_Table',
79 'Replicate_Wild_Do_Table',
80 'Replicate_Wild_Ignore_Table',
84 'Exec_Master_Log_Pos',
95 'Seconds_Behind_Master',
98 * define important variables, which need to be watched for
99 * correct running of replication in slave mode
101 * @usedby PMA_getHtmlForReplicationStatusTable()
103 // TODO change to regexp or something, to allow for negative match.
104 // To e.g. highlight 'Last_Error'
106 $slave_variables_alerts = array(
107 'Slave_IO_Running' => 'No',
108 'Slave_SQL_Running' => 'No',
110 $slave_variables_oks = array(
111 'Slave_IO_Running' => 'Yes',
112 'Slave_SQL_Running' => 'Yes',
115 // check which replication is available and
116 // set $server_{master/slave}_status and assign values
118 // replication info is more easily passed to functions
119 $GLOBALS['replication_info'] = array();
121 foreach ($replication_types as $type) {
122 if (count($
{"server_{$type}_replication"}) > 0) {
123 $GLOBALS['replication_info'][$type]['status'] = true;
125 $GLOBALS['replication_info'][$type]['status'] = false;
127 if ($GLOBALS['replication_info'][$type]['status']) {
128 if ($type == "master") {
129 PMA_fillReplicationInfo(
130 $type, 'Do_DB', $server_master_replication[0],
134 PMA_fillReplicationInfo(
135 $type, 'Ignore_DB', $server_master_replication[0],
138 } elseif ($type == "slave") {
139 PMA_fillReplicationInfo(
140 $type, 'Do_DB', $server_slave_replication[0],
144 PMA_fillReplicationInfo(
145 $type, 'Ignore_DB', $server_slave_replication[0],
146 'Replicate_Ignore_DB'
149 PMA_fillReplicationInfo(
150 $type, 'Do_Table', $server_slave_replication[0],
154 PMA_fillReplicationInfo(
155 $type, 'Ignore_Table', $server_slave_replication[0],
156 'Replicate_Ignore_Table'
159 PMA_fillReplicationInfo(
160 $type, 'Wild_Do_Table', $server_slave_replication[0],
161 'Replicate_Wild_Do_Table'
164 PMA_fillReplicationInfo(
165 $type, 'Wild_Ignore_Table', $server_slave_replication[0],
166 'Replicate_Wild_Ignore_Table'
173 * Fill global replication_info variable.
175 * @param string $type Type: master, slave
176 * @param string $replicationInfoKey Key in replication_info variable
177 * @param array $mysqlInfo MySQL data about replication
178 * @param string $mysqlKey MySQL key
182 function PMA_fillReplicationInfo(
183 $type, $replicationInfoKey, $mysqlInfo, $mysqlKey
185 $GLOBALS['replication_info'][$type][$replicationInfoKey]
186 = empty($mysqlInfo[$mysqlKey])
190 $mysqlInfo[$mysqlKey]
193 return $GLOBALS['replication_info'][$type][$replicationInfoKey];
197 * Extracts database or table name from string
199 * @param string $string contains "dbname.tablename"
200 * @param string $what what to extract (db|table)
202 * @return string the extracted part
204 function PMA_extractDbOrTable($string, $what = 'db')
206 $list = explode(".", $string);
215 * Configures replication slave
217 * @param string $action possible values: START or STOP
218 * @param string $control default: null,
219 * possible values: SQL_THREAD or IO_THREAD or null.
220 * If it is set to null, it controls both
221 * SQL_THREAD and IO_THREAD
222 * @param mixed $link mysql link
224 * @return mixed output of DatabaseInterface::tryQuery
226 function PMA_Replication_Slave_control($action, $control = null, $link = null)
228 $action = /*overload*/mb_strtoupper($action);
229 $control = /*overload*/mb_strtoupper($control);
231 if ($action != "START" && $action != "STOP") {
234 if ($control != "SQL_THREAD" && $control != "IO_THREAD" && $control != null) {
238 return $GLOBALS['dbi']->tryQuery($action . " SLAVE " . $control . ";", $link);
242 * Changes master for replication slave
244 * @param string $user replication user on master
245 * @param string $password password for the user
246 * @param string $host master's hostname or IP
247 * @param int $port port, where mysql is running
248 * @param array $pos position of mysql replication,
249 * array should contain fields File and Position
250 * @param bool $stop shall we stop slave?
251 * @param bool $start shall we start slave?
252 * @param mixed $link mysql link
254 * @return string output of CHANGE MASTER mysql command
256 function PMA_Replication_Slave_changeMaster($user, $password, $host, $port,
257 $pos, $stop = true, $start = true, $link = null
260 PMA_Replication_Slave_control("STOP", null, $link);
263 $out = $GLOBALS['dbi']->tryQuery(
264 'CHANGE MASTER TO ' .
265 'MASTER_HOST=\'' . $host . '\',' .
266 'MASTER_PORT=' . ($port * 1) . ',' .
267 'MASTER_USER=\'' . $user . '\',' .
268 'MASTER_PASSWORD=\'' . $password . '\',' .
269 'MASTER_LOG_FILE=\'' . $pos["File"] . '\',' .
270 'MASTER_LOG_POS=' . $pos["Position"] . ';', $link
274 PMA_Replication_Slave_control("START", null, $link);
281 * This function provides connection to remote mysql server
283 * @param string $user mysql username
284 * @param string $password password for the user
285 * @param string $host mysql server's hostname or IP
286 * @param int $port mysql remote port
287 * @param string $socket path to unix socket
289 * @return mixed $link mysql link on success
291 function PMA_Replication_connectToMaster(
292 $user, $password, $host = null, $port = null, $socket = null
295 $server["host"] = $host;
296 $server["port"] = $port;
297 $server["socket"] = $socket;
299 // 5th parameter set to true means that it's an auxiliary connection
300 // and we must not go back to login page if it fails
301 return $GLOBALS['dbi']->connect($user, $password, false, $server, true);
304 * Fetches position and file of current binary log on master
306 * @param mixed $link mysql link
308 * @return array an array containing File and Position in MySQL replication
309 * on master server, useful for PMA_Replication_Slave_changeMaster
311 function PMA_Replication_Slave_binLogMaster($link = null)
313 $data = $GLOBALS['dbi']->fetchResult('SHOW MASTER STATUS', null, null, $link);
316 if (! empty($data)) {
317 $output["File"] = $data[0]["File"];
318 $output["Position"] = $data[0]["Position"];