There seem to be very few guides on setting up mysql replication for failover (and failback) for 5.6 using gtid. The official manual shows how to do it for an existing replicated setup, but not for a new setup. I have combined several sources to come up with the following steps, but I don't know:
- how (and at what point) to correctly seed the slaves,
- whether users/grants need to be done on both master and slaves,
- why I should use "skip-slave-start" in the config, and if I do, what happens if it becomes the master? etc.
- how to failover, and how to make old master a slave (especially as most articles hard code master/slave specific directives in the my.cnf)
I have found articles how to do it pre-gtid, but it is unclear how the steps for this translate into gtid world.
Phase 1 (pre-replication)
- Install mysql 5.6.19 (>19 has changes which break our app) on server a.
- create database "foo" on server a
- setup our application to use server a (via its ip) and generate a lot of data in foo.
Phase 2 setup slave
- install mysql 5.6.19 on server b.
Phase 3 copy master data to slave. Or should this be phase 4 or 5?.
- Do something???
- mysqldump mydb > mydb.sql
- do something on slave???
- restore mydb.sql to slave
- do something else on slave???
Phase 4 setup replication (MASTER)
- Presumably we should stop the application, and tell everyone to stop using db.
- edit my.cnf
add:
log-bin = mysql-bin
server-id = 1
binlog_format = MIXED ## or ROW?
enforce-gtid-consistency
gtid-mode=ON
binlog_do_db = foo ## dont know if this is needed?
log-slave-updates ## dont know if this is needed?
relay-log-recovery ## dont know if this is needed?
- service mysql restart
- setup rep user on master:
mysql -u root -p
create user 'replicator'@'%' identified by 'xxx';
grant replication slave on *.* to 'replicator'@'%';
Phase 5 setup SLAVE
- edit my.cnf:
Add:
server_id = 2
log_bin = mysql-bin
binlog_format = MIXED ## or ROW?
skip_slave_start ## is this needed? What if this server becomes master???
gtid_mode = on
enforce_gtid_consistency
log_slave_updates
binlog_do_db = foo ## dont know if this is needed?
- create users? Do we need to do the same create user and grant as for the master? What if this slave becomes the master?
- service mysql restart
Phase 6 start replication
On slave only?
SQL> CHANGE MASTER TO
MASTER_HOST='master_ip',
MASTER_PORT=3306,
MASTER_USER='slave_user_name',
MASTER_PASSWORD='xxx',
MASTER_AUTO_POSITION=???;
SQL> START SLAVE;
Phase 7, master dies make slave new master
- ?
- Stop our application servers, manually change the IP to the slave. Is there a better way?
- start our application servers.
Phase 8, master backup make it new slave
- ?