29

Recently I started getting an error every time I try and add a content no matter what type. This is the error that shows up in the browser:

Additional uncaught exception thrown while handling exception.

Original PDOException: SQLSTATE[HY000]: General error: 2006 MySQL server has gone away: SELECT COUNT(cid) FROM {comment} WHERE status = :status; Array ( [:status] => 0 ) in comment_count_unpublished() (line 313 of /Applications/MAMP/htdocs/modules/comment/comment.module).

Additional

PDOException: SQLSTATE[HY000]: General error: 2006 MySQL server has gone away: INSERT INTO {watchdog} (uid, type, message, variables, severity, link, location, referer, hostname, timestamp) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7, :db_insert_placeholder_8, :db_insert_placeholder_9); Array ( [:db_insert_placeholder_0] => 1 [:db_insert_placeholder_1] => php [:db_insert_placeholder_2] => %type: !message in %function (line %line of %file). [:db_insert_placeholder_3] => a:6:{s:5:"%type";s:12:"PDOException";s:8:"!message";s:154:"SQLSTATE[HY000]: General error: 2006 MySQL server has gone away: SELECT COUNT(cid) FROM {comment} WHERE status = :status; Array ( [:status] => 0 ) ";s:9:"%function";s:27:"comment_count_unpublished()";s:5:"%file";s:56:"/Applications/MAMP/htdocs/modules/comment/comment.module";s:5:"%line";i:313;s:14:"severity_level";i:3;} [:db_insert_placeholder_4] => 3 [:db_insert_placeholder_5] => [:db_insert_placeholder_6] => http://localhost:8888/?q=node%2Fadd%2Farcade-project& [:db_insert_placeholder_7] => http://localhost:8888/ [:db_insert_placeholder_8] => 127.0.0.1 [:db_insert_placeholder_9] => 1338336955 ) in dblog_watchdog() (line 154 of /Applications/MAMP/htdocs/modules/dblog/dblog.module).

I also get the following error in my php error file:

PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 2006 MySQL server has gone away' in /Applications/MAMP/htdocs/includes/database/database.inc:2136

Stack trace:
0 /Applications/MAMP/htdocs/includes/database/database.inc(2136): PDOStatement->execute(Array)
1 /Applications/MAMP/htdocs/includes/database/database.inc(664): DatabaseStatementBase->execute(Array, Array)
2 /Applications/MAMP/htdocs/includes/database/database.inc(2315): DatabaseConnection->query('SELECT expire, ...', Array, Array)
3 /Applications/MAMP/htdocs/includes/lock.inc(167): db_query('SELECT expire, ...', Array)
4 /Applications/MAMP/htdocs/includes/lock.inc(146): lock_may_be_available('theme_registry:...')
5 /Applications/MAMP/htdocs/includes/theme.inc(447): lock_acquire('theme_registry:...')
6 /Applications/MAMP/htdocs/includes/bootstrap.inc(427): ThemeRegistry->set(Array)
7 [internal function]: DrupalCacheArray->__destruct()
8 {main} thrown in /Applications/MAMP/htdocs/includes/database/database.inc on line 2136

I am having a little trouble deciphering these error messages. Could anyone help me understand what's going on here?

4 Answers 4

39

9 times out of 10 this error is caused by an insufficiently large max_allowed_packet setting in your MySQL server's my.cnf file.

The standard default for MAMP in my experience is 1M, which isn't usually enough for Drupal (especially considering the size of some of the cache strings it generates).

Try increasing that setting gradually to see if the error disappears. I'm sure a quick google search will tell you how to do that in MAMP.

4
  • Luckily my case did not fall into the 1 times out of 10 category. Your recommendations worked like a charm! Thanks!
    – Mike2012
    Commented May 30, 2012 at 16:52
  • Facing same error in windows. Have configured apache,mysql and php stack locally...please guide Commented Apr 10, 2013 at 7:22
  • And in case of shared hosting, what can i do? Commented Dec 23, 2015 at 14:50
  • Contact your host, ask them to make the change. If they can't/won't, you need to decide whether this host is the right one for you @mohammedaminebourkadi.
    – Clive
    Commented Dec 23, 2015 at 15:03
7

This MySQL/MariaDB error:

Error: 2006 (CR_SERVER_GONE_ERROR) - MySQL server has gone away

means basically that the client couldn't send a question to the server.

This could happen either by temporary glitch, too big or invalid SQL query, misconfiguration of your server or limitation of your hosting provider.

In general this error could be result of several things, such as:

  • a query to the server is incorrect or too large,

    Solution: Increase max_allowed_packet variable.

    Note: Make sure the variable is under [mysqld] section, not [mysql].

    Note: Don't forget to restart the MySQL/MariaDB server.

  • You got a timeout from the TCP/IP connection on the client side.

    Solution: Increase wait_timeout variable.

  • You tried to run a query after the connection to the server has been closed.

    Solution: A logic error in the application should be corrected.

  • Host name lookups failed (e.g. DNS server issue), or server has been started with --skip-networking option.

    Another possibility is that your firewall blocks the MySQL port (e.g. 3306 by default).

  • The running thread has been killed, so retry again.

  • You have encountered a bug where the server died while executing the query.

  • A client running on a different host does not have the necessary privileges to connect.

  • And many more, so learn more at: B.5.2.9 MySQL server has gone away.

For further details, please check your MySQL or system logs (e.g. /var/log/messages).

To debug MySQL server or client, please check: 26.5 Debugging and Porting MySQL.


In case you're trying to import the database from the file using drush or mysql command, you can:

  • Add a force option (-f) for mysql to proceed and execute rest of the queries.

    This is useful if the database has some large queries related to cache which are large, but not relevant anyway.

    Using drush, try: cat foo.sql | $(drush sqlconnect) -f

  • Try applying --max-allowed-packet option for mysql with smaller values.

  • Increase max_allowed_packet and wait_timeout in your server config (e.g. ~/.my.cnf).

  • Dump the original database again by using --skip-extended-insert option to break down the large queries. Then import the file again.


See also: ERROR 2006 (HY000): MySQL server has gone away

3

The answer that @Clive gives normally is the case, but there can be an additional cause, especially with node add forms.

Node add forms typically are big, and processing them can use a lot of memory (especially if there is any image processing during the save, like with the cropping modules). If the server runs out of memory, the mysqld process may get killed off which results in the same "gone away" message.

The clue is to look in the server logs. On a CentOS machine, you may see the following entry in /var/log/messages

Jan 1 00:00:00 servername kernel: Out of memory: Kill process XXXX (mysqld) score XXX or sacrifice child
Jan 1 00:00:00 servername kernel: Killed process XXXX, UID XX, (mysqld) total-vm:XXXkB, anon-rss:XXXkB, file-rss:XXkB

The solution here is to either add more RAM, or add/increase swap.

0

I've had great success in fixing the 2006 MySQL server has gone away error with the following Drupal module:

Asynchronous Prefetch Database Query Cache

Just follow the installation instructions, which requires adding a few lines of code to settings.php and then visit admin/reports/status and apply advice given.

Most times, just by enabling the module and adding code to settings.php the error goes away straight away.

1
  • Remember to SELECT @@innodb_buffer_pool_size; to review current size and an increase of 10% in your my.cnf (or my.ini) will frequently eliminate the problem. Commented Jan 7, 2020 at 0:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.