Using Multiple Databases for Wordpress with LudicrousDB and HyperDB

HyperDB or LudicrousDB?

How To Run LudicrousDB

// A
$wpdb->add_database( array(
'host' => DB_HOST,
'user' => DB_USER,
'password' => DB_PASSWORD,
'name' => DB_NAME,
) );
// B
$wpdb->add_database( array(
'host' => DB_HOST,
'user' => DB_USER,
'password' => DB_PASSWORD,
'name' => DB_NAME,
'write' => 0,
'read' => 1,
'dataset' => 'global',
'timeout' => 0.2,
) );
host: The DB server. If DB is on the same server it should be localhost and if it’s on another server just use its IP address or domain name along with port number. IP-ADDRESS:PORT / Default port is 3306. (required)user: DB username (required)password: DB password (required)name: DB name (required)write: Can WP write on this DB? Yes = 1, No = 0 / Default is 1 (optional)read: Can WP read data from this DB? Yes = 1, No = 0 / Default is 1 (optional)dataset: Just a name for a group of tables in DB / Default is 'global' (optional)timeout: Connection timeout / Default is 0.2 (optional)
// A
$wpdb->add_database( array(
'host' => DB_HOST,
'user' => DB_USER,
'password' => DB_PASSWORD,
'name' => DB_NAME,
) );
// B
$wpdb->add_database( array(
'host' => db.example.com:3306,
'user' => testserver,
'password' => 5kjh4k8#$#@%9,
'name' => secondDB,
'write' => 1,
'read' => 1,
'dataset' => 'wpposts'
) );
global $wpdb;
$wpdb->add_callback( 'db_callback' );
function db_callback( $query, $wpdb ) {
if ( $wpdb->base_prefix . 'posts' == $wpdb->table || $wpdb->base_prefix . 'postmeta' == $wpdb->table ) {
return 'wpposts';
}
}
$wpdb->add_callback( 'db_callback2' );
function db_callback2( $query, $wpdb ) {
if ( preg_match("/^{$wpdb->base_prefix}\d+_posts/", $wpdb->table) || preg_match("/^{$wpdb->base_prefix}\d+_postmeta/", $wpdb->table) ) {
return 'wpposts';
}
}
$wpdb->add_callback( 'db_callback3' );
function db_callback3( $query, $wpdb ) {
if ( preg_match("/^{$wpdb->base_prefix}\gf_/", $wpdb->table) || preg_match("/^{$wpdb->base_prefix}\d+_gf_/", $wpdb->table) ) {
return 'wpposts';
}
}
Logo

华为、百度、京东云现已入驻,来创建你的专属开发者社区吧!

更多推荐