Translated using Weblate (Estonian)
[phpmyadmin.git] / libraries / replication.inc.php
blob834d38ab2f5cb850fedd3170cb37477382912ccf
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 * get slave replication from server
21 $server_slave_replication = $GLOBALS['dbi']->fetchResult('SHOW SLAVE STATUS');
23 /**
24 * replication types
26 $replication_types = array('master', 'slave');
29 /**
30 * define variables for master status
32 $master_variables = array(
33 'File',
34 'Position',
35 'Binlog_Do_DB',
36 'Binlog_Ignore_DB',
39 /**
40 * Define variables for slave status
42 $slave_variables = array(
43 'Slave_IO_State',
44 'Master_Host',
45 'Master_User',
46 'Master_Port',
47 'Connect_Retry',
48 'Master_Log_File',
49 'Read_Master_Log_Pos',
50 'Relay_Log_File',
51 'Relay_Log_Pos',
52 'Relay_Master_Log_File',
53 'Slave_IO_Running',
54 'Slave_SQL_Running',
55 'Replicate_Do_DB',
56 'Replicate_Ignore_DB',
57 'Replicate_Do_Table',
58 'Replicate_Ignore_Table',
59 'Replicate_Wild_Do_Table',
60 'Replicate_Wild_Ignore_Table',
61 'Last_Errno',
62 'Last_Error',
63 'Skip_Counter',
64 'Exec_Master_Log_Pos',
65 'Relay_Log_Space',
66 'Until_Condition',
67 'Until_Log_File',
68 'Until_Log_Pos',
69 'Master_SSL_Allowed',
70 'Master_SSL_CA_File',
71 'Master_SSL_CA_Path',
72 'Master_SSL_Cert',
73 'Master_SSL_Cipher',
74 'Master_SSL_Key',
75 'Seconds_Behind_Master',
77 /**
78 * define important variables, which need to be watched for
79 * correct running of replication in slave mode
81 * @usedby PMA_getHtmlForReplicationStatusTable()
83 // TODO change to regexp or something, to allow for negative match.
84 // To e.g. highlight 'Last_Error'
86 $slave_variables_alerts = array(
87 'Slave_IO_Running' => 'No',
88 'Slave_SQL_Running' => 'No',
90 $slave_variables_oks = array(
91 'Slave_IO_Running' => 'Yes',
92 'Slave_SQL_Running' => 'Yes',
95 // check which replication is available and
96 // set $server_{master/slave}_status and assign values
98 // replication info is more easily passed to functions
100 * @todo use $replication_info everywhere instead of the generated variable names
102 $replication_info = array();
104 foreach ($replication_types as $type) {
105 if (count(${"server_{$type}_replication"}) > 0) {
106 ${"server_{$type}_status"} = true;
107 $replication_info[$type]['status'] = true;
108 } else {
109 ${"server_{$type}_status"} = false;
110 $replication_info[$type]['status'] = false;
112 if (${"server_{$type}_status"}) {
113 if ($type == "master") {
114 ${"server_{$type}_Do_DB"} = explode(
115 ",", $server_master_replication[0]["Binlog_Do_DB"]
117 $replication_info[$type]['Do_DB'] = ${"server_{$type}_Do_DB"};
119 ${"server_{$type}_Ignore_DB"} = explode(
120 ",", $server_master_replication[0]["Binlog_Ignore_DB"]
122 $replication_info[$type]['Ignore_DB'] = ${"server_{$type}_Ignore_DB"};
123 } elseif ($type == "slave") {
124 ${"server_{$type}_Do_DB"} = explode(
125 ",", $server_slave_replication[0]["Replicate_Do_DB"]
127 $replication_info[$type]['Do_DB'] = ${"server_{$type}_Do_DB"};
129 ${"server_{$type}_Ignore_DB"} = explode(
130 ",", $server_slave_replication[0]["Replicate_Ignore_DB"]
132 $replication_info[$type]['Ignore_DB'] = ${"server_{$type}_Ignore_DB"};
134 ${"server_{$type}_Do_Table"} = explode(
135 ",", $server_slave_replication[0]["Replicate_Do_Table"]
137 $replication_info[$type]['Do_Table'] = ${"server_{$type}_Do_Table"};
139 ${"server_{$type}_Ignore_Table"} = explode(
140 ",", $server_slave_replication[0]["Replicate_Ignore_Table"]
142 $replication_info[$type]['Ignore_Table']
143 = ${"server_{$type}_Ignore_Table"};
145 ${"server_{$type}_Wild_Do_Table"} = explode(
146 ",", $server_slave_replication[0]["Replicate_Wild_Do_Table"]
148 $replication_info[$type]['Wild_Do_Table']
149 = ${"server_{$type}_Wild_Do_Table"};
151 ${"server_{$type}_Wild_Ignore_Table"} = explode(
152 ",", $server_slave_replication[0]["Replicate_Wild_Ignore_Table"]
154 $replication_info[$type]['Wild_Ignore_Table']
155 = ${"server_{$type}_Wild_Ignore_Table"};
161 * Extracts database or table name from string
163 * @param string $string contains "dbname.tablename"
164 * @param string $what what to extract (db|table)
166 * @return $string the extracted part
168 function PMA_extractDbOrTable($string, $what = 'db')
170 $list = explode(".", $string);
171 if ('db' == $what) {
172 return $list[0];
173 } else {
174 return $list[1];
179 * Configures replication slave
181 * @param string $action possible values: START or STOP
182 * @param string $control default: null,
183 * possible values: SQL_THREAD or IO_THREAD or null.
184 * If it is set to null, it controls both
185 * SQL_THREAD and IO_THREAD
186 * @param mixed $link mysql link
188 * @return mixed output of DatabaseInterface::tryQuery
190 function PMA_Replication_Slave_control($action, $control = null, $link = null)
192 $action = strtoupper($action);
193 $control = strtoupper($control);
195 if ($action != "START" && $action != "STOP") {
196 return -1;
198 if ($control != "SQL_THREAD" && $control != "IO_THREAD" && $control != null) {
199 return -1;
202 return $GLOBALS['dbi']->tryQuery($action . " SLAVE " . $control . ";", $link);
206 * Changes master for replication slave
208 * @param string $user replication user on master
209 * @param string $password password for the user
210 * @param string $host master's hostname or IP
211 * @param int $port port, where mysql is running
212 * @param array $pos position of mysql replication,
213 * array should contain fields File and Position
214 * @param bool $stop shall we stop slave?
215 * @param bool $start shall we start slave?
216 * @param mixed $link mysql link
218 * @return output of CHANGE MASTER mysql command
220 function PMA_Replication_Slave_changeMaster($user, $password, $host, $port,
221 $pos, $stop = true, $start = true, $link = null
223 if ($stop) {
224 PMA_Replication_Slave_control("STOP", null, $link);
227 $out = $GLOBALS['dbi']->tryQuery(
228 'CHANGE MASTER TO ' .
229 'MASTER_HOST=\'' . $host . '\',' .
230 'MASTER_PORT=' . ($port * 1) . ',' .
231 'MASTER_USER=\'' . $user . '\',' .
232 'MASTER_PASSWORD=\'' . $password . '\',' .
233 'MASTER_LOG_FILE=\'' . $pos["File"] . '\',' .
234 'MASTER_LOG_POS=' . $pos["Position"] . ';', $link
237 if ($start) {
238 PMA_Replication_Slave_control("START", null, $link);
241 return $out;
245 * This function provides connection to remote mysql server
247 * @param string $user mysql username
248 * @param string $password password for the user
249 * @param string $host mysql server's hostname or IP
250 * @param int $port mysql remote port
251 * @param string $socket path to unix socket
253 * @return mixed $link mysql link on success
255 function PMA_Replication_connectToMaster(
256 $user, $password, $host = null, $port = null, $socket = null
258 $server = array();
259 $server["host"] = $host;
260 $server["port"] = $port;
261 $server["socket"] = $socket;
263 // 5th parameter set to true means that it's an auxiliary connection
264 // and we must not go back to login page if it fails
265 return $GLOBALS['dbi']->connect($user, $password, false, $server, true);
268 * Fetches position and file of current binary log on master
270 * @param mixed $link mysql link
272 * @return array an array containing File and Position in MySQL replication
273 * on master server, useful for PMA_Replication_Slave_changeMaster
275 function PMA_Replication_Slave_binLogMaster($link = null)
277 $data = $GLOBALS['dbi']->fetchResult('SHOW MASTER STATUS', null, null, $link);
278 $output = array();
280 if (! empty($data)) {
281 $output["File"] = $data[0]["File"];
282 $output["Position"] = $data[0]["Position"];
284 return $output;
288 * Get list of replicated databases on master server
290 * @param mixed $link mysql link
292 * @return array array of replicated databases
295 function PMA_Replication_Master_getReplicatedDbs($link = null)
297 // let's find out, which databases are replicated
298 $data = $GLOBALS['dbi']->fetchResult('SHOW MASTER STATUS', null, null, $link);
300 $do_db = array();
301 $ignore_db = array();
303 if (! empty($data[0]['Binlog_Do_DB'])) {
304 $do_db = explode(',', $data[0]['Binlog_Do_DB']);
306 if (! empty($data[0]['Binlog_Ignore_DB'])) {
307 $ignore_db = explode(',', $data[0]['Binlog_Ignore_DB']);
310 $tmp_alldbs = $GLOBALS['dbi']->query('SHOW DATABASES;', $link);
311 while ($tmp_row = $GLOBALS['dbi']->fetchRow($tmp_alldbs)) {
312 if ($GLOBALS['dbi']->isSystemSchema($tmp_row[0])) {
313 continue;
315 if (count($do_db) == 0) {
316 if (array_search($tmp_row[0], $ignore_db) !== false) {
317 continue;
319 $dblist[] = $tmp_row[0];
321 } else {
322 if (array_search($tmp_row[0], $do_db) !== false) {
323 $dblist[] = $tmp_row[0];
326 } // end while
328 return $link;