The biggest caveat with only 2 machines in a cluster is the possibility of "split-brain", in which the nodes lose communication with one another, and each assumes that they are now the Master, possibly getting out of sync with the other. This risk is mitigated if you can absolutely ensure that writes will not go to the 2 Masters separately while they are not in communication. It is generally advisable to have at least 3 (and always preferably an odd number of nodes) to prevent these types of situations.
I did something similar to what you are proposing (with 3 nodes, not just two) as a MariaDB/Galera Cluster. In actuality, it is a Multi-Master setup, in which a write to any server will be propagated to the others, but in practice I treat it as a Master-Slave with automatic failover.
For Context, I will describe how I do my Master-Slave automatic failover using HAProxy:
I use HAProxy on my application servers and set up two ports, one that my application treats as the Master for writes, and one that the application treats as the Slave for reads. In my HAProxy, the Master port is set to use only the Master, but fail to the Slaves in a particular order. If the Master goes down, HAProxy detects it and redirects writes to the next server, which becomes the de facto Master.
My HAProxy Slave port balances between the Slave nodes (you could include the Master, too) using one of several different balancing algorithms. If one of Slaves goes down, HAProxy continues balancing with whatever is available until the Slave comes back up.
If the Master goes down, HAProxy re-directs and the application just works. When the Master comes back up, it syncs with the other machines before accepting connections. When it begins accepting connections, then HAProxy starts sending writes to it, and it becomes the Master again, and all is right with the world.
In your situation, you can do something similar, using HAProxy as your connection/failover tool. You do not have to split out your reads and writes, but I do so as to take advantage of the extra machines. I do like the MariaDB/Galera solution. I would hesitate only having 2 nodes. It feels dangerous.
Best Answer
With your current configuration, if Server A goes down, HAProxy will switch to Server B. But as soon as Server A is back up and running on port 3306, HAProxy will switch back to Server A, which is not what you want.
The best way to handle this use case is to use the stick-table parameter.
With a stick table based on the destination IP address, HAProxy will redirect connections to server B even when server A is operational again.
It will only switch back to server A when you restart the HAProxy service or if server B fails or if you disable it manually.
Here is an example of a configuration:
You will find more information about this on this HAProxy blog post and in HAProxy documentation (see stick-table, stick on and peers options)