MongoDB add replica-set to external server by ip - mongodb

I have created 3 Digitalocean droplet. By default, I choose Ubuntu 18.06 and MongoDB 4.
Here I have 3 droplets by default MongoDB config and all are up. I have access to "mongo" shell for all of them.
Now I want to run a replica-set setting by this code:
rs.initiate(
{_id : "rs0",
members: [
{ _id: 0, host: "20.30.40.50:27017" },
{ _id: 1, host: "20.30.40.51:27017" },
{ _id: 2, host: "20.30.40.52:27017" }
]
})
In this config, I just told MongoDB that rune the replica set and it retrieves me the error
no replies config has been received
I did not add any bindIp yet also when I added bindIp, I could not start MongoDB again. I put in mongo.conf like this:
bindIp: 127.0.0.1,20.30.40.51,20.30.40.52
Also, There is a private network between these 3 droplets by IP example: 10.10.1.1 Can I take advantage of this private IP to make it easier and safer?

Related

How to auto config mongodb replica set

I want to create a MongoDB replica set, and according to the documentation I need to run sth like this in my first mongo instance in order to config the replica set and this works fine. However, I was wondering if there is a way to automate this process and don't ssh to the server and run this piece of code every time. I tried putting it in a config file but it didn't work
rs.initiate( {
_id : "rs0",
members: [
{ _id: 0, host: "mongodb0.example.net:27017" },
{ _id: 1, host: "mongodb1.example.net:27017" },
{ _id: 2, host: "mongodb2.example.net:27017" }
]
})
Are you talking about situation where you create replica set(s) with docker, terraform, ... kind of tools?
You can always include local init -script what have that rs.initiate(...) command.
Replica set needs ALWAYS initial initiate -command. Without it, it doesn't exist.
Although the question is a bit old, I think it is important to show a possible solution to future readers!
This repo https://github.com/deRemo/dockerized-mongodb presents a python script to automate the configuration of a replica set in a "dockerized" scenario. Have a look at configure_rs.py
Here I extrapolated the important bits:
#!/usr/bin/python3
import os,sys
if len(sys.argv) < 2:
print("USAGE: ./script ip1 ip2 ... (hostname are also valid, if recognised)")
exit()
print("Configuring replica set...")
members = []
for i in range(1, len(sys.argv)):
prio = 1 if i == 1 else 0 #prioritize first member for the primary election
members.append("{_id: "+str(i-1)+", host: \'"+str(sys.argv[i])+"\', priority: "+str(prio)+"}")
#stringify members and prepare rs config
members = ', '.join(d for d in members)
cfg = "{_id: 'rs0', members: ["+members+"]}"
os.system("./mongo --host "+sys.argv[1]+" --eval \"JSON.stringify(db.adminCommand({'replSetInitiate' : "+cfg+"}))\"")
The script takes as input the hostnames of the machines where your mongod instances are deployed (make sure that they are reachable over your network)

ReplicaSetId conflict while adding node MongoDB

When I try to add a new node to my replicate set I get this message:
{
"ok" : 0,
"errmsg" : "Our replica set ID of 5890ad86c92c6c88e8573df0 did not match that of 10.0.253.3:27017, which is 5890a6b137e1380d1e697f2a",
"code" : 103,
"codeName" : "NewReplicaSetConfigurationIncompatible"
}
I had the same error and impossible to find out why ... I come back on the post to send the solution if others pass here.
Simply do not initialize the replicate on both servers:
I have two separate X and Y servers without mongodb, X and Y are IP addresses or domains.
Install mongodb on both servers
Edit the sudo nano /etc/mongod.conf configuration files on both servers
[in file] Replace bindIp: 127.0.0.1 with bindIp: 127.0.0.1,X on the X server
[in file] Replace bindIp: 127.0.0.1 with bindIp: 127.0.0.1,Y on the Y server
[in file] Replace #replication: with replication: on both servers
[in file] Add replSetName: "​​myReplicatName" line under replication: on both servers
Launch mongo with the configuration file on both servers
Only on server X run mongo and type commands
Mongo commands:
rs.initiate ({
_id: "rs0",
members: [{
_id: 1,
host: "X:27017"
}]
});
rs.add("Y:YPORT");

How to set-up Mongo replica set on Kubernetes?

I'm trying set-up a three node Mongo replica set on Kubernetes. My thoughts is start three pods, with Mongo in each one, when the pod starts, it automatically sets up the replica set, so I need to modify the Mongo image Dockerfile to execute some command in Mongo shell.
But I am stuck at executing the command rs.initiate(). If I just use the command rs.initiate() without parameters it works, but when adding parameters then it occurs error like
rs.initiate({ "_id": "rs0", "members" : [ { "_id" : 0, "host" : "172.18.248.87:27017" } ]})
But when executing the pod and using the same command with parameters, it works. The same situation with the command rs.add() . How to resolve this?
Plus: in my Dockerfile I use mongo admin --port 27017 --eval "rs.initiate()" to execute the command .
You should check hosts file (hostname) in pod and add :
ip_address mongoslave
ip_address mongomaster
For example :
172.18.248.87 mongomaster
Then you add parametters:
rs.initiate()
rsconf = {
_id: "rs0",
members: [
{
_id: 0,
host: "mongomaster:27017"
}
]
}
rs.reconfig(rsconf, {force: true})

