Lost connection to MySQL server during query

I have this intermitent problem…

Warning: mysql_connect() [function.mysql-connect]: Lost connection to MySQL server during query in /var/www/localhost/htdocs/xyz.php on line 32

Can not connect to the database. Lost connection to MySQL server during query

I can get rid of this error by setting register_globals on in my php.ini but I want to know if there is a real fix without doing this.

Here is a sample of what I am doing in my xyz.php file to connect

define('LOC','LOC');

define('USER', 'USER');

define('PASS', 'PASS');

define('DB', 'DB');

mysql_connect(LOC,USER,PASS)

or die("Can not connect to the database. " . mysql_error());

mysqlselectdb(DB)

or die("Can not select database. " . mysql_error());

24 Replies

@fifo:

define('LOC','LOC');

define('USER', 'USER');

define('PASS', 'PASS');

define('DB', 'DB');

mysql_connect(LOC,USER,PASS)

or die("Can not connect to the database. " . mysql_error());

mysqlselectdb(DB)

or die("Can not select database. " . mysql_error());

I normally have something like:

$LOC = "LOC";

$USER = "USER";

$PASS = "PASS";

$DB = "DB";

$link = mysql_connect($LOC, $USER, $PASS)

or die("Can't Connect");

mysqlselectdb($DB, $link) or die("Can't Connect");

That kind of setup has always worked for me.

It seems to be the same thing.

I't does work however about 25 % of the times I am accessing the site I get that error. But the other 75% it works fine…

Let me check if I have the $link variable in there I might and I will get back as soon as I have access to my files..

mysqlselectdb($DB, $link) or die("Can't Connect");

Yes I realise that they are basically the same, I just was thinking that having $link = mysql_connect($LOC, $USER, $PASS) rather than just mysql_connect($LOC, $USER, $PASS) and also having mysql_select_db($DB, $link) helps because it explicitly uses that link to connect that way so you are less likely to lose your connection. I'm not sure if it actually makes a difference as I have never had any problems, perhaps it is a problem with your mysql server, this however is just speculation on my part but maybe you should check your apache and MySQL logs.

ok that sounds like a reasonable explanation. I dont think its my MySql installation because I just upgraded to the latest and greatest and I a still getting the error. Its kind of wierd …

Either way thanks for the help im going to try that out and let you know how it goes…

Ok so I tried it out but I am still getting those errors every once in a while. I will however leave my connection as you said because it makes more scence than what I was doing.

Can I pick your brain for any more Ideas?

I looked in my /etc/php directory and I still had some files from my old php4 installation apache2-php4 or something of that sort. I removed that and rebooted and well it hasnt done it since. Ist there an explanation for this? where the configurations fighting against each other? Well its probalbly too soon to say its working but I have been trying to crash it and it hasnt so far…. lets just wait and see…

I'd say it's possible that were conflicts arising from having multiple configuration type things present but I couldn't be sure, getting rid of stuff you don't need anymore can't be a bad thing though I would say. If you have any other problems then I'd be happy to help, if I can.

Good Luck 8)

Great thank you!! Everthing is still working great!!!

Glad to be of help. I'm normally seeking help so it's nice to help someone. I wish you all the best. :)

Ok I guess that really didnt fix the probelm. It happened again today and it brought the server down as it has done before in the past. Any other suggestiong? in the mean time I will try to enable register_globals

Even the register_globals does nothing for me. Got the error again any suggestions?

http://dev.mysql.com/doc/refman/5.0/en/gone-away.html

So I recompiled with the debug flag and now I can see the so called trace. Im not sure what to make of it but this seems to be where it is getting stuck. Something about a vio error? I dont see a stack trace like the one mentioned in the documentation as noted above. I have tried tweeking some of the timeout values but ended up putting them back as that did not help. Im going crazy with this. Funny thing is I have the exact code running on another server and it works fine… So I dont hink it has anything to do with the queries I am running… Either way I am open eyes and ears about this Im not sure what I should try next. Once I get this the server either hangs or crashes…

6384: | | | | | | | | exit: ptr: 0x8cc8178

T@16384: | | | | | | | <_mymalloc

T@16384: | | | | | | | >_myfree

T@16384: | | | | | | | | enter: ptr: 0x8cc18b0

T@16384: | | | | | | | <_myfree

T@16384: | | | | | | <_myrealloc

T@16384: | | | | | <miextra
T@16384: | | | | threadtable
T@16384: | | | threadtables
T@16384: | | | >free
root

T@16384: | | | | enter: root: 0x8ccdfcc flags: 1

T@16384: | | | root
T@16384: | | command
T@16384: | | >do_command

T@16384: | | | >vioisblocking

T@16384: | | | | exit: 0

T@16384: | | | isblocking
T@16384: | | | >vio_read

T@16384: | | | | enter: sd=16, buf=0x8ccf388, size=4

T@16384: | | | | vio_error: Got error 11 during read

T@16384: | | | | exit: -1

T@16384: | | | <vioread
T@16384: | | | info: vio
read returned -1, errno: 11

