Pair of Docker images for Apache2 Webserver and MySQL as a read-replica against a remote master.
- Docker
- Terminal/CLI skills
- Further details for Webserver Config
git clone https://github.com/Wier-Stewart/dev-replica.git
- Rename
webserver.sample
towebserver.env
and fill in the info. - Rename
mysql_slave.sample
tomysql_slave.env
and fill in the info. - Edit
docker-compose.yml
file: set thevolumes
line that looks like this~/domains/:/var/www/domains
to fit the pattern:/local-path/on-mac/to-domains/:/var/www/domains
- Set your
/etc/hosts
to be127.0.0.1 local.domain.com
. If you're on a Mac, this is easier: Gasmask.
Build only needs to happen once (usually). See "Docker Lifecycle Commands" at the end for the full commands.
$ docker-compose build
$ docker-compose up
Once docker-compose up
is up:
- The site
local.domain.com
should be available. - The database should be accessible via
127.0.0.1
(notlocalhost
oddly) on port 3306 from your Mac. - The database will be accessible via
mysql_slave
from the webserver
E.g., your wp-config.php file would have a line like this:
define('DB_HOST', 'mysql_slave');
The goal is to have all database-reads go to mysql_slave
locally, and all database-writes
go to the remote master db. For this, WordPress requires HyperDB.
It's not a normal plugin installation, and really only requires 2 files:
Here's a mini version that will work with the usual wp-config.php variables:
<?php
//./db-config.php
$wpdb->save_queries = true;
$wpdb->persistent = false;
$wpdb->max_connections = 10;
$wpdb->check_tcp_responsiveness = true;
//replicate on local.domain.com only
if( defined( "WP_CLI" ) && WP_CLI ){
//don't say anything, it can mess with output parsing!
}else if(stripos($_SERVER['HTTP_HOST'], 'local.')!==false ){ //replicate if local, only, not dev or preview
$wpdb->add_database(array(
"host" => "mysql_slave", // If port is other than 3306, use host:port.
"user" => DB_USER,
"password" => DB_PASSWORD,
"name" => DB_NAME,
"write" => 0,
"read" => 1, // replication_ok() here is preferred.
"dataset" => "global",
"timeout" => 0.5,
));
}
$wpdb->add_database(array(
"host" => DB_HOST, // If port is other than 3306, use host:port.
"user" => DB_USER,
"password" => DB_PASSWORD,
"name" => DB_NAME,
"write" => 1,
"read" => 1,
"dataset" => "global",
"timeout" => .9,
));
save this to ./wp-content/db.php
$ docker-compose build
$ docker-compose up
$ docker-compose up
$ docker-compose stop
$ docker-compose stop
Stopping webserver ... done
Stopping mysql_slave ... done
$ docker rm webserver
$ docker rm mysql_slave
$ docker volume prune
WARNING! This will remove all volumes not used by at least one container.
Are you sure you want to continue? [y/N] y