Setup MySQL Master to Master replication/automatic failover when each Master also has a Slave
MHA orchestrates the reconnection of slaves as needed.
Orchestrator orchestrates the reconnection of slaves as needed.
Galera (PXC, MariaDB) eliminates the need for failover by having all nodes writable, plus automatic repair of nodes that were offline.
Fabric is another way.
Doing the task yourself is possible, but complex, risky, etc, etc.