Discussion:
status of the mongodb
d***@public.gmane.org
2013-05-19 00:26:55 UTC
Permalink
HI,

We have a mongodb database which has one primary and 2 replicats. when i
login to one database, i would like to check the status of the database if
its primary or secondary. do we have a command to check on it?

Thanks,
Prasad
--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongodb-user-/***@public.gmane.org
To unsubscribe from this group, send email to
mongodb-user+unsubscribe-/***@public.gmane.org
See also the IRC channel -- freenode.net#mongodb

---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
Bernie Hackett
2013-05-19 02:18:55 UTC
Permalink
If you're talking about the mongo shell, it tells you:

$ /mongo
MongoDB shell version: 2.4.3
connecting to: test
repl0:PRIMARY>

If instead you are talking about connecting using one of the drivers, you
Post by d***@public.gmane.org
import pymongo
c = pymongo.MongoClient()
c.test.command('ismaster')['ismaster']
True

The entire result looks like this:

repl0:PRIMARY> rs.isMaster()
{
"setName" : "repl0",
"ismaster" : true,
"secondary" : false,
"hosts" : [
"devbox:27017"
],
"arbiters" : [
"devbox:27019"
],
"primary" : "devbox:27017",
"me" : "devbox:27017",
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"localTime" : ISODate("2013-05-19T02:16:54.540Z"),
"ok" : 1
}

Notice there is a "secondary" field that would be true if you connected to
a secondary. There is also a "primary" field that tells you who the current
primary is, if you aren't connected to it.
http://docs.mongodb.org/manual/reference/command/isMaster/
Post by d***@public.gmane.org
HI,
We have a mongodb database which has one primary and 2 replicats. when i
login to one database, i would like to check the status of the database if
its primary or secondary. do we have a command to check on it?
Thanks,
Prasad
--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To unsubscribe from this group, send email to
See also the IRC channel -- freenode.net#mongodb
---
You received this message because you are subscribed to the Google Groups
"mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/groups/opt_out.
--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongodb-user-/***@public.gmane.org
To unsubscribe from this group, send email to
mongodb-user+unsubscribe-/***@public.gmane.org
See also the IRC channel -- freenode.net#mongodb

---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
d***@public.gmane.org
2013-05-19 07:30:37 UTC
Permalink
Hi,

i am trying to check the status from a shell script.

check_status

stat=`cat $LOG_DIR/status.out |grep PRIMARY |wc -l`
if [ $stat -eq 1 ] ;then
echo "database running as primary, so exiting"
echo "Abort Program !!!"
exit $ERROR
fi


when i run the above from shell script the output is as below

cat $LOG_DIR/status.out
MongoDB shell version: 2.2.4
connecting to: 127.0.0.1:27017/test
bye

i am looking for some command to check the status of the db which i logged
in (not the other 2 db's in the 3 node replication).

Thanks,
Prasad
Post by Bernie Hackett
$ /mongo
MongoDB shell version: 2.4.3
connecting to: test
repl0:PRIMARY>
If instead you are talking about connecting using one of the drivers, you
Post by d***@public.gmane.org
import pymongo
c = pymongo.MongoClient()
c.test.command('ismaster')['ismaster']
True
repl0:PRIMARY> rs.isMaster()
{
"setName" : "repl0",
"ismaster" : true,
"secondary" : false,
"hosts" : [
"devbox:27017"
],
"arbiters" : [
"devbox:27019"
],
"primary" : "devbox:27017",
"me" : "devbox:27017",
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"localTime" : ISODate("2013-05-19T02:16:54.540Z"),
"ok" : 1
}
Notice there is a "secondary" field that would be true if you connected to
a secondary. There is also a "primary" field that tells you who the current
primary is, if you aren't connected to it.
http://docs.mongodb.org/manual/reference/command/isMaster/
Post by d***@public.gmane.org
HI,
We have a mongodb database which has one primary and 2 replicats. when i
login to one database, i would like to check the status of the database if
its primary or secondary. do we have a command to check on it?
Thanks,
Prasad
--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To unsubscribe from this group, send email to
See also the IRC channel -- freenode.net#mongodb
---
You received this message because you are subscribed to the Google Groups
"mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/groups/opt_out.
--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongodb-user-/***@public.gmane.org
To unsubscribe from this group, send email to
mongodb-user+unsubscribe-/***@public.gmane.org
See also the IRC channel -- freenode.net#mongodb

---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
Bernie Hackett
2013-05-19 15:44:48 UTC
Permalink
You could run this in your shell script and test the result:

mongo --quiet --eval "print(db.runCommand('ismaster').ismaster ? 1 : 0)"

That will return 1 if the server is the primary, 0 otherwise.
Post by d***@public.gmane.org
Hi,
i am trying to check the status from a shell script.
check_status
stat=`cat $LOG_DIR/status.out |grep PRIMARY |wc -l`
if [ $stat -eq 1 ] ;then
echo "database running as primary, so exiting"
echo "Abort Program !!!"
exit $ERROR
fi
when i run the above from shell script the output is as below
cat $LOG_DIR/status.out
MongoDB shell version: 2.2.4
connecting to: 127.0.0.1:27017/test
bye
i am looking for some command to check the status of the db which i logged
in (not the other 2 db's in the 3 node replication).
Thanks,
Prasad
Post by Bernie Hackett
$ /mongo
MongoDB shell version: 2.4.3
connecting to: test
repl0:PRIMARY>
If instead you are talking about connecting using one of the drivers, you
Post by d***@public.gmane.org
import pymongo
c = pymongo.MongoClient()
c.test.command('ismaster')['**ismaster']
True
repl0:PRIMARY> rs.isMaster()
{
"setName" : "repl0",
"ismaster" : true,
"secondary" : false,
"hosts" : [
"devbox:27017"
],
"arbiters" : [
"devbox:27019"
],
"primary" : "devbox:27017",
"me" : "devbox:27017",
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"localTime" : ISODate("2013-05-19T02:16:54.**540Z"),
"ok" : 1
}
Notice there is a "secondary" field that would be true if you connected
to a secondary. There is also a "primary" field that tells you who the
current primary is, if you aren't connected to it.
http://docs.mongodb.org/**manual/reference/command/**isMaster/<http://docs.mongodb.org/manual/reference/command/isMaster/>
Post by d***@public.gmane.org
HI,
We have a mongodb database which has one primary and 2 replicats. when i
login to one database, i would like to check the status of the database if
its primary or secondary. do we have a command to check on it?
Thanks,
Prasad
--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To unsubscribe from this group, send email to
See also the IRC channel -- freenode.net#mongodb
---
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send
For more options, visit https://groups.google.com/**groups/opt_out<https://groups.google.com/groups/opt_out>
.
--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To unsubscribe from this group, send email to
See also the IRC channel -- freenode.net#mongodb
---
You received this message because you are subscribed to the Google Groups
"mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/groups/opt_out.
--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongodb-user-/***@public.gmane.org
To unsubscribe from this group, send email to
mongodb-user+unsubscribe-/***@public.gmane.org
See also the IRC channel -- freenode.net#mongodb

---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
d***@public.gmane.org
2013-05-20 00:47:13 UTC
Permalink
Thanks Bernie Hackett, it worked.

Regards,
Prasad
Post by Bernie Hackett
mongo --quiet --eval "print(db.runCommand('ismaster').ismaster ? 1 : 0)"
That will return 1 if the server is the primary, 0 otherwise.
Post by d***@public.gmane.org
Hi,
i am trying to check the status from a shell script.
check_status
stat=`cat $LOG_DIR/status.out |grep PRIMARY |wc -l`
if [ $stat -eq 1 ] ;then
echo "database running as primary, so exiting"
echo "Abort Program !!!"
exit $ERROR
fi
when i run the above from shell script the output is as below
cat $LOG_DIR/status.out
MongoDB shell version: 2.2.4
connecting to: 127.0.0.1:27017/test
bye
i am looking for some command to check the status of the db which i
logged in (not the other 2 db's in the 3 node replication).
Thanks,
Prasad
Post by Bernie Hackett
$ /mongo
MongoDB shell version: 2.4.3
connecting to: test
repl0:PRIMARY>
If instead you are talking about connecting using one of the drivers,
you can use the ismaster command and look at the 'ismaster' field in the
Post by d***@public.gmane.org
import pymongo
c = pymongo.MongoClient()
c.test.command('ismaster')['**ismaster']
True
repl0:PRIMARY> rs.isMaster()
{
"setName" : "repl0",
"ismaster" : true,
"secondary" : false,
"hosts" : [
"devbox:27017"
],
"arbiters" : [
"devbox:27019"
],
"primary" : "devbox:27017",
"me" : "devbox:27017",
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"localTime" : ISODate("2013-05-19T02:16:54.**540Z"),
"ok" : 1
}
Notice there is a "secondary" field that would be true if you connected
to a secondary. There is also a "primary" field that tells you who the
current primary is, if you aren't connected to it.
http://docs.mongodb.org/**manual/reference/command/**isMaster/<http://docs.mongodb.org/manual/reference/command/isMaster/>
Post by d***@public.gmane.org
HI,
We have a mongodb database which has one primary and 2 replicats. when
i login to one database, i would like to check the status of the database
if its primary or secondary. do we have a command to check on it?
Thanks,
Prasad
--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To unsubscribe from this group, send email to
See also the IRC channel -- freenode.net#mongodb
---
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send
For more options, visit https://groups.google.com/**groups/opt_out<https://groups.google.com/groups/opt_out>
.
--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To unsubscribe from this group, send email to
See also the IRC channel -- freenode.net#mongodb
---
You received this message because you are subscribed to the Google Groups
"mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/groups/opt_out.
--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongodb-user-/***@public.gmane.org
To unsubscribe from this group, send email to
mongodb-user+unsubscribe-/***@public.gmane.org
See also the IRC channel -- freenode.net#mongodb

---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
Loading...