简体   繁体   中英

Php Mysqli Persistent connection error

I have a problem in my script that appen sometimes,usually when i leave it for some time(like 5-10 minutes)without requests i get this error:

Warning: mysqli_connect() [function.mysqli-connect]: MySQL server has gone away in FILE.php on line 30

and this is the code that gives me error is this:

$this->db=mysqli_connect('p:'.$this->db_host,$this->db_user,$this->db_pwd,$this->db_name);

The code is contained on a singleton class and i'm currently developing on windows7 with

  • Apache 2.2.11
  • MySQL 5.1.36
  • PHP 5.3.0

(i use WampServer 2.0i)but i'll obviously use linux on the final server.

Thanks.

It easy... PHP 5.1 doesn't support it

http://php.net/manual/en/mysqli.persistconns.php

requires PHP 5.3

Are you checking for errors after you connect? That might reveal the need for a reconnection. Though looking at the documentation it appears there's no way to force mysqli to create a new connection.

You could use mysqli_ping() to check the server before each call to the database but that's kind of a pain if you have a bunch.

The other thing to do if you admin the MySQL server or are on good terms with the person who does, is to increase the idle timeout on the server itself.

Personally IMHO, persistent connections are buggy in PHP and unless you have a very high-traffic site the increase in speed isn't enough to make it worth the trouble.

Check timeout values in your mysql server especially connect_timeout and wait_timeout.

When Connection closes by timeout you need to reconnect -> that should be done by your application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM