Testing mongo replication on OSX - mongodb

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

Related

mongodb remote authentication failure Mongod v4

I have recently installed mongodb on one of our windows 2016 server with below configuration and running mongod as
mongod --config C:\mongo_db\config\mongo_db_configuration.conf --auth
--config file
net:
# MongoDB server listening port
#bindIp: 0.0.0.0,192.168.43.250
port: 27017
bindIpAll: true
storage:
# Data store directory
dbPath: "C:\\mongo_db\\db"
mmapv1:
# Reduce data files size and journal files size
smallFiles: true
systemLog:
# Write logs to log file
destination: file
path: "C:\\mongo_db\\logs\\mongodb.log"
security:
authorization: enabled
We have created a root user in admin database
MongoDB Enterprise > db.runCommand({connectionStatus:1})
{
"authInfo" : {
"authenticatedUsers" : [
{
"user" : "root",
"db" : "admin"
}
],
"authenticatedUserRoles" : [
{
"role" : "dbAdminAnyDatabase",
"db" : "admin"
},
{
"role" : "root",
"db" : "admin"
}
]
},
"ok" : 1
}
I am able to connect to the mongod instance locally (mongo --port 27017 -u "root" -p "12345" --authenticationDatabase admin) but getting error while connecting remotely.the server firewall is disabled.
"C:\Mongodb>mongo.exe 192.168.0.171:27017/admin -u root -p 12345
MongoDB shell version v4.0.4
connecting to: mongodb://192.168.0.171:27017/admin
Implicit session: session { "id" : UUID("c68e9dd5-6f7b-4356-bc3e-688a7e2b3e1d") }
MongoDB server version: 4.0.4
2018-11-26T23:50:47.534+0800 E QUERY [js] Error: Authentication failed. :
DB.prototype._authOrThrow#src/mongo/shell/db.js:1685:20
#(auth):6:1
#(auth):1:2
exception: login failed"
I stumbled upon this when I was having the same problem. I was connecting to a MongoDB server version 4.2.6 from a MongoDB shell version v3.6.9. In case anyone else finds this question like I did, here's the syntax that worked:
mongo --host mongodb://username:password#IP:PORT/ --authenticationDatabase admin
I'm also having similar issue with my virtual machines. I have 2 linux and 1 windows machine (which are remotely connected to my windows machine). Let's say Linux A has mongo with credentials. When I try to login from my windows machine, I got the same error that you mention.
However, when I connect mongo from linux B to linux A, I can successfully connect from shell using:
mongo --host 192.168.1.6 --port 27017 -u username -p 'pwd'
I use exact same command from windows as well but somehow it fails to login. Maybe this command could work on your environment but waiting for proper solution as well.

Configure a Replica Set in local and on a cloud-server

I'm trying to configure a Replica Set with 2 members in local e 1 member on a cloud-server.
I started the two instance from local in this way:
mongod --port 27117 --dbpath mongodb/rs0-0 --logpath mongodb/rs0-0/mongo.log --replSet rs0 --fork
mongod --port 27118 --dbpath mongodb/rs0-1 --logpath mongodb/rs0-1/mongo.log --replSet rs0 --fork
and then I started the instance on my cloud-server (after opened the port):
mongod --port 27119 --dbpath mongoRS/rs0-2 --logpath mongoRS/rs0-2/mongo.log --replSet rs0 --fork
So, I started the server to configure the Replica Set:
mongo --port 27117
rsconf = {
_id: "rs0",
members: [{
_id: 0,
host: "localhost:27117"
}]
}
rs.initiate( rsconf )
rs.add("localhost:27118")
Until now all was correct. But when I tryed to add the server-instance a error occurred:
rs.add("myServer.it:27119")
"errmsg" : "Either all host names in a replica set configuration must be localhost references, or none must be; found 2 out of 3",
So I'm thinking that I cannot set this configuration or what?
While trying to add different member in a replSet, you should avoid using localhost(specially when your servers are located at different environments).
Please try below options:
Remove existing replSet member: rs.remove("localhost:27118")
Try to add the server back with host ip(Local Server IP): rs.add("your_server_IP:27118")
Add the cloud server with Public IP: rs.add("Cloud_server_public_IP:27119")
Also ensure 27117,27118 and 27119 ports are opened from both Localhost and Cloud server.
Thanks,
Deep

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
}

mongoDB set name does not match

I have got 3 mongod demo replicates servers running on my machine. I have used following command to create replicated server:
F:\>mongod --replSet test2 --dbpath 2 --port 27112 --oplogSize 50 --logpath log.2 --logappend
all output going to: log.2
Like this I have test1 on 27111 port, test2 on 27112 port and test3 on 27113 port. Yet at configuration I am getting an error:
cfg = {
"_id" : "test1",
"members" : [
{
"_id" : 0,
"host" : "localhost:27111"
},
{
"_id" : 1,
"host" : "localhost:27112"
},
{
"_id" : 2,
"host" : "localhost:27113"
}
]
}
> rs.initiate( cfg )
{
"errmsg" : "couldn't initiate : set name does not match the set name host localhost:27112 expects",
"ok" : 0
}
Now what do I have to do to make it match ?
All the sets I would run should have the same name.
For an instance if I run
mongod --replSet test2 --dbpath 2 --port 27112 --oplogSize 50 --logpath log.2 --logappend
all output going to: log.2
Then the other mongo servers I would intend to be in that set, should have the same name test2
The name or variable used in "cfg" = { "_id":="test1" }
should confirm with the name used in the following for parameter replSet for all the members in that replication set.
Then things will work smoothly for you
"start mongod --replSet "test1" --logpath "1.log" --dbpath C:\Replica\rs01 --port 27017 --oplogSize 64
Good luck
Rao
The steps below worked for me:
mongod --port 27017 --dbpath "C:\MongoDB\data01" --replSet rs0 --bind_ip localhost
mongod --port 27018 --dbpath "C:\MongoDB\data02" --replSet rs0 --bind_ip localhost
These two lines start two different mongo instances in different ports.
On the second instance, I have added replSet=rs0 in mongod.conf.
Then add the secondary in rs0 node by this command:
rs.add("localhost:27018")
Try with the name of your machine or 127.0.0.1. It's a best practice to use the DNS name.
MongoDB replicaset doesn't accept localhost. Try using the actual machine name while adding the node to the replicaset as following:
rs.add("MY_MACHINE_NAME:27017");
When adding the replica sets, the --replset should be same for all three. For example if for primary it was "rs01", it should be same for other two also .

Convert a Standalone to a Replica Set, not work

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()