Translated using Weblate (Czech)
[phpmyadmin.git] / libraries / classes / Replication.php
blobd53fada5eb0c0bf242740289cf2a1d62518b705f
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Replication helpers
6 * @package PhpMyAdmin
7 */
8 namespace PhpMyAdmin;
10 use PhpMyAdmin\Core;
11 use PhpMyAdmin\DatabaseInterface;
13 /**
14 * PhpMyAdmin\Replication class
16 * @package PhpMyAdmin
18 class Replication
20 /**
21 * Fill global replication_info variable.
23 * @param string $type Type: master, slave
24 * @param string $replicationInfoKey Key in replication_info variable
25 * @param array $mysqlInfo MySQL data about replication
26 * @param string $mysqlKey MySQL key
28 * @return array
30 public static function fillInfo(
31 $type, $replicationInfoKey, array $mysqlInfo, $mysqlKey
32 ) {
33 $GLOBALS['replication_info'][$type][$replicationInfoKey]
34 = empty($mysqlInfo[$mysqlKey])
35 ? array()
36 : explode(
37 ",",
38 $mysqlInfo[$mysqlKey]
41 return $GLOBALS['replication_info'][$type][$replicationInfoKey];
44 /**
45 * Extracts database or table name from string
47 * @param string $string contains "dbname.tablename"
48 * @param string $what what to extract (db|table)
50 * @return string the extracted part
52 public static function extractDbOrTable($string, $what = 'db')
54 $list = explode(".", $string);
55 if ('db' == $what) {
56 return $list[0];
57 } else {
58 return $list[1];
62 /**
63 * Configures replication slave
65 * @param string $action possible values: START or STOP
66 * @param string $control default: null,
67 * possible values: SQL_THREAD or IO_THREAD or null.
68 * If it is set to null, it controls both
69 * SQL_THREAD and IO_THREAD
70 * @param mixed $link mysql link
72 * @return mixed output of DatabaseInterface::tryQuery
74 public static function slaveControl($action, $control = null, $link = null)
76 $action = mb_strtoupper($action);
77 $control = mb_strtoupper($control);
79 if ($action != "START" && $action != "STOP") {
80 return -1;
82 if ($control != "SQL_THREAD" && $control != "IO_THREAD" && $control != null) {
83 return -1;
86 return $GLOBALS['dbi']->tryQuery($action . " SLAVE " . $control . ";", $link);
89 /**
90 * Changes master for replication slave
92 * @param string $user replication user on master
93 * @param string $password password for the user
94 * @param string $host master's hostname or IP
95 * @param int $port port, where mysql is running
96 * @param array $pos position of mysql replication,
97 * array should contain fields File and Position
98 * @param bool $stop shall we stop slave?
99 * @param bool $start shall we start slave?
100 * @param mixed $link mysql link
102 * @return string output of CHANGE MASTER mysql command
104 public static function slaveChangeMaster($user, $password, $host, $port,
105 array $pos, $stop = true, $start = true, $link = null
107 if ($stop) {
108 self::slaveControl("STOP", null, $link);
111 $out = $GLOBALS['dbi']->tryQuery(
112 'CHANGE MASTER TO ' .
113 'MASTER_HOST=\'' . $host . '\',' .
114 'MASTER_PORT=' . ($port * 1) . ',' .
115 'MASTER_USER=\'' . $user . '\',' .
116 'MASTER_PASSWORD=\'' . $password . '\',' .
117 'MASTER_LOG_FILE=\'' . $pos["File"] . '\',' .
118 'MASTER_LOG_POS=' . $pos["Position"] . ';', $link
121 if ($start) {
122 self::slaveControl("START", null, $link);
125 return $out;
129 * This function provides connection to remote mysql server
131 * @param string $user mysql username
132 * @param string $password password for the user
133 * @param string $host mysql server's hostname or IP
134 * @param int $port mysql remote port
135 * @param string $socket path to unix socket
137 * @return mixed $link mysql link on success
139 public static function connectToMaster(
140 $user, $password, $host = null, $port = null, $socket = null
142 $server = array();
143 $server['user'] = $user;
144 $server['password'] = $password;
145 $server["host"] = Core::sanitizeMySQLHost($host);
146 $server["port"] = $port;
147 $server["socket"] = $socket;
149 // 5th parameter set to true means that it's an auxiliary connection
150 // and we must not go back to login page if it fails
151 return $GLOBALS['dbi']->connect(DatabaseInterface::CONNECT_AUXILIARY, $server);
155 * Fetches position and file of current binary log on master
157 * @param mixed $link mysql link
159 * @return array an array containing File and Position in MySQL replication
160 * on master server, useful for self::slaveChangeMaster
162 public static function slaveBinLogMaster($link = null)
164 $data = $GLOBALS['dbi']->fetchResult('SHOW MASTER STATUS', null, null, $link);
165 $output = array();
167 if (! empty($data)) {
168 $output["File"] = $data[0]["File"];
169 $output["Position"] = $data[0]["Position"];
171 return $output;