Today's Question:  What are you most afraid of as a programmer?        GIVE A SHOUT

Technical Article => Database =>  MySQL

Reproduce "MySQL server has gone away" in PHP

  sonic0002      2013-04-15 11:33:14      4,894    0    0

If you want to debug the issue of "MySQL server has gone away", you can reproduce it with below steps:

Modify configuration file:

  1. sudo vi /etc/mysql/my.cnf 

Make below changes:

  1. [mysqld]  
  2. wait_timeout = 30  
  3. interactive_timeout = 30 

Restart the service:

  1. sudo /etc/init.d/mysql restart 

Write below PHP codes:

  1. $link = mysql_connect('127.0.0.1''root''root');  
  2. if (!$link) {  
  3.     die('Could not connect: ' . mysql_error());  
  4. }  
  5. echo 'Connected successfully';  
  6.   
  7. sleep(31);  
  8. $result = mysql_query('show variables;');  
  9. if (!$result) {  
  10.     die('Invalid query: ' . mysql_error());  
  11. }  
  12. while ($row = mysql_fetch_assoc($result)) {  
  13.     var_dump($row);  
  14. }  
  15. mysql_free_result($result);  
  16.   
  17. mysql_close($link);  
  18. ?> 

Run:

  1. $ php mysql.php   
  2. Connected successfully  
  3. Invalid query: MySQL server has gone away 

Or wait for 30 seconds in command line mode:

  1. mysql> select variables like '%timeout';  
  2. ERROR 2006 (HY000): MySQL server has gone away  
  3. No connection. Trying to reconnect...  
  4. Connection id:    40  
  5. Current database: *** NONE *** 

Now you can do what you want to do. For example add mysql_ping to let it restart itself.

  1. function get_conn() {  
  2.     $conn = mysql_connect('127.0.0.1''root''root');  
  3.     if (!$conn) {  
  4.         die('Could not connect: ' . mysql_error() . '\n');  
  5.     }  
  6.     return $conn;  
  7. }  
  8.   
  9. $conn = get_conn();  
  10.   
  11. sleep(31);  
  12. if (!mysql_ping($conn)) {  
  13.     mysql_close($conn);  
  14.     $conn = get_conn();  
  15.     echo 'Reconnect\n';  
  16. }  
  17.   
  18. $result = mysql_query('show variables;');  
  19. if (!$result) {  
  20.     die('Invalid query: ' . mysql_error());  
  21. }  
  22. while ($row = mysql_fetch_assoc($result)) {  
  23.     var_dump($row);  
  24. }  
  25. mysql_free_result($result);  
  26.   
  27. mysql_close($conn);  
  28. ?> 

If you are using C/C++, you can use below method to let mysql_ping restart itself after establishing the connection.

  1. char mysql_reconnect = 1;   
  2. mysql_options(mysql->conn, MYSQL_OPT_RECONNECT, (char *)&mysql_reconnect); 

Source : http://blog.csdn.net/ffb/article/details/8792009

MYSQL DEBUG RMYSQL SERVER HAS GONE AWAY

  SAVE AS PDF   MARK AS READ   MARK AS IMPORTANT

Share on Facebook  Share on Twitter  Share on Google+  Share on Weibo  Share on Reddit  Share on Digg  Share on Tumblr    Delicious

  RELATED


  0 COMMENT


No comment for this article.


  WRITE ARTICLE

abc.wtf Bing

By sonic0002
When you type abc.xyz, you will see the homepage of Alphabet, the new parent company of Google. But what if you type abc.wtf in your browser address bar? Go and give a try and don't forget to give us a like.