rs.initiate method not working on MongoDB - mongodb

I'm running MongoDB 3.2.15 on Ubuntu 16.04 LTS
I can't execute rs.initiate();. It returns the following error
{
"ok" : 0,
"errmsg" : "assertion src/mongo/db/repl/replset_commands.cpp:275",
"code" : 8
}
opened mongo with the following arguments
mongod --config /etc/mongod.conf --replSet rs0
Configuration file :
storage:
dbPath: /var/lib/mongodb
journal:
enabled: true
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
net:
port: 27017
bindIp: 0.0.0.0
The log file :
2017-07-26T08:25:38.400+0000 I NETWORK [HostnameCanonicalizationWorker] Starting hostname canonicalization worker
2017-07-26T08:25:42.186+0000 I COMMAND [conn1] initiate : no configuration specified. Using a default configuration for the set
2017-07-26T08:25:42.186+0000 I - [conn1] Assertion failure h != "localhost" src/mongo/db/repl/replset_commands.cpp 275

Try this (success for me):
mongo
> config = {_id:"rs0", members:[ {_id:0, host:"127.0.0.1:27017"}]}
{
"_id" : "rs0",
"members" : [
{
"_id" : 0,
"host" : "127.0.0.1:27017"
}
]
}
> rs.initiate(config)
{ "ok" : 1 }
Reference: http://blog.csdn.net/Aegeaner/article/details/56277129

Related

MongoDB - ReplicaSet - Failed to refresh key cache

