PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/phpmyadmin/libraries/replication.inc.php

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