php - Better way to keep mysql connection alive other than pinging -


i'm sure lot of developers have run dreaded "mysql has gone away" issue, when dealing long running scripts such reserved background or cron jobs. caused connection between mysql , php being dropped. best way prevent happening?

i use custom cdbconnection class setactive method straight out of here:

http://www.yiiframework.com/forum/index.php/topic/20063-general-error-2006-mysql-server-has-gone-away/page__p__254495#entry254495

this worked great , has stopped mysql gone away issue. unfortunately, i've been running random issue after inserting new record database via cactiverecord, yii fails set primary key value properly. end pk value of 0. looked issue more , able reproduce on local machine. seems custom cdbconnection::setactive() method might culprit. when run cactiverecord::save() method, yii prepares necessary sql , executes via pdo. after this, yii uses pdo::lastinsertid() grab latest inserted id , populates models pk attribute. happens though if whatever reason, initial insert command takes more few seconds complete? triggers mysql ping action in custom setactive() method, waits 2 second difference between current timestamp , last active timestamp. noticed when pdo insert query, followed pdo select query, pdo::lastinsertid(), end last insert id value of 0.

i can't if what's happening on our live servers issue randomly occurs it's been way have been able reproduce it.

there many reasons server has gone away error, documented in mysql documentation. couple common tricks try are:

  • increase wait_timeout in my.cnf file. see innodb_lock_wait_timeout , lock_wait_timeout if locks need remain locked longer period of time.

    the number of seconds server waits activity on noninteractive connection before closing it.

  • increase max_allowed_packet in my.cnf file. large data packets can trip connection , cause closed abruptly.

    you must increase value if using large blob columns or long strings. should big largest blob want use. protocol limit max_allowed_packet 1gb. value should multiple of 1024; nonmultiples rounded down nearest multiple.


Comments