Merge branch 'QA_4_4' into QA_4_5
[phpmyadmin.git] / libraries / replication.inc.php
blob2e0ecd71f0b2d4e24882f6288ec10335557be0e8
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 = '" . PMA_Util::sqlAddSlashes(
31 $_REQUEST['master_connection']
32 ) . "'"
34 $GLOBALS['url_params']['master_connection'] = $_REQUEST['master_connection'];
38 /**
39 * get slave replication from server
41 $server_slave_replication = $GLOBALS['dbi']->fetchResult('SHOW SLAVE STATUS');
43 /**
44 * replication types
46 $replication_types = array('master', 'slave');
49 /**
50 * define variables for master status
52 $master_variables = array(
53 'File',
54 'Position',
55 'Binlog_Do_DB',
56 'Binlog_Ignore_DB',
59 /**
60 * Define variables for slave status
62 $slave_variables = array(
63 'Slave_IO_State',
64 'Master_Host',
65 'Master_User',
66 'Master_Port',
67 'Connect_Retry',
68 'Master_Log_File',
69 'Read_Master_Log_Pos',
70 'Relay_Log_File',
71 'Relay_Log_Pos',
72 'Relay_Master_Log_File',
73 'Slave_IO_Running',
74 'Slave_SQL_Running',
75 'Replicate_Do_DB',
76 'Replicate_Ignore_DB',
77 'Replicate_Do_Table',
78 'Replicate_Ignore_Table',
79 'Replicate_Wild_Do_Table',
80 'Replicate_Wild_Ignore_Table',
81 'Last_Errno',
82 'Last_Error',
83 'Skip_Counter',
84 'Exec_Master_Log_Pos',
85 'Relay_Log_Space',
86 'Until_Condition',
87 'Until_Log_File',
88 'Until_Log_Pos',
89 'Master_SSL_Allowed',
90 'Master_SSL_CA_File',
91 'Master_SSL_CA_Path',
92 'Master_SSL_Cert',
93 'Master_SSL_Cipher',
94 'Master_SSL_Key',
95 'Seconds_Behind_Master',
97 /**
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;
124 } else {
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],
131 'Binlog_Do_DB'
134 PMA_fillReplicationInfo(
135 $type, 'Ignore_DB', $server_master_replication[0],
136 'Binlog_Ignore_DB'
138 } elseif ($type == "slave") {
139 PMA_fillReplicationInfo(
140 $type, 'Do_DB', $server_slave_replication[0],
141 'Replicate_Do_DB'
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],
151 'Replicate_Do_Table'
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
180 * @return array
182 function PMA_fillReplicationInfo(
183 $type, $replicationInfoKey, $mysqlInfo, $mysqlKey
185 $GLOBALS['replication_info'][$type][$replicationInfoKey]
186 = empty($mysqlInfo[$mysqlKey])
187 ? array()
188 : explode(
189 ",",
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);
207 if ('db' == $what) {
208 return $list[0];
209 } else {
210 return $list[1];
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") {
232 return -1;
234 if ($control != "SQL_THREAD" && $control != "IO_THREAD" && $control != null) {
235 return -1;
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
259 if ($stop) {
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
273 if ($start) {
274 PMA_Replication_Slave_control("START", null, $link);
277 return $out;
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
294 $server = array();
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);
314 $output = array();
316 if (! empty($data)) {
317 $output["File"] = $data[0]["File"];
318 $output["Position"] = $data[0]["Position"];
320 return $output;