Skip to content

Instantly share code, notes, and snippets.

@harisrozak
Last active October 2, 2020 04:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harisrozak/ce6ff946af1fc032927ab95640c510a1 to your computer and use it in GitHub Desktop.
Save harisrozak/ce6ff946af1fc032927ab95640c510a1 to your computer and use it in GitHub Desktop.
WordPress HyperDB setting for multisite with 3 datasets: global, even_sites, odd_sites
<?php
/**
* HyperDB configuration file
*
* This file should be installed at ABSPATH/db-config.php
*
* $wpdb is an instance of the hyperdb class which extends the wpdb class.
*
* See readme.txt for documentation.
*/
/** Variable settings **/
/**
* save_queries (bool)
* This is useful for debugging. Queries are saved in $wpdb->queries. It is not
* a constant because you might want to use it momentarily.
* Default: false
*/
$wpdb->save_queries = false;
/**
* persistent (bool)
* This determines whether to use mysql_connect or mysql_pconnect. The effects
* of this setting may vary and should be carefully tested.
* Default: false
*/
$wpdb->persistent = false;
/**
* max_connections (int)
* This is the number of mysql connections to keep open. Increase if you expect
* to reuse a lot of connections to different servers. This is ignored if you
* enable persistent connections.
* Default: 10
*/
$wpdb->max_connections = 10;
/**
* check_tcp_responsiveness
* Enables checking TCP responsiveness by fsockopen prior to mysql_connect or
* mysql_pconnect. This was added because PHP's mysql functions do not provide
* a variable timeout setting. Disabling it may improve average performance by
* a very tiny margin but lose protection against connections failing slowly.
* Default: true
*/
$wpdb->check_tcp_responsiveness = true;
/**
* This is a multisite even-odd setting
* using 2 different databases to save the even and odd sites number
*/
$wpdb->add_database( array(
'host' => sprintf( "%s:%d", DB_HOST, 10047 ), // host:port
'user' => DB_USER,
'password' => DB_PASSWORD,
'name' => DB_NAME,
'dataset' => 'global'
) );
$wpdb->add_database( array(
'host' => sprintf( "%s:%d", DB_HOST, 10047 ), // host:port
'user' => DB_USER,
'password' => DB_PASSWORD,
'name' => 'multisite_site_even',
'dataset' => 'even_sites'
) );
$wpdb->add_database( array(
'host' => sprintf( "%s:%d", DB_HOST, 10047 ), // host:port
'user' => DB_USER,
'password' => DB_PASSWORD,
'name' => 'multisite_site_odd',
'dataset' => 'odd_sites',
) );
$wpdb->add_callback( 'resolve_with_dataset' );
function resolve_with_dataset( $query, $wpdb ) {
// Multisite blog tables are "{$base_prefix}{$blog_id}_*"
if ( preg_match("/^{$wpdb->base_prefix}\d+_/i", $wpdb->table) ) {
// break down into just wp_86_ prefix
$prefix_matches = array();
preg_match("/^{$wpdb->base_prefix}\d+_/i", $wpdb->table, $prefix_matches);
$site_table_prefix = $prefix_matches[0];
$site_id_matches = array();
// pull out any numerical matches here
preg_match("/\d+/i", $site_table_prefix, $site_id_matches);
$site_id = $site_id_matches[0];
if ( ( ( int ) $site_id % 2 ) == 0) {
return 'even_sites';
}
else {
return 'odd_sites';
}
}
else {
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment