10

I've got this problem that I can't solve. Partly because I can't explain it with the right terms. I'm new to this so sorry for this clumsy question.

Below you can see an overview of my goal.

I want configure Replication Set in MongoDB for that i tried like this

use local
db.dropDatabase()

config = { _id: "rs0", members:[
{_id: 0, host: 'localhost:27017'}]
}

rs.initiate(config)

i hope every thing is correct only but here its showing the following error message

{ "errmsg" : "server is not running with --replSet", "ok" : 0 }

Anything wrong i did here ?

Any ideas ?

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
Naresh
  • 681
  • 2
  • 12
  • 39
  • 1
    the error message tells you exactly what you need to do: change your configuration file (usually /etc/mongodb.conf, depends on your Linux distribution), add this line: replSet = [replica set name] – yaoxing Apr 22 '14 at 05:02
  • @yaoxing still i'm getting same error message... – Naresh Apr 22 '14 at 05:08
  • Please see my anwer here : https://stackoverflow.com/a/61929189/3904109 – DragonFire May 21 '20 at 09:41

2 Answers2

20

You can actually follow the MongoDB Manual to setup your replica set. It's pretty clear. Here are some critical steps described below:

  1. Change your configuration file and add the following line.Don't mess it up with master/slave replication, because replica set is meant to be a replacement of master/slave replication. So you may want to remove those master/slave related config from your configuration files.

    replSet = [set name]
    

    EDIT: The replSet does not seem to exist anymore in recent versions of mongoDB, at least it is no longer documented. The following seems to have done the trick in my case.

    replication: 
      replSetName: "smm"
    
  2. Restart your mongod instance:

    systemctl restart mongodb
    // or
    service mongod restart
    
  3. go to local database and initiate your replica set. Don't pass anything to the initiate function, and mongodb will handle everything just well. Note it will use your hostname as the name of current instance and as I know it's not that easy to change. So you may want to change your host name before doing so.

    use local
    rs.initiate()
    

That's it. Your set is good to go. If you have other member to join the set, you need to do the 1/2 steps, and go to your primary instance and type:

rs.add("hostname:port")

Only when you want to change configs of replica set, do you need to type:

var conf = rs.conf();
// change your conf here
rs.reconfig(conf);

Note this will lead to server offline a little bit time. If you are doing it online, be careful.

toto_tico
  • 17,977
  • 9
  • 97
  • 116
yaoxing
  • 4,003
  • 2
  • 21
  • 30
  • near to rs.initiate() i'm getting same error message { "errmsg" : "server is not running with --replSet", "ok" : 0 } – Naresh Apr 22 '14 at 05:19
  • & if i tried forcefully with rs.reconfig(conf) its giving the following err " TypeError: rs.conf() has no properties shell/utils.js:1494 " – Naresh Apr 22 '14 at 05:22
  • how did you start your mongod instance? with command or as a daemon? – yaoxing Apr 22 '14 at 05:22
  • if you are starting it with shell command, it should be something like: mongod --replSet rs0 --dbpath /var/lib/mongo/... – yaoxing Apr 22 '14 at 05:23
  • so just add the --replSet [set name] to your command, that should do the trick. – yaoxing Apr 22 '14 at 05:24
  • if i run the following query mongod --replSet rs0 --dbpath /var/lib/mongo/... i'm getting this error [initandlisten] MongoDB starting : pid=5186 port=27017 dbpath=/var/lib/mongo/ 64-bit host=naresh [initandlisten] db version v2.0.4, pdfile version 4.5 [initandlisten] git version: nogitversion [initandlisten] build info: Linux yellow 2.6.24-29-server #1 SMP Tue Oct 11 15:57:27 UTC 2011 x86_64 BOOST_LIB_VERSION=1_46_1 [initandlisten] options: { dbpath: "/var/lib/mongo/", replSet: "rs0" } [initandlisten] exception in initAndListen: 10296 dbpath (/var/lib/mongo/) does not exist, terminating... – Naresh Apr 22 '14 at 05:31
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/51149/discussion-between-naresh-and-yaoxing) – Naresh Apr 22 '14 at 05:31
  • @yaoxing, the `replSet` might no longer exist, at least I could not find it in the [current documentation](https://docs.mongodb.com/manual/reference/configuration-options/#replication-options). Insted I tried `replSetName: "the_name"`, inside `replication:` in the configuration file (in my case, `//etc/mongod.conf`). I will edit your answer as it won't be visualized properly as a comment. – toto_tico Mar 19 '19 at 15:38
  • @toto_tico you are right. that is a mistake. thanks for editing. – yaoxing Mar 25 '19 at 06:10
  • Thanks man, it was a day looking for in documentation – Masoud Darzi Sep 16 '20 at 12:40
1

I would wish to discuss the concept of replication and replica set in MongoDB.

How do we get availability and fault tolerance? And by that we mean if that node goes down, we want to still be able to use the system. And if the primary nodes goes down and we lose it entirely for some reason, let's say there's a lost between backups or a hardware damage making the system unusable. And so what we do to solve both those problems is we introduce replication.

A replica set refers to a set of mongod and mongo nodes that act together and all mirror each other in terms of data. There is one primary and the other nodes are secondaries. But, that selection is dynamic. And the data written to the primary will asynchronously replicate to the secondaries. The application and drivers stay connected to the primary and will and can only write to the primary. Say, if primary goes down, one of the secondaries will perform an election to elect a new primary. To elect a new primary, we have to have a strict majority of the original number of nodes. So, since the original number of nodes here was 3, we need 2 nodes to elect a new primary and that's the number we have. So, if one went down, then anyone else can become primary. And in that case, the app will connect to the primary for the rights, through the driver. All transparently.

Later if the down servers gets up, it will join the replica set as a secondary. And the minimum number of nodes is 3 - because if we have less than 3, then what would remain would not be a majority of that set of the original set. And so, there would be no way to elect the new primary. So, we would go with no primary, which means we could no longer take rights.

Zameer Ansari
  • 28,977
  • 24
  • 140
  • 219