Translated using Weblate (Slovenian)
[phpmyadmin.git] / libraries / replication.inc.php
blob262719b7bbe47329843d038edeead80d1da7a6c0
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Replication helpers
6 * @package PhpMyAdmin
7 */
9 use PMA\libraries\DatabaseInterface;
11 if (! defined('PHPMYADMIN')) {
12 exit;
15 /**
16 * get master replication from server
18 $server_master_replication = $GLOBALS['dbi']->fetchResult('SHOW MASTER STATUS');
20 /**
21 * set selected master server
23 if (! empty($_REQUEST['master_connection'])) {
24 /**
25 * check for multi-master replication functionality
27 $server_slave_multi_replication = $GLOBALS['dbi']->fetchResult(
28 'SHOW ALL SLAVES STATUS'
30 if ($server_slave_multi_replication) {
31 $GLOBALS['dbi']->query(
32 "SET @@default_master_connection = '"
33 . $GLOBALS['dbi']->escapeString(
34 $_REQUEST['master_connection']
35 ) . "'"
37 $GLOBALS['url_params']['master_connection'] = $_REQUEST['master_connection'];
41 /**
42 * get slave replication from server
44 $server_slave_replication = $GLOBALS['dbi']->fetchResult('SHOW SLAVE STATUS');
46 /**
47 * replication types
49 $replication_types = array('master', 'slave');
52 /**
53 * define variables for master status
55 $master_variables = array(
56 'File',
57 'Position',
58 'Binlog_Do_DB',
59 'Binlog_Ignore_DB',
62 /**
63 * Define variables for slave status
65 $slave_variables = array(
66 'Slave_IO_State',
67 'Master_Host',
68 'Master_User',
69 'Master_Port',
70 'Connect_Retry',
71 'Master_Log_File',
72 'Read_Master_Log_Pos',
73 'Relay_Log_File',
74 'Relay_Log_Pos',
75 'Relay_Master_Log_File',
76 'Slave_IO_Running',
77 'Slave_SQL_Running',
78 'Replicate_Do_DB',
79 'Replicate_Ignore_DB',
80 'Replicate_Do_Table',
81 'Replicate_Ignore_Table',
82 'Replicate_Wild_Do_Table',
83 'Replicate_Wild_Ignore_Table',
84 'Last_Errno',
85 'Last_Error',
86 'Skip_Counter',
87 'Exec_Master_Log_Pos',
88 'Relay_Log_Space',
89 'Until_Condition',
90 'Until_Log_File',
91 'Until_Log_Pos',
92 'Master_SSL_Allowed',
93 'Master_SSL_CA_File',
94 'Master_SSL_CA_Path',
95 'Master_SSL_Cert',
96 'Master_SSL_Cipher',
97 'Master_SSL_Key',
98 'Seconds_Behind_Master',
101 * define important variables, which need to be watched for
102 * correct running of replication in slave mode
104 * @usedby PMA_getHtmlForReplicationStatusTable()
106 // TODO change to regexp or something, to allow for negative match.
107 // To e.g. highlight 'Last_Error'
109 $slave_variables_alerts = array(
110 'Slave_IO_Running' => 'No',
111 'Slave_SQL_Running' => 'No',
113 $slave_variables_oks = array(
114 'Slave_IO_Running' => 'Yes',
115 'Slave_SQL_Running' => 'Yes',
118 // check which replication is available and
119 // set $server_{master/slave}_status and assign values
121 // replication info is more easily passed to functions
122 $GLOBALS['replication_info'] = array();
124 foreach ($replication_types as $type) {
125 if (count(${"server_{$type}_replication"}) > 0) {
126 $GLOBALS['replication_info'][$type]['status'] = true;
127 } else {
128 $GLOBALS['replication_info'][$type]['status'] = false;
130 if ($GLOBALS['replication_info'][$type]['status']) {
131 if ($type == "master") {
132 PMA_fillReplicationInfo(
133 $type, 'Do_DB', $server_master_replication[0],
134 'Binlog_Do_DB'
137 PMA_fillReplicationInfo(
138 $type, 'Ignore_DB', $server_master_replication[0],
139 'Binlog_Ignore_DB'
141 } elseif ($type == "slave") {
142 PMA_fillReplicationInfo(
143 $type, 'Do_DB', $server_slave_replication[0],
144 'Replicate_Do_DB'
147 PMA_fillReplicationInfo(
148 $type, 'Ignore_DB', $server_slave_replication[0],
149 'Replicate_Ignore_DB'
152 PMA_fillReplicationInfo(
153 $type, 'Do_Table', $server_slave_replication[0],
154 'Replicate_Do_Table'
157 PMA_fillReplicationInfo(
158 $type, 'Ignore_Table', $server_slave_replication[0],
159 'Replicate_Ignore_Table'
162 PMA_fillReplicationInfo(
163 $type, 'Wild_Do_Table', $server_slave_replication[0],
164 'Replicate_Wild_Do_Table'
167 PMA_fillReplicationInfo(
168 $type, 'Wild_Ignore_Table', $server_slave_replication[0],
169 'Replicate_Wild_Ignore_Table'
176 * Fill global replication_info variable.
178 * @param string $type Type: master, slave
179 * @param string $replicationInfoKey Key in replication_info variable
180 * @param array $mysqlInfo MySQL data about replication
181 * @param string $mysqlKey MySQL key
183 * @return array
185 function PMA_fillReplicationInfo(
186 $type, $replicationInfoKey, $mysqlInfo, $mysqlKey
188 $GLOBALS['replication_info'][$type][$replicationInfoKey]
189 = empty($mysqlInfo[$mysqlKey])
190 ? array()
191 : explode(
192 ",",
193 $mysqlInfo[$mysqlKey]
196 return $GLOBALS['replication_info'][$type][$replicationInfoKey];
200 * Extracts database or table name from string
202 * @param string $string contains "dbname.tablename"
203 * @param string $what what to extract (db|table)
205 * @return string the extracted part
207 function PMA_extractDbOrTable($string, $what = 'db')
209 $list = explode(".", $string);
210 if ('db' == $what) {
211 return $list[0];
212 } else {
213 return $list[1];
218 * Configures replication slave
220 * @param string $action possible values: START or STOP
221 * @param string $control default: null,
222 * possible values: SQL_THREAD or IO_THREAD or null.
223 * If it is set to null, it controls both
224 * SQL_THREAD and IO_THREAD
225 * @param mixed $link mysql link
227 * @return mixed output of DatabaseInterface::tryQuery
229 function PMA_Replication_Slave_control($action, $control = null, $link = null)
231 $action = mb_strtoupper($action);
232 $control = mb_strtoupper($control);
234 if ($action != "START" && $action != "STOP") {
235 return -1;
237 if ($control != "SQL_THREAD" && $control != "IO_THREAD" && $control != null) {
238 return -1;
241 return $GLOBALS['dbi']->tryQuery($action . " SLAVE " . $control . ";", $link);
245 * Changes master for replication slave
247 * @param string $user replication user on master
248 * @param string $password password for the user
249 * @param string $host master's hostname or IP
250 * @param int $port port, where mysql is running
251 * @param array $pos position of mysql replication,
252 * array should contain fields File and Position
253 * @param bool $stop shall we stop slave?
254 * @param bool $start shall we start slave?
255 * @param mixed $link mysql link
257 * @return string output of CHANGE MASTER mysql command
259 function PMA_Replication_Slave_changeMaster($user, $password, $host, $port,
260 $pos, $stop = true, $start = true, $link = null
262 if ($stop) {
263 PMA_Replication_Slave_control("STOP", null, $link);
266 $out = $GLOBALS['dbi']->tryQuery(
267 'CHANGE MASTER TO ' .
268 'MASTER_HOST=\'' . $host . '\',' .
269 'MASTER_PORT=' . ($port * 1) . ',' .
270 'MASTER_USER=\'' . $user . '\',' .
271 'MASTER_PASSWORD=\'' . $password . '\',' .
272 'MASTER_LOG_FILE=\'' . $pos["File"] . '\',' .
273 'MASTER_LOG_POS=' . $pos["Position"] . ';', $link
276 if ($start) {
277 PMA_Replication_Slave_control("START", null, $link);
280 return $out;
284 * This function provides connection to remote mysql server
286 * @param string $user mysql username
287 * @param string $password password for the user
288 * @param string $host mysql server's hostname or IP
289 * @param int $port mysql remote port
290 * @param string $socket path to unix socket
292 * @return mixed $link mysql link on success
294 function PMA_Replication_connectToMaster(
295 $user, $password, $host = null, $port = null, $socket = null
297 $server = array();
298 $server['user'] = $user;
299 $server['password'] = $password;
300 $server["host"] = PMA_sanitizeMySQLHost($host);
301 $server["port"] = $port;
302 $server["socket"] = $socket;
304 // 5th parameter set to true means that it's an auxiliary connection
305 // and we must not go back to login page if it fails
306 return $GLOBALS['dbi']->connect(databaseinterface::CONNECT_AUXILIARY, $server);
309 * Fetches position and file of current binary log on master
311 * @param mixed $link mysql link
313 * @return array an array containing File and Position in MySQL replication
314 * on master server, useful for PMA_Replication_Slave_changeMaster
316 function PMA_Replication_Slave_binLogMaster($link = null)
318 $data = $GLOBALS['dbi']->fetchResult('SHOW MASTER STATUS', null, null, $link);
319 $output = array();
321 if (! empty($data)) {
322 $output["File"] = $data[0]["File"];
323 $output["Position"] = $data[0]["Position"];
325 return $output;