6

I get this error so frequently that the php_error log file increases 1MB every 2 seconds. and the site is very slow.

I have tried to add this line to wp-db.php

$this->query("set session wait_timeout=600" );

but it did not help.

The web server is IIS 7, latest version of mysql and wordpress

4
  • 1
    "MySQL server has gone away" means the database server keeps timing out or otherwise closing the connection. Are you on shared hosting? Perhaps your shared database server is stretched beyond its capacity. Commented Mar 28, 2013 at 23:26
  • no, it's a dedicated windows server Commented Mar 28, 2013 at 23:45
  • Are you running stock WP? Poorly written plug-ins can slam the DB server with heavy queries. Did you make any changes before this issue began? Commented Mar 28, 2013 at 23:50
  • I have updated some plugins, but wp is very unresponsive since I have installed windows 2008 server and mysql 5.2, in 2003 server it was ok. Commented Mar 29, 2013 at 0:34

3 Answers 3

7

This trick will work for all WordPress versions. Open your Wordpress directory. The directory will contain the folders :

wp-admin
wp-content
wp-includes

Open wp-includes. Search for the wp-db.php file. If you found the file, open it using a text editor. Using the text editor's search tool, search for :

$this->ready = true;

Once you found the line, add the following lines just after the found line :

//WP Query Gone Away Error Fix
$this->query("set session wait_timeout=600");

You can also follow https://subinsb.com/fix-wordpress-error-mysql-server-has-gone-away link to view in more detials.

Sign up to request clarification or add additional context in comments.

2 Comments

I found several publications about this error, but none were as clear and precise as yours. Thank you!
Fantastically clear and more importantly, seems to have worked. Cheers mate.
3

Run SHOW STATUS WHERE Variable_name LIKE '%onn%' to see how many open connections you have. Mine looks like this:

Variable_name        Value
Aborted_connects         1
Connections            629
Max_used_connections     3
Ssl_client_connects      0
Ssl_connect_renegotiates 0
Ssl_finished_connects    0
Threads_connected        2

Run SHOW PROCESSLIST to see what processes are active.

The problem may be that you are opening too many connections and not closing them. Running these commands will at least give insight into what may be the issue.

9 Comments

Aborted_connects 1 Connections 1556 Max_used_connections 18 Ssl_client_connects 0 Ssl_connect_renegotiates 0 Ssl_finished_connects 0 Threads_connected 9
It seems that I have many processes open of the mailserver, I think someone is spamming my server.
I have installed APC cache extension for PHP and W3 Cache for WP and now runs fast and no errors: www.paper-backgrounds.com
Cool site. Update your copyright with <?=date("Y");?> for the year.
Connections is number of connections attempted since instance started. Divide by uptime for connections per second average. SHOW GLOBAL STATUS LIKE 'uptime'; will give you seconds since instance started.
|
1

In my case I had a huge database (4.5 GB) imported from 5.7 to 8.0 and when running wp search-replace the error MySQL server has gone away occurred on wp-cli's queries on huge tables. So I've changed the /etc/mysql/conf.d/custom.cnf to the following:

[mysqld]

datadir = /var/lib/mysql/
socket = /var/run/mysqld/mysqld.sock
pid_file = /var/run/mysqld/mysqld.pid

tmp_table_size = 256M
max_heap_table_size = 256M
max_connections = 2000
open_files_limit = 100000

innodb_buffer_pool_instances = 8

# since 8.0 takes care of:
# innodb_buffer_pool_size
# innodb_log_file_size
# innodb_log_files_in_group
# innodb_flush_method
#https://dev.mysql.com/doc/refman/8.0/en/innodb-dedicated-server.html:
#Only consider enabling innodb_dedicated_server if the MySQL instance resides on a dedicated server where it can use all available system resources. For example, consider enabling if you run MySQL Server in a Docker container or dedicated VM that only runs MySQL. Enabling innodb_dedicated_server is not recommended if the MySQL instance shares system resources with other applications.
innodb_dedicated_server = On

innodb_buffer_pool_dump_at_shutdown = 1
innodb_buffer_pool_load_at_startup = 1

max_connections = 500

Since then not only the import of the sql file went 10 times faster (only 10 minutes by making advantage of the full machine's 50-60 GB memory), but also the wp-cli's queries now run without any issues.

Though, the key setting that helped me to get rid of the error (innodb_dedicated_server = On) is only applicable on docker containers and VPSs / dedicated servers running only MySQL. If the mysql server is running along other servers (PHP, Nginx etc) on one machine / in one container then you need to adjust the four values that innodb_dedicated_server is taking care of by yourself.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.