Community Documentation

Increase MySQL max_allowed_packet for MySQL

Last updated August 18, 2009. Created by ainigma32 on February 22, 2009.
Edited by mvc, kenorb, sime. Log in to edit this page.

This mini module will set the max_allowed_packet session variable for MySQL at the start of each page view.
It is meant as a solution for environments where you don't have access to the global max_allowed_packet variable and the provider can't or won't increase it for you.

Note that this no longer works with MySQL 5.0.x or newer. Setting this variable per session is allowed in 4.1.x but is ignored in 5.1.x and ignored in 5.0.x. Further, this variable is read-only as of MySQL 5.0.84 and 5.1.31. You can check which version of MySQL you are running on your site's status report, at admin/reports/status.

The info file (save as max_packet.info):

name = Max packet
description = Utility module that tries to increase the size of the max_allowed_packet session variable for MySQL.
core = 6.x

Make sure the description is on one line otherwise Drupal will mark the module as incompatible

The module file (save as max_packet.module):

<?php
/**
* Implementation of hook_init().
*/
function max_packet_init(){
 
//Set the value to 16 MB.
 
db_query('SET SESSION max_allowed_packet=16*1024*1024');
}

/**
* Implementation of hook_help().
*/
function max_packet_help($path, $arg) {
 
$output = '';
  switch (
$path) {
    case
"admin/help#max_packet":
     
//Retrieve the current value to see if the module is working
     
$result db_query("SHOW VARIABLES LIKE 'max_allowed_packet'");
      while (
$variables = db_fetch_object($result)) {
       
$current_max_packet = $variables->Value / (1024 * 1024);
      }     
     
$output = '<p>'t("Current value of max_allowed_packet: !current_max_packet MB", array('!current_max_packet' => $current_max_packet)) .'</p>';
      break;
  }
  return
$output;
}
?>

When you look at the help page of this module you can tell if the session variable was set successfully.

Alternatively you can use existing Db Tweaks module:
http://www.drupal.org/project/db_tweaks

Page status

No known problems

Log in to edit this page

About this page

Drupal version
Drupal 6.x
Audience
Developers and coders, Site administrators

Reference

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.
nobody click here