简体   繁体   中英

MongoDB how to become master

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

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.

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.. :-)

-$

您正在尝试在Secondary上创建一个新集合。这就是为什么它给出错误,请使用primary来创建新集合

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM