2 /* vim: set expandtab sw=4 ts=4 sts=4: */
10 use PhpMyAdmin\DatabaseInterface
;
12 if (! defined('PHPMYADMIN')) {
17 * get master replication from server
19 $server_master_replication = $GLOBALS['dbi']->fetchResult('SHOW MASTER STATUS');
22 * set selected master server
24 if (! empty($_REQUEST['master_connection'])) {
26 * check for multi-master replication functionality
28 $server_slave_multi_replication = $GLOBALS['dbi']->fetchResult(
29 'SHOW ALL SLAVES STATUS'
31 if ($server_slave_multi_replication) {
32 $GLOBALS['dbi']->query(
33 "SET @@default_master_connection = '"
34 . $GLOBALS['dbi']->escapeString(
35 $_REQUEST['master_connection']
38 $GLOBALS['url_params']['master_connection'] = $_REQUEST['master_connection'];
43 * get slave replication from server
45 $server_slave_replication = $GLOBALS['dbi']->fetchResult('SHOW SLAVE STATUS');
50 $replication_types = array('master', 'slave');
54 * define variables for master status
56 $master_variables = array(
64 * Define variables for slave status
66 $slave_variables = array(
73 'Read_Master_Log_Pos',
76 'Relay_Master_Log_File',
80 'Replicate_Ignore_DB',
82 'Replicate_Ignore_Table',
83 'Replicate_Wild_Do_Table',
84 'Replicate_Wild_Ignore_Table',
88 'Exec_Master_Log_Pos',
99 'Seconds_Behind_Master',
102 * define important variables, which need to be watched for
103 * correct running of replication in slave mode
105 * @usedby PMA_getHtmlForReplicationStatusTable()
107 // TODO change to regexp or something, to allow for negative match.
108 // To e.g. highlight 'Last_Error'
110 $slave_variables_alerts = array(
111 'Slave_IO_Running' => 'No',
112 'Slave_SQL_Running' => 'No',
114 $slave_variables_oks = array(
115 'Slave_IO_Running' => 'Yes',
116 'Slave_SQL_Running' => 'Yes',
119 // check which replication is available and
120 // set $server_{master/slave}_status and assign values
122 // replication info is more easily passed to functions
123 $GLOBALS['replication_info'] = array();
125 foreach ($replication_types as $type) {
126 if (count($
{"server_{$type}_replication"}) > 0) {
127 $GLOBALS['replication_info'][$type]['status'] = true;
129 $GLOBALS['replication_info'][$type]['status'] = false;
131 if ($GLOBALS['replication_info'][$type]['status']) {
132 if ($type == "master") {
133 PMA_fillReplicationInfo(
134 $type, 'Do_DB', $server_master_replication[0],
138 PMA_fillReplicationInfo(
139 $type, 'Ignore_DB', $server_master_replication[0],
142 } elseif ($type == "slave") {
143 PMA_fillReplicationInfo(
144 $type, 'Do_DB', $server_slave_replication[0],
148 PMA_fillReplicationInfo(
149 $type, 'Ignore_DB', $server_slave_replication[0],
150 'Replicate_Ignore_DB'
153 PMA_fillReplicationInfo(
154 $type, 'Do_Table', $server_slave_replication[0],
158 PMA_fillReplicationInfo(
159 $type, 'Ignore_Table', $server_slave_replication[0],
160 'Replicate_Ignore_Table'
163 PMA_fillReplicationInfo(
164 $type, 'Wild_Do_Table', $server_slave_replication[0],
165 'Replicate_Wild_Do_Table'
168 PMA_fillReplicationInfo(
169 $type, 'Wild_Ignore_Table', $server_slave_replication[0],
170 'Replicate_Wild_Ignore_Table'
177 * Fill global replication_info variable.
179 * @param string $type Type: master, slave
180 * @param string $replicationInfoKey Key in replication_info variable
181 * @param array $mysqlInfo MySQL data about replication
182 * @param string $mysqlKey MySQL key
186 function PMA_fillReplicationInfo(
187 $type, $replicationInfoKey, $mysqlInfo, $mysqlKey
189 $GLOBALS['replication_info'][$type][$replicationInfoKey]
190 = empty($mysqlInfo[$mysqlKey])
194 $mysqlInfo[$mysqlKey]
197 return $GLOBALS['replication_info'][$type][$replicationInfoKey];
201 * Extracts database or table name from string
203 * @param string $string contains "dbname.tablename"
204 * @param string $what what to extract (db|table)
206 * @return string the extracted part
208 function PMA_extractDbOrTable($string, $what = 'db')
210 $list = explode(".", $string);
219 * Configures replication slave
221 * @param string $action possible values: START or STOP
222 * @param string $control default: null,
223 * possible values: SQL_THREAD or IO_THREAD or null.
224 * If it is set to null, it controls both
225 * SQL_THREAD and IO_THREAD
226 * @param mixed $link mysql link
228 * @return mixed output of DatabaseInterface::tryQuery
230 function PMA_Replication_Slave_control($action, $control = null, $link = null)
232 $action = mb_strtoupper($action);
233 $control = mb_strtoupper($control);
235 if ($action != "START" && $action != "STOP") {
238 if ($control != "SQL_THREAD" && $control != "IO_THREAD" && $control != null) {
242 return $GLOBALS['dbi']->tryQuery($action . " SLAVE " . $control . ";", $link);
246 * Changes master for replication slave
248 * @param string $user replication user on master
249 * @param string $password password for the user
250 * @param string $host master's hostname or IP
251 * @param int $port port, where mysql is running
252 * @param array $pos position of mysql replication,
253 * array should contain fields File and Position
254 * @param bool $stop shall we stop slave?
255 * @param bool $start shall we start slave?
256 * @param mixed $link mysql link
258 * @return string output of CHANGE MASTER mysql command
260 function PMA_Replication_Slave_changeMaster($user, $password, $host, $port,
261 $pos, $stop = true, $start = true, $link = null
264 PMA_Replication_Slave_control("STOP", null, $link);
267 $out = $GLOBALS['dbi']->tryQuery(
268 'CHANGE MASTER TO ' .
269 'MASTER_HOST=\'' . $host . '\',' .
270 'MASTER_PORT=' . ($port * 1) . ',' .
271 'MASTER_USER=\'' . $user . '\',' .
272 'MASTER_PASSWORD=\'' . $password . '\',' .
273 'MASTER_LOG_FILE=\'' . $pos["File"] . '\',' .
274 'MASTER_LOG_POS=' . $pos["Position"] . ';', $link
278 PMA_Replication_Slave_control("START", null, $link);
285 * This function provides connection to remote mysql server
287 * @param string $user mysql username
288 * @param string $password password for the user
289 * @param string $host mysql server's hostname or IP
290 * @param int $port mysql remote port
291 * @param string $socket path to unix socket
293 * @return mixed $link mysql link on success
295 function PMA_Replication_connectToMaster(
296 $user, $password, $host = null, $port = null, $socket = null
299 $server['user'] = $user;
300 $server['password'] = $password;
301 $server["host"] = Core
::sanitizeMySQLHost($host);
302 $server["port"] = $port;
303 $server["socket"] = $socket;
305 // 5th parameter set to true means that it's an auxiliary connection
306 // and we must not go back to login page if it fails
307 return $GLOBALS['dbi']->connect(databaseinterface
::CONNECT_AUXILIARY
, $server);
310 * Fetches position and file of current binary log on master
312 * @param mixed $link mysql link
314 * @return array an array containing File and Position in MySQL replication
315 * on master server, useful for PMA_Replication_Slave_changeMaster
317 function PMA_Replication_Slave_binLogMaster($link = null)
319 $data = $GLOBALS['dbi']->fetchResult('SHOW MASTER STATUS', null, null, $link);
322 if (! empty($data)) {
323 $output["File"] = $data[0]["File"];
324 $output["Position"] = $data[0]["Position"];