Making a replica in MongoDB - mongodb

I am trying to create a replica in mongodb. I have used the following commands to make a replica. I got the following outputs.
cfg = { _id : "mySet", members : [ { _id : 0, host : "localhost:27017"
} ] }
rs.initiate(cfg)
Output:
{
"ok" : 0,
"errmsg" : "Attempting to initiate a replica set with name mySet, but command line reports 0; rejecting",
"code" : 93,
"codeName" : "InvalidReplicaSetConfig"
}
i get the following output:
017-09-22T14:22:22.093+0530 E QUERY [thread1] ReferenceError: conf is not defined :
#(shell):1:1
When i ran show dbs and rs.config(), i get the following errors:
show dbs
2017-09-22T14:23:56.234+0530 E QUERY [thread1] Error: listDatabases
failed:{
"ok" : 0,
"errmsg" : "not master and slaveOk=false",
"code" : 13435,
"codeName" : "NotMasterNoSlaveOk"
} :
_getErrorWithCode#src/mongo/shell/utils.js:25:13 .
Mongo.prototype.getDBs#src/mongo/shell/mongo.js:62:1
shellHelper.show#src/mongo/shell/utils.js:769:19
shellHelper#src/mongo/shell/utils.js:659:15 .
#(shellhelp2):1:1
> rs.config()
2017-09-22T14:24:23.718+0530 E QUERY [thread1] Error: Could not
retrieve replica set config: {
"info" : "run rs.initiate(...) if not yet done for the set",
"ok" : 0,
"errmsg" : "no replset config has been received",
"code" : 94,
"codeName" : "NotYetInitialized"
} :
rs.conf#src/mongo/shell/utils.js:1276:11
#(shell):1:1

delete all replication and oplog.
use local
db.dropDatabase()
restart the mongo
config = {_id: "repl1", members:[
{_id: 0, host: 'localhost:15000'},
{_id: 1, host: '192.168.2.100:15000'}]
}
rs.initiate(config);

Related

"show dbs" fails on mongo shell

This is the first time I am trying to connect to this mongo instance, which is setup by my ex-colleague. when I run "show dbs", I have seen such message:
rs0:SECONDARY> show dbs
2022-01-05T18:33:11.282+0000 E QUERY [js] uncaught exception: Error: listDatabases failed:{
"operationTime" : Timestamp(1641407590, 1),
"ok" : 0,
"errmsg" : "not master and slaveOk=false",
"code" : 13435,
"codeName" : "NotMasterNoSlaveOk",
"$clusterTime" : {
"clusterTime" : Timestamp(1641407590, 1),
"signature" : {
"hash" : BinData(0,"...="),
"keyId" : NumberLong("...")
}
}
} :
_getErrorWithCode#src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs/<#src/mongo/shell/mongo.js:135:19
Mongo.prototype.getDBs#src/mongo/shell/mongo.js:87:12
shellHelper.show#src/mongo/shell/utils.js:906:13
shellHelper#src/mongo/shell/utils.js:790:15
#(shellhelp2):1:1
rs0:SECONDARY>
Any ideas what could be wrong ?
Thanks,
Jack
Following is the screenshot How I got that failure.
You need to execute:
rs.slaveOk()
From the SECONDARY to allow show dbs after ...

mongodb giving error not master with replication not initalized

Installed a fresh instance of mongodb 3.6 on Ubuntu 16.x
While creating user mongo is giving error as Error: couldn't add user: not master
also same error with show dbs
{
"ok" : 0,
"errmsg" : "not authorized on admin to execute command { listDatabases: 1.0, lsid: { id: UUID(\"f9e0590a4-b20a-b21b9eecf627\") }, $db: \"admin\" }",
"code" : 13,
"codeName" : "Unauthorized"
}
Replication is not enabled
{
"info" : "run rs.initiate(...) if not yet done for the set",
"ok" : 0,
"errmsg" : "no replset config has been received",
"code" : 94,
"codeName" : "NotYetInitialized"
}
Why this mongo instance is behaving like slave when its not configured to do so.
I have added hosts entries for other slave instances but not initialised them yet.

sh.status() error processing query: ns=config.mongosTree

