One of the biggest problems i have with Wordpress is its database. The more data is saved on it, the slower it becomes and have bad effects on the whole website and user experience and results in a huge decrease in the number of visitors and users. So, what can we do to make it run like a swiss watch? if you search for this issue, you’ll find lots of articles telling you things like, use less plugins, optimize DB with a plugin, use CDN[totally irrelevant], do this, do that and etc. OK, these are good but do you know what can make your Wordpress more awesome? Using multiple databases for a single Wordpress website. YES, You read that correctly. Here i want to show you how i connected more than one DB to one WP website and as a result we could handle a huge amount of users at the same time and made everyone happier. Let’s go.
HyperDB or LudicrousDB?
HyperDB is a very advanced database class that replaces a few of the WordPress built-in database functions. It can be connect to an arbitrary number of database servers.
It supports: 1- Read and write servers (replication) 2- Configurable priority for reading and writing 3- Local and remote datacenters 4- Private and public networks 5- Different tables on different databases/hosts 6- Smart post-write master reads 7- Failover for downed host 8- Advanced statistics for profiling 9- WordPress Multisite
It is based on the code currently used in production on WordPress.com with many MySQL servers spanning multiple datacenters.
HyperDB is created by Automatic but it hasn’t been updated for a while and that is why it can not work properly for some users[Maybe it can work for you]. So what’s the alternative? LudicrousDB!
LudicrousDB is an advanced database interface for WordPress that supports replication, fail-over, load balancing, and partitioning, based on Automattic’s HyperDB drop-in.
As you read, you can do a lot with Wordpress DB. In this method we will connect WP to 2 databases and move some of the tables to another database so as to make a balance and keep website up in high traffic hours.
Note: The procedure is almost the same for both LudicrousDB and HyperDB. So if you learn how to work with LudicrousDB you can also work with HyperDB.
How To Run LudicrousDB
1- To start, download LudicrousDB from their Github page and upload it to Wordpress panel from “Add Plugin” page.
2- Do not activate the plugin after installing. Copy the db.php from /wp-content/plugins/ludicrousdb/ludicrousdb/drop-ins/ and paste it inside wp-content directory.
3- From the same path find the file db-config.php and copy it to the root of your Wordpress directory where the wp-config.php is.
Now all of the files we need are ready to setup. It’s time to configure WP DB. I assume that you have another database on a new server with all the necessary information for access.
4- Open db-config.php in the WP root and scroll down to the end of thefile to see these codes:
// 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,
) );
As you see, with add_database you can add as many database as you need. But what are the parameters?
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)
5- The First part of this code is for connecting WP to the main DB. So we leave the code bellow as is and no change is needed unless it’s on another server which is simple to configure.
// A
$wpdb->add_database( array(
'host' => DB_HOST,
'user' => DB_USER,
'password' => DB_PASSWORD,
'name' => DB_NAME,
) );
6- Let’s add another DB using the second part of our code.
// B
$wpdb->add_database( array(
'host' => db.example.com:3306,
'user' => testserver,
'password' => 5kjh4k8#$#@%9,
'name' => secondDB,
'write' => 1,
'read' => 1,
'dataset' => 'wpposts'
) );
Easy, Right? I just removed the timeout parameter, since the default value was fine for me. But you can add and change it if you want.
Now that our WP is connected to 2 DBs, we want to move some our tables from main DB to 2nd DB and tell WP to read and write data from 2nd DB. In this example those tables are where WP stores posts and metadata.
7- Using PHPMyadmin, export wp_posts and wp_postmeta tables from main DB and import them to the 2nd DB. (You can drop those tables after tests)
8- Now we need add_callback. With add_callback we filter WP tables and tell WP to read and write data of those tables from 2nd table.
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';
}
}
What if you have a multisite WP?
$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';
}
}
Another example for multisite DB for gravity forms tables:
$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';
}
}
Congratulations. That’s it. You have successfully connected Wordpress to two databases. In case you have an online shop with a huge number of visitors and customers you can move WooCommerce tables to another server and set more resources for WooCommerce with this method.
Need to do more with LudicrousDB? Checkout their Github page. The procedure is almost the same for HyperDB.
This method is tested on Wordpress 6.0, PHP 7.4 and MYSQL 8.0.



所有评论(0)