To set up the replica set, I've run in 3 separate terminal tabs:
$ sudo mongod --replSet rs0 --dbpath /data/mining --port 27017
$ sudo mongod --replSet rs0 --dbpath /data/mining2 --port 27018
$ sudo mongod --replSet rs0 --dbpath /data/mining3 --port 27019
Then, I configured replication in the Mongo shell and verified that it worked:
> var rsconf = {
_id: "rs0",
members: [
{
_id: 0,
host: 'localhost:27017'
},
{
_id: 1,
host: 'localhost:27018'
},
{
_id: 2,
host: 'localhost:27019'
}
]
};
> rs.initiate(rsconf);
{
"info": "Config now saved locally. Should come online in about a minute.",
"ok": 1
}
// Some time later...
> rs.status()
{
"set": "rs0",
"date": ISODate("2013-06-17T13:23:45-0400"),
"myState": 2,
"syncingTo": "localhost:27017",
"members": [
{
"_id": 0,
"name": "localhost:27017",
"health": 1,
"state": 1,
"stateStr": "PRIMARY",
"uptime": 4582,
"optime": {
"t": 1371489546,
"i": 1
},
"optimeDate": ISODate("2013-06-17T13:19:06-0400"),
"lastHeartbeat": ISODate("2013-06-17T13:23:44-0400"),
"lastHeartbeatRecv": ISODate("2013-06-17T13:23:44-0400"),
"pingMs": 0
},
{
"_id": 1,
"name": "localhost:27018",
"health": 1,
"state": 2,
"stateStr": "SECONDARY",
"uptime": 5034,
"optime": {
"t": 1371489546,
"i": 1
},
"optimeDate": ISODate("2013-06-17T13:19:06-0400"),
"self": true
},
{
"_id": 2,
"name": "localhost:27019",
"health": 1,
"state": 2,
"stateStr": "SECONDARY",
"uptime": 4582,
"optime": {
"t": 1371489546,
"i": 1
},
"optimeDate": ISODate("2013-06-17T13:19:06-0400"),
"lastHeartbeat": ISODate("2013-06-17T13:23:44-0400"),
"lastHeartbeatRecv": ISODate("2013-06-17T13:23:45-0400"),
"pingMs": 0,
"syncingTo": "localhost:27017"
}
],
"ok": 1
}
My script runs fine against the primary:
$ ./runScripts.sh -h localhost -p 27017
MongoDB shell version: 2.4.3
connecting to: localhost:27017/test
Successful completion
However, against either secondary:
$ ./runScripts.sh -h localhost -p 27018
MongoDB shell version: 2.4.3
connecting to: localhost:27017/test
Mon Jun 17 13:30:22.989 JavaScript execution failed: count failed:
{ "note" : "from execCommand", "ok" : 0, "errmsg" : "not master" }
at src/mongo/shell/query.js:L180
failed to load: /.../.../myAggregateScript.js
I've read in multiple places to use rs.slaveOk() or db.getMongo().setSlaveOk(), but neither of these had any effect, whether entered from the shell or called in my script. These statements did not throw errors when called, but they didn't fix the problem, either.
Does anyone know why I can't configure my replset to allow querying of the secondary?
rs.slaveOk() run in the mongo shell will allow you to read from secondaries. Here is a demonstration using the mongo shell under MongoDB 2.4.3:
$ mongo --port 27017
MongoDB shell version: 2.4.3
connecting to: 127.0.0.1:27017/test
replset:PRIMARY> db.foo.save({})
replset:PRIMARY> db.foo.find()
{ "_id" : ObjectId("51bf5dbd473d5e80fc095b17") }
replset:PRIMARY> exit
$ mongo --port 27018
MongoDB shell version: 2.4.3
connecting to: 127.0.0.1:27018/test
replset:SECONDARY> db.foo.find()
error: { "$err" : "not master and slaveOk=false", "code" : 13435 }
replset:SECONDARY> rs.slaveOk()
replset:SECONDARY> db.foo.find()
{ "_id" : ObjectId("51bf5dbd473d5e80fc095b17") }
replset:SECONDARY> db.foo.count()
1
You have to run the command rs.slaveOk() in the secondary server's shell.
Use the following to run queries on your MongoDB secondary:
db.getMongo().setReadPref('secondary')
Related
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?
I've setup a simple server configuration for testing sharding functionnalities purpose and i get the error above.
My configuration is pretty simple: one config server, one shard server and one mongos (respectively in 127.0.0.1:27019, 127.0.0.1:27018, 127.0.0.1:27017).
Everything looks to work well until i try to shard a collection, the command gives me the following:
sh.shardCollection("test.test", { "test" : 1 } )
{
"ok" : 0,
"errmsg" : "ns not found",
"code" : 26,
"codeName" : "NamespaceNotFound",
"operationTime" : Timestamp(1590244259, 5),
"$clusterTime" : {
"clusterTime" : Timestamp(1590244259, 5),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
The config server and shard server outputs show no errors:
2020-05-23T10:39:46.629-0400 I SHARDING [conn11] about to log metadata event into changelog: { _id: "florent-Nitro-AN515-53:27018-2020-05-23T10:39:46.629-0400-5ec935b2bec982e313743b1a", server: "florent-Nitro-AN515-53:27018", shard: "rs0", clientAddr: "127.0.0.1:58242", time: new Date(1590244786629), what: "shardCollection.start", ns: "test.test", details: { shardKey: { test: 1.0 }, collection: "test.test", uuid: UUID("152add6f-e56b-40c4-954c-378920eceede"), empty: false, fromMapReduce: false, primary: "rs0:rs0/127.0.0.1:27018", numChunks: 1 } }
2020-05-23T10:39:46.620-0400 I SHARDING [conn25] distributed lock 'test' acquired for 'shardCollection', ts : 5ec935b235505bcc59eb60c5
2020-05-23T10:39:46.622-0400 I SHARDING [conn25] distributed lock 'test.test' acquired for 'shardCollection', ts : 5ec935b235505bcc59eb60c7
2020-05-23T10:39:46.637-0400 I SHARDING [conn25] distributed lock with ts: 5ec935b235505bcc59eb60c7' unlocked.
2020-05-23T10:39:46.640-0400 I SHARDING [conn25] distributed lock with ts: 5ec935b235505bcc59eb60c5' unlocked.
Of course the collection exists on primary shard:
rs0:PRIMARY> db.test.stats()
{
"ns" : "test.test",
"size" : 216,
"count" : 6,
"avgObjSize" : 36,
"storageSize" : 36864,
"capped" : false,
...
}
I have no idea what could be wrong here, i'd much appreciate any help :)
EDIT:
Here is the detail about steps i follom to run servers, i probably misunderstand something :
Config server:
sudo mongod --configsvr --replSet rs0 --port 27019 --dbpath /srv/mongodb/cfg
mongo --port 27019
Then in mongo shell
rs.initiate(
{
_id: "rs0",
configsvr: true,
members: [
{ _id : 0, host : "127.0.0.1:27019" }
]
}
)
Sharded server:
sudo mongod --shardsvr --replSet rs0 --dbpath /srv/mongodb/shrd1/ --port 27018
mongo --port 27018
Then in shell:
rs.initiate(
{
_id: "rs0",
members: [
{ _id : 0, host : "127.0.0.1:27018" }
]
}
)
db.test.createIndex({test:1})
Router:
sudo mongos --configdb rs0/127.0.0.1:27019
mongo
Then in shell:
sh.addShard('127.0.0.1:27018')
sh.enableSharding('test')
sh.shardCollection('test.test', {test:1})
That error happens sometimes when some routers have out of date ideas of what databases/collections exist in the sharded cluster.
Try running https://docs.mongodb.com/manual/reference/command/flushRouterConfig/ on each mongos (i.e. connect to each mongos sequentially by itself and run this command on it).
I just misunderstood one base concept: config servers and shard servers are distinct and independant mongodb instances, so each must be part of distinct replicasets .
So replacing
sudo mongod --configsvr --replSet rs0 --port 27019 --dbpath /srv/mongodb/cfg
with
sudo mongod --configsvr --replSet rs0Config --port 27019 --dbpath /srv/mongodb/cfg
makes the configuration work.
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
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 --------
I were trying to save my pdf file in Mongo Db's gridFS and then searching in that pdfs using elastic search. I performed following :
1) Mongo DB Side:
mongod --port 27017 --replSet rs0 --dbpath "D:\Mongo-DB\mongodb-win32-i386-2.0.7\data17"
mongod --port 27018 --replSet rs0 --dbpath "D:\Mongo-DB\mongodb-win32-i386-2.0.7\data18"
mongod --port 27019 --replSet rs0 --dbpath "D:\Mongo-DB\mongodb-win32-i386-2.0.7\data19"
mongo localhost:27017
rs.initiate()
rs.add("hostname:27018")
rs.add("hostname:27019")
mongofiles -hlocalhost:27017 --db testmongo --collection files --type application/pdf put D:\Sherlock-Holmes.pdf
2) Elastic Search Side (Installed Plugins : bigdesk/head/mapper-attachments/river-mongodb)
-> Using Elastic Search Head given following request from "Any request" tab
URL : http://localhost:9200/_river/mongodb/
_meta/PUT
{
"type": "mongodb",
"mongodb": {
"db": "testmongo",
"collection": "fs.files",
"gridfs": true,
"contentType": "",
"content": "base64 /path/filename | perl -pe 's/\n/\\n/g'"
},
"index": {
"name": "testmongo",
"type": "files",
"content_type": "application/pdf"
}
}
Now i am trying to access following URL :
http://localhost:9200/testmongo/files/508e82e21e43def09b5e1602?pretty=true
I got following response (Which i believe is as expected) :
{
"_index" : "testmongo",
"_type" : "files",
"_id" : "508e82e21e43def09b5e1602",
"_version" : 1,
"exists" : true, "_source" : {"_id":"508e82e21e43def09b5e1602","filename":"D:\\Sherlock-Holmes.pdf","chunkSize":262144,"uploadDate":"2012-10-29T13:21:38.969Z","md5":"025fa2046f9254d2aecb9e52ae851065","length":98272,"contentType":"application/pdf"}
}
But when i were trying to search on this pdf using following URL:
http://localhost:9200/testmongo/files/_search?q=Albers&pretty=true
Its giving me following result :
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 0,
"max_score" : null,
"hits" : [ ]
}
}
Here its showing me no any hit but word "Albers" present in this pdf. Please help. Thanks in advance.
i think you have to specify the property to be searched
http://localhost:9200/testmongo/files/_search?q=<PROPERTYNAME>:Albers&pretty=true
or even for complex searches
$ curl -XPOST 'http://localhost:9200testmongo/files/_search?q' -d '{
<PROPERTYNAME> : "value",
<PROPERTYNAME> : {
<PROPERTYNAME> : "value",
<PROPERTYNAME> : "value"
}
}
'
but as far as i know you can only search for your defined properties after indexing your data.