I am deploying my first replica set on a Windows 10 machine with MongoDB 5.0.
For the purpose of the tutorial, I want all the servers running on my machine, on different ports.
A)
I created my first server with :
mongod --replSet rs0 --port 27018 --dbpath C:\data\R0S1
I opened another command line prompt and I connected to it with mongo --port 27018.
I iniated the set with the command rs.initiate().
B)
I created my second and third server with :
mongod --replSet rs0 --port 27019 --dbpath C:\data\R0S2
mongod --replSet rs0 --port 27020 --dbpath C:\data\R0S3
(I ran each of these commands in new command line prompts)
C)
I added the second and third server to the set by connecting to the primary server (on 27018
with mongo --port 27018) with :
rs.add("localhost:27019")
rs.add("localhost:27020")
At this stage, everything has worked as expected.
Running rs.conf() gives me :
{
"_id" : "rs0",
"version" : 5,
"term" : 1,
"members" : [
{
"_id" : 0,
"host" : "localhost:27018",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"secondaryDelaySecs" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "localhost:27019",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"secondaryDelaySecs" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "localhost:27020",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"secondaryDelaySecs" : NumberLong(0),
"votes" : 1
}
],
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"catchUpTimeoutMillis" : -1,
"catchUpTakeoverDelayMillis" : 30000,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("61cc297329dca2f0673c2cff")
}
}
D) In a new command line prompt, I create my fourth server (with the idea of making it an arbiter) with :
mongod --replSet rs0 --port 30000 --dbpath C:\data\arb
Here I have a problem.
The log file for this new server contains the following error :
{"t":{"$date":"2021-12-29T10:50:51.767+01:00"},"s":"I", "c":"-",
"id":4939300, "ctx":"monitoring-keys-for-HMAC","msg":"Failed to
refresh key cache","attr":{"error":"NotYetInitialized: Cannot use
non-local read concern until replica set is finished
initializing.","nextWakeupMillis":19200}}
When connecting to the primary and trying to do "rs.addArb(localhost:30000)", the command blocks and does nothing, because of the above error on server on port 30000 I believe.
Do you have any ideas on what's going on and how I could solve my issue ?
---- edit 1 ----
below is my mongod.cfg file :
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: C:\Program Files\MongoDB\Server\5.0\data
journal:
enabled: true
# engine:
# wiredTiger:
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: C:\Program Files\MongoDB\Server\5.0\log\mongod.log
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1
#processManagement:
#security:
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options:
#auditLog:
#snmp:
it might be due to majority read concern introduced as part of mongodb 5.0 as default setting.
Check this if gives read concern as majority then change it to 2.
db.adminCommand({ "getDefaultRWConcern" : 1 })
This might be an issue with mongodb -https://www.mongodb.com/community/forums/t/single-node-replicaset-never-finishing-instanciating-error-cannot-use-non-local-read-concern-until-replica-set-is-finished-initializing/164815/3

mongodb replica set error "...replSetHeartbeat requires authentication..." after running rs.initiate()

I have 2 virtual machines with mongodb running on both. I have created a user and roles and added data for testing. Everything works until I try to set up a Replica set. When I run rs.initiate() I get the "...replSetHeartbeat requires authentication... " error seen below. I can connect to the "hulk" server from "hawkeye" manually by passing --host "nodeserver-hulk:27017. Any Ideas?
system
Ubuntu Server 18.04.4 LTS
Mongod v.4.2.3
rs.initiate command
rs.initiate(
{
_id: "r1",
version: 1,
members: [
{ _id: 0, host : "nodeserver-hulk:27017"},
{ _id: 1, host : "nodeserver-hawkeye:27017"}
]
}
)
rs.initiate error
{
"ok" : 0,
"errmsg" : "replSetInitiate quorum check failed because not all proposed set members responded affirmatively: nodeserver-hawkeye:27017 failed with command replSetHeartbeat requires authentication",
"code" : 74,
"codeName" : "NodeNotFound"
}
mongod.conf file(same for both servers)
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: /mnt/mongo/data
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0
# how the process runs
processManagement:
timeZoneInfo: /usr/share/zoneinfo
#security:
security:
authorization: 'enabled'
#operationProfiling:
#replication:
replication:
replSetName: 'r1'
#sharding:
## Enterprise-Only Options:
#auditLog:
#snmp:
show users command
> show users
{
"_id" : "admin.james",
"userId" : UUID("3ed97f2e-de49-4b98-84c8-566b34805863"),
"user" : "james",
"db" : "admin",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
},
{
"role" : "dbOwner",
"db" : "admin"
},
{
"role" : "clusterAdmin",
"db" : "admin"
},
{
"role" : "readWriteAnyDatabase",
"db" : "admin"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}
You need to add a keyFile in your security config so that each node can authenticate against the others.
https://docs.mongodb.com/manual/tutorial/enforce-keyfile-access-control-in-existing-replica-set/
security:
keyFile: <path-to-keyfile>

Print runtime mongo configuration via mongo cli

Was looking at this: How to check the current configuration of MongoDB
Tried it below:
use admin
switched to db admin
db.runCommand( { getCmdLineOpts: 1 } )
{ "argv" : [ "mongod" ], "parsed" : { }, "ok" : 1 }
My /etc/mongod.conf file:
user#mongo:/# cat /etc/mongod.conf
net:
bindIp: 127.0.0.1
port: 27017
Something I'm missing?
It is very likely that your mongod process is not running with the configuration file /etc/mongod.conf specified. If you are, you should be seeing the configuration file in the output of getCmdLineOpts(). For example:
{
"argv" : [
"/usr/bin/mongod",
"--config",
"/etc/mongod.conf"
],
"parsed" : {
"config" : "/etc/mongod.conf"
}
}
For more information see getCmdLineOpts command

MongoDB Unauthorized: replSetGetConfig

I am trying to setup a replica set with mongodb 3.4 and am facing the following error. Have tried searching around a bit but am not able to find a solution.
root#mongo-db-1:~# mongo MongoDB shell version v3.4.0 connecting to: mongodb://127.0.0.1:27017 MongoDB server version: 3.4.0
> use admin
switched to db admin
> db.auth('admin','****');
1
> db.system.users.find();
{ "_id" : "admin.admin", "user" : "admin", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "DKkxOnMHCSSPwJCJyLA9Eg==", "storedKey" : "9aD//lm3eyeBN2LqZeTdqvvKXlU=", "serverKey" : "OX07H3FVQ447OqGMD7mCmX0WU0M=" } }, "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] }
> rs.conf()
2016-12-20T09:58:45.579+0530 E QUERY [main] Error: Could not retrieve replica set config: {
"ok" : 0,
"errmsg" : "not authorized on admin to execute command { replSetGetConfig: 1.0 }",
"code" : 13,
"codeName" : "Unauthorized"
} :
rs.conf#src/mongo/shell/utils.js:1262:11
#(shell):1:1
MongoDB Logs
2016-12-20T09:58:01.278+0530 I NETWORK [thread1] connection accepted from 127.0.0.1:60804 #2 (1 connection now open)
2016-12-20T09:58:01.279+0530 I NETWORK [conn2] received client metadata from 127.0.0.1:60804 conn2: { application: { name: "MongoDB Shell" }, driver: { name: "MongoDB Internal Client", version: "3.4.0" }, os: { type: "Linux", name: "Ubuntu", architecture: "x86_64", version: "14.04" } }
2016-12-20T09:58:01.282+0530 I ACCESS [conn2] Unauthorized: not authorized on admin to execute command { getLog: "startupWarnings" }
2016-12-20T09:58:01.285+0530 I ACCESS [conn2] Unauthorized: not authorized on admin to execute command { replSetGetStatus: 1.0, forShell: 1.0 }
2016-12-20T09:58:19.044+0530 I ACCESS [conn2] Successfully authenticated as principal admin on admin
2016-12-20T09:58:19.046+0530 I ACCESS [conn2] Unauthorized: not authorized on admin to execute command { replSetGetStatus: 1.0, forShell: 1.0 }
2016-12-20T09:58:45.578+0530 I ACCESS [conn2] Unauthorized: not authorized on admin to execute command { replSetGetConfig: 1.0 }
Is there something I am missing here?
Mongo Config
root#mongo-db-1:~# cat /etc/mongod.conf
storage:
dbPath: /var/lib/mongodb
journal:
enabled: true
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1
security:
authorization: enabled
keyFile: /thefile
processManagement:
fork: true
replication:
replSetName: rs0
You must give clusterMagnager permissions to your "admin" user. To add the clusterManager role to your admin user you have to execute the following function after you authenticate.
db.grantRolesToUser(
"admin",
[ "clusterManager" ]
)
Your "admin" database user only has the userAdminAnyDatabase role.
However, the clusterManager role is needed to execute the replSetGetConfig command:
https://docs.mongodb.com/manual/reference/privilege-actions/#authr.replSetGetConfig
You will need to grant this role to your user to be able to execute this command. This can be done using db.grantRolesToUser().

