HyperDBを使うと真っ白な画面になる

WordPressの冗長化のプラグインで有名なHyperDB。今回このプラグインを使うと画面が真っ白になりました。

構成

  • Webサーバー:apache2.4、php7
  • DBサーバー×2:Mariadb

DBサーバー2台はレプリケーションの設定が終わっている前提となります。プラグインをダウンロードしてファイルを書き換えをします。

db-config.php

001<?php /** * HyperDB configuration file * * This file should be installed at ABSPATH/db-config.php * * $wpdb is an instance of the hyperdb class which extends the wpdb class. * * See readme.txt for documentation. */ /** * Introduction to HyperDB configuration * * HyperDB can manage connections to a large number of databases. Queries are * distributed to appropriate servers by mapping table names to datasets. * * A dataset is defined as a group of tables that are located in the same * database. There may be similarly-named databases containing different * tables on different servers. There may also be many replicas of a database * on different servers. The term "dataset" removes any ambiguity. Consider a * dataset as a group of tables that can be mirrored on many servers. * * Configuring HyperDB involves defining databases and datasets. Defining a * database involves specifying the server connection details, the dataset it * contains, and its capabilities and priorities for reading and writing. * Defining a dataset involves specifying its exact table names or registering * one or more callback functions that translate table names to datasets. */ /** Variable settings **/ /** * save_queries (bool) * This is useful for debugging. Queries are saved in $wpdb->queries. It is not
002 * a constant because you might want to use it momentarily.
003 * Default: false
004 */
005$wpdb->save_queries = false;
006 
007/**
008 * persistent (bool)
009 * This determines whether to use mysql_connect or mysql_pconnect. The effects
010 * of this setting may vary and should be carefully tested.
011 * Default: false
012 */
013$wpdb->persistent = false;
014 
015/**
016 * max_connections (int)
017 * This is the number of mysql connections to keep open. Increase if you expect
018 * to reuse a lot of connections to different servers. This is ignored if you
019 * enable persistent connections.
020 * Default: 10
021 */
022$wpdb->max_connections = 10;
023 
024/**
025 * check_tcp_responsiveness
026 * Enables checking TCP responsiveness by fsockopen prior to mysql_connect or
027 * mysql_pconnect. This was added because PHP's mysql functions do not provide
028 * a variable timeout setting. Disabling it may improve average performance by
029 * a very tiny margin but lose protection against connections failing slowly.
030 * Default: true
031 */
032$wpdb->check_tcp_responsiveness = true;
033 
034/** Configuration Functions **/
035 
036/**
037 * $wpdb->add_database( $database );
038 *
039 * $database is an associative array with these parameters:
040 * host          (required) Hostname with optional :port. Default port is 3306.
041 * user          (required) MySQL user name.
042 * password      (required) MySQL user password.
043 * name          (required) MySQL database name.
044 * read          (optional) Whether server is readable. Default is 1 (readable).
045 *                         Also used to assign preference. See "Network topology".
046 * write         (optional) Whether server is writable. Default is 1 (writable).
047 *                          Also used to assign preference in multi-master mode.
048 * dataset       (optional) Name of dataset. Default is 'global'.
049 * timeout       (optional) Seconds to wait for TCP responsiveness. Default is 0.2
050 * lag_threshold (optional) The minimum lag on a slave in seconds before we consider it lagged.
051 *                          Set null to disable. When not set, the value of $wpdb->default_lag_threshold is used.
052 */
053 
054/**
055 * $wpdb->add_table( $dataset, $table );
056 *
057 * $dataset and $table are strings.
058 */
059 
060/**
061 * $wpdb->add_callback( $callback, $callback_group = 'dataset' );
062 *
063 * $callback is a callable function or method. $callback_group is the
064 * group of callbacks, this $callback belongs to.
065 *
066 * Callbacks are executed in the order in which they are registered until one
067 * of them returns something other than null.
068 *
069 * The default $callback_group is 'dataset'. Callback in this group
070 * will be called with two arguments and expected to compute a dataset or return null.
071 * $dataset = $callback($table, &$wpdb);
072 *
073 * Anything evaluating to false will cause the query to be aborted.
074 *
075 * For more complex setups, the callback may be used to overwrite properties of
076 * $wpdb or variables within hyperdb::connect_db(). If a callback returns an
077 * array, HyperDB will extract the array. It should be an associative array and
078 * it should include a $dataset value corresponding to a database added with
079 * $wpdb->add_database(). It may also include $server, which will be extracted
080 * to overwrite the parameters of each randomly selected database server prior
081 * to connection. This allows you to dynamically vary parameters such as the
082 * host, user, password, database name, lag_threshold and TCP check timeout.
083 */
084 
085/** Masters and slaves
086 *
087 * A database definition can include 'read' and 'write' parameters. These
088 * operate as boolean switches but they are typically specified as integers.
089 * They allow or disallow use of the database for reading or writing.
090 *
091 * A master database might be configured to allow reading and writing:
092 *   'write' => 1,
093 *   'read'  => 1,
094 * while a slave would be allowed only to read:
095 *   'write' => 0,
096 *   'read'  => 1,
097 *
098 * It might be advantageous to disallow reading from the master, such as when
099 * there are many slaves available and the master is very busy with writes.
100 *   'write' => 1,
101 *   'read'  => 0,
102 * HyperDB tracks the tables that it has written since instantiation and sending
103 * subsequent read queries to the same server that received the write query.
104 * Thus a master set up this way will still receive read queries, but only
105 * subsequent to writes.
106 */
107 
108 
109/**
110 * Network topology / Datacenter awareness
111 *
112 * When your databases are located in separate physical locations there is
113 * typically an advantage to connecting to a nearby server instead of a more
114 * distant one. The read and write parameters can be used to place servers into
115 * logical groups of more or less preferred connections. Lower numbers indicate
116 * greater preference.
117 *
118 * This configuration instructs HyperDB to try reading from one of the local
119 * slaves at random. If that slave is unreachable or refuses the connection,
120 * the other slave will be tried, followed by the master, and finally the
121 * remote slaves in random order.
122 * Local slave 1:   'write' => 0, 'read' => 1,
123 * Local slave 2:   'write' => 0, 'read' => 1,
124 * Local master:    'write' => 1, 'read' => 2,
125 * Remote slave 1:  'write' => 0, 'read' => 3,
126 * Remote slave 2:  'write' => 0, 'read' => 3,
127 *
128 * In the other datacenter, the master would be remote. We would take that into
129 * account while deciding where to send reads. Writes would always be sent to
130 * the master, regardless of proximity.
131 * Local slave 1:   'write' => 0, 'read' => 1,
132 * Local slave 2:   'write' => 0, 'read' => 1,
133 * Remote slave 1:  'write' => 0, 'read' => 2,
134 * Remote slave 2:  'write' => 0, 'read' => 2,
135 * Remote master:   'write' => 1, 'read' => 3,
136 *
137 * There are many ways to achieve different configurations in different
138 * locations. You can deploy different config files. You can write code to
139 * discover the web server's location, such as by inspecting $_SERVER or
140 * php_uname(), and compute the read/write parameters accordingly. An example
141 * appears later in this file using the legacy function add_db_server().
142 */
143 
144/**
145 * Slaves lag awareness
146 *
147 * HyperDB accommodates slave lag by making decisions, based on the defined lag
148 * threshold. If the lag threshold is not set, it will ignore the slave lag.
149 * Otherwise, it will try to find a non-lagged slave, before connecting to a lagged one.
150 *
151 * A slave is considered lagged, if it's replication lag is bigger than the lag threshold
152 * you have defined in $wpdb->$default_lag_threshold or in the per-database settings, using
153 * add_database(). You can also rewrite the lag threshold, by returning
154 * $server['lag_threshold'] variable with the 'dataset' group callbacks.
155 *
156 * HyperDB does not check the lag on the slaves. You have to define two callbacks
157 * callbacks to do that:
158 *
159 * $wpdb->add_callback( $callback, 'get_lag_cache' );
160 *
161 * and
162 *
163 * $wpdb->add_callback( $callback, 'get_lag' );
164 *
165 * The first one is called, before connecting to a slave and should return
166 * the replication lag in seconds or false, if unknown, based on $wpdb->lag_cache_key.
167 *
168 * The second callback is called after a connection to a slave is established.
169 * It should return it's replication lag or false, if unknown,
170 * based on the connection in $wpdb->dbhs[ $wpdb->dbhname ].
171 */
172 
173/** Sample Configuration 1: Using the Default Server **/
174/** NOTE: THIS IS ACTIVE BY DEFAULT. COMMENT IT OUT. **/
175 
176/**
177 * This is the most basic way to add a server to HyperDB using only the
178 * required parameters: host, user, password, name.
179 * This adds the DB defined in wp-config.php as a read/write server for
180 * the 'global' dataset. (Every table is in 'global' by default.)
181 */
182$wpdb->add_database(array(
183    'host'     => '192.168.0.2',     // If port is other than 3306, use host:port.
184    'user'     => DB_USER,
185    'password' => DB_PASSWORD,
186    'name'     => DB_NAME,
187    'write'    => 1,
188    'read'     => 2,
189));
190 
191/**
192 * This adds the same server again, only this time it is configured as a slave.
193 * The last three parameters are set to the defaults but are shown for clarity.
194 */
195$wpdb->add_database(array(
196    'host'     => '192.168.0.3',     // If port is other than 3306, use host:port.
197    'user'     => DB_USER,
198    'password' => DB_PASSWORD,
199    'name'     => DB_NAME,
200    'write'    => 0,
201    'read'     => 1,
202    //'dataset'  => 'global',
203    //'timeout'  => 0.2,
204));
205 
206/** Sample Configuration 2: Partitioning **/
207 
208/**
209 * This example shows a setup where the multisite blog tables have been
210 * separated from the global dataset.
211 */
212/*
213$wpdb->add_database(array(
214    'host'     => 'global.db.example.com',
215    'user'     => 'globaluser',
216    'password' => 'globalpassword',
217    'name'     => 'globaldb',
218));
219$wpdb->add_database(array(
220    'host'     => 'blog.db.example.com',
221    'user'     => 'bloguser',
222    'password' => 'blogpassword',
223    'name'     => 'blogdb',
224    'dataset'  => 'blog',
225));
226$wpdb->add_callback('my_db_callback');
227function my_db_callback($query, $wpdb) {
228    // Multisite blog tables are "{$base_prefix}{$blog_id}_*"
229    if ( preg_match("/^{$wpdb->base_prefix}\d+_/i", $wpdb->table) )
230        return 'blog';
231}
232*/
233 
234 
235/** Sample helper functions from WordPress.com **/
236 
237/**
238 * This is back-compatible with an older config style. It is for convenience.
239 * lhost, part, and dc were removed from hyperdb because the read and write
240 * parameters provide enough power to achieve the desired effects via config.
241 *
242 * @param string $dataset Datset: the name of the dataset. Just use "global" if you don't need horizontal partitioning.
243 * @param int $part Partition: the vertical partition number (1, 2, 3, etc.). Use "0" if you don't need vertical partitioning.
244 * @param string $dc Datacenter: where the database server is located. Airport codes are convenient. Use whatever.
245 * @param int $read Read group: tries all servers in lowest number group before trying higher number group. Typical: 1 for slaves, 2 for master. This will cause reads to go to slaves unless all slaves are unreachable. Zero for no reads.
246 * @param bool $write Write flag: is this server writable? Works the same as $read. Typical: 1 for master, 0 for slaves.
247 * @param string $host Internet address: host:port of server on internet.
248 * @param string $lhost Local address: host:port of server for use when in same datacenter. Leave empty if no local address exists.
249 * @param string $name Database name.
250 * @param string $user Database user.
251 * @param string $password Database password.
252 */
253/*
254function add_db_server($dataset, $part, $dc, $read, $write, $host, $lhost, $name, $user, $password, $timeout = 0.2 ) {
255    global $wpdb;
256 
257    // dc is not used in hyperdb. This produces the desired effect of
258    // trying to connect to local servers before remote servers. Also
259    // increases time allowed for TCP responsiveness check.
260    if ( !empty($dc) && defined(DATACENTER) && $dc != DATACENTER ) {
261        if ( $read )
262            $read += 10000;
263        if ( $write )
264            $write += 10000;
265        $timeout = 0.7;
266    }
267 
268    // You'll need a hyperdb::add_callback() callback function to use partitioning.
269    // $wpdb->add_callback( 'my_func' );
270    if ( $part )
271        $dataset = $dataset . '_' . $part;
272 
273    $database = compact('dataset', 'read', 'write', 'host', 'name', 'user', 'password', 'timeout');
274 
275    $wpdb->add_database($database);
276 
277    // lhost is not used in hyperdb. This configures hyperdb with an
278    // additional server to represent the local hostname so it tries to
279    // connect over the private interface before the public one.
280    if ( !empty( $lhost ) ) {
281        if ( $read )
282            $database['read'] = $read - 0.5;
283        if ( $write )
284            $database['write'] = $write - 0.5;
285        $wpdb->add_database( $database );
286    }
287}
288*/
289 
290/**
291 * Sample replication lag detection configuration.
292 *
293 * We use mk-heartbeat (http://www.maatkit.org/doc/mk-heartbeat.html)
294 * to detect replication lag.
295 *
296 * This implementation requires the database user
297 * to have read access to the heartbeat table.
298 *
299 * The cache uses shared memory for portability.
300 * Can be modified to work with Memcached, APC and etc.
301 */
302 
303/*
304  
305$wpdb->lag_cache_ttl = 30;
306$wpdb->shmem_key = ftok( __FILE__, "Y" );
307$wpdb->shmem_size = 128 * 1024;
308 
309$wpdb->add_callback( 'get_lag_cache', 'get_lag_cache' );
310$wpdb->add_callback( 'get_lag',       'get_lag' );
311 
312function get_lag_cache( $wpdb ) {
313    $segment = shm_attach( $wpdb->shmem_key, $wpdb->shmem_size, 0600 );
314    $lag_data = @shm_get_var( $segment, 0 );
315    shm_detach( $segment );
316 
317    if ( !is_array( $lag_data ) || !is_array( $lag_data[ $wpdb->lag_cache_key ] ) )
318        return false;
319 
320    if ( $wpdb->lag_cache_ttl < time() - $lag_data[ $wpdb->lag_cache_key ][ 'timestamp' ] )
321        return false;
322 
323    return $lag_data[ $wpdb->lag_cache_key ][ 'lag' ];
324}
325 
326function get_lag( $wpdb ) {
327    $dbh = $wpdb->dbhs[ $wpdb->dbhname ];
328 
329    if ( !mysql_select_db( 'heartbeat', $dbh ) )
330        return false;
331 
332    $result = mysql_query( "SELECT UNIX_TIMESTAMP() - UNIX_TIMESTAMP(ts) AS lag FROM heartbeat LIMIT 1", $dbh );
333 
334    if ( !$result || false === $row = mysql_fetch_assoc( $result ) )
335        return false;
336 
337    // Cache the result in shared memory with timestamp
338    $sem_id = sem_get( $wpdb->shmem_key, 1, 0600, 1 ) ;
339    sem_acquire( $sem_id );
340    $segment = shm_attach( $wpdb->shmem_key, $wpdb->shmem_size, 0600 );
341    $lag_data = @shm_get_var( $segment, 0 );
342     
343    if ( !is_array( $lag_data ) )
344        $lag_data = array();
345 
346    $lag_data[ $wpdb->lag_cache_key ] = array( 'timestamp' => time(), 'lag' => $row[ 'lag' ] );
347    shm_put_var( $segment, 0, $lag_data );
348    shm_detach( $segment );
349    sem_release( $sem_id );
350 
351    return $row[ 'lag' ];
352}
353 
354*/
355 
356// The ending PHP tag is omitted. This is actually safer than including it.

参考サイトを元にhostを書き換え、datasetとtimeoutはコメントアウトしました。
※コメントアウトしたのは参考サイトにはこの二つが乗っていなかったためになります。

wp-config.php

wp-config.phpも少し書き換えをします

1/** データベースの照合順序 (ほとんどの場合変更する必要はありません) */
2define('DB_COLLATE', '');
3 
4 
5//ハイパーDBの読み込み
6define('DB_CONFIG_FILE', ABSPATH . 'db-config.php');

これでOKです。後はプラグインファイルに付属しているdb.phpをwp-contentの直下にアップロードするだけみたいです。

img_7207_001

真っ白になりました。

Webサイトは真っ白になりました。db.phpを削除したら元に戻りました。php7だからなのかわかりませんが情報が古くてわかりませんでした原因は。とりあえず同じような症状の人がいたら教えていただけると助かります。

参考サイト

個人支援・寄付について

サイトラボでは個人支援・寄付を受けております。ご協力いただける方はお願いいたします。当サイトではビットコインで受け付けております。

  • ビットコイン:3LHnADwZwUbic2L45EnVJEykiG6KfbqrwS