[Osaka/Yokohama] Looking for infrastructure/server side engineers!

[Osaka/Yokohama] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Reservation system development] EDISONE customization development service

[Reservation system development] EDISONE customization development service

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[Global exclusive service] Beyond's MSP in North America and China

[Global exclusive service] Beyond's MSP in North America and China

[YouTube] Beyond official channel “Biyomaru Channel”

[YouTube] Beyond official channel “Biyomaru Channel”

「HyperDB」WordPressでDBの冗長化・負荷分散を行うプラグイン

インフラエンジニアの伊藤です。

AWSなどクラウドを使うと、簡単にサーバを複製してロードバランサー経由でサイトへのアクセスを冗長化・負荷分散出来るようになりました。

この状態であればWebサーバに関しては冗長化・負荷分散されていますが、データベースが冗長化・負荷分散されていません。

もしデータベースに対してのアクセスが多かった場合、
Webサーバへの負荷は大丈夫だけど、DBサーバ側の処理が詰まってサイトが表示されないという可能性が出てきます。

前置きが長くなりましたが、WordpressはいわゆるLAMP環境で動くコンテンツ。
ということはDBを使用しているので、Webサーバを冗長化していてもDBが詰まってしまうと…

503 Service Temporarily Unavailable

見たくないエラーが出てきます。。

そんなとき、DBも冗長化してあげましょう!!
ということで、WorpdressのDB冗長化を実現するプラグイン「HyperDB」の紹介です。

AWSの場合、RDSの冗長化は簡単に可能ですよね。
マスターとなるRDSの「リードレプリカ」を作成するだけでOKです。
なので説明は割愛します。

HyperDBをインストールする

HyperDBプラグインのインストールは少々複雑です。
というのも、Wordpress管理画面からインストールは可能なのですが、
実際の設定はphpファイルに直接書き込む必要があります。

インストール後は、Wordpressのwp-content/plugins/hyperdbに以下のファイルがあるかと思います。
「db-config.php」
サーバにSSHで接続出来る方はSSHで、ftp等のみで接続出来る方は事前に設定を入れて対象のディレクトリに
アップロードする必要があります。

まずはwp-config.phpに以下の内容を記載します。「DBの設定ファイルはdb-config.phpだよ」です。

# vim /pass/to/docroot/wp-config.php
define('DB_CONFIG_FILE', ABSPATH . 'db-config.php');

続けて設定をしていきましょう。

HyperDBの設定

設置したdb-config.phpに対して、以下の設定をしていきます。

まずはマスターサーバの設定をします。(217行目付近、v1.1にて確認)

# vim /pass/to/docroot/db-config.php
$wpdb->add_database(array(
        'host'     => 'RDS(マスターサーバ)のエンドポイントを入力',
        'user'     => DB_USER,
        'password' => DB_PASSWORD,
        'name'     => DB_NAME,
        'write'    => 1,
        'read'     => 2,
));

hostはRDSのエンドポイントを入力しますが、デフォルトの3306ポート以外を使用する場合、
<エンドポイント>:<ポート番号>とコロンに続けてポート番号を入力する必要があります。

write、readがポイントです。

  • write:DBへの書き込み優先度
  • read:DBへの読み込みの優先度

「0」に設定すると書き込み、読み込みを行わない設定になります。
上記の場合だと、マスターサーバへの書き込み優先度が高く、読み込み優先度は低くなります。

続けてスレーブサーバの設定です。
先ほどのマスターサーバの設定の下にでも追記してください。

# vim /pass/to/docroot/db-config.php
$wpdb->add_database(array(
        'host'     => 'RDS(スレーブサーバ)のエンドポイントを入力',
        'user'     => DB_USER,
        'password' => DB_PASSWORD,
        'name'     => DB_NAME,
        'write'    => 0,
        'read'     => 1,
));

ここでもwrite,readが威力を発揮します。

  • write:0=そのサーバに対して書き込みを行わない
  • read:1=読み込みの優先度が高く、writeが0なので読み込み専用

この設定をすると、スレーブサーバにはreadのみが行われます。
同様に、複数のスレーブサーバを登録してあげると、読み込みがスレーブサーバにのみ行われるようになります。
これでスレーブサーバでDBへの読み込み(つまり通常のアクセス)が負荷分散されるようになりました。

WordPressではDBに書き込みを行うのは、設定の変更や記事の追加など、管理画面から行うものばかりになります。
それはマスターサーバへ書き込み優先度を高く設定しているので問題ありません。

ということで、これでWordpressのDBの冗長化・負荷分散がされるようになりました!

これで急なアクセスにも安心!ですね。
HyperDB — WordPress Plugins

この記事がお役に立てば【 いいね 】のご協力をお願いいたします!
0
読み込み中...
0 票, 平均: 0.00 / 10
4,481
X facebook はてなブックマーク pocket
[2024.6.30 CentOS support ended] CentOS server migration solution

[2024.6.30 CentOS support ended] CentOS server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

The person who wrote this article

About the author