Mysql – With MySQL 5.6, Is there any way to keep a replication slave X seconds behind_the_master

MySQLmysql-5.6replication

It'd be really nice for disaster recovery to be able to set this up. We are doing snapshots, but this seems like it'd be a really nice backup to have. For example, I'd like to always keep a replicated slave 30 minutes behind our master, so that we could have a sort of live backup all the time.

This is a secondary replicated server, as ypercube points out — if you only have a single replicated server, running it 30 minutes behind would mean you'd potentially lose 30 minutes of data in the case of the master crashing.

Although I suspect the relevant binlogs for those 30 minutes of binlog events would actually be stored on the slave (and just not processed until it was 30 minutes behind), so I'm thinking that I wouldn't actually, even if this were the only slave.

I'll be doing some testing to confirm this behavior, as it is relevant for some potential use-cases for us. I'll update this later.

Best Answer

If you really want a live slave 30 minutes behind its master then use the following

CHANGE MASTER TO ...
MASTER_DELAY = 1800;

This will setup replication 30 minutes behind

See the CHANGE MASTER TO Documentation

In light of ypercube's comment, you should do the following:

Setup two slaves - Slave1 with 30 minute delay - Slave2 with no delay (Run mysqldump --single-transaction for point-in-time snaphosts)