Connecting to a replica set with mongo CLI

4,296 views
Skip to first unread message

aharbick

unread,
Jan 18, 2011, 2:33:16 PM1/18/11
to mongodb-user
Is there a way to connect to a mongo replica set through the mongo
CLI? For example I've got a replica set with three replicas. If I
do:
mongo myhost1.com/my_database

it'll connect but a command like "show tables" will trigger:
Tue Jan 18 14:24:51 uncaught exception: error: { "$err" : "not
master", "code" : 10107 }

I've monkeyed around with specifying multiple hosts e.g.:
mongo myhost1.com/my_database,myhost2.com/my_database,myhost3.com/
my_database
mongo -h myhost1.com,myhost2.com,myhost3.com my_database
etc.

but I get warnings that imply I'm not doing something right. Is this
possible?

Scott Hernandez

unread,
Jan 18, 2011, 2:39:32 PM1/18/11
to mongod...@googlegroups.com
The javascript shell is meant to connect to any specific host. It will
not connect to the primary/master node of the replicaset
automatically, like some drivers will do.

You should just connect to the primary (master) directly.

If you want to run queries on secondaries (not-master) then you must
set slaveOk.
> db.getMongo().setSlaveOk()

You can also get help:
> help
> rs.help()

> --
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
>
>

aharbick

unread,
Jan 18, 2011, 3:11:14 PM1/18/11
to mongodb-user
> The javascript shell is meant to connect to any specific host. It will
> not connect to the primary/master node of the replicaset
> automatically, like some drivers will do.

I figured that.

> You should just connect to the primary (master) directly.

That's the problem. Generally I know what the master is but when it
switches around (say one of the replicas bounces) then I have to play
a guessing game.

>
> If you want to run queries on secondaries (not-master) then you must
> set slaveOk.
>
> > db.getMongo().setSlaveOk()

Yeah, that's probably a good solution for most of my use cases.

Thanks!
Andy

Scott Hernandez

unread,
Jan 18, 2011, 3:37:37 PM1/18/11
to mongod...@googlegroups.com
On Tue, Jan 18, 2011 at 12:11 PM, aharbick <ahar...@gmail.com> wrote:
>> The javascript shell is meant to connect to any specific host. It will
>> not connect to the primary/master node of the replicaset
>> automatically, like some drivers will do.
>
> I figured that.
>
>> You should just connect to the primary (master) directly.
>
> That's the problem.  Generally I know what the master is but when it
> switches around (say one of the replicas bounces) then I have to play
> a guessing game.

You can write a script to get the master fairly easily. That is what
the drivers do:
http://www.mongodb.org/display/DOCS/Replica+Set+Commands#ReplicaSetCommands-Commands

Just write a shell script that does that call from a seed server/list
and starts mongo with the primary host.

You could also ask for a feature in the shell for this...
http://jira.mongodb.org/

>> If you want to run queries on secondaries (not-master) then you must
>> set slaveOk.
>>
>> > db.getMongo().setSlaveOk()
>
> Yeah, that's probably a good solution for most of my use cases.
>
> Thanks!
> Andy
>

Wade Kaple

unread,
Jan 9, 2013, 11:54:16 AM1/9/13
to mongod...@googlegroups.com
I was having a similar issue, but in the context of automated deployments.  In order to not have to double check for the primary, I created a findMaster.js file that I pass into the shell before the script that I am trying to execute.  Contents of the file (check if this is primary, otherwise connect to primary):

var master = db.isMaster();
if (master.ismaster == false)
{
  db = connect(master.primary + '/' + db);
}

Then just call the shell with this file first:
mongo <address>/<db> findMaster.js <scriptToExec>.js

The reassignment of the db variable is then available to the second script.

Asya Kamsky

unread,
Apr 10, 2013, 11:21:51 PM4/10/13
to mongod...@googlegroups.com
Wade,

The problem with commenting on extremely old threads is that you might miss that the shell now has ability to connect to a replica set (and reconnect to primary automatically).

Just use
'mongo -host replSetName/host:port'   where you would use your replica set name and give one of the hosts that's currently up - the shell will figure out the rest.

Asya

Cindy Conway

unread,
Dec 27, 2014, 4:42:10 PM12/27/14
to mongod...@googlegroups.com
Perfect. This was just what I was looking for. Thank you Asya.
Reply all
Reply to author
Forward
0 new messages