【问题标题】:How to set up HyperDB for WordPress website?如何为 WordPress 网站设置 HyperDB?
【发布时间】:2017-10-23 08:20:32
【问题描述】:

我有一个大型数据库,我想将其拆分为多个数据库,但连接到我的 WordPress 网站。我通过互联网搜索并找到了使用 WordPress Codex 提供的HyperDB class 的解决方案。我下载了文件,然后尝试如下

$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-content 文件夹中。这样做我得到空白页。

谁能详细说明如何设置数据库和HyperDB脚本的过程?我的意思是如何制作从库或HyperDB会自动制作从库?如何将任何表拆分为从数据库?我的意思是整个过程。

【问题讨论】:

  • 不要使用这个类,它已经过时了,在任何最近的 PHP 版本中都不起作用
  • 那么还有什么可以替代这个呢?
  • 我不知道。只需使用您的 wordpress 人员正在使用的任何内容 - wpdb 或它的名称
  • 您遇到了什么问题?也许通过优化数据库更容易解决。
  • 实际上,我正在构建一个 WordPress 网站,其中包含大约 1000 万种产品。我计划将相应数据库中的产品作为类别名称上传。例如,男性类别的产品将进入男性数据库,就像女性产品将进入女性数据库一样。我的实际数据库大小是 1GB。在这种情况下我能做什么?

标签: database wordpress


【解决方案1】:

如果您正在构建一个大容量的 Wordpress 网站,并且数据库是瓶颈(通常情况如此),那么 HyperDB 绝对是正确的 Wordpress 解决方案。 HyperDB 由 Automattic 创作,在 Wordpress.com 上使用,被设计为适当的 Wordpress 插件。

HyperDB 支持 MySQL 只读副本和分区,您使用哪种取决于您的用例。

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

这并不完全适合您的情况;您必须调整它以适合您的设置。

// 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,然后从那里开始。

此时,您需要做出架构决策。如果您只是想减小数据库的大小,那么现在这会将您的产品写入第二个数据库。或者,您可能有第二台数据库服务器来完全分担不同数据库的负载。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-27
    • 2022-12-11
    • 2015-12-21
    • 1970-01-01
    • 1970-01-01
    • 2016-08-28
    • 2018-11-15
    • 2017-10-02
    相关资源
    最近更新 更多