This is a pretty basic question but I can't find any answers on it. I know you use mongos to interface with a sharded collection. However, if I have a replica set which is not sharded how do I set up mongos so that my application can use both the primary and secondaries in that set? Is it even necessary to use mongos or will having my application's mongo driver (php in this case) connect to one server in the set have it automatically enumerate the other members in the set and connect to them as well?
You can connect to a replica set directly from the driver, a
mongos
is not required unless you are running in a sharded environment. The drivers generally use theisMaster
command to determine the state of the set and will send commands to the primary and the secondary as appropriate (depending on the read preferences set, if you are using the latest versions). The drivers will also handle the automatic failover if the primary goes down (with a small delay of course).Once some people try sharding, they prefer connecting to the
mongos
in general and letting it manage the communications back to the sets as you can see from this request. For now, to usemongos
in this way, you need to have the minimum parts of the sharded environment configured, even if you just have (in effect) one shard.Also, occasionally people will know that they are going to have to shard in the future and start with one shard in anticipation of that future need so that they don't need to do anything other than add a shard to the config.
In general, the main pre-requisite to have
mongos
run is theconfig
database (and hence yourconfig
servers) up and running. The connection string that you pass to themongos
will be a list of thoseconfig
servers (and should be the same on everymongos
you start) and at least one (of the three) will have to be up and healthy for themongos
to function.