mysql_pconn_max_links.phpt 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. --TEST--
  2. Persistent connections and mysql.max_persistent
  3. --SKIPIF--
  4. <?php
  5. require_once('skipif.inc');
  6. require_once('skipifconnectfailure.inc');
  7. require_once('table.inc');
  8. if ($socket)
  9. $host = sprintf("%s:%s", $host, $socket);
  10. else if ($port)
  11. $host = sprintf("%s:%s", $host, $port);
  12. // we need a second DB user to test for a possible flaw in the ext/mysql[i] code
  13. if (!$link = mysql_connect($host, $user, $passwd, true))
  14. die(sprintf("skip Cannot connect [%d] %s", mysql_errno(), mysql_error()));
  15. if (!mysql_select_db($db, $link))
  16. die(sprintf("skip [%d] %s", mysql_errno($link), mysql_error($link)));
  17. if (!$res = mysql_query('SHOW VARIABLES LIKE "old_passwords"', $link)) {
  18. die(sprintf("skip [%d] %s", mysql_errno($link), mysql_error($link)));
  19. }
  20. if (mysql_num_rows($res) != 1) {
  21. die(sprintf("skip Can't check if old_passwords = ON"));
  22. }
  23. $row = mysql_fetch_assoc($res);
  24. mysql_free_result($res);
  25. if ($row['Value'] == "ON")
  26. die(sprintf("skip Test will fail because old_passwords = ON. Hint: old passwords are insecure!"));
  27. if (!$res = mysql_query("SELECT CURRENT_USER() AS _user", $link))
  28. die(sprintf("skip [%d] %s", mysql_errno($link), mysql_error($link)));
  29. $row = mysql_fetch_assoc($res);
  30. mysql_free_result($res);
  31. $host = substr($row['_user'], strrpos($row['_user'], "@") + 1, strlen($row['_user']));
  32. mysql_query('DROP USER pcontest', $link);
  33. mysql_query(sprintf('DROP USER pcontest@"%s"', mysql_real_escape_string($host, $link)), $link);
  34. if (!mysql_query(sprintf('CREATE USER pcontest@"%s" IDENTIFIED BY "pcontest"', mysql_real_escape_string($host, $link)), $link)) {
  35. printf("skip Cannot create second DB user [%d] %s", mysql_errno($link), mysql_error($link));
  36. mysql_close($link);
  37. die();
  38. }
  39. // we might be able to specify the host using CURRENT_USER(), but...
  40. if (!mysql_query(sprintf('GRANT SELECT ON TABLE %s.test TO pcontest@"%s"', $db, mysql_real_escape_string($host, $link)), $link)) {
  41. printf("skip Cannot GRANT SELECT to second DB user [%d] %s", mysql_errno($link), mysql_error($link));
  42. mysql_query(sprintf('REVOKE ALL PRIVILEGES, GRANT OPTION FROM pcontest@"%s"', mysql_real_escape_string($host, $link)), $link);
  43. mysql_query(sprintf('DROP USER pcontest@"%s"', mysql_real_escape_string($host, $link)), $link);
  44. mysql_close($link);
  45. die();
  46. }
  47. mysql_close($link);
  48. ?>
  49. --INI--
  50. mysql.max_links=3
  51. mysql.max_persistent=2
  52. mysql.allow_persistent=1
  53. --FILE--
  54. <?php
  55. require_once("connect.inc");
  56. require_once('table.inc');
  57. if ($socket)
  58. $host = sprintf("%s:%s", $host, $socket);
  59. else if ($port)
  60. $host = sprintf("%s:%s", $host, $port);
  61. if (!$plink = mysql_pconnect($host, $user, $passwd))
  62. printf("[001] Cannot connect using the second DB user created during SKIPIF, [%d] %s\n",
  63. mysql_errno(), mysql_error());
  64. if (!mysql_select_db($db, $plink))
  65. printf("[002] [%d] %s\n", mysql_errno($plink), mysql_error($plink));
  66. ob_start();
  67. phpinfo();
  68. $phpinfo = strip_tags(ob_get_contents());
  69. ob_end_clean();
  70. $phpinfo = substr($phpinfo, strpos($phpinfo, 'MySQL Support => enabled'), 500);
  71. if (!preg_match('@Active Persistent Links\s+=>\s+(\d+)@ismU', $phpinfo, $matches))
  72. printf("[003] Cannot get # active persistent links from phpinfo()");
  73. $num_plinks = $matches[1];
  74. if (!$res = mysql_query('SELECT id, label FROM test WHERE id = 1', $plink))
  75. printf("[004] Cannot run query on persistent connection of second DB user, [%d] %s\n",
  76. mysql_errno($plink), mysql_error($plink));
  77. if (!$row = mysql_fetch_assoc($res))
  78. printf("[005] Cannot run fetch result, [%d] %s\n",
  79. mysql_errno($plink), mysql_error($plink));
  80. mysql_free_result($res);
  81. var_dump($row);
  82. // change the password for the second DB user and kill the persistent connection
  83. if (!$res = mysql_query("SELECT CURRENT_USER() AS _user", $link))
  84. printf("[006] [%d] %s", mysql_errno($link), mysql_error($link));
  85. $row = mysql_fetch_assoc($res);
  86. mysql_free_result($res);
  87. $host = substr($row['_user'], strrpos($row['_user'], "@") + 1, strlen($row['_user']));
  88. $sql = sprintf('SET PASSWORD FOR pcontest@"%s" = PASSWORD("newpass")', mysql_real_escape_string($host, $link));
  89. if (!mysql_query($sql, $link))
  90. printf("[007] Cannot change PW of second DB user, [%d] %s\n", mysql_errno($link), mysql_error($link));
  91. // persistent connections cannot be closed but only be killed
  92. $pthread_id = mysql_thread_id($plink);
  93. if (!mysql_query(sprintf('KILL %d', $pthread_id), $link))
  94. printf("[008] Cannot KILL persistent connection of second DB user, [%d] %s\n", mysql_errno($link), mysql_error($link));
  95. // give the server a second to really kill the thread
  96. sleep(1);
  97. if (!$res = mysql_query("SHOW FULL PROCESSLIST", $link))
  98. printf("[009] [%d] %s\n", mysql_errno($link), mysql_error($link));
  99. $running_threads = array();
  100. while ($row = mysql_fetch_assoc($res))
  101. $running_threads[$row['Id']] = $row;
  102. mysql_free_result($res);
  103. if (isset($running_threads[$pthread_id]))
  104. printf("[010] Persistent connection has not been killed\n");
  105. // we might get the old handle
  106. if ($plink = @mysql_pconnect($host, 'pcontest', 'pcontest'))
  107. printf("[011] Can connect using the old password, [%d] %s\n",
  108. mysql_errno(), mysql_error());
  109. ob_start();
  110. phpinfo();
  111. $phpinfo = strip_tags(ob_get_contents());
  112. ob_end_clean();
  113. $phpinfo = substr($phpinfo, strpos($phpinfo, 'MySQL Support => enabled'), 500);
  114. if (!preg_match('@Active Persistent Links\s+=>\s+(\d+)@ismU', $phpinfo, $matches))
  115. printf("[012] Cannot get # active persistent links from phpinfo()\n");
  116. $num_plinks_kill = $matches[1];
  117. if ($num_plinks_kill > $num_plinks)
  118. printf("[013] Statistics seems to be wrong, got %d active persistent links, expecting < %d links\n",
  119. $num_plinks_kill, $num_plinks);
  120. // The first connection has been closed, the last pconnect() was unable to connect -> no connection open
  121. // We must be able to connect because max_persistent limit has not been reached
  122. if (!$plink = mysql_pconnect($host, 'pcontest', 'newpass'))
  123. die(sprintf("[014] Cannot connect using the second DB, [%d] %s\n",
  124. mysql_errno(), mysql_error()));
  125. if (!mysql_select_db($db, $plink))
  126. printf("[015] [%d] %s\n", mysql_errno($plink), mysql_error($plink));
  127. if (!$res = mysql_query('SELECT id, label FROM test WHERE id = 1', $plink))
  128. printf("[016] Cannot run query on persistent connection of second DB user, [%d] %s\n",
  129. mysql_errno($plink), mysql_error($plink));
  130. if (!$row = mysql_fetch_assoc($res))
  131. printf("[017] Cannot run fetch result, [%d] %s\n",
  132. mysql_errno($plink), mysql_error($plink));
  133. mysql_free_result($res);
  134. var_dump($row);
  135. mysql_query(sprintf('REVOKE ALL PRIVILEGES, GRANT OPTION FROM pcontest@"%s"', mysql_real_escape_string($host, $link)), $link);
  136. mysql_query(sprintf('DROP USER pcontest@"%s"', mysql_real_escape_string($host, $link)), $link);
  137. mysql_close($link);
  138. print "done!";
  139. ?>
  140. --CLEAN--
  141. <?php
  142. // connect + select_db
  143. require_once("connect.inc");
  144. if (!$link = my_mysql_connect($host, $user, $passwd, $db, $port, $socket)) {
  145. printf("[c001] Cannot connect to the server using host=%s/%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
  146. $host, $myhost, $user, $db, $port, $socket);
  147. }
  148. if (!$res = mysql_query("SELECT CURRENT_USER() AS _user", $link))
  149. printf("[c002] [%d] %s", mysql_errno($link), mysql_error($link));
  150. $row = mysql_fetch_assoc($res);
  151. mysql_free_result($res);
  152. $host = substr($row['_user'], strrpos($row['_user'], "@") + 1, strlen($row['_user']));
  153. @mysql_query(sprintf('REVOKE ALL PRIVILEGES, GRANT OPTION FROM pcontest@"%s"', mysql_real_escape_string($host, $link)), $link);
  154. @mysql_query(sprintf('DROP USER pcontest@"%s"', mysql_real_escape_string($host, $link)), $link);
  155. mysql_close($link);
  156. ?>
  157. --EXPECTF--
  158. Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in %s on line %d
  159. Deprecated: mysql_pconnect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in %s on line %d
  160. array(2) {
  161. [%u|b%"id"]=>
  162. %unicode|string%(1) "1"
  163. [%u|b%"label"]=>
  164. %unicode|string%(1) "a"
  165. }
  166. Deprecated: mysql_pconnect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in %s on line %d
  167. array(2) {
  168. [%u|b%"id"]=>
  169. %unicode|string%(1) "1"
  170. [%u|b%"label"]=>
  171. %unicode|string%(1) "a"
  172. }
  173. done!