PageRenderTime 58ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/public/phpmyadmin/libraries/replication.inc.php

https://gitlab.com/qbarbosa/klindev
PHP | 321 lines | 186 code | 30 blank | 105 comment | 25 complexity | 1e047e9f4592a73701d08bb1d8ea1d50 MD5 | raw file
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Replication helpers
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. if (! defined('PHPMYADMIN')) {
  9. exit;
  10. }
  11. /**
  12. * get master replication from server
  13. */
  14. $server_master_replication = $GLOBALS['dbi']->fetchResult('SHOW MASTER STATUS');
  15. /**
  16. * set selected master server
  17. */
  18. if (! empty($_REQUEST['master_connection'])) {
  19. /**
  20. * check for multi-master replication functionality
  21. */
  22. $server_slave_multi_replication = $GLOBALS['dbi']->fetchResult(
  23. 'SHOW ALL SLAVES STATUS'
  24. );
  25. if ($server_slave_multi_replication) {
  26. $GLOBALS['dbi']->query(
  27. "SET @@default_master_connection = '" . PMA_Util::sqlAddSlashes(
  28. $_REQUEST['master_connection']
  29. ) . "'"
  30. );
  31. $GLOBALS['url_params']['master_connection'] = $_REQUEST['master_connection'];
  32. }
  33. }
  34. /**
  35. * get slave replication from server
  36. */
  37. $server_slave_replication = $GLOBALS['dbi']->fetchResult('SHOW SLAVE STATUS');
  38. /**
  39. * replication types
  40. */
  41. $replication_types = array('master', 'slave');
  42. /**
  43. * define variables for master status
  44. */
  45. $master_variables = array(
  46. 'File',
  47. 'Position',
  48. 'Binlog_Do_DB',
  49. 'Binlog_Ignore_DB',
  50. );
  51. /**
  52. * Define variables for slave status
  53. */
  54. $slave_variables = array(
  55. 'Slave_IO_State',
  56. 'Master_Host',
  57. 'Master_User',
  58. 'Master_Port',
  59. 'Connect_Retry',
  60. 'Master_Log_File',
  61. 'Read_Master_Log_Pos',
  62. 'Relay_Log_File',
  63. 'Relay_Log_Pos',
  64. 'Relay_Master_Log_File',
  65. 'Slave_IO_Running',
  66. 'Slave_SQL_Running',
  67. 'Replicate_Do_DB',
  68. 'Replicate_Ignore_DB',
  69. 'Replicate_Do_Table',
  70. 'Replicate_Ignore_Table',
  71. 'Replicate_Wild_Do_Table',
  72. 'Replicate_Wild_Ignore_Table',
  73. 'Last_Errno',
  74. 'Last_Error',
  75. 'Skip_Counter',
  76. 'Exec_Master_Log_Pos',
  77. 'Relay_Log_Space',
  78. 'Until_Condition',
  79. 'Until_Log_File',
  80. 'Until_Log_Pos',
  81. 'Master_SSL_Allowed',
  82. 'Master_SSL_CA_File',
  83. 'Master_SSL_CA_Path',
  84. 'Master_SSL_Cert',
  85. 'Master_SSL_Cipher',
  86. 'Master_SSL_Key',
  87. 'Seconds_Behind_Master',
  88. );
  89. /**
  90. * define important variables, which need to be watched for
  91. * correct running of replication in slave mode
  92. *
  93. * @usedby PMA_getHtmlForReplicationStatusTable()
  94. */
  95. // TODO change to regexp or something, to allow for negative match.
  96. // To e.g. highlight 'Last_Error'
  97. //
  98. $slave_variables_alerts = array(
  99. 'Slave_IO_Running' => 'No',
  100. 'Slave_SQL_Running' => 'No',
  101. );
  102. $slave_variables_oks = array(
  103. 'Slave_IO_Running' => 'Yes',
  104. 'Slave_SQL_Running' => 'Yes',
  105. );
  106. // check which replication is available and
  107. // set $server_{master/slave}_status and assign values
  108. // replication info is more easily passed to functions
  109. $GLOBALS['replication_info'] = array();
  110. foreach ($replication_types as $type) {
  111. if (count(${"server_{$type}_replication"}) > 0) {
  112. $GLOBALS['replication_info'][$type]['status'] = true;
  113. } else {
  114. $GLOBALS['replication_info'][$type]['status'] = false;
  115. }
  116. if ($GLOBALS['replication_info'][$type]['status']) {
  117. if ($type == "master") {
  118. PMA_fillReplicationInfo(
  119. $type, 'Do_DB', $server_master_replication[0],
  120. 'Binlog_Do_DB'
  121. );
  122. PMA_fillReplicationInfo(
  123. $type, 'Ignore_DB', $server_master_replication[0],
  124. 'Binlog_Ignore_DB'
  125. );
  126. } elseif ($type == "slave") {
  127. PMA_fillReplicationInfo(
  128. $type, 'Do_DB', $server_slave_replication[0],
  129. 'Replicate_Do_DB'
  130. );
  131. PMA_fillReplicationInfo(
  132. $type, 'Ignore_DB', $server_slave_replication[0],
  133. 'Replicate_Ignore_DB'
  134. );
  135. PMA_fillReplicationInfo(
  136. $type, 'Do_Table', $server_slave_replication[0],
  137. 'Replicate_Do_Table'
  138. );
  139. PMA_fillReplicationInfo(
  140. $type, 'Ignore_Table', $server_slave_replication[0],
  141. 'Replicate_Ignore_Table'
  142. );
  143. PMA_fillReplicationInfo(
  144. $type, 'Wild_Do_Table', $server_slave_replication[0],
  145. 'Replicate_Wild_Do_Table'
  146. );
  147. PMA_fillReplicationInfo(
  148. $type, 'Wild_Ignore_Table', $server_slave_replication[0],
  149. 'Replicate_Wild_Ignore_Table'
  150. );
  151. }
  152. }
  153. }
  154. /**
  155. * Fill global replication_info variable.
  156. *
  157. * @param string $type Type: master, slave
  158. * @param string $replicationInfoKey Key in replication_info variable
  159. * @param array $mysqlInfo MySQL data about replication
  160. * @param string $mysqlKey MySQL key
  161. *
  162. * @return array
  163. */
  164. function PMA_fillReplicationInfo(
  165. $type, $replicationInfoKey, $mysqlInfo, $mysqlKey
  166. ) {
  167. $GLOBALS['replication_info'][$type][$replicationInfoKey]
  168. = empty($mysqlInfo[$mysqlKey])
  169. ? array()
  170. : explode(
  171. ",",
  172. $mysqlInfo[$mysqlKey]
  173. );
  174. return $GLOBALS['replication_info'][$type][$replicationInfoKey];
  175. }
  176. /**
  177. * Extracts database or table name from string
  178. *
  179. * @param string $string contains "dbname.tablename"
  180. * @param string $what what to extract (db|table)
  181. *
  182. * @return string the extracted part
  183. */
  184. function PMA_extractDbOrTable($string, $what = 'db')
  185. {
  186. $list = explode(".", $string);
  187. if ('db' == $what) {
  188. return $list[0];
  189. } else {
  190. return $list[1];
  191. }
  192. }
  193. /**
  194. * Configures replication slave
  195. *
  196. * @param string $action possible values: START or STOP
  197. * @param string $control default: null,
  198. * possible values: SQL_THREAD or IO_THREAD or null.
  199. * If it is set to null, it controls both
  200. * SQL_THREAD and IO_THREAD
  201. * @param mixed $link mysql link
  202. *
  203. * @return mixed output of DatabaseInterface::tryQuery
  204. */
  205. function PMA_Replication_Slave_control($action, $control = null, $link = null)
  206. {
  207. $action = /*overload*/mb_strtoupper($action);
  208. $control = /*overload*/mb_strtoupper($control);
  209. if ($action != "START" && $action != "STOP") {
  210. return -1;
  211. }
  212. if ($control != "SQL_THREAD" && $control != "IO_THREAD" && $control != null) {
  213. return -1;
  214. }
  215. return $GLOBALS['dbi']->tryQuery($action . " SLAVE " . $control . ";", $link);
  216. }
  217. /**
  218. * Changes master for replication slave
  219. *
  220. * @param string $user replication user on master
  221. * @param string $password password for the user
  222. * @param string $host master's hostname or IP
  223. * @param int $port port, where mysql is running
  224. * @param array $pos position of mysql replication,
  225. * array should contain fields File and Position
  226. * @param bool $stop shall we stop slave?
  227. * @param bool $start shall we start slave?
  228. * @param mixed $link mysql link
  229. *
  230. * @return string output of CHANGE MASTER mysql command
  231. */
  232. function PMA_Replication_Slave_changeMaster($user, $password, $host, $port,
  233. $pos, $stop = true, $start = true, $link = null
  234. ) {
  235. if ($stop) {
  236. PMA_Replication_Slave_control("STOP", null, $link);
  237. }
  238. $out = $GLOBALS['dbi']->tryQuery(
  239. 'CHANGE MASTER TO ' .
  240. 'MASTER_HOST=\'' . $host . '\',' .
  241. 'MASTER_PORT=' . ($port * 1) . ',' .
  242. 'MASTER_USER=\'' . $user . '\',' .
  243. 'MASTER_PASSWORD=\'' . $password . '\',' .
  244. 'MASTER_LOG_FILE=\'' . $pos["File"] . '\',' .
  245. 'MASTER_LOG_POS=' . $pos["Position"] . ';', $link
  246. );
  247. if ($start) {
  248. PMA_Replication_Slave_control("START", null, $link);
  249. }
  250. return $out;
  251. }
  252. /**
  253. * This function provides connection to remote mysql server
  254. *
  255. * @param string $user mysql username
  256. * @param string $password password for the user
  257. * @param string $host mysql server's hostname or IP
  258. * @param int $port mysql remote port
  259. * @param string $socket path to unix socket
  260. *
  261. * @return mixed $link mysql link on success
  262. */
  263. function PMA_Replication_connectToMaster(
  264. $user, $password, $host = null, $port = null, $socket = null
  265. ) {
  266. $server = array();
  267. $server["host"] = $host;
  268. $server["port"] = $port;
  269. $server["socket"] = $socket;
  270. // 5th parameter set to true means that it's an auxiliary connection
  271. // and we must not go back to login page if it fails
  272. return $GLOBALS['dbi']->connect($user, $password, false, $server, true);
  273. }
  274. /**
  275. * Fetches position and file of current binary log on master
  276. *
  277. * @param mixed $link mysql link
  278. *
  279. * @return array an array containing File and Position in MySQL replication
  280. * on master server, useful for PMA_Replication_Slave_changeMaster
  281. */
  282. function PMA_Replication_Slave_binLogMaster($link = null)
  283. {
  284. $data = $GLOBALS['dbi']->fetchResult('SHOW MASTER STATUS', null, null, $link);
  285. $output = array();
  286. if (! empty($data)) {
  287. $output["File"] = $data[0]["File"];
  288. $output["Position"] = $data[0]["Position"];
  289. }
  290. return $output;
  291. }