if I run sh.status in mongos I get the error:
assert: command failed: {
"ok" : 0,
"errmsg" : "error processing query: ns=config.mongosTree: ping $gt new Date(1532712420740)\nSort: {}\nProj: {}\n No query solutions",
"code" : 2
} : aggregate failed
_getErrorWithCode#src/mongo/shell/utils.js:25:13
doassert#src/mongo/shell/assert.js:16:14
assert.commandWorked#src/mongo/shell/assert.js:290:5
DBCollection.prototype.aggregate#src/mongo/shell/collection.js:1312:5
printShardingStatus#src/mongo/shell/utils_sh.js:611:13
sh.status#src/mongo/shell/utils_sh.js:78:5
#(shell):1:1
2018-07-27T11:28:05.644-0600 E QUERY [thread1] Error: command failed: {
"ok" : 0,
"errmsg" : "error processing query: ns=config.mongosTree: ping $gt new Date(1532712420740)\nSort: {}\nProj: {}\n No query solutions",
"code" : 2
} : aggregate failed :
_getErrorWithCode#src/mongo/shell/utils.js:25:13
doassert#src/mongo/shell/assert.js:16:14
assert.commandWorked#src/mongo/shell/assert.js:290:5
DBCollection.prototype.aggregate#src/mongo/shell/collection.js:1312:5
printShardingStatus#src/mongo/shell/utils_sh.js:611:13
sh.status#src/mongo/shell/utils_sh.js:78:5
I reinstalled all mongo-enterprise packages but still the error persist, all the shards are working correctly, since I can perform find from mongos.
thanks

mongodb returning error using $out with mongos

I have a mongodb cluster containing two shards, each a replication set of 3 nodes, and a config server cluster again with three nodes.
All seems to be in order apart from when I attempt to write a collection at the end of an aggregation pipeline using the $out operator:
db.getCollection('restaurants').aggregate([
{$match : {$text : {$search : "steak"}}},
{$out : "steak_restaurants"}
])
This is return the following error:
ailed to execute script.
Error: Assert: command failed: { "ok" : 0, "errmsg" : "listIndexes
failed: { ok: 0.0, errmsg: \"not master and slaveOk=false\", code:
13435, codeName: \"NotMasterNoSlaveOk\" }", "code" : 18631,
"codeName" : "Location18631" } : aggregate failed
_getErrorWithCode#src/mongo/shell/utils.js:25:13 doassert#src/mongo/shell/assert.js:16:14
assert.commandWorked#src/mongo/shell/assert.js:370:5
DBCollection.prototype.aggregate#src/mongo/shell/collection.js:1319:5
#(shell):1:1
Error: command failed: { "ok" : 0, "errmsg" : "listIndexes failed: {
ok: 0.0, errmsg: \"not master and slaveOk=false\", code: 13435,
codeName: \"NotMasterNoSlaveOk\" }", "code" : 18631, "codeName" :
"Location18631" } : aggregate failed :
_getErrorWithCode#src/mongo/shell/utils.js:25:13 doassert#src/mongo/shell/assert.js:16:14
assert.commandWorked#src/mongo/shell/assert.js:370:5
DBCollection.prototype.aggregate#src/mongo/shell/collection.js:1319:5
#(shell):1:1
The mongo shell is connected to a mongos router. If I execute the same against mongod, this works fine.
The error suggests that somehow reading is taking place from a secondary, however, I have not set any read preference and I would have thought that mongos would resolve the primary correctly by itself. Any ideas or insight please?
Appears to only be occurring when executed through Robo 3T. Executing through a raw shell works fine.
I had the same problem today and solved it by removing the "getCollection".
Can you try running:
db.restaurants.aggregate([
{$match : {$text : {$search : "steak"}}},
{$out : "steak_restaurants"}
])

Error initialising MongoDB replica set in Docker container

I have created a MongoDB docker container, with a replica set, using this command:
docker run -d --name mongo -v /data/db:/data/db mongo --replSet name
The container starts running.
I then try to initiate the replica set, using this command:
rs.initiate()
{
"info2" : "no configuration specified. Using a default configuration for the set",
"me" : "fa07bcdd8591:27017",
"info" : "try querying local.system.replset to see current configuration",
"ok" : 0,
"errmsg" : "already initialized",
"code" : 23
}
But it gives the error message "already initialized".
When I check the health of the replica set with the rs.status() command
rs.status()
{
"state" : 10,
"stateStr" : "REMOVED",
"uptime" : 102,
"optime" : {
"ts" : Timestamp(1472680081, 1),
"t" : NumberLong(77)
},
"optimeDate" : ISODate("2016-08-31T21:48:01Z"),
"ok" : 0,
"errmsg" : "Our replica set config is invalid or we are not a member of it",
"code" : 93
}
It says the replica set config is invalid.
It looks you didn't specify any replica set configuration.
The replica set initiate example:
rs.initiate(
{
_id: "myReplSet",
version: 1,
members: [
{ _id: 0, host : "mongodb0.example.net:27017" },
{ _id: 1, host : "mongodb1.example.net:27017" },
{ _id: 2, host : "mongodb2.example.net:27017" }
]
}
)