MySQL (minicluster) with a Master and a Slave, MySqlFailover and MySqlRouter.
This repository describes how to setup above components so MySQL (5.7) replication can be achieved.
At this point we have not yet produced an "all-in-one" compose file. So this repository is made up of 3 compose files:
- docker-compose-mysqlcluser-master.yml
- docker-compose-mysqlcluser-slave.yml
- docker-compose-mysqlcluser-connection.yml
As soon as we get to the point of proper health checks inside the images we intend to make 1 general compose file to be able to setup the cluster in 1 go.
At the moment we have not yet implemented the "secrets" offered by Docker. This means that the root password has to be put in the compose files.
Keep an eye on this repository since it is still under development and subject to change.
- Ubuntu (16.04 or higher) or RHEL host(s)
- Docker v1.13.1 (minimum)
- Experimental Mode must be set to true (to be able to use "docker deploy" with compose v3 files)
- Must run in Swarm Mode
- Preferably at least 2 nodes (Manager + Worker) to run Master and Slave on
- 1 overlay network to run MySQL Stack in
Service | Purpose |
---|---|
MySQL | Main database server for the Master and Slave |
MySQLFailover | Service to failover to Slave in case Master dies |
MySQLRouter | Router to balance client connections to active Master |
The following directories need to be present on the host or on a central storage solution. These directories will me used for connecting volumes to to services/containers.
Component | Directory | Remarks |
---|---|---|
mysql01 (Master) | /var/dockerdata/mysqlcluster/mysql01/conf.d | Directory for storing configuration files |
/var/dockerdata/mysqlcluster/mysql01/initdb.d | Directory with sql initialisation files for fresh DB | |
/var/dockerdata/mysqlcluster/mysql01/lib | Data storage for MySQL instance | |
/var/dockerdata/mysqlcluster/mysql01/log | Directory where bin-log files for replication will be stored | |
mysql02 (Slave) | /var/dockerdata/mysqlcluster/mysql02/conf.d | Directory for storing configuration files |
/var/dockerdata/mysqlcluster/mysql02/initdb.d | Directory with sql initialisation files for fresh DB | |
/var/dockerdata/mysqlcluster/mysql02/lib | Data storage for MySQL instance | |
/var/dockerdata/mysqlcluster/mysql02/log | Directory where bin-log files for replication will be stored | |
mysqlrouter | /var/dockerdata/mysqlcluster/mysqlrouter/log | Directory where log will be written |
$ docker swarm init
$ docker network create -d overlay mysqlcluster
Join 1 or more nodes to the cluster according to Docker documentation.
Make sure to look through the compose files for the volume mappings. In this example all is mapped to /var/dockerdata/mysqlcluser/. Adjust this to your own liking or create the same structure as used in this example (Preparation).
In this basic setup the user is used in all communication. The password for this user has to be put in the compose files.
This setup will create a custom config file with all the needed info to setup replication for the Master and the Slave. The config file can be viewed after creation of the services in their respective config.d volumes.
Database wise there are 2 options:
- Create a fresh DB from the initdb.d directory
- Copy an existing database to the \lib\ directory
When either of those is setup run below compose file:
$ docker deploy --compose-file docker-compose-mysql-cluster-master.yml mysqlcluster
Now wait till the master is up and running and accepting client connections
$ docker deploy --compose-file docker-compose-mysql-cluster-slave.yml mysqlcluster
The replication will now be setup and the Master database will get copied to the Slave. Wait till the process is completed and the Slave is accepting client connections
$ docker deploy --compose-file docker-compose-mysql-cluster-connect.yml mysqlcluster
Now that everything is up and running you can connect to the cluster over port 3306 (MySQLRouter) or by accessing the Master and Slave directly over port 3307 and port 3308.
Nicomak blog (https://github.com/nicomak) has been used as a base on how to setup replication between 2 MySQL servers.
The files are free to use and you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation.