How to configure a replica set with MongoDB
Asked Answered
C

3

13

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 ?

Cartulary answered 22/4, 2014 at 4:48 Comment(3)
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]Zapateado
@Zapateado still i'm getting same error message...Cartulary
Please see my anwer here : https://mcmap.net/q/904510/-setting-up-mongodb-replica-setOutbreak
Z
23

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.

Zapateado answered 22/4, 2014 at 5:14 Comment(10)
near to rs.initiate() i'm getting same error message { "errmsg" : "server is not running with --replSet", "ok" : 0 }Cartulary
& if i tried forcefully with rs.reconfig(conf) its giving the following err " TypeError: rs.conf() has no properties shell/utils.js:1494 "Cartulary
how did you start your mongod instance? with command or as a daemon?Zapateado
if you are starting it with shell command, it should be something like: mongod --replSet rs0 --dbpath /var/lib/mongo/...Zapateado
so just add the --replSet [set name] to your command, that should do the trick.Zapateado
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...Cartulary
let us continue this discussion in chatCartulary
@yaoxing, the replSet might no longer exist, at least I could not find it in the current documentation. 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.Bubonocele
@Bubonocele you are right. that is a mistake. thanks for editing.Zapateado
Thanks man, it was a day looking for in documentationMisdoubt
A
2

Linux & Windows

None of the solutions worked for me. Most of them suggest to stop the service or shutdown the db before initializing replica nodes which is either misleading or outdated (anyways not working!).

Here's the working solution as of today.

LINUX

NOTE: All steps in Terminal!

  1. Set replica name using command bellow: (Mine is called "rs0").

echo -e "replication:\n replSetName: \"rs0\"" | sudo tee -a /etc/mongod.conf

  1. Restart mongod service

sudo systemctl restart mongod

  1. Make sure the service/server is running.

sudo systemctl status mongod

  1. Enter mongosh (MongoDb Shell must be installed) enter image description here

  2. Initialize your members. First one is Primary

rs.initiate({ _id: 'rs0', members: [ { _id: 0, host: 'localhost:27017' } ]})

  1. Check the status using rs.status(). Here's my output: enter image description here

  2. Run this command if you want the service to run at Linux startup.

sudo systemctl enable mongod

Creadit https://docs.rocket.chat/setup-and-configure/environment-configuration/mongodb-configuration/configure-a-replica-set-for-mongodb

Windows

  1. Go to this folder C:\Program Files\MongoDB\Server\X.0\bin (Replace the X with your version) and open mongod.cfg with a text editor as Admin. (e.g. VSCode does the admin job).
  2. Add this at the end of the lines:

Note: "rs0" is my node's name. Rename it as you wish.

    replication:
      replSetName: rs0
  1. In Windows, search for Services App and open it.

  2. Find MongoDb Server and Restart it.

  3. In CMD/Terminal, enter mongosh (MongoDb Shell must be installed) enter image description here

  4. Initialize your members. First one is Primary

rs.initiate({ _id: 'rs0', members: [ { _id: 0, host: 'localhost:27017' } ]})

  1. Check the status using rs.status().
Amadavat answered 3/2 at 12:19 Comment(0)
S
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.

Stivers answered 24/9, 2016 at 14:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.