php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #26490 Lost connection to MySQL server during query
Submitted: 2003-12-01 13:16 UTC Modified: 2004-10-01 01:00 UTC
Votes:81
Avg. Score:4.5 ± 0.9
Reproduced:67 of 69 (97.1%)
Same Version:27 (40.3%)
Same OS:37 (55.2%)
From: chris dot noden at monstermob dot com Assigned:
Status: No Feedback Package: MySQL related
PHP Version: 4CVS, 5CVS (2004-03-13) OS: *
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-03-11 11:39 UTC] theseer@php.net
This happens on the following versions:

 php 4.3.3, 4.3.4, 4.3.5RC3

 with bundled or external mysql client code
 with shared or buildin

 MySQL Server Version here is 4.0.18

 The MySQL Log shows this row on a 'connection lost' error:
 Aborted connection 2048 to db: [....] (Got an error reading communication packets)


 [2004-06-11 00:23 UTC] Ryan dot Springer at billingconcepts dot com
Is everyone who is effected by this bug running Red Hat?  If so, this may be a libc problem.  See:

http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=75128
 [2004-06-20 20:22 UTC] theseer@php.net
I cannot speak for chris but at least we are running Redhat/Fedora Systems using MySQL rpms from mysql.com.

I will check the workarounds mentioned on the redhat page and report back.
 [2004-06-22 23:30 UTC] phpbug at nathandial dot com
I've got the same bug on gentoo. PHP 4.3.6, mysql 4.0.18
 [2004-07-13 21:25 UTC] gmartine at adinet dot com dot uy
Hi,

 I have this very same error using "php 4.3.7" / "mysql 4.0.20a" under windows 2000.

Regards.
 [2004-07-16 19:51 UTC] genio at qwest dot net
I've been able to reproduce the error on OpenBSD using php5 and mysql5 alpha. I assumed the error occured due to the fact im using an alpha release version of mysql, in my case it might not be a php bug because Im able to reproduce a similar mysql error with a jsp servlet connection
 [2004-07-17 09:58 UTC] jdittmer at ppp0 dot net
Just wanted to say, that I see the very same error with php 4.3.8 and postgres. (pconnect & connect) System is Debian/GNU Linux
 [2004-07-20 15:00 UTC] garak at studenti dot it
Same problem.
Connect to mysql server on LAN.
PHP 4.3.7
MySQL 4.0.20
Linux 2.4.19
 [2004-07-21 17:20 UTC] florian dot heigl at m4f dot net
I got the same error w/o using php - connecting via bin/mysql...

It only happens *sometimes* :)
ERROR 2013 at line 78: Lost connection to MySQL server during query


OS is Tru64 5.1B-4, MySQL 4.0.20
 [2004-08-03 07:27 UTC] adams at mars-project dot com
I also get this error, but its on an internal mysql server.

PHP 4.3.7
MySQL 4.0.18
FreeBSD 4.10

And just to note when i do get this error i get a php core dump as well.
 [2004-08-10 17:11 UTC] christian at jul dot net
Happened for me on php 4.3.8 / mysql 4.0.20 / debian unstable

Seems like putting the db connection after the fork solved the problem. Apparently the connection dies when the connecting process exits, but should be kept alive as long as   there is living child processes.
 [2004-08-12 19:20 UTC] theseer@php.net
This is not a bug at all but rather a problem created by the reuse of connections within php:
The mysql_connect() command does NOT open a new connection by default. So even if one calls mysql_connect() in the forked child, no new conenction is opened. Adding the addtional parameter to force a new connection fixes the problem right away.


 [2004-09-22 21:03 UTC] tommy at allstardirectories dot com
The suggested work-around does not work for me.  I'm still seeing intermittent connection losses (About 5-10 per day).
 [2004-09-22 21:06 UTC] tru at gtwreck dot com
All you need to do is check out this query on google, and you can see that a lot of sites have this issue while google is indexing them...
http://www.google.com/search?hl=en&lr=&ie=UTF-8&q=%22Lost+connection+to+MySQL+server+during+query%22+mysql_connect
 [2004-09-23 12:05 UTC] theseer@php.net
Due to people claiming the "workaround" doesn't fix their problems, the bug is reopened.

Please provide detailed feedback on how to reproduce the problem.
 [2004-10-01 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2005-04-14 16:38 UTC] Andreas dot Oesterhelt at InTradeSys dot com
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,
--Andreas
 [2005-04-14 19:16 UTC] Andreas dot Oesterhelt at InTradeSys dot com
After further thinking at least my test case
and Chris' original case are non-bugs:

The child inherits the parent's open connection.

When it dies, all its open files, including the
(unused) inherited connection are closed.

If the parent subsequently calls mysql_query() on
that closed connection, a new one will be opened.
But if the child termination happens while the parent
is executing a query, the parent indeed loses its
connection during its query.

Sorry for wasting your time,
--Andreas

PS: Fork logic in testcase script was flawed, too.
Should have been: if (!$is_child).
 [2006-08-10 21:52 UTC] mcote at voyagesconstellation dot com
Same 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;
}
 [2006-12-15 15:46 UTC] 18sanny at azet dot sk
If this image is hard to read, reload the page.
 [2007-05-23 06:38 UTC] david at ols dot es
I have the same problem with php 4.4.4, from time to time
both pconnect and connect fail with error " Lost connection to MySQL server during query", but only when using a mysql server on a remote machine, never when using local sockets
 [2007-06-15 09:00 UTC] angel at support-24 dot com
I have this issue only while crawling something. 
For example I have 50 000 curl requests to a page. I crawl the page, gather info and store it in a database, only if the page contains what I'm looking for.

I have noticed that in about every 50 queries the mysql insert query crashes with this error: Lost connection to MySQL server during query

System is: Windows XP, Apache 2.2 MySQL 5.0
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 04:01:29 2024 UTC