Convert a Standalone to a Replica Set, not work - mongodb

I need to Convert a Standalone to a Replica Set
I'm using the following documentation:
http://docs.mongodb.org/manual/tutorial/convert-standalone-to-replica-set/
After executing the following:
mongod --port 27017 --replSet rs0
in the terminal shows me the following
Thu Nov 15 10:07:57 [rsStart] trying to contact MY_HOST.local:27017
Thu Nov 15 10:07:57 [rsStart] couldn't connect to MY_HOST.local:27017:
couldn't connect to server MY_HOST.local:27017
Thu Nov 15 10:07:57 [rsStart] replSet can't get local.system.replset
config from self or any seed (yet)
In mongo shell, the command results of rs.initiate() and rs.status(), are the following:
> rs.initiate()
{
"startupStatus" : 4,
"info" : "rs0",
"errmsg" : "all members and seeds must be reachable to initiate set",
"ok" : 0
}
> rs.status()
{
"startupStatus" : 4,
"errmsg" : "can't currently get local.system.replset config from self
or any seed (EMPTYUNREACHABLE)",
"ok" : 0
}
Please I need help and tell me I'm doing wrong or what I'm missing
Thanks for your attention

It looks like your mongod already thinks it is part of a replicaset. If you run "rs.status()" you will probably see more than one host. Also, the hostname that mongod thinks it has, needs to be resolvable back to itself. Do not use "localhost" as a hostname in your replicaset configuration.

See this is what you could do , I did same on stand alone PC
1.create 3 directory where data will be stored
eg - c:/data/rep1 , c:/data/rep2 , c:/data/rep3
2.Close all previous instance of mongodb
3.Through command prompt do the following (if ur path is set) else go the folder where mongod exist
command prompt
/>mongod -port 27001 -dbpath -c:/data/rep1 -replSet replication
new command prompt
/>mongod -port 27002 -dbpath -c:/data/rep2 -replSet replication
new command prompt
/>mongod -port 27003 -dbpath -c:/data/rep3 -replSet replication
now connect to any of mongod via another command prompt
/>mongo -port 27001
then we need to write configuration of replica sets
cfg={_id:"replication",members:[{_id:1,host:"localhost:27001"},
{_id:2,host:"localhost:27002"},{_id:3,host:"localhost:27003"}] }
rs.initiate(cfg)
replication will start
rs.status()

Related

MongoServerError: "No host described in new configuration with {version: 1, term: 0} for replica set rs0 maps to this node," How do I fix this?

Started mongo server using
mongod --port 27018 --dbpath "/usr/local/var/mongodb/data/db0" --replSet rs0 --bind_ip "locahost,0.0.0.0"
And connected to the instance using
mongosh mongodb://127.0.0.1:27018/vndr
Tried to initiate a replica set using rs.initiate()
Then got the error
---
> rs.initiate()
{
"ok" : 0,
"errmsg" : "No host described in new configuration with {version: 1, term: 0} for replica set rs0 maps to this node",
"code" : 93,
"codeName" : "InvalidReplicaSetConfig"
}
On the server log noticed these lines saying ""Nnadozies-MacBook-Pro.local" not known
{"t":{"$date":"2022-08-18T14:52:20.861+01:00"},"s":"W", "c":"NETWORK", "id":21207, "ctx":"conn1","msg":"getaddrinfo() failed","attr":{"host":"Nnadozies-MacBook-Pro.local","error":"nodename nor servname provided, or not known"}}
{"t":{"$date":"2022-08-18T14:52:25.866+01:00"},"s":"I", "c":"NETWORK", "id":4834700, "ctx":"conn1","msg":"isSelf could not connect via connectSocketOnly","attr":{"hostAndPort":"Nnadozies-MacBook-Pro.local:27018","error":{"code":6,"codeName":"HostUnreachable","errmsg":"couldn't connect to server Nnadozies-MacBook-Pro.local:27018, connection attempt failed: HostNotFound: Could not find address for Nnadozies-MacBook-Pro.local:27018: SocketException: Host not found (authoritative)"}}}
{"t":{"$date":"2022-08-18T14:52:25.867+01:00"},"s":"E", "c":"REPL", "id":21425, "ctx":"conn1","msg":"replSetInitiate error while validating config","attr":{"error":{"code":74,"codeName":"NodeNotFound","errmsg":"No host described in new configuration with {version: 1, term: 0} for replica set rs0 maps to this node"},"config":{"_id":"rs0","version":1,"members":[{"_id":0,"host":"Nnadozies-MacBook-Pro.local:27018"}]}}}
Followed response in this similar question and added the line 127.0.0.1 Nnadozies-MacBook-Pro.local to my /etc/hosts file to fix this.
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
# Added by Docker Desktop
# To allow the same kube context to work on the host and the container:
127.0.0.1 kubernetes.docker.internal
# End of section
127.0.0.1 figmadaemon.com
127.0.0.1 Nnadozies-MacBook-Pro.local
Posting this because the similar question wasn't really clear on how to solve the problem. I think this makes it clearer that the hostname mongodb was looking at could not be resolved to an IP address because it wasn't in /etc/hosts
Tip on editing /etc/hosts: use sudo permissions for write access
Other than that please I'm curious what other way I can run mongodb instances so they use known hostnames.

MongoDb Sharding on Local Machine

I am trying to create a sharding on my local machine. But when I create MongoDB shard server in the bin directory using this command start mongod --shardsvr --port 2005 -logpath C:\data\shard\s0\log\s0.log --dbpath C:\data\shard\s0 nothing happens so I went on to create mongos router mongos --port 2007 --configdb test/localhost:2001. Mongos created successfully and then I add shard to mongos using command sh.addShard("localhost:2005") then this error appears "errmsg" : "failed to run command { isMaster: 1 } when attempting to add shard localhost:2005 :: caused by :: HostUnreachable: Error connecting to localhost:2005 (127.0.0.1:2005) :: caused by :: No connection could be made because the target machine actively refused it.". I don't know how to deal with this error.

MongoDB server is not running with replset

I do a MongoDB homework and follow the steps, but I faced the problem " server is not running with replset"
I do the step is :
`"start mongod --replSet m101 --logpath 1.log --dbpath \data\rs1 --port 27017 --smallfiles --oplogSize 64
start mongod --replSet m101 --logpath 2.log --dbpath \data\rs2 --port 27018 --smallfiles --oplogSize 64
start mongod --replSet m101 --logpath 3.log --dbpath \data\rs3 --port 27019 --smallfiles --oplogSize 64
Now connect to a mongo shell and make sure it comes up.
mongo --port 27017
Now you will create the replica set. Type the following commands into the mongo shell:
config = { _id: "m101", members:[`
{ _id : 0, host : "localhost:27017"},
{ _id : 1, host : "localhost:27018"},
{ _id : 2, host : "localhost:27019"} ]
};
rs.initiate(config);"
I do not know where is the problem, someone said the problem maybe is another mongod is running. But I have not any other mongod running in the same time.
Thank you!
It happened the same to me as well. The problem was that I have runned mongod without any parameters before I started launching the nodes.
First kill all the mongo, mongod and mongos instances to guarantee the environment is clear.
ps -ef | grep -i 'mongo'
sudo service mongod stop
The problem cause is simple. The default port for mongod and mongos is 27017. So, what happened to me was that the first replica node was never created. The following command was connecting me to a default mongod instance:
mongo --port 27017
I hope this solves your problem. ;)
Check what command line options the mongod you are connected to in the shell is running with:
> use admin
switched to db admin
> db.runCommand("getCmdLineOpts")
{
"argv" : [
"mongod",
"--dbpath",
"/data/db",
"--replSet",
"rs0"
],
"parsed" : {
"replication" : {
"replSet" : "rs0"
},
"storage" : {
"dbPath" : "/data/db"
}
},
"ok" : 1
}
Do you see the replSet option?
You can also try a different way of initiating the replica set. Instead of typing in the configuration and passing it to rs.initiate(), just run rs.initiate(). It will set up default config with one member- the server you are connected to in the shell. Then you can add the other two servers. See the tutorial for more information.
For MacOS Homebrew setup & default configuration
Edit default configuration mongodb file nano /opt/homebrew/etc/mongod.conf
Insert the following piece of code
replication:
replSetName: "rs0"
Save and restart the mongodb service brew services restart mongodb-community#6.0
Exec the following command in mongosh shell rs.initiate()
$ mongosh
> use admin
admin> rs.initiate()
success response:
{
info2: 'no configuration specified. Using a default configuration for the set',
me: '127.0.0.1:27017',
ok: 1
}

Created mongod replication Set but fail to connect

