mongodb localhost replicasetup - mongodb

I want to setup a replicasetup in my local machine,I am using to instances of mongodb(mongod1.conf,mongod2.conf), rs initiated mongo1 on port 27018 and i want to add the members to 27018 , rs.add('ThinkPad-X230:27019') it is throwing an error called
commands :
mongod --replSet Replicaset1 --dbpath home/data --port 27018
mongo --port 27018
>> rs.initiate()
>> rs.add("ThinkPad-X230:27019")
mongod --dbpath home/data2 --port 27019
mongo --port 27019
i have checked the db.serverStatus().host in 27019 port and adding host name "ThinkPad-X230:27019" to rs.add() members it is throwing the error.
{
"ok" : 0,
"errmsg" : "Either all host names in a replica set configuration must be localhost references, or none must be; found 1 out of 2",
"code" : 103,
"codeName" : "NewReplicaSetConfigurationIncompatible",
"operationTime" : Timestamp(1568943205, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1568943205, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}

As you started your first instance of mongod with --replSet Replicaset1 option, it is configured to be a part of Replicaset1 replica set.
And when you initialised the replica set, this instance was added to the replica set as a member. Below is the output snippet of rs.status()
{
"_id" : 0,
"name" : "localhost:27018",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 228,
"optime" : {
"ts" : Timestamp(1569751005, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2019-09-29T09:56:45Z"),
"electionTime" : Timestamp(1569750830, 2),
"electionDate" : ISODate("2019-09-29T09:53:50Z"),
"configVersion" : 3,
"self" : true
}
As you can see the name of the member is "localhost:2018".
So, when you try to add another member to this replica set as rs.add('ThinkPad-X230:27019'), it gives you the following error which is a valid error to be thrown, as one member is "localhost:2018" and another which you are trying to add is "ThinkPad-X230:27019" and both must be localhost.
"errmsg" : "Either all host names in a replica set configuration must be localhost references, or none must be; found 1 out of 2"
Try to add the member using the following command,
rs.add("localhost:27019")
And it will be added successfully.

Related

[Mongodb][OpsManager]Mongodb secondary instances are not adding to replica set

I have created 3 mongodb instances with below mongod.conf [Primary1,Secondary1,Secondary2]
net:
port: 27017
bindIp: 0.0.0.0
replication:
replSetName: Replica1
I have started the 3 instance using " sudo service mongod start "
If I connect primary server to other 2 servers using mongo –host "ip" –port 27017..its working and connected.
Issue 1:
After I do rs.initiate() and rs.conf()
Secondary 1 is included in members but when I use rs.status() but showing stateStr" : "STARTUP"
And If I check its log means below error :
"msg":"Failed to reap transaction table","attr":{"error":"NotYetInitialized: Replication has not yet been configured"}}
Issue 2:
Because of issue 1 if I try to rs.add() for Secondary 2 ,its not working
rs.status() reponse
{
"_id" : 0,
"name" : "primary:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
.......
}
{
"_id" : 1,
"name" : "secondary1:27017",
"health" : 1,
"state" : 0,
"stateStr" : "STARTUP",
"uptime" : 1579,
"optime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2021-01-08T04:47:35.455Z"),
"lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : -2,
"configTerm" : -1
}
Log Output of secondary 1's mongodb.log
{"t":{"$date":"2021-01-08T04:39:16.634+00:00"},"s":"I", "c":"CONNPOOL", "id":22576, "ctx":"ReplNetwork","msg":"Connecting","attr":{"hostAndPort":"primary:27017"}}
{"t":{"$date":"2021-01-08T04:39:18.004+00:00"},"s":"I", "c":"CONTROL", "id":20714, "ctx":"LogicalSessionCacheRefresh","msg":"Failed to refresh session cache, will try again at the next refresh interval","attr":{"error":"NotYetInitialized: Replication has not yet been configured"}}
{"t":{"$date":"2021-01-08T04:39:18.004+00:00"},"s":"I", "c":"CONTROL", "id":20712, "ctx":"LogicalSessionCacheReap","msg":"Sessions collection is not set up; waiting until next sessions reap interval","attr":{"error":"NamespaceNotFound: config.system.sessions does not exist"}}
{"t":{"$date":"2021-01-08T04:39:36.634+00:00"},"s":"I", "c":"CONNPOOL", "id":22576, "ctx":"ReplNetwork","msg":"Connecting","attr":{"hostAndPort":"primary:27017"}}
Issue got fixed. Primary database was able to connect to secondary hosts but in return secondary host can't. So I adjusted the security groups and connected. Takeaway is all the primary and secondary hosts should be able to connect with each other in mongodb port (27017)
Same issue happened for me , networks and everything is fine . Issue with host entry missing for primary server, still secondary was trying to connect DNS name
"ctx":"ReplNetwork","msg":"Connecting","attr":{"hostAndPort":"MONGODB01:27717"}}
Immediately have added into host entry everything is synced

"No host described in new configuration 1 for replica set rs0 maps to this node " : Mongodb

I have installed docker in 3 Ubuntu 16.04 VMs, for each of those VMs docker's containers I run mongodb image using the commande :
docker run -d -p 27017:27017 -v data:/data/db mongo --replSet "rs0"
After that I changed the file
/etc/hosts of each Vms with adding their Ip(vms) addresses in each file of the 3 Vms
cat /etc/hosts
127.0.0.1 localhost
127.0.1.1 mongodb0
aa.bb.cc.ee mongodb0
aa.bb.cc.ff mongodb1
aa.bb.cc.gg mongodb2
when I use the commande rs.initiate () to deploy replicaset
rs.initiate ( {
_id : "rs0", members: [
{ _id: 0, host: "mongodb0:27017" },
{ _id: 1, host: "mongodb1:27017" },
{ _id: 2, host: "mongodb2:27017" }
] })
I have this error :
"operationTime" : Timestamp(0, 0),
"ok" : 0,
"errmsg" : "No host described in new configuration 1 for replica set rs0 maps to this node",
"code" : 93,
"codeName" : "InvalidReplicaSetConfig",
"$clusterTime" : {
"clusterTime" : Timestamp(0, 0),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
how can I fix this issue, please?

How mongo db replicaset works in Azure

I have deployed the Mongo DB replica set using this template mongodb-replica-set-centos.
Mongo DB VM 1 (primary):
ps aux | grep mongo
root 10161 0.7 0.5 797140 40900 ? SLl 05:18 0:05 mongod --dbpath /var/lib/mongo/ --replSet repset --logpath /var/log/mongodb/mongod.log --fork --config /etc/mongod.conf
sshuser 10347 0.0 0.0 112640 960 pts/0 S+ 05:29 0:00 grep --color=auto mongo
Mongo DB database:-
mongo -u mongoadmin -p mongoadmin admin
MongoDB shell version: 3.2.19
connecting to: admin
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings:
2018-03-23T05:18:21.137+0000 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-03-23T05:18:21.137+0000 I CONTROL [initandlisten]
repset:PRIMARY> rs.status()
{
"set" : "repset",
"date" : ISODate("2018-03-23T07:38:45.694Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"members" : [
{
"_id" : 0,
"name" : "52.170.83.3:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 8426,
"optime" : {
"ts" : Timestamp(1521782318, 3),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-03-23T05:18:38Z"),
"electionTime" : Timestamp(1521782318, 1),
"electionDate" : ISODate("2018-03-23T05:18:38Z"),
"configVersion" : 2,
"self" : true
},
{
"_id" : 1,
"name" : "10.0.1.5:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 8407,
"optime" : {
"ts" : Timestamp(1521782318, 3),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-03-23T05:18:38Z"),
"lastHeartbeat" : ISODate("2018-03-23T07:38:45.538Z"),
"lastHeartbeatRecv" : ISODate("2018-03-23T07:38:42.546Z"),
"pingMs" : NumberLong(1),
"configVersion" : 2
}
],
"ok" : 1
}
Mongo DB VM 2 (secondary):
ps aux | grep mongo
root 10115 0.4 0.5 447908 37892 ? SLl 05:11 0:17 mongod --dbpath /var/lib/mongo/ --config /etc/mongod.conf --replSet repset --logpath /var/log/mongodb/mongod.log --fork
sshuser 10269 0.0 0.0 112640 960 pts/0 S+ 06:21 0:00 grep --color=auto mongo
Mongo DB database:-
mongo -u mongoadmin -p mongoadmin admin
MongoDB shell version: 3.2.19
connecting to: admin
2018-03-23T07:38:54.311+0000 E QUERY [thread1] Error: Authentication failed. :
DB.prototype._authOrThrow#src/mongo/shell/db.js:1441:20
#(auth):6:1
#(auth):1:2
exception: login failed
Mongo DB VM 3(secondary):-
ps aux | grep mongo
root 10122 0.6 0.5 795472 40420 ? SLl 05:12 0:26 mongod --dbpath /var/lib/mongo/ --config /etc/mongod.conf --replSet repset --logpath /var/log/mongodb/mongod.log --fork
sshuser 10381 0.0 0.0 112640 960 pts/0 S+ 06:21 0:00 grep --color=auto mongo
Mongo DB database:-
mongo -u mongoadmin -p mongoadmin admin
MongoDB shell version: 3.2.19
connecting to: admin
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings:
2018-03-23T05:12:19.613+0000 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-03-23T05:12:19.613+0000 I CONTROL [initandlisten]
repset:SECONDARY> rs.status()
{
"set" : "repset",
"date" : ISODate("2018-03-23T07:39:04.009Z"),
"myState" : 2,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"members" : [
{
"_id" : 0,
"name" : "52.170.83.3:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 8425,
"optime" : {
"ts" : Timestamp(1521782318, 3),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-03-23T05:18:38Z"),
"lastHeartbeat" : ISODate("2018-03-23T07:39:02.571Z"),
"lastHeartbeatRecv" : ISODate("2018-03-23T07:39:03.573Z"),
"pingMs" : NumberLong(1),
"electionTime" : Timestamp(1521782318, 1),
"electionDate" : ISODate("2018-03-23T05:18:38Z"),
"configVersion" : 2
},
{
"_id" : 1,
"name" : "10.0.1.5:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 8806,
"optime" : {
"ts" : Timestamp(1521782318, 3),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-03-23T05:18:38Z"),
"infoMessage" : "could not find member to sync from",
"configVersion" : 2,
"self" : true
}
],
"ok" : 1
}
Questions:-
Why I am unable to login into Mongo DB VM 2?
After I shutdown Mongo DB VM 3, will Mongo DB VM 2 acts as a secondary node?
If I shutdown Mongo DB VM 1, Will any one of secondary node act as a primary node?
All three questions are answered by the same fact: DB VM2 is not part of the replica set. It's clear from the rs.status() information that only two nodes are registered as part of the replica set, VM1 and VM3.
The implications are that:
On DB VM2, it is not part of the replica set so it does not have the authentication credentials you are trying to log in with
No, DB VM2 will not act as a secondary node; because it is not part of the replica set
In the current setup, with only 2 nodes in the replica set, if you shut down either node (VM1 or VM3) then the other node will not elect itself primary, because it cannot command a majority in an election.
Take a look at the docs on Replica Set Elections to understand what the majority is and why it matters; and take a look at DB VM2 to understand why it is not part of your replica set. Did you ever actually add it?

replica set in MongoDB using docker, primary has error and stops being primary when another member is added to the set

I have two docker containers running a mongo instance each, they were initialized like this:
docker run --name mongodb-shard-1-node-1 -d -v mongodb-shard-1-node-1:/data/db -p 27031:27017 mongo --replSet rs0 --smallfiles --oplogSize 128
when i do docker inspect mongodb-shard-1-node-1 it shows the ip 172.17.0.2
docker run --name mongodb-shard-1-node-2 -d -v mongodb-shard-1-node-2:/data/db -p 27020:27017 mongo --replSet rs0 --smallfiles --oplogSize 128
when i do docker inspect mongodb-shard-1-node-2 it shows the ip 172.17.0.4
So i proceed to access mongodb-shard-1-node-1 by using docker exec -it mongodb-shard-1-node-1 mongo and i initialize it as the primary member like this:
rs.initiate()
{
"info2" : "no configuration specified. Using a default configuration for the set",
"me" : "ee3c41ef76b2:27017",
"ok" : 1
}
Then I proceed to add the mongodb-shard-1-node-2 to this member for it to work as a secondary member, at first it looks like it worked:
rs0:PRIMARY> rs.add("172.17.0.4:27017")
{ "ok" : 1 }
rs0:PRIMARY> rs.status()
{
"set" : "rs0",
"date" : ISODate("2016-05-20T01:04:02.095Z"),
"myState" : 1,
"term" : NumberLong(1),
"members" : [ervalMillis" : NumberLong(2000),
{ "_id" : 0,
"name" : "ee3c41ef76b2:27017",
"state" : 1,,
"uptime" : 27,PRIMARY",
"optime""ts" : Timestamp(1463706237, 1),
}, "t" : NumberLong(1)
"infoMessage" : "could not find member to sync from",
"electionDate" : ISODate("2016-05-20T01:03:43Z"),
"self" : truen" : 2,
{,
"name" : "172.17.0.4:27017",
"state" : 0,,
"uptime" : 4,"STARTUP",
"optime""ts" : Timestamp(0, 0),
}, "t" : NumberLong(-1)
"lastHeartbeat" : ISODate("2016-05-20T01:04:01.187Z"),
"pingMs" : NumberLong(0),Date("1970-01-01T00:00:00Z"),
} "configVersion" : -2
"ok" : 1
}
but right away it fails for some reason and i have no idea why, here's what i get:
rs0:PRIMARY> rs.status()
2016-05-20T01:04:18.007+0000 E QUERY [thread1] Error: error doing query:
failed: network error while attempting to run command 'replSetGetStatus' on host '127.0.0.1:27017' :
DB.prototype.runCommand#src/mongo/shell/db.js:135:1
DB.prototype.adminCommand#src/mongo/shell/db.js:153:16
rs.status#src/mongo/shell/utils.js:1090:12
#(shell):1:1
2016-05-20T01:04:18.012+0000 I NETWORK [thread1] trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
2016-05-20T01:04:18.018+0000 I NETWORK [thread1] reconnect 127.0.0.1:27017 (127.0.0.1) ok
rs0:SECONDARY
What is wrong? How do I fix it?
Edit: just to clarify, i had already tried the connections between the containers by doing what this part of the documentation says at: Test Connections Between all Members
Had my question answered here:
https://dba.stackexchange.com/a/139145/91866
I'm gonna quote the whole answer:
Your primary is trying to auto-configure itself as ee3c41ef76b2:27017 and that then resolves to the loopback (127.0.0.1) which is then not responding on the container as it expects. Depending on what the second container does to resolve ee3c41ef76b2, and especially it it does not resolve to 172.17.0.2, it will probably not be able to talk to the primary either.
Assuming you are correct about the connectivity (and you have verified that the instances are listening on the IP and not just the loopbasck) then you need to override the automatic detection and be explicit when you are calling rs.initiate(), something like this:
rs.initiate(
{
_id: "rs0",
version: 1,
members: [
{ _id: 0, host : "172.17.0.2:27017" },
{ _id: 1, host : "172.17.0.4:27017" }
]
}
)

MonogoDB Replica Set Status Not changing from Startup to Secondary

I have setup a MongoDB replica set with 3 nodes(vm's running CentOS). One node became Primary other 2 stuck in Startup. When these 2 nodes will change their states from startup to secondary.
aryabhata:PRIMARY> rs.status()
{
"set" : "aryabhata",
"date" : ISODate("2016-04-30T08:10:45.173Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "localhost.localdomain:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 69091,
"optime" : Timestamp(1461935462, 1),
"optimeDate" : ISODate("2016-04-29T13:11:02Z"),
"electionTime" : Timestamp(1461934754, 1),
"electionDate" : ISODate("2016-04-29T12:59:14Z"),
"configVersion" : 459192,
"self" : true
},
{
"_id" : 1,
"name" : "repset1.com:27017",
"health" : 1,
"state" : 0,
"stateStr" : "STARTUP",
"uptime" : 92,
"optime" : Timestamp(0, 0),
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2016-04-30T08:10:44.485Z"),
"lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
"pingMs" : 0,
"configVersion" : -2
},
{
"_id" : 2,
"name" : "repset2.com:27017",
"health" : 1,
"state" : 0,
"stateStr" : "STARTUP",
"uptime" : 68382,
"lastHeartbeat" : ISODate("2016-04-30T08:10:43.974Z"),
"lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
"pingMs" : 0,
"configVersion" : -2
}
],
"ok" : 1
}
My problem fix with set ip address for Primary instead hostname
cfg = rs.conf()
cfg.members[0].host = "public-or-private-primary-ip:27017"
rs.reconfig(cfg)
after that secondary state change to STARTUP2
From primary check whether you are able to connect to secondary
mongo --host repset1.com --port 27017
When the above one fails may be firewall or BindIP issue.
Check bind_ip (should be 0.0.0.0, change in mongodb.conf is it's 127.0.0.1):
netstat -plunt | grep :27017 | grep LISTEN
Look at the log-files of secondaries, why they are stuck. Did they receive the configuration details?
Try to reconfigure, see mongo replicaset reconfigure
For me the problem was that primary had authorization enabled. In this case the secondaries always stayed in STARTUP.
To use authorization you need to set keyFile in configuration file of all nodes (primary and secondary).
Create mongodb key file on linux:
openssl rand -base64 741 > mongodb.key
chmod 600 mongodb.key
chown mongod:mongod mongodb.key
mongod.conf file:
replication:
replSetName: rs0
security:
authorization: enabled
keyFile: /home/mongodb.key
Source MongoDB replica set with simple password authentication
It requires that both the primary resolves the host name to IP of the secondary and also the secondary resolves the hostname of the primary to an IP.
In my case I forgot to add in hosts file for the secondary to resolve the hostname of the primary. Once I updated the hosts file in the secondary, the state of the secondary transitioned to STARTUP2 and then to SECONDARY.