Merge remote-tracking branch 'origin/master'
[phpmyadmin.git] / libraries / replication.inc.php
blobf4a4cd4aa417cc20f432ab91c43420c715b79e08
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 = $GLOBALS['dbi']->fetchResult('SHOW MASTER STATUS');
18 /**
19 * set selected master server
21 if (! empty($_REQUEST['master_connection'])) {
22 /**
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']
33 ) . "'"
35 $GLOBALS['url_params']['master_connection'] = $_REQUEST['master_connection'];
39 /**
40 * get slave replication from server
42 $server_slave_replication = $GLOBALS['dbi']->fetchResult('SHOW SLAVE STATUS');
44 /**
45 * replication types
47 $replication_types = array('master', 'slave');
50 /**
51 * define variables for master status
53 $master_variables = array(
54 'File',
55 'Position',
56 'Binlog_Do_DB',
57 'Binlog_Ignore_DB',
60 /**
61 * Define variables for slave status
63 $slave_variables = array(
64 'Slave_IO_State',
65 'Master_Host',
66 'Master_User',
67 'Master_Port',
68 'Connect_Retry',
69 'Master_Log_File',
70 'Read_Master_Log_Pos',
71 'Relay_Log_File',
72 'Relay_Log_Pos',
73 'Relay_Master_Log_File',
74 'Slave_IO_Running',
75 'Slave_SQL_Running',
76 'Replicate_Do_DB',
77 'Replicate_Ignore_DB',
78 'Replicate_Do_Table',
79 'Replicate_Ignore_Table',
80 'Replicate_Wild_Do_Table',
81 'Replicate_Wild_Ignore_Table',
82 'Last_Errno',
83 'Last_Error',
84 'Skip_Counter',
85 'Exec_Master_Log_Pos',
86 'Relay_Log_Space',
87 'Until_Condition',
88 'Until_Log_File',
89 'Until_Log_Pos',
90 'Master_SSL_Allowed',
91 'Master_SSL_CA_File',
92 'Master_SSL_CA_Path',
93 'Master_SSL_Cert',
94 'Master_SSL_Cipher',
95 'Master_SSL_Key',
96 'Seconds_Behind_Master',
98 /**
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;
125 } else {
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],
132 'Binlog_Do_DB'
135 PMA_fillReplicationInfo(
136 $type, 'Ignore_DB', $server_master_replication[0],
137 'Binlog_Ignore_DB'
139 } elseif ($type == "slave") {
140 PMA_fillReplicationInfo(
141 $type, 'Do_DB', $server_slave_replication[0],
142 'Replicate_Do_DB'
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],
152 'Replicate_Do_Table'
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
181 * @return array
183 function PMA_fillReplicationInfo(
184 $type, $replicationInfoKey, $mysqlInfo, $mysqlKey
186 $GLOBALS['replication_info'][$type][$replicationInfoKey]
187 = empty($mysqlInfo[$mysqlKey])
188 ? array()
189 : explode(
190 ",",
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);
208 if ('db' == $what) {
209 return $list[0];
210 } else {
211 return $list[1];
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") {
233 return -1;
235 if ($control != "SQL_THREAD" && $control != "IO_THREAD" && $control != null) {
236 return -1;
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
260 if ($stop) {
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
274 if ($start) {
275 PMA_Replication_Slave_control("START", null, $link);
278 return $out;
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
295 $server = array();
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);
315 $output = array();
317 if (! empty($data)) {
318 $output["File"] = $data[0]["File"];
319 $output["Position"] = $data[0]["Position"];
321 return $output;