5

I have a 3 node replica set running on 3 different servers. I have finished setting it up and everything works great. Now I want to give my users two connection strings:

  1. Connect to this "xyz" for write

  2. Connect to this "abc" for read only traffic

I am also trying to set up a CNAME for each of the connections, to be able to have the flexibility to change in the future.

Thanks and appreciate your time in advance.

9

Ref: https://docs.mongodb.com/manual/reference/connection-string/

If you are using replica set you do not use your mongod hostname in your connecton string. Rather use all mongos hostname and replica set name.

As an example:

To describe a connection to a replica set named test, with the following mongod hosts:

db1.example.net on port 27017 and db2.example.net on port 2500. You would use a connection string that resembles the following:

mongodb://db1.example.net,db2.example.net:2500/?replicaSet=test

Writes will always go to primary and you do not have to do anything. Config and mongos will communicate among themselves will redirect to primary.

You can configure Write concern which describes the kind of assurances that the mongod and the driver provide to the application regarding the success and durability of the write operation. For a full explanation of write concern and write operations in general, see Write Concern.

Read Preference Options

Read preferences describe the behavior of read operations with regards to replica sets. These parameters allow you to specify read preferences on a per-connection basis in the connection string. The default value is primary, which sends all read operations to the replica set’s primary.

The read preference values are the following:

  • primary
  • primaryPreferred
  • secondary
  • secondaryPreferred
  • nearest

For descriptions of each value, see Read Preference Modes.

| improve this answer | |
4

As per MongoDB BOL Here To Check Replica Set Status & display the current state of the replica set and current state of each member, run the rs.status() method in a mongo shell connected to the replica set’s primary.

NOTE: The rs.status() method is a wrapper that runs the replSetGetStatus database command.

For Example

Given a replica set with three members running on three separate hosts:

m1.example.net
m2.example.net
m3.example.net
All three use the default port 27017.

Test Connections Between all Members

Test the connection from m1.example.net to the other hosts with the following operation set m1.example.net:

mongo --host m2.example.net --port 27017

mongo --host m3.example.net --port 27017
Test the connection from m2.example.net to the other two hosts with the following operation set from m2.example.net, as in:

mongo --host m1.example.net --port 27017

mongo --host m3.example.net --port 27017
You have now tested the connection between m2.example.net and m1.example.net in both directions.

Test the connection from m3.example.net to the other two hosts with the following operation set from the m3.example.net host, as in:

mongo --host m1.example.net --port 27017

mongo --host m2.example.net --port 27017
If any connection, in any direction fails, check your networking and firewall configuration and reconfigure your environment to allow these connections.

As MongoDB BOL Here The replSetGetStatus command returns the status of the replica set from the point of view of the server that processed the command. replSetGetStatus must be run against the admin database. The command has the following prototype form:

{ replSetGetStatus: 1 }

The following example runs the replSetGetStatus command on the admin database of the replica set primary:

db.adminCommand( { replSetGetStatus : 1 } )

Consider the following example output:

{
   "set" : "replset",
   "date" : ISODate("2016-11-02T20:02:16.543Z"),
   "myState" : 1,
   "term" : NumberLong(1),
   "heartbeatIntervalMillis" : NumberLong(2000),
   "optimes" : {
         "lastCommittedOpTime" : {
            "ts" : Timestamp(1478116934, 1),
            "t" : NumberLong(1)
         },
         "readConcernMajorityOpTime" : {
            "ts" : Timestamp(1478116934, 1),
            "t" : NumberLong(1)
         },
         "appliedOpTime" : {
            "ts" : Timestamp(1478116934, 1),
            "t" : NumberLong(1)
         },
         "durableOpTime" : {
            "ts" : Timestamp(1478116934, 1),
            "t" : NumberLong(1)
         }
      },

   "members" : [
      {
            "_id" : 0,
            "name" : "m1.example.net:27017",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 269,
            "optime" : {
                        "ts" : Timestamp(1478116934, 1),
                        "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2016-11-02T20:02:14Z"),
            "infoMessage" : "could not find member to sync from",
            "electionTime" : Timestamp(1478116933, 1),
            "electionDate" : ISODate("2016-11-02T20:02:13Z"),
            "configVersion" : 1,
            "self" : true
      },
      {
            "_id" : 1,
            "name" : "m2.example.net:27017",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 14,
            "optime" : {
               "ts" : Timestamp(1478116934, 1),
               "t" : NumberLong(1)
            },
            "optimeDurable" : {
               "ts" : Timestamp(1478116934, 1),
               "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2016-11-02T20:02:14Z"),
            "optimeDurableDate" : ISODate("2016-11-02T20:02:14Z"),
            "lastHeartbeat" : ISODate("2016-11-02T20:02:15.618Z"),
            "lastHeartbeatRecv" : ISODate("2016-11-02T20:02:14.866Z"),
            "pingMs" : NumberLong(0),
            "syncingTo" : "m3.example.net:27017",
            "configVersion" : 1
      },
      {
            "_id" : 2,
            "name" : "m3.example.net:27017",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 14,
            "optime" : {
               "ts" : Timestamp(1478116934, 1),
               "t" : NumberLong(1)
            },
            "optimeDurable" : {
               "ts" : Timestamp(1478116934, 1),
               "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2016-11-02T20:02:14Z"),
            "optimeDurableDate" : ISODate("2016-11-02T20:02:14Z"),
            "lastHeartbeat" : ISODate("2016-11-02T20:02:15.619Z"),
            "lastHeartbeatRecv" : ISODate("2016-11-02T20:02:14.787Z"),
            "pingMs" : NumberLong(0),
            "syncingTo" : "m1.example.net:27018",
            "configVersion" : 1
      }
   ],
   "ok" : 1,
   "$clusterTime" : {
      "clusterTime" : Timestamp(1510716515, 1),
      "signature" : {
         "hash" : BinData(0,"B2ViX7XLzFLS5Fl9XEuFXbwKIM4="),
         "keyId" : NumberLong("6488045157173166092")
      }
   },
   "operationTime" : Timestamp(1510716515, 1)
}
| improve this answer | |
-1

You can also use the following:

mongo-local "mongodb:/Host1,Host2,Host3/<database>?replicaSet=<replica_set_name>" --eval '<querry>'
| improve this answer | |
  • 3
    This would be a better answer if you explained how it answered the posted question and how it differed from the existing answers. – Michael Green Aug 13 '19 at 0:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.