MongoDB how to become master

6.2k views Asked by At

I am creating a MongoDB database through a linux terminal and I am trying to create a collection for the database.

But when I run the command: db.createCollection("mainCollection") I get the following error message: { "note" : "from execCommand", "ok" : 0, "errmsg" : "not master" }

I'm not exactly sure what this means. How can I make the database master?

Thanks

3

There are 3 answers

3
Rob Moore On BEST ANSWER

You have started the mongod with the --replSet option (or equivalent configuratin file option). That puts the mongod into a mode where it will not allow any writes until it receives a replica set configuration.

For an existing replica set this is accomplished by doing a rs.add("<host>:<port>") on the existing primary for the replica set.

Based on the conversation around this question I think you have a single MongoDB instance and do not plan to have a true multi-member replica set. If that is the case you have two options:

  • Stop trying to run a replica set
    1. Stop the mongod.
    2. Wipe the data directory for the mongod process.
    3. Restart the mongod process without the --replSet option on the command line/config.
  • Initialize the mongod as a single node replica set.
    1. Run rs.initiate() from the shell (no config is required). You will get disconnected but the shell will automatically reconnect and then you can create collections and do other writes.
4
Sachin Shukla On

Thats expected behaviour "Viratan" if you are querying on the replica set and the shell you are connected with is not the Primary.

You can do either of these two things.

  1. Disconnect from the current shell and connect with the Primary. In mongo shell you would see it written "PRIMARY" or "SECONDARY".

  2. In case you want the same member as the primary then you can increase the priority of that particular member and/or force the formar primary to stepDown. once the primary is step down, the election would occur and because the desired member has higher priority that would become primary. you can follow the below link to change the priority of a member http://docs.mongodb.org/manual/tutorial/force-member-to-be-primary

Once you are connected with Primary in either ways, you can query the DB and do your stuff.

Happy Mongoing.. :-)

-$

0
naseer khusro On

you are trying to create a new collection on secondary.thats why it is giving as error please use primary to create new