Loading...


Related Products


Date Published: February 6, 2025

How do I override the default MySQL wait_timeout setting?

Issue

Sometimes, you might have the requirement to override MySQL's default setting for wait_timeout on Acquia Cloud or Site Factory. One possible situation is that your logs may be showing messages like these:

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 2006 MySQL server has gone away in [..snip..]/core/lib/Drupal/Core/Database/[..snip..]

On Acquia Cloud, changing the MySQL timeout can't be done on the MySQL server (as it is a platform-wide setting). However, individual Drupal applications can override that setting on a 'per session' basis, i.e. for every PHP process that bootstraps Drupal and connects to the database.

The way to do this is to add some code in your settings.php file (for Acquia Cloud users), or in your pre and post settings hooks for Acquia Cloud Site Factory. 

Resolution

Acquia Cloud without BLT

The following recommendations are for both Acquia Cloud Platform infrastructure and Acquia Cloud Classic infrastructure environments which do not use Acquia BLT in their code base.

The recommended additions to settings.php start with a + sign:

 if (file_exists('/var/www/site-php')) {
+   global $conf, $databases;   $conf['acquia_hosting_settings_autoconnect'] = FALSE;
   // Use your existing 'require' statement here
   require('/var/www/site-php/XXXXXX/XXXXX-settings.inc');
  
+   // For Cloud Platform you also need to raise interactive_timeout
+   // See https://ahmedahamid.com/amazon-aurora-mysql-and-wait-timeout/
+   $databases['default']['default']['init_commands'] = array(
+     'wait_timeout' => 'SET SESSION wait_timeout=1200',
+     'interactive_timeout' => 'SET SESSION interactive_timeout=1200',
+   );
+  if (function_exists('acquia_hosting_db_choose_active')){
+     acquia_hosting_db_choose_active(
+       $conf['acquia_hosting_site_info']['db'],
+       'default',
+       $databases,
+       $conf
+     );
+   }
 }

Acquia Cloud using BLT

The following recommendations are for both Acquia Cloud Platform infrastructure and Acquia Cloud Classic infrastructure environments that use Acquia BLT in their code base.

If you are using BLT, look for your existing require...blt.settings.php statement and add the lines that start with a + sign around it.

 if (file_exists('/var/www/site-php')) {
+  global $conf, $databases;
+  $conf['acquia_hosting_settings_autoconnect'] = FALSE;

  // Use your existing 'require' statement here 
  require DRUPAL_ROOT . "/../vendor/acquia/blt/settings/blt.settings.php";

+  $databases['default']['default']['init_commands'] = array( 
+    'wait_timeout' => 'SET SESSION wait_timeout=1200',
+    'interactive_timeout' => 'SET SESSION interactive_timeout=1200',
+  );
+  if (function_exists('acquia_hosting_db_choose_active')){
+    acquia_hosting_db_choose_active(
+      $conf['acquia_hosting_site_info']['db'],
+      'default',
+      $databases,
+      $conf
+    );
+  }
 }

Site Factory users

For Site Factory users the above snippet needs to be split in two. The first snippet should be placed in your pre-settings-php hook, and the latter on your post-settings-php hook:

pre-settings-php

global $conf, $databases;
$conf['acquia_hosting_settings_autoconnect'] = FALSE;
post-settings-php
$databases['default']['default']['init_commands'] = array(
  'wait_timeout' => "SET SESSION wait_timeout=1200",
  'interactive_timeout' => "SET SESSION interactive_timeout=1200",
);
acquia_hosting_db_choose_active();

Checking the current MySQL variables

Once you have the appropriate snippet in your code base, you can confirm that the overrides are working for your application by running the following drush eval command below. You can use this command as a starting point and modify it as needed for your application's purposes.
 
 The command outputs the MySQL variables through the Drupal database connection being used in settings.php, for Acquia Cloud or pre-settings-php hook, and post-settings-php hook, for Site Factory.
 

Note

You might not see the override values with a drush sql-cli session, which is why the Drupal\Core\Database\Database::getConnection function is being used.

# Edit the Site URL
# Remove the last |egrep... section to see all variables. 
drush --uri=[Site-URL] eval '$x = (array)Drupal\Core\Database\Database::getConnection("default", "default")->query("SHOW VARIABLES;")->fetchAll(); foreach ($x as $v) { echo $v->Variable_name . " => " . $v->Value . PHP_EOL; }' |egrep '^(wait|interactive)_timeout'

This should show the values you've overridden above.

An example output:

interactive_timeout => 1200wait_timeout => 1200
 

Did not find what you were looking for?

If this content did not answer your questions, try searching or contacting our support team for further assistance.

Back to Section navigation