Docker automatic MySQL slave propagation

Docker automatic MySQL slave propagationIn this post, we’ll discuss Docker automatic MySQL slave propagation for help with scaling.

In my previous posts on the Docker environment, I covered Percona XtraDB Cluster. Percona XtraDB Cluster can automatically scale by conveniently adding new nodes using the highly automated State Snapshot Transfer. State Snapshot Transfer allows a new node to copy data from an existing node (I still want to see how this is possible with MySQL Group Replication).

This is not the case with regular MySQL Replication. With MySQL Replication, the slave setup still requires manual steps (well, unless you’ve already scripted it for your environment). At least these are “simple” steps (ha!). Percona XtraBackup can setup replication with less work (see this link for details:, but it still requires poking around and switching between servers.

However, nothing stops us from making it more automatic (similar to SST in Percona XtraDB Cluster), especially with Docker images. Why Docker? Because Docker provides a highly-controlled environment where we can orchestrate how scripts get executed. Severalnines provides a great intro into MySQL with Docker.

There are a few more components for this setup:

Before jumping to my solution, I should point to some work in this area by Joyent:

I propose my image, with sources on GitHub

First, we need to start a master node:

I assume that we’ve created the network replicaset_net already, either bridge or overlay.

You can create a slave by pointing to the master container:

The started node will automatically connect to MASTER_HOST, copy the data and perform all the steps needed to start the slave.

You can even copy data from a running slave, instead of the master, like this:

This node will copy data from SLAVE_HOST, but then will point itself to MASTER_HOST.

Docker Network lets you use container names "replicaset_master" and "replicaset_slave1" instead of IP addresses, which is very convenient.

As the result of above, we have one master and two slaves running. We can start as many slave nodes as needed.

Please remember, this is more proof-of-concept than “production ready” images, but it gives a good direction for implementation.

Share this post

Comments (2)

  • Alexander Chekalin

    In I found typo of “FLASH TABLES WITH READ LOCK”. I do believe it’s “FLUSH …”

    September 14, 2016 at 2:59 am
  • Tim Birkett (@pysysops)

    Hey Vadim,

    This is an interesting post and sounds promising but…

    There’s a problem with how you calculate the server_id if networks are in a range like range.

    You end up with a number in scientific notation form: 2017-03-08T12:41:40.854133Z 0 [ERROR] mysqld: Error while setting value ‘3.23226e+09’ to ‘server_id’

    As docker usually uses a default range like to issue IP’s this a problem. There is a work around: Set the Docker network subnet to use something like:

    The next issue is with the image itself. There doesn’t seem to be a Dockerfile for perconalab/ps-master-slave so it’s really hard to see what is going on in the image. Apparently xbstream is missing so the entrypoint script never syncs over the data using netcat…

    I’m not sure if that image has changed? Other than those minor issues this looks really promising.

    March 8, 2017 at 7:52 am

Comments are closed.