Translated using Weblate (Estonian)
[phpmyadmin.git] / libraries / replication.inc.php
blob76a9c7889d18c8bd947300cb889e7ed36e288737
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Replication helpers
6 * @package PhpMyAdmin
7 */
9 use PhpMyAdmin\Core;
10 use PhpMyAdmin\DatabaseInterface;
12 if (! defined('PHPMYADMIN')) {
13 exit;
16 /**
17 * get master replication from server
19 $server_master_replication = $GLOBALS['dbi']->fetchResult('SHOW MASTER STATUS');
21 /**
22 * set selected master server
24 if (! empty($_REQUEST['master_connection'])) {
25 /**
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']
36 ) . "'"
38 $GLOBALS['url_params']['master_connection'] = $_REQUEST['master_connection'];
42 /**
43 * get slave replication from server
45 $server_slave_replication = $GLOBALS['dbi']->fetchResult('SHOW SLAVE STATUS');
47 /**
48 * replication types
50 $replication_types = array('master', 'slave');
53 /**
54 * define variables for master status
56 $master_variables = array(
57 'File',
58 'Position',
59 'Binlog_Do_DB',
60 'Binlog_Ignore_DB',
63 /**
64 * Define variables for slave status
66 $slave_variables = array(
67 'Slave_IO_State',
68 'Master_Host',
69 'Master_User',
70 'Master_Port',
71 'Connect_Retry',
72 'Master_Log_File',
73 'Read_Master_Log_Pos',
74 'Relay_Log_File',
75 'Relay_Log_Pos',
76 'Relay_Master_Log_File',
77 'Slave_IO_Running',
78 'Slave_SQL_Running',
79 'Replicate_Do_DB',
80 'Replicate_Ignore_DB',
81 'Replicate_Do_Table',
82 'Replicate_Ignore_Table',
83 'Replicate_Wild_Do_Table',
84 'Replicate_Wild_Ignore_Table',
85 'Last_Errno',
86 'Last_Error',
87 'Skip_Counter',
88 'Exec_Master_Log_Pos',
89 'Relay_Log_Space',
90 'Until_Condition',
91 'Until_Log_File',
92 'Until_Log_Pos',
93 'Master_SSL_Allowed',
94 'Master_SSL_CA_File',
95 'Master_SSL_CA_Path',
96 'Master_SSL_Cert',
97 'Master_SSL_Cipher',
98 'Master_SSL_Key',
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;
128 } else {
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],
135 'Binlog_Do_DB'
138 PMA_fillReplicationInfo(
139 $type, 'Ignore_DB', $server_master_replication[0],
140 'Binlog_Ignore_DB'
142 } elseif ($type == "slave") {
143 PMA_fillReplicationInfo(
144 $type, 'Do_DB', $server_slave_replication[0],
145 'Replicate_Do_DB'
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],
155 'Replicate_Do_Table'
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
184 * @return array
186 function PMA_fillReplicationInfo(
187 $type, $replicationInfoKey, $mysqlInfo, $mysqlKey
189 $GLOBALS['replication_info'][$type][$replicationInfoKey]
190 = empty($mysqlInfo[$mysqlKey])
191 ? array()
192 : explode(
193 ",",
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);
211 if ('db' == $what) {
212 return $list[0];
213 } else {
214 return $list[1];
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") {
236 return -1;
238 if ($control != "SQL_THREAD" && $control != "IO_THREAD" && $control != null) {
239 return -1;
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
263 if ($stop) {
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
277 if ($start) {
278 PMA_Replication_Slave_control("START", null, $link);
281 return $out;
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
298 $server = array();
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);
320 $output = array();
322 if (! empty($data)) {
323 $output["File"] = $data[0]["File"];
324 $output["Position"] = $data[0]["Position"];
326 return $output;