首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何为HyperDB网站设置WordPress?

如何为HyperDB网站设置WordPress?
EN

Stack Overflow用户
提问于 2017-10-23 08:20:32
回答 1查看 3.1K关注 0票数 0

我有一个大型数据库,我想把它分成几个数据库,但连接到我的WordPress站点。我在网上搜索,并找到了一个使用HyperDB类的解决方案,这是由WordPress代码库提供的。我下载了这些文件,并尝试如下所示

代码语言:javascript
复制
$wpdb->add_database(array(
    'host'     => 'localhost',     // If port is other than 3306, use host:port.
    'user'     => 'root',
    'password' => '',
    'name'     => 'partitioning',
));

/**
 * This adds the same server again, only this time it is configured as a slave.
 * The last three parameters are set to the defaults but are shown for clarity.
 */
$wpdb->add_database(array(
    'host'     => 'localhost',     // If port is other than 3306, use host:port.
    'user'     => 'root',
    'password' => '',
    'name'     => 'slave',
    'write'    => 0,
    'read'     => 1,
    'dataset'  => 'global',
    'timeout'  => 0.2,
));

我使用xampp作为开发版本。安装WordPress站点后,我将db-config.php连同wp-config.php文件目录和db.php放在wp文件夹中。这样我就得到了空白页。

有谁能一步一步地阐述如何设置数据库和HyperDB脚本呢?我的意思是如何使从数据库或HyperDB将自动生成从数据库?如何将任何表拆分为从数据库?我是说整个过程。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-11 01:17:38

如果您正在构建一个大容量的Wordpress站点,而且数据库是瓶颈(通常是这样),那么HyperDB绝对是正确的Wordpress解决方案。HyperDB是由Automattic编写的,在Wordpress.com上使用,并被设计成一个合适的Wordpress插件.

HyperDB支持MySQL读取副本和分区,而您使用的则取决于用例.

实际上,这些配置选项在HyperDB Config文件中有很好的文档说明,该文件如下所示:https://plugins.trac.wordpress.org/browser/hyperdb/trunk/db-config.php

这不适合您的情况;您必须调整这个以适应您的设置。

代码语言:javascript
复制
// This is the default database configuration
//
$wpdb->add_database(array(
        'host'     => 'localhost',
        'user'     => 'root',
        'password' => '',
        'name'     => 'regular_db',
));

// This is your secondary database.
//
$wpdb->add_database(array(
        'host'     => 'localhost',
        'user'     => 'root',
        'password' => '',
        'name'     => 'product_db',
        'dataset'  => 'products',
));

// This magic detects which database table is being read from/written to, and switches
// hyperdb connection based on that.
$wpdb->add_callback('my_db_product_detector');
function my_db_product_detector($query, $wpdb) {

    // This makes the assumption that you've only got one relevant table, wp_products.
    // Update this to whatever your table name(s) are.
    if ( preg_match('^wp_products$', $wpdb->table) ) {
        return 'products';
    }
}

首先,要测试在现有数据库上是否有效,您应该将product_db更改为regular_db,然后从那里开始。

此时,您需要做出架构决策。如果您只是想缩小您的DB的大小,那么现在将您的产品写到第二个DB。或者,您可能有第二个数据库服务器来在不同的数据库之间共享负载。

创建read副本也是值得考虑的,但这取决于您正在处理的生态系统。如果您使用AWS,那么RDS将非常简单地创建read副本。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46884797

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档