T@16384: | | | >thr_alarm

T@16384: | | | | enter: thread: T@16384 sec: 28800

T@16384: | | | | info: reschedule

T@131081: | >process_alarm

T@131081: | | info: sig: 14 active alarms: 1

T@131081: | alarm
T@16384: | | | alarm
T@16384: | | | >vio_blocking

T@16384: | | | | enter: setblockingmode: 1 old_mode: 0

T@16384: | | | | exit: 0

T@16384: | | | <vioblocking
T@16384: | | | >vio
read

T@16384: | | | | enter: sd=16, buf=0x8ccf388, size=4

vio_read

T@16384: | | | | enter: sd=16, buf=0x8ccf388, size=4

T@16384: | | | | vio_error: Got error 11 during read

T@16384: | | | | exit: -1

mysqld.err doesnt say much at least nothing about an error:q!

InnoDB: !!!!!!!!!!!!!! UNIV_DEBUG switched on !!!!!!!!!!!!!!!

060412 18:22:28 InnoDB: Started; log sequence number 0 47123

/usr/sbin/mysqld: ready for connections.

Version: '4.1.14-debug-log' socket: '/var/run/mysqld/mysqld.sock' port: 3306 Gentoo Linux mysql-4.1.14-r1

mysql.err says nothing same for mysql.log and mysqld.log

this is what I get when I try to run status on mysqladmin

mysqladmin -i 5 -r status

Uptime: 1295 Threads: 2 Questions: 1987 Slow queries: 0 Opens: 134 Flush tables: 1 Open tables: 43 Queries per second avg: 1.534 Memory in use: 17001K Max memory used: 19577K

Uptime: 1300 Threads: 4 Questions: 2049 Slow queries: 0 Opens: 134 Flush tables: 1 Open tables: 43 Queries per second avg: 1.576 Memory in use: 17117K Max memory used: 19577K

Uptime: 1305 Threads: 2 Questions: 2115 Slow queries: 0 Opens: 134 Flush tables: 1 Open tables: 43 Queries per second avg: 1.621 Memory in use: 17001K Max memory used: 19577K

Uptime: 1310 Threads: 4 Questions: 2184 Slow queries: 0 Opens: 134 Flush tables: 1 Open tables: 43 Queries per second avg: 1.667 Memory in use: 17116K Max memory used: 19577K

Lost connection to MySQL server during query

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)blocking
alarm
alarm
read
is
blocking
command
root
threadtables
threadtable

I enabled mysql.trace_mode in the php.ini file like so

mysql.trace_mode = On

and this is what I got on my page. Why would this be happening?

Warning: Unknown: 1 result set(s) not freed. Use mysqlfreeresult to free result sets which were requested using mysql_query() in Unknown on line 0

is this just the current queries on the page? thats what it seems like to me… Should they be closed once the page is shown?

I think I may have found my problem

I wasn't releasing my result sets from my queries. like you saw in the warnings above. Im not sure why this would make mysql crash but it did. I thought the result sets automatically closed upon exit of the query or the page? Or even time out after a while.

Here was the solution

mysqlfreeresult("QUERY VARIABLE HERE");

?>

Problem is back! even with all my pages releasing the results. I also tried closing the mysql connection but that didnt seem to help either. I have not done this to all my pages though. Is it advisable to close my connection on every page? I do multiple quieries to the same database. :?

Have you ran mysqlcheck (or whatever it's called) on all your tables?

Just a thought…

-Chris

No actualy I havent I will try that and get back to you.

2 clients are using or haven't closed the table properly

this is all I get but its on acouple of tables…

did it a couple of times more and evretrything checks out ok

This might give us a clue.

I have max media manager installed after getting errors so I know its not because of this and I get this whenever I try to see statistics.

Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 35 bytes) in /var/www/localhost/htdocs/xyz/max-v0.3.21-alpha/lib/pear/HTML/Template/Flexy/Tokenizer.php on line 1197

seems that is a different error I just upped the memory to 16M instead of the default 8 and I got rid of the problem above.

Havent had mysql crash today yet but I dont think the two problems were related.

Server still crashing randomly no Idea what to try next. The code seems to work great on the development machine without a problem.

Development machine has same setup gentoo os and mysql versions are identical.

I just unmerged removed all traces of mysql nd reinstalled mysql 5.0… still seems to have the same problem.

is this a bad setup for a production server?

Directive Local Value Master Value

mysql.allow_persistent On On

mysql.connect_timeout 60 60

mysql.default_host no value no value

mysql.default_password no value no value

mysql.default_port no value no value

mysql.default_socket no value no value

mysql.default_user no value no value

mysql.max_links Unlimited Unlimited

mysql.max_persistent Unlimited Unlimited

mysql.trace_mode On On

mysql.trace_mode I just turned this off

host 45 reboot this morning might have solved my problem.

I can't pinpoint it though because I un-merged mysql removed all databases and re-emerged mysql then restored my databases.

everything was running until host 45 just stopped responding at around 7:12 EST.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct