Posted: 2015-02-22 20:13:02
If you are having a reoccurring problem where Mysql refuses connections with the following error message:
Warning: mysql_pconnect(): MySQL server has gone away in C:\WampDeveloper\Websites\domain.name\webroot\script\file.php on line X
Note that you are using
To fix this, you have several different options:
Tell PHP to silently use non-persistent connections even when persistent connections are specified…
Edit php.ini (C:\WampDeveloper\Config\Php\) and update or add this into the
; Allow or prevent persistent links. mysql.allow_persistent=Off
This will close each connection when the script ends, regardless if “
mysql_pconnect” or “
mysql_connect” is used.
Don’t use the persistent versions of PHP functions and methods for MySQL connections. Persistent connections are never recommended, and should only be used in special circumstances (because they tend to just cause connection exhaustion while offering no real performance gains).
Instead of “
mysql_pconnect” just use “
mysql_connect“. Both functions use the same exact API (i.e., parameters), and you can safely search-and-replace all occurrences of “
mysql_pconnect(” with “
mysql_connect(” in your script files.
Also note that the above functions are depreciated starting with PHP 5.5, and will be removed from PHP in later versions. If you can, switch to using PDO methods.
Otherwise, increase the number of allowed connections…
Edit my.ini (C:\WampDeveloper\Config\Mysql\) and add this into the
Increase the number of MySQL connections allowed:
max_connections = 151
* The above is the default value.
Depending on the situation, decrease the persistent connection timeout value (so those persistent connections are closed and recycled faster):
interactive_timeout = 28800 wait_timeout = 28800
* The above are the default values, in seconds (8 hours).
These settings might not exist in my.ini, you have to add them into the
[mysqld] section of the file.
1. These are the default values for each setting… If you put them “as-is”, they will have no effect… As those settings + values, even while they might not be present in my.ini, are already set by default by MySQL for itself on each start-up.
2. These values don’t technically fix anything, they just try to mitigate around some underlining problems with connection timeouts caused by issues in the script. They are temporary patchwork (in most cases).
3. Take care setting these values correctly. max_connections might need to be doubled, and/or the timeout value might need to be lowered to 300 (seconds). But if you increase or decrease these values too much, other issues could start happening.
4. Don’t use these settings if they do not fix anything.
5. You’ll need to restart Apache and MySQL after making changes to my.ini and php.ini.