PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/phpmyadmin/libraries/replication.inc.php

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