Mongo Java Replica Set - Can't find master - mongodb

I'm having problem with connecting to Mongo Replica Set in production. Replica Set has master on one machine, and secundary + arbiter on other machine.
When I run my application locally it connects and uses Replica Set properly. I'm using Windows OS.
Mongo URI is like this:
mongodb://192.168.2.95:20000,192.168.2.96:20000/testDatabase
I'm using Java Driver 2.10.1.
When I deploy application in production server, It can't use replica set. Error is "can't find master".
When I change mongo URI to use single node, like this:
mongodb://192.168.2.95:20000/testDatabase
then it works!
What's the problem? I have search the web and I found few people with similar problem but I didn't found any proper solution...

Related

Restart MongoDB Replica Set

I tried to search but most of the solutions are very old, and I feel there will be a better solution available which I am unable to find.
Problem
I created the replica set, and it's working. I tried to connect my docker backed application, and it was connecting to it.
Later I changed the dbpath from dbpath=/var/lib/mongodb to the new path dbpath=/data/mongodb. I didn't restart the replica set after this change.
I ran my backend application and it couldn't connect to the replica set. I think its because I didn't restart the MogoDB ReplicaSet. I am using Mongo version 4.4.5
I want to restart the MongoDB Replica Set to check if this can solve the issue.
My connection string :
mongodb://username:pwd#prod-privateIP-1:27017,prod-privateIP-2:27017,prod-privateIP-3:27017/prodDB?replicaSet=replica1&authSource=admin
What is the best practice/recommended approach to restart the MongoDB Replica Set ?

What's the equivalent of `rs.slaveOk()` for config file in MongoDB?

I have set up my MongoDB on local machine with replicaSet, and I have this error:
An error occurred while loading navigation: 'not master and
slaveOk=false': It is recommended to change your read preference in
the connection dialog to Primary Preferred or Secondary Preferred or
provide a replica set name for a full topology connection.
If I don't exec this command on secondary servers,
rs.slaveOk()
After using the command, the problem is solved, but only temporarily. After restarting the servers, the above error pops up again, I again have to stop it using the command.
How can I somehow define the slaveOk in a config file, so that I don't have to allow slaveOk each time I start the server?
Your connection string should look something like this:
mongodb://mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?replicaSet=myRepl
If you are connecting from shell
mongo "mongodb://mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?replicaSet=myRepl"
OR
mongo --host myRepl/mongodb0.example.com.local:27017,mongodb1.example.com.local:27017,mongodb2.example.com.local:27017
Where myRepl is the replica set name, mongodbX.example.com.local:27017 are your nodes.

Can't create user

I was using MongoDB version 2.6.6 on Google Compute Engine and used the click to deploy method.
rs0:SECONDARY> db.createUser({user:"admin", pwd:"secret_password", roles:[{role:"root", db:"admin"}]})
2015-07-13T15:02:28.434+0000 Error: couldn't add user: not master at src/mongo/shell/db.js:1004
rs0:SECONDARY> use admin
switched to db admin
rs0:SECONDARY> db.createUser({user:"admin", pwd:"secret_password", roles:["root"]})
2015-07-13T15:13:28.591+0000 Error: couldn't add user: not master at src/mongo/shell/db.js:1004
I had a similar problem with mongo 3.2:
Error: couldn't add user: not master :
When trying to create a new user, with root role.
I was using only a local copy of mongo.
In my mongod.conf file I had the following uncommented:
replication:
replSetName: <node name>
Commenting that out and restarting fixed the problem. I guess mongo thought it was part of a replication set, and was confused as to who the Master was.
Edit:
I've also found that if you ARE trying to setup a replication set, and you get the above error, then run:
rs.initiate()
This will start a replication set, and set the current node as PRIMARY.
Exit, and then log back in and you should see:
PRIMARY>
Now create users as needed.
I ran into this error when scripting replica set creation.
The solution was to add a delay between rs.initiate() and db.createUser().
Replica set creation is seemingly done in background and it takes time for the primary node to actually become primary. In interactive use this doesn't cause a problem because there is a delay while typing the next command, but when scripting the interactions the delay may need to be forced.
MongoDB will be deployed in a cluster of Compute Engine instances (also known as a MongoDB replica set). Each instance will use a boot disk and separate disk for database files.
Primary and master nodes are the nodes that can accept writes. MongoDB’s replication is “single-master:” only one node can accept write operations at a time.
Secondary and slave nodes are read-only nodes that replicate from the primary.
Your error message looks like you are trying to add the user on the secondary. Try adding the user in the primary.
I ran into this issue when I thought I was running mongo 3.4 but it was mongo 3.6. Uninstalling 3.6 and installing 3.4 fixed my issue.

Using replica sets in Meteor

With Mongoose, you can connect to replica set like so:
mongoose.connect('mongodb://username:password#host:port/database,mongodb://username:password#host:port,mongodb://username:password#host:port?options...' [, options]);
How do you connect to an external MongoDB replica set with Meteor?
You can simply export MONGO_URL with replicaSet configured. And it works pretty well. Make sure to use meteor version 0.6.4 or greater.
See my article for configuring it.

Mongo on Linux, From Java App, throwing Exception can't find a master

I am running MongoDb before on Windows and my Java app was connecting perfectly. Now I switch the MongoDb to Linux, and started simply as "./mongod". But whenever I try to connect to Mongo, I got the following exception.
Caused by: com.mongodb.MongoException: can't find a master
at com.mongodb.DBTCPConnector.checkMaster(DBTCPConnector.java:434)
at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:209)
at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:305)
at com.mongodb.DBCollection.findOne(DBCollection.java:647)
at com.mongodb.DBCollection.findOne(DBCollection.java:626)
at com.mongodb.DBApiLayer$MyCollection.createIndex(DBApiLayer.java:364)
at com.mongodb.DBCollection.createIndex(DBCollection.java:436)
at com.mongodb.DBCollection.ensureIndex(DBCollection.java:515)
at com.google.code.morphia.DatastoreImpl.ensureIndex(DatastoreImpl.java:245)
at com.google.code.morphia.DatastoreImpl.ensureIndexes(DatastoreImpl.java:310)
at com.google.code.morphia.DatastoreImpl.ensureIndexes(DatastoreImpl.java:279)
at com.google.code.morphia.DatastoreImpl.ensureIndexes(DatastoreImpl.java:340)
at com.google.code.morphia.DatastoreImpl.ensureIndexes(DatastoreImpl.java:333)
That's not a replica master/single problem (as i understood, you are using UMongo).
Before connection, try to change server settings from "localhost:27017" to "127.0.0.1:27017"
Sounds like your configs are different between linux and windows mongo servers. Ensure your linux server has joined the replica set properly , and is not firewalled off from the other servers. All the documentation is here: http://www.mongodb.org/display/DOCS/Replica+Sets