How can I add shard at mongodb cluster?

I have setting-up mongodb 3.2 cluster as below configuration on my debian machines
# Config Server - mongod.conf
storage:
dbPath: /data/mongodb
journal:
enabled: true
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
net:
port: 27019
bindIp: 0.0.0.0
replication:
replSetName: rs0
sharding:
clusterRole: "configsvr"
# Shard Server 1 - mongod.conf
storage:
dbPath: /data/mongodb
journal:
enabled: true
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
net:
port: 27017
bindIp: 0.0.0.0
replication:
replSetName: rs0
# Shard Server 2 - mongod.conf
storage:
dbPath: /data/mongodb
journal:
enabled: true
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
net:
port: 27017
bindIp: 0.0.0.0
replication:
replSetName: rs0
and run mongos to add shard on query router server
$)mongos --configdb mongo-config:27019
but I can't add shard.
mongos> sh.addShard("rs0/mongo-db1:27017")
{
"ok" : 0,
"errmsg" : "Cannot add rs0/mongo-db1:27017 as a shard since it is part of a config server replica set",
"code" : 96
}
mongos> sh.addShard("rs0/mongo-db1:27017")
{
"ok" : 0,
"errmsg" : "Surprised to discover that mongo-db1:27017 does not believe it is a config server",
"code" : 72
}
getSharmap Command.
mongos> db.runCommand("getShardMap");
{
"map" : {
"config" : "rs0/mongo-db1:27017,mongo-db2:27017",
"mongo-db1:27017" : "rs0/mongo-db1:27017,mongo-db2:27017",
"mongo-db2:27017" : "rs0/mongo-db1:27017,mongo-db2:27017",
"rs0/mongo-config:27019" : "rs0/mongo-config:27019",
"rs0/mongo-db1:27017,mongo-db2:27017" : "rs0/mongo-db1:27017,mongo-db2:27017"
},
"ok" : 1
}
here is mongos log
2016-04-06T15:05:14.243+0000 I SHARDING [Balancer] about to log metadata event into actionlog: { _id: "mongo-router1-2016-04-06T15:05:14.243+0000-570525aa6b2b3378bb9daf56", server: "mongo-router1", clientAddr: "", time: new Date(1459955114243), what: "balancer.round", ns: "", details: { executionTimeMillis: 3, errorOccured: true, errmsg: "could not get updated shard list from config server due to Surprised to discover that mongo-db1:27017 does not believe it is a config server" } }
what I'm missing .?
Since your shard you are trying to add is in rs0 replica set and since rs0 holds your config server it will not let you create the shard. Try adding it to a different replica set such as rs1 and that should work.