Reputation: 721
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 ?
Upvotes: 14
Views: 28787
Reputation: 1914
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.
NOTE: All steps in Terminal!
echo -e "replication:\n replSetName: \"rs0\"" | sudo tee -a /etc/mongod.conf
sudo systemctl restart mongod
sudo systemctl status mongod
Exit with Ctrl+C
.
Initialize your members. First one is Primary
rs.initiate({ _id: 'rs0', members: [ { _id: 0, host: 'localhost:27017' } ]})
Exit mongosh by Ctrl+C
.
Again make sure the service/server is running.
sudo systemctl status mongod
Exit with Ctrl+C
.
Run this command if you want the service to run at Linux startup.
sudo systemctl enable mongod
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).Note: "rs0" is my node's name. Rename it as you wish.
replication:
replSetName: rs0
In Windows, search for Services
App and open it.
Find MongoDb Server
and Restart it.
In CMD/Terminal, enter mongosh
(MongoDb Shell must be installed)
Initialize your members. First one is Primary
rs.initiate({ _id: 'rs0', members: [ { _id: 0, host: 'localhost:27017' } ]})
rs.status()
.Upvotes: 3
Reputation: 4203
You can actually follow the MongoDB Manual to setup your replica set. It's pretty clear. Here are some critical steps described below:
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"
Restart your mongod instance:
systemctl restart mongodb
// or
service mongod restart
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.
Upvotes: 24
Reputation: 31345
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.
Upvotes: 1