使用mongo集群与PHP,PHP如何选择正确的服务器

I have a mongo primary + slave cluster, and a PHP server. The PHP server does both read and write operation.

However, mongo does not allow write operation on slaves. I can't simply use a auto-balance system like haproxy.

If I had to manually choose what server to write to and what server to read from, it would then be stupid.

So, what's the best practice on this?

Best practice is to move out of master-slave and use replica sets.

MongoDB 3.2 deprecates the use of master-slave replication for components of sharded clusters.

Replica sets replace master-slave replication for most use cases. If possible, use replica sets rather than master-slave replication for all new production deployments.

Replica Sets provides all the functionality of master-slave deployments, replica sets are also more robust for production use. Master-slave replication preceded replica sets and made it possible to have a large number of non-master (i.e. slave) nodes, as well as to restrict replicated operations to only a single database; however, master-slave replication provides less redundancy and does not automate failover.

Please see

Deploy Master-Slave Equivalent using Replica Sets

Convert a Master-Slave Deployment to a Replica Set