This playbook will setup a MySQL replication by doing the following steps:
- Backup the current databases from the MySQL master
- Compress the backup
- Download the backup
- Upload and uncompress the backup to the MySQL slave
- Stop MySQL on the slave
- Delete all old MySQL databases from the MySQL slave
- Import the backup
- Start MySQL on the slave
- Configute the MySQL slave process
- Start the MySQL slave process
The slave will use SSL to connect to the master. So you have to make sure that your master has a valid SSL certificate that can be validated by a CA from /etc/ssl/certs
on the slave. (Checkout Letsencrypt)
WARNING: This playbook will delete all previous data from the MySQL slave!
- A working MySQL installation on the master with valid SSL setup.
- A replication User on the master. (
GRANT REPLICATION SLAVE ON *.* TO 'SOMEIP'@'SLAVEHOST';
) You may also want to useREQUIRE SSL
. - innobackupex to backup the master.
- A working MySQL installation on the slave.
Option | Default | Description |
---|---|---|
master | The MySQL master from your Ansible inventory. | |
slave | The MySQL slave from your Ansible inventory. All MySQL data from this host will be deleted. | |
mysql_replication_master | master | The Hostname or IP that will be set as MASTER_HOST in MySQL. |
mysql_replication_user | MASTER_USER in MySQL. | |
mysql_replication_password | MASTER_PASSWORD in MySQL. |
Assuming that you defined mysql_replication_user
and mysql_replication_password
in your host_vars you can simply run:
ansible-playbook main.yml -e 'master=master.exmaple.com slave=slave.example.com'