2

I have a WordPress website. I want to split my existing database into two parts and store in a remote database.

How can I set up a remote database connection for existing WordPress?

If it's not possible to split existing database, how can I connect a remote database for future records?

Thank you.

2
  • 3
    Of note, this will be quite slow, and you won't be able to store some WordPress tables but not others in different databases, e.g. you can't put the posts table on one server, and the user table on another. Perhaps some context on why you want to do this would be helpful
    – Tom J Nowell
    Mar 17, 2015 at 17:18
  • 1
    +1 to @TomJNowell 's point. Additionally, it's a bad design pattern to segment data like this; keep your data together, and work on infrastructure to improve performance.
    – user12479
    Mar 17, 2015 at 17:42

1 Answer 1

1

You can set up another connection and use that database the same way you would interact with the WP database with $wpdb.

This example creates an instance in $odb to be used the same as you would use $wpdb:

global $odb;
$db_user = "Your DB Username";
$db_pass = "Your DB Password";
$db_name = "Your DB Name";
$db_host = "Your DB Hostname";
$odb = connect_to_intermediary_db( $db_user, $db_pass, $db_name, $db_host );

function connect_to_intermediary_db( $db_user, $db_pass, $db_name, $db_host ) {
    $db = new wpdb( $db_user, $db_pass, $db_name, $db_host );
    return $db;
}

/*
 * You can now use $odb the same as you would
 * $wpdb, but it is connected to your custom
 * database.
 */
2
  • There is no connect_to_intermediary_db in WordPress
    – Tom J Nowell
    Mar 17, 2015 at 17:43
  • 1
    oops... you are correct - I forgot part of what I meant to post. Edited now to include the wpdb connection.
    – butlerblog
    Mar 17, 2015 at 18:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy