Replica Set Error Code 76 - mongodb

In ref to mongo dba course trying to create replica set as asked shown by instructor in El Capitano (Single machine only), I get following error. I have three members:
(mongodb was installed using homebrew)
Step I: Setting up config
cfg ={ _id :"abc", members:[{_id:0, host:"localhost:27001"}, {_id:1, host:"localhost:27002"}, {_id:2, host:"localhost:27003"}] }
{
"_id" : "abc",
"members" : [
{
"_id" : 0,
"host" : "localhost:27001"
},
{
"_id" : 1,
"host" : "localhost:27002"
},
{
"_id" : 2,
"host" : "localhost:27003"
}
]
}
STEP II: Initialize the Config.
rs.reconfig(cfg)
2015-10-05T11:34:27.082-0400 E QUERY Error: Could not retrieve replica set config: { "ok" : 0, "errmsg" : "not running with --replSet", "code" : 76 }
at Function.rs.conf (src/mongo/shell/utils.js:1017:11)
at Function.rs.reconfig (src/mongo/shell/utils.js:969:22)
at (shell):1:4 at src/mongo/shell/utils.js:1017

Make sure you have the replSetName name configured in /etc/mongod.conf
replication:
replSetName: "somename"
Then restart your mongod.
sudo service mongod stop
sudo service mongod start
sudo service mongod restart

You are not running replica set with the repl set name.The solution is to set a replication set name in the mongod config file using the paramater --replSet.
eg) --replSet=test_replica
Once changes are done in config file restart the server.

Related

MongoDB - locahost Not authorized on admin to execute command

I'm trying to initialize a config replica set on a single member. If I run,
mongo --host localhost:27017
It works but as soon as I run the command to initialize the replica set.
rs.initiate({_id : "c_rs1", configsvr: true, members: [{ _id: 1, host : "10.0.0.2:2701"}]})
I get this error
{
"ok" : 0,
"errmsg" : "not authorized on admin to execute command { replSetInitiate: { _id: \"c_rs1\", configsvr: true, members: [ { _id: 1.0, host: \"10.0.0.2:27017\" } ] } }",
"code" : 13,
"codeName" : "Unauthorized"
}
However, if I run this instead
mongo --port 27017
We get this when we run the exact same command.
{ ok : 1 }
This is a problem when I have to run the script to initialize a replica set on a different server because I cannot simply define the host and assume there's a config mongod instance running on the right port on the same server. Any ideas on why the --host option isn't working at the moment?
FYI: security.authentication = disabled in the config file for the mongod

Single Instance Mongodb Replica Set - cannot perform query/insert operations

After installing mongodb, I ran mongod with
mongod --dbpath <pathtodb> --logpath <pathtolog> --replSet rs0
I then connected with the mongo shell and ran
rs.initiate()
I then tried to insert a document into a collection, but received an error:
> db.blah.insert({a:1})
WriteResult({ "writeError" : { "code" : undefined, "errmsg" : "not master" } })
Looking at rs.status(), I see the status is REMOVED:
> rs.status()
{
"state" : 10,
"stateStr" : "REMOVED",
"uptime" : 1041,
"optime" : Timestamp(1429037007, 1),
"optimeDate" : ISODate("2015-04-14T18:43:27Z"),
"ok" : 0,
"errmsg" : "Our replica set config is invalid or we are not a member of it",
"code" : 93
}
I have no idea what I could have done to mess this up. This should have worked I think. How do I get past this?
As above answers said, the config is not set correctly.
I tried to re-init the replica, but got the error msg:
singleNodeRepl:OTHER> rs.initiate({ _id: "rs0", members: [ { _id: 0, host : "localhost:27017" } ] } )
{
"info" : "try querying local.system.replset to see current configuration",
"ok" : 0,
"errmsg" : "already initialized",
"code" : 23,
"codeName" : "AlreadyInitialized"
}
The solution is to reconf the mongo:
singleNodeRepl:OTHER> rsconf = rs.conf()
singleNodeRepl:OTHER> rsconf.members = [{_id: 0, host: "localhost:27017"}]
[ { "_id" : 0, "host" : "localhost:27017" } ]
singleNodeRepl:OTHER> rs.reconfig(rsconf, {force: true})
{ "ok" : 1 }
singleNodeRepl:OTHER>
singleNodeRepl:SECONDARY>
singleNodeRepl:PRIMARY>
Problem here is that you ran rs.initiate().. As EMPTY! You didn't tell what machines belongs to that replica set.
So..
rs.initiate({
_id: "rs0",
version: 1,
members: [
{ _id: 0, host : "address.to.this.machine:27017" }
]
}
)
Short Answer:
I needed to do:
rs.initiate({_id:'rs0', version: 1, members: [{_id: 0, host:'localhost:27017'}]}
rather than rs.initiate().
Long Answer:
I am almost the same as #Madbreaks and #Yihe 's comment, but I was in the different background so that I'm adding my comment here.
Background
I used docker container of mongoDB and initiated the replicaset by rs.initiate(). (The data volume is mounted to the host, but it is out-of-topic here).
What Happend
When I restart the mongoDB container, the error "MongoError: not master and slaveOk=false" happened. Yes, the error message was different from #d0c_s4vage 's, but the workaround is the same as #Yihe 's.
Root Cause
The root cause was dynamically assigned hostname as follows:
rs0:PRIMARY> rs.conf()
{
"_id" : "rs0",
...
"members" : [
{
...
"host" : "ee4ed99b555e:27017", # <----- !!!!
Where, the host "ee4..." above comes from docker container's internal hostname; this is set by my first rs.initiate(). This would be changed when recreate container. In my case, localhost is fine because of single server and single replicaset for 'rocketchat' app evaluation purpose.
I'm also facing same issue, I tried below steps,
NOTE: If you have already cluster setup follow my steps
I stopped particular server (host : "address.to.this.machine:27017")
Remove mongod.lock file
create one more data directory
- (deafult: /data/db, new Data directory: /data/db_rs0)
update the **configuration ** file
-change dbpath ( "/data/db_rs0" ),
- check bindIP (default: 127.0.0.0 to 0.0.0.0)
Check Hostname & Hosts
hostname
sudo vi /etc/hosts
add to hosts
127.0.0.0 hostname
127.0.1.1 hostname
(add your Public/Private IP) hostname
Start the MONGODB server in
sudo /usr/bin/mongod -f /etc/mongod.conf &
rs.initiate({ _id: "rs0", members: [ { _id: 0, host : "hostname:27017" } ] } )
rs.status()
{
....
"members": [
{
"_id": 0,
"name": "hostname:27017",
"health": 1,
"state": 1,
"stateStr": "PRIMARY",
"uptime": 98231,
"optime": {
"ts": Timestamp(1474963880, 46),
"t": NumberLong(339)
},
"optimeDate": ISODate("2016-09-27T08:11:20Z"),
"electionTime": Timestamp(1474956813, 1),
"electionDate": ISODate("2016-09-27T06:13:33Z"),
"configVersion": 12,
"self": true
},
...........
]
"ok": 1
}
-------- THANK YOU --------

mongo replica set member is fatal

I'm trying to configure a replica set of 3 members on 3 different Linux machines. Im running the mongod with replset in the config file. I mistakley set 'rs.initialize' in 2 machines, and now I have found the primary and tried to add the other instances, but it says that the config file is not from the same version.
How can I remove the replica set and start everything back from scratch?
If the following is true:
Is this a brand-new deployment.
There is no data you need to keep.
You can do the following:
Shutdown all 3 mongods.
Remove all files and directories from the
"dbpath" partition in all 3 machines
Restart all 3 mongods Connect
to one of the mongodds and submit the following command
config = { "_id": "rs0", "members" : [
{ "_id" : 0, "host" : "##Your DNS NAME:PORTNUMBER#" },
{ "_id" : 1, "host" : "##Your DNS NAME:PORTNUMBER#" },
{ "_id" : 2, "host" : "##Your DNS NAME:PORTNUMBER#" } ]
}
rs.initiate(config)

exception: hosts cannot switch between localhost and hostname

I created a replication set.
I added localhost in the set in the beginning, but when I try to edit the member with the actual hostname. I get error "exception: hosts cannot switch between localhost and hostname"
I need to get rid of localhost:27017 because, otherwise, it doesn't let me enter any other member as hostname (i.e. non-localhost address)
my-rs0:PRIMARY> cfg=rs.conf();
{
"_id" : "my-rs0",
"version" : 1,
"members" : [
{
"_id" : 0,
"host" : "localhost:27017"
}
]
}
my-rs0:PRIMARY> cfg.members[0].host="my-server04:27017"
my-rs0:PRIMARY> cfg
{
"_id" : "my-rs0",
"version" : 1,
"members" : [
{
"_id" : 0,
"host" : "my-server04:27017"
}
]
}
using rs.reconfig(cfg);
my-rs0:PRIMARY> rs.reconfig(cfg);
{
"errmsg" : "exception: hosts cannot switch between localhost and hostname",
"code" : 13645,
"ok" : 0
}
no luck with rs.add("my-server04:27017") or rs.remove("localhost:27017") as well.
my-rs0:PRIMARY> rs.add("my-server04:27017");
{
"errmsg" : "exception: can't use localhost in repl set member names except when using it for all members",
"code" : 13393,
"ok" : 0
}
I have tried all the reconfiguration methods mentioned here Replica Set Reconfig steps
But, none fixing above issue. Already spent hours, I am really frustrated.
I had the same problem and I fixed it without dropping any database. Just edited the host field of the member in the local.system.replset collection to match the local ip and then restarted mongod. Everything worked perfect.
It looks like you'll need to scrap your replicaset and start over.
I believe that when you initiated your Replica Set, you explicitly passed it a config document that references your MongoDB instance using localhost.
As I was investigating this, I brought up a replica set. When I initiated the replica set using rs.initiate() (without passing a config document) it used host name by default.
rs.initiate()
rs.conf()
{
"_id" : "stack1",
"version" : 1,
"members" : [
{
"_id" : 0,
"host" : "MY-HOSTNAME:28001"
}
]
}
This post describes the need to complete clear out your database files to create a fresh replica set.
Once I did this, I initiated a new replica set in the by passing a configuration document:
cfg = {
"_id" : "stack1",
"version" : 1,
"members" : [
{
"_id" : 0,
"host" : "localhost:28001"
}
]
}
rs.initiate(cfg)
rs.conf()
{
"_id" : "stack1",
"version" : 1,
"members" : [
{
"_id" : 0,
"host" : "localhost:28001"
}
]
}
Long story short, you'll need to delete all of the files in your --dbpath directory and re-create the replica set, without explicitly specifying "localhost" as your hostname.
I did according to the docs:
Restarted MongDB on another port (e.g. 37107) to prevent user connections to it.
Then started a shell on it:
$ mongo --port 37017
Then updated the configuration:
use local
cfg = db.system.replset.findOne( { "_id": "my-rs0" } )
cfg.members[0].host = "my-server04:27017"
db.system.replset.update( { "_id": "my-rs0" } , cfg )
Then restarted MongoDB on the original port.

MongoDB as windows service and setting up replicaSet

I have installed MongoDB and its set up as windows service. When I try to set up replicaSet I am getting error "Only one usage of each socket address (protocol/network address/port) is normally permitted. for socket: 0.0.0.0:27017".
So, I have stopped the windows service and set up replicaSet. The replicaSet is working fine now. But, I don't see the windows service up and running. Does that means I can't set up replicaSet and MongoDB service at the same time?
You can set up replica set and MongoDB service at the same time on Windows. Since you have already set up a replica set, you are aware that you need to have a data directory and a log file for each replica set member. If you are running all replica set members on a single machine, each replica set member must be assigned a different port number. The sample provided is for development or functional testing only. Setting up all replica set members on a single machine will constitute a single point of failure in addition to being a total performance drag.
Create a configuration file for each replica set member including data directory, log file, port number, and replica set name. For example, I have a replica set of 3 members, one primary Mongodb running on port 27017 and two secondary, Mongodb1 on port 37017, and Mongodb2 on port 47017. The replica set name is rs1.
Here is the configuration file for instance Mongodb.
# mongod.conf
# data directory
dbpath=C:\data\db
# log file
logpath=C:\mongodb-win32-i386-2.4.4\log\mongo.log
logappend=true
#port number
port=27017
#replica set name
replSet=rs1
Here is the configuration file for one of the secondaries.
# mongo.conf
# data directory
dbpath=C:\data\db2
# log file
logpath=C:\mongodb-win32-i386-2.4.4\log2\mongo.log
logappend=true
# port number
port=47017
# replica set name
replSet=rs1
The following link provides a complete list of configuration file options:
http://docs.mongodb.org/manual/reference/configuration-options/
Add all three MongoDB instances as Windows service. Since I did not specify the service and service display name, the MongoDB service will use the default service/service display name MongoDB
C:\mongodb-2.4.4\bin>mongod --config C:\mongodb-2.4.4\mongod.cfg --install
Install the other two MongoDB instances with service name and service display name.
C:\mongodb-2.4.4\bin>mongod --config C:\mongodb-2.4.4\mongod1.cfg --serviceName MongoDB1 --serviceDisplayName MongoDB1 --install
C:\mongodb-2.4.4\bin>mongod --config C:\mongodb-2.4.4\mongod2.cfg --serviceName MongoDB2 --serviceDisplayName MongoDB2 --install
Start all three instances of MongDB
C:\mongodb-2.4.4\bin>net start mongodb
The Mongo DB service is starting.
The Mongo DB service was started successfully.
C:\mongodb-2.4.4\bin>net start mongodb1
The MongoDB1 service is starting.
The MongoDB1 service was started successfully.
C:\mongodb-2.4.4\bin>net start mongodb2
The MongoDB2 service is starting.
The MongoDB2 service was started successfully.
Verify the status of all three Windows service using the sc command with query option.
C:\mongodb-2.4.4\bin>sc query mongodb
C:\mongodb-2.4.4\bin>sc query mongodb1
C:\mongodb-2.4.4\bin>sc query mongodb2
Configure replica set from MongoDB shell. In the following example, the MongoDB instance on port 27017 will be the primary replica set member.
C:\mongodb-2.4.4\bin>mongo --port 27017
Set replica set configuration from MongoDB shell.
> config = { _id: "rs1", members:[
... { _id : 0, host : "localhost:27017"},
... { _id : 1, host : "localhost:37017"},
... { _id : 2, host : "localhost:47017"}
... ] }
At MongoDB shell, initialize the replica set and verify its status.
> rs.initiate(config)
{
"info" : "Config now saved locally. Should come online in about a minute.",
"ok" : 1
}
> rs.status()
{
"set" : "rs1",
"date" : ISODate("2013-07-02T18:40:27Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "localhost:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 651,
"optime" : {
"t" : 1372790393,
"i" : 1
},
"optimeDate" : ISODate("2013-07-02T18:39:53Z"),
"self" : true
},
{
"_id" : 1,
"name" : "localhost:37017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 31,
"optime" : {
"t" : 1372790393,
"i" : 1
},
"optimeDate" : ISODate("2013-07-02T18:39:53Z"),
"lastHeartbeat" : ISODate("2013-07-02T18:40:26Z"),
"lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
"pingMs" : 0,
"syncingTo" : "localhost:27017"
},
{
"_id" : 2,
"name" : "localhost:47017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 31,
"optime" : {
"t" : 1372790393,
"i" : 1
},
"optimeDate" : ISODate("2013-07-02T18:39:53Z"),
"lastHeartbeat" : ISODate("2013-07-02T18:40:26Z"),
"lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
"pingMs" : 0,
"syncingTo" : "localhost:27017"
}
],
"ok" : 1
}
rs1:PRIMARY>
You can also check the status of the secondaries. Here I am connecting to one of the secondaries on port 37017.
C:\mongodb-2.4.4\bin>mongo --port 37017
The following prompt will present in MongoDB shell showing the secondary status.
rs1:SECONDARY>
A tutorial on deploying a replica set can be found here:
https://docs.mongodb.com/manual/tutorial/deploy-replica-set/
Not covering the service part.
but setting up sharded replica set.
Try this blog to setup mongodb cluster on your local, it has all the configuration files with windows script to create required folder structure.
https://medium.com/#arun2pratap/set-up-mongodb-sharded-cluster-for-windows-with-just-a-double-click-6eedbb7b79e