MongoDB replica set configuration through js file

I have configured a replica set for MongoDB manually.
Once I have launched the daemons on 3 nodes and after they are up and running, from the node which I have identified as "Primary" I run the following command:
mongo
This will drop me into the mongo shell
use admin
Create replica set config:
cfg = {
_id: 'csReplicaSet',
members: [
{ _id: 0, host: 'node1IpAddress:27017'},
{ _id: 1, host: 'node2IpAddress:27017'},
{ _id: 2, host: 'node3IpAddress:27017', arbiterOnly: true}
]
}
rs.initiate(cfg)
I want to know how can I do these steps in a JavaScript file that will be recognised by mongo. I want to automate these steps.
Passing your init script to the mongo shell
Programmatically, you're almost there. You could save your instructions to a JavaScript file and run this on the mongo command line. You should also capture & print the results of rs.initiate() in case there is an error:
eg. contents of initreplica.js:
var cfg = { _id: 'csReplicaSet',
members: [
{ _id: 0, host: 'node1IpAddress:27017'},
{ _id: 1, host: 'node2IpAddress:27017'},
{ _id: 2, host: 'node3IpAddress:27017', arbiterOnly: true}
]
};
var error = rs.initiate(cfg);
printjson(error);
Run this from the command line with:
mongo node1IpAddress:27017/admin initreplica.js
Note that this assumes your three mongod nodes have been started on the same IP addresses including --replSet csReplicaSet parameter. You also only need to do this init sequence once unless you are rebuilding the replica set from scratch (and there is no existing replica set config).
I would also recommend reading the section of the manual on Writing Scripts for the mongo shell. It includes some helpful hints such as how to open new connections to other MongoDB servers as well as the JavaScript equivalents of some shell helpers like use <db>.
Automating a production deployment
If you're looking for ways to automate building a production MongoDB cluster, you should look into recipes for configuration management tools like Chef or Puppet. There is also an Automation feature coming soon as part of MMS (MongoDB Management Service); the Automation feature is currently in beta testing.
I Just run it this way
mongo --port 27018 init_replica.set.js
and this is the script for init from the
config = { _id: "m101", members:[
{ _id : 0, host : "MYHOST:27017" ,priority : 0, slaveDelay : 5 },
{ _id : 1, host : "MYHOST:27018"},
{ _id : 2, host : "MYHOST:27019"} ]
};
rs.initiate(config);
rs.status();

How to modify replica set config?

I have a mongo 2 node cluster running, with this replica set config.
config = {_id: "repl1", members:[
{_id: 0, host: 'localhost:15000'},
{_id: 1, host: '192.168.2.100:15000'}]
}
I have to move these both nodes on to new servers. I have copied everything from old to new servers, but I'm running into issues while reconfiguring the replica config due to ip change on the 2nd node.
I have tried this.
config = {_id: "repl1", members:[
{_id: 0, host: 'localhost:15000'},
{_id: 1, host: '192.168.2.200:15000'}]
}
rs.reconfig(config)
{
"startupStatus" : 1,
"errmsg" : "loading local.system.replset config (LOADINGCONFIG)",
"ok" : 0
}
It shows above message, but change is not happening.
I also tried changing replica set name but pointing to the same data dirs.
I am getting the following error:
rs.initiate()
{
"errmsg" : "local.oplog.rs is not empty on the initiating member. cannot initiate.",
"ok" : 0
}
What are the right steps to change the IP but keeping the data on the 2nd node, or do i need to recreate/resync the 2nd node?
Well , I had the same problem.
I had to delete all replication and oplog.
use local
db.dropDatabase()
restart your mongo with new set name
config = {_id: "repl1", members:[
{_id: 0, host: 'localhost:15000'},
{_id: 1, host: '192.168.2.100:15000'}]
}
rs.initiate(config)
I hope this works for you too
You can use force option when reconfiguring replica set:
rs.reconfig(config, {force: true})
Note that, as Adam already suggested in the comments, you should have at least 3 nodes: 2 full nodes and 1 arbiter (minimum supported configuration) or 3 full nodes (minimum recommended configuration) so that primary node can be elected.
I realise this is an old post, but I discovered I was getting this exact same error when trying to change the port used by secondaries in my replica set.
In my case, I needed to stop the secondary whose config I was changing, and bring it up on its new address and port BEFORE applying the changed config on the Primary.
This is in the mongo documentation, but the order in which I had to bring things up and down was something I'd mis-read on the first pass, so for clarity I've repeated that here:
Shut down the secondary member of the replica set you are moving.
Bring that secondary back up at its new address
Make the configuration change as detailed in the original post above
You can use rs.reconfig option. First retrieve the current configuration with rs.conf(). Modify the configuration document as needed, and then pass the modified document to rs.reconfig()
More info in docs.