Replication.php 5.4 KB

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