|   | php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
| 
  [2003-12-01 13:16 UTC] chris dot noden at monstermob dot com
 Description:
------------
MySQL version 4.0.13 running on a different server on the LAN using pconnect:
After using a pcntl_fork() the child thread suffers from the "2013 : Lost connection to MySQL server" error above.
I can continue to use the MySQL connection without connecting again implying that the connection has miraculously re-established itself!
The error can occur at any stage in a query, (eg during submission or reading the results of the query).
The error usually manifests itself when the query returns no results.  The error comes from the parent process!!
I have tried all sorts of workarounds, to no avail.
Reproduce code:
---------------
do {
	$sql = "SELECT stuff FROM db WHERE a=b";
	$qryID = mysql_query($sql,$Link_ID
	while (mysql_fetch_array($qryID)) {
		// Fork off a child
		$is_parent = pcntl_fork();
		if ($is_parent > 0) {
			// I am the child - do some stuff
			exit;
		} else {
			// I am the parent do some stuff
		} // end if/else
		
		sleep(1);
} while (condition);	
Expected result:
----------------
Normal database flow.
Actual result:
--------------
Error 2013 - Lost connection to MySQL server during query
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             | |||||||||||||||||||||||||||||||||||||
|  Copyright © 2001-2025 The PHP Group All rights reserved. | Last updated: Fri Oct 31 06:00:01 2025 UTC | 
As requested by theseer @php.net I'm adding a test case that steadily reproduces the problem for me. Both on a Single CPU Pentium 4, Linux 2.4.29, PHP 4.3.10, MySQL 4.1.10, libc 2.2.5 machine, as well as on a four CPU Ultra Sparc, Linux 2.4.27, PHP 4.3.10, MySQL 4.1.10a, libc 2.3.2 box I get the same results. The proposed workaround doesn't change that behaviour. The example code assumes there is a database testcasedb, to which a user testcaseuser on localhost using testcasepw has access. It needs to contain a table like this, although the table type really doesn't seem to matter: CREATE TABLE `testtable` (`row1` varchar(40) NOT NULL default '', `tstamp` timestamp NULL default NULL) DEFAULT CHARSET=latin1 ; <?php $dbres = mysql_connect('localhost', 'testcaseuser', 'testcasepw'); $parent_sql = "INSERT INTO testcasedb.testtable VALUES ('some test', now())"; $child_sql = "DELETE FROM testcasedb.testtable WHERE row1 = 'some test'"; do { for ($i = 1; $i <= 20; $i++) { print("Parent query starts\n"); if (!mysql_query($parent_sql, $dbres)) { print("Parent Mysql Error: " . mysql_error($dbres) . "\n"); } } $is_child = pcntl_fork(); if ($is_child > 0) { print("Child connection and query start\n"); $dbres = mysql_connect('localhost', 'testcaseuser', 'testcasepw', true); if (!mysql_query($child_sql, $dbres)) { print("Child Mysql Error: " . mysql_error($dbres) . "\n"); } exit; } print("Parent next iteration\n"); } while (true); ?> Basically the parent process loops forever, spawning children that each open a connection of their own, do a query and then die. You'll see that the parent will report Lost connections soon, although the children are doing their best not to reuse the parent's connection. Hope this helps to deeper look into the issue. Thanks for listening, --AndreasSame problem, sometimes! But if we keep trying we usually get result in 2-3 tries, this code works to keep trying without hanging forever! $result = FALSE; $counter = 0; while($result==FALSE) { $res = mysql_query($waitquery); $result = mysql_fetch_assoc($res); $counter++; error_log("error happened! - mysql Connection was lost!! try#$counter); if ($counter==5)//prevents major major hang! ;-) break; }