Configure WP HyperDB plugin

Task: Add and configure HyperDB plugin to work with master and slave MySQL databases.

Implementation:

Configure MySQL master and slave from instructions in this post: https://scalan.com/docker-setup-mysql-master-slave/.

Add WordPress container to docker-compose file:

...

  wordpress:
    image: wordpress:php7.2
    depends_on:
      - mysqldb-master
      - mysqldb-slave
    ports:
      - "80:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST: mysqldb-master:3306
      WORDPRESS_DB_USER: testuser
      WORDPRESS_DB_PASSWORD: testuser
      WORDPRESS_DB_NAME: testdb
    volumes:
      - ./app:/var/www/html
      - ./logs:/var/log

Build docker environment... Wait till MySQL master and slave will be created and configured...

Open http://localhost and setup WordPress:

Download HyperDB plugin from WP site and put it into:
db.php to wp-content
db-config to root WP directory




Configure db-config.php: (find and set two database objects. First to work with master (writer) and second to work with slave (reader) )

/**
 * This is the most basic way to add a server to HyperDB using only the
 * required parameters: host, user, password, name.
 * This adds the DB defined in wp-config.php as a read/write server for
 * the 'global' dataset. (Every table is in 'global' by default.)
 */
$wpdb->add_database(array(
	'host'     => 'mysqldb-master',     // If port is other than 3306, use host:port.
	'user'     => DB_USER,
	'password' => DB_PASSWORD,
	'name'     => DB_NAME,
	'write'    => 1,
	'read'     => 1,
));

/**
 * 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'     => 'mysqldb-slave',     // If port is other than 3306, use host:port.
	'user'     => DB_USER,
	'password' => DB_PASSWORD,
	'name'     => DB_NAME,
	'write'    => 0,
	'read'     => 1,
));

Check site working... Open http://localhost:

To be sure that configuration is correct create test post and check it on the home page.

Done.

Leave a Reply

Your email address will not be published. Required fields are marked *




Enter Captcha Here :