I have created mongoDB replication set using this command:
F:\data>mongod --replSet set1 --dbpath 1 --port 27101
--oplogSize 50 --logpath log.1 --logappend --fork
It does not recognize --fork and it is not there in --help as well (I guess) so I have excluded it and got following output. I think there is nothing wrong so far.
all output going to: log.1
Then when I try to use mongo console using this command
F:\data>mongo --port 27101
It returns
MongoDB shell version: 2.2.2
connecting to: 127.0.0.1:27101/test
Thu Feb 14 01:36:33 Error: couldn't connect to server 127.0.0.1:27101 src/mongo/
shell/mongo.js:93
exception: connect failed
Now what can be done to make it work?
Actually I in the parameter I had this data path --dbpath 1 so I need to have a directory named '1' in my current directory but I had created it in data directory by misunderstanding.
Thankyou #Alptigin Jalayr and others who has lead me to the correct direction.

Testing mongo replication on OSX

In two different terminals, I start up mongod with:
mongod --dbpath 1 --port 27001 --smallfiles --oplogSize 50 --logpath log.1 --replSet test
mongod --dbpath 2 --port 27002 --smallfiles --oplogSize 50 --logpath log.2 --replSet test
(data subdirectories 1 and 2 already created).
I go into a third window and run the mongo shell against one of the mongod instances
mongo --port 27001
In the shell, I setup a configuration as
cfg = { _id: "test", members: [ {_id:0, host: "localhost:27001"}, {_id:1, host:"localhost:27002"} ] }
then run:
use admin
rs.initiate(cfg)
The response from the shell is:
{
"startupStatus" : 4,
"info" : "hotest",
"errmsg" : "all members and seeds must be reachable to initiate set",
"ok" : 0
}
The logfile for instance on port 27001 reports:
Sun Nov 18 18:32:56 [rsStart] trying to contact macbookpro.local:27001
Sun Nov 18 18:32:56 [rsStart] couldn't connect to macbookpro.local:27001: couldn't connect to server macbookpro.local:27001
Sun Nov 18 18:32:56 [rsStart] replSet can't get local.system.replset config from self or any seed (yet)
It appears that the rs.initiate(cfg) cannot connect to the mongod instance on port 27001. But when I run mongod without --replSet the mongo shell connects just fine.
It isn't clear where I am screwing up but I will bet it is obvious.
Found it!
It turns out that when one installs MongoDB on OSX with brew a configuration file is created at /usr/local/etc/mongo.conf. In this file there was the line:
# Only accept local connections
bind_ip = 127.0.0.1
Well, if you look at the log the rsStart error was:
Sun Nov 18 18:32:56 [rsStart] trying to contact macbookpro.local:27001
Sun Nov 18 18:32:56 [rsStart] couldn't connect to macbookpro.local:27001: couldn't connect to server macbookpro.local:27001
Sun Nov 18 18:32:56 [rsStart] replSet can't get local.system.replset config from self or any seed (yet)
and if I ping macbookpro.local the IP address returned was 192.168.41.1. 192.168.41.1 <> 127.0.0.1 so mongod was refusing to respond. Removing this line from the configuration allowed be to setup replication successfully.
sheesh. I am such an idiot. Looked for hours at this and kept thinking that the machine name when connected to the network was 127.0.0.1. Let this be a lesson to you---don't get old!
Figuring out the hostnames can be painful, so instead of passing the config object and trying to work out what your hostname should be, just do rs.initiate() with no arguments on the first host. That will create the required config, discovering the correct value for you. It will print out the "valid" version of the config that it created. Here's a sample:
> rs.initiate()
{
"info2" : "no configuration explicitly specified -- making one",
"me" : "adamc-mbp.local:27001",
"info" : "Config now saved locally. Should come online in about a minute.",
"ok" : 1
}
Copy and paste the "me" field and use the new port number, pass to rs.add():
sotest:PRIMARY> rs.add("adamc-mbp.local:27002")
{ "ok" : 1 }
Now take a look at your config:
sotest:PRIMARY> rs.conf()
{
"_id" : "sotest",
"version" : 2,
"members" : [
{
"_id" : 0,
"host" : "adamc-mbp.local:27001"
},
{
"_id" : 1,
"host" : "adamc-mbp.local:27002"
}
]
}
You should be good to go :)
I had the same issue. Somehow mongo resolves localhost as your machine's name macbookpro.local.
Just add this line to your /etc/hosts file :
127.0.0.1 macbookpro.local