[MongoDB Ops Manager]mongo.mongoUri ops manager db hosts connection not working - mongodb

I have installed MongoDB Ops Manager Database [3 instances]
/etc/mongod.conf -> bindIp: 0.0.0.0
And I have Installed Ops Manager Application
In /conf/conf-mms.properties my config for mongo.mongoUri is below
mongo.mongoUri=mongodb://1.2.3.4:27017,5.6.7.8:27017,9.10.11.12:27017
Error:
Failure to connect to configured mongo instance: Config{loadBalance=false.....
pre-flight is getting failed
Notes:
mongod is running in all db servers
If single db is mentioned in mongo.mongoUri then application can connect to that database like mongo.mongoUri=mongodb://1.2.3.4:27017
I guess I need to create as replica set as per doc so i tried below command in main database server
mongod --port 27017 --dbpath /home/ubuntu/data/appdb --replSet rs0 --bind_ip localhost,5.6.7.8,9.10.11.12
{"t":{"$date":"2021-01-06T17:36:07.509+00:00"},"s":"E", "c":"STORAGE", "id":20568, "ctx":"initandlisten","msg":"Error setting up listener","attr":{"error":{"code":9001,"codeName":"SocketException","errmsg":"Cannot assign requested address"}}}

Issue is fixed. Single(standalone) database host was able to connect. But for multiple databases to connect we need to implement replica set first for the mongodb databases.

Related

How do I connect to mongodb on MacOS after adding configurations for replica set?

I want to tail mongo collection's documents and in order to do so, I need to activate the oplog. Oplog requires some extra configuration in the mongod.conf file and these configurations are not letting mongo connected.
I tried to look into questions pertaining to tailing mongodb, setting conf file but none could address my need of connecting to mongodb after setting replciaset configs.
My conf file, location of which is /usr/local/etc/mongod.conf is:
systemLog:
destination: file
path: /usr/local/var/log/mongodb/mongo.log
logAppend: true
storage:
dbPath: /usr/local/var/mongodb
net:
bindIp: 127.0.0.1
replSet=rs0
oplogSize=1024
I am not getting anything in logs, brew services list command also shows the following:
mongodb started aviralsrivastava /Users/aviralsrivastava/Library/LaunchAgents/homebrew.mxcl.mongodb.plist
Although, when rs0 is set, the started signal is yellow in color, with the last two lines commented, its green.
I expect mongodb to get connected, mongo shell to start but all I get is connection refused. If I comment the last two lines, mongo gets connected. And yes, I did restart mongodb via brew services restart mongodb each time I changed the config file.
Following is the error when I try to connect via command mongodb:
➜ etc mongo
MongoDB shell version v4.0.3
connecting to: mongodb://127.0.0.1:27017
2019-04-27T08:53:31.350+0530 E QUERY [js] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
connect#src/mongo/shell/mongo.js:257:13
#(connect):1:6
exception: connect failed
mongod --config path/to/config-file-portx --replSet rsName
connect:
mongo --port xxx
config:
config:
rs.initialize();
rs.add(host:port-y);
rs.status()
connect with replica:
mongo "mongodb://host1,host2,host3/myDB?replicaSet=rsName"

MongoDB Sharding - Unable to reach primary for set c1

I have a huge amount of data, therefore I am going to use sharding on MongoDB. I have exactly followed the same steps mentioned here, but getting the warning Unable to reach primary for set c1 when I execute the command mongos --configdb c1/mongodb-test-config on the front-end machine.
Also, when I execute the next command mentioned in the linked tutorial mongo localhost:27017 on the front-end machine, getting the error couldn't connect to server localhost:27017.
Notes in advance:
I have already configured iptables to allow connections on the port 27017
I have set bindIp to 0.0.0.0 in the mongod.conf file
MongoDB Version: 4.0.2
Ubuntu Version: 16.04.5
All the machines (2 shards, 1 config server, and 1 front-end) are located in the same network, and successfully connect with each other.

Unable to launch mongos

I am attempting a simple sharding set up (on a single host without any replica set). However I am unable to go any further because this is what happens when i try to start mongos:
C:\>mongos --configdb localhost:27010 --port 27011
I get:
BadValue: configdb supports only replica set connection string
try 'mongos --help' for more information
I am failing to see what is lacking. I tried mongos --help, but according to that valid arguments for --configdb are <config replset name>/<host1:port>, <host2:port>, etc. But this is what I've done.
I have not done anything else than starting the config server:
mongod --configsvr --port 27010
which is the one I am trying to connect the mongos to.
Any ideas on how this can be resolved?
Thankful for any advice in advance.
You have set up the config server as a standalone mongod process, but as of MongoDB 3.4 that isn't supported: it must be a replicaset:
config servers: Config servers store metadata and configuration settings for the cluster. As of MongoDB 3.4, config servers must be deployed as a replica set (CSRS).
The minimum setup is to have a single mongod process, configured as a 1-member replica set; then your mongos process connects to the replica set:
mongos --configdb replsetname/localhost:27010 --port 27011

How to disable remote connections to MongoDB?

Normally the answer to question is to set:
bindIp: 127.0.0.1
I have this set in /etc/mongod.conf. Unfortunately I am still allowed access to this database remotely. I have restarted the Mongo service a couple times, to no avail.
Does anyone have an idea as to why my database is still accessible remotely?
I'm using MongoDB version 3.0.9
Remoting in to mongod clients using bindIp = 127.0.0.1 is possible through an SSH tunnel because the shell session is seen as 127.0.0.1.
Enabling bind_ip = 127.0.0.1 should be sufficient. Restart MongoDB server after the changes are done.
References:
http://greenwireit.com/it-tech-support-articles/enable-remote-access-default-mongodb-installation/
https://www.mkyong.com/mongodb/mongodb-allow-remote-access/
http://wptrafficanalyzer.in/blog/enabling-and-disabling-remote-access-to-a-mongodb-server/
Perhaps you must specify the mongodb.conf file when loading your mongod instance. Like so:
mongod --fork --config /etc/mongodb.conf --logpath mongodblogs/mongodb.log --dbpath mongod
This is best variant in security aspect:
su <NOTROOTUSER>
mongod --dbpath data --bind_ip localhost
Create new user on you server then log in.
Root is not recommended for running mongo server.

How do you connect to a replicaset from a MongoDB shell?

If I'm writing an application which connects to mongodb then I can provide a seed list for a replicaset, and the driver will direct me to the master node, where I can run write commands.
How do I specify the seed list for a commandline mongo shell in order to conenct to a replicaset.
To connect to a replica set Primary use the mongo shell --host option:
mongo --host replicaSetName/host1[:porthost1],host2[:porthost1],host3[:porthost3],etc
For example:
$ mongo --host rs1/john.local:27019,john.local:27018
MongoDB shell version: v3.4.9
connecting to: mongodb://john.local:27019,john.local:27018/?replicaSet=rs1
2017-10-12T14:13:03.094+0000 I NETWORK [thread1] Starting new replica set monitor for rs1/john.local:27019,john.local:27018
2017-10-12T14:13:03.096+0000 I NETWORK [thread1] Successfully connected to john.local:27019 (1 connections now open to john.local:27019 with a 5 second timeout)
2017-10-12T14:13:03.096+0000 I NETWORK [thread1] Successfully connected to john.local:27018 (1 connections now open to john.local:27018 with a 5 second timeout)
rs1:PRIMARY> db
test
rs1:PRIMARY>
Note: From versions 3.4.2 to 3.4.10 there was a bug (SERVER-28072) which prevented specifying the db after when using --host or --port.
The answers above are for Mongo 3.2.
According to Mongo 3.4 documentation, the shell was changed a bit:
In 3.2:
mongo --host host1,host2,host3/myRS myDB
or,
mongo --host host1:27017,host2:27017,host3:27017/myRS myDB
In 3.4:
mongo "mongodb://host1,host2,host3/myDB?replicaSet=myRS"
or,
mongo "mongodb://host1:27017,host2:27017,host3:27017/myDB?replicaSet=myRS"
All you have to do is to use --host and give it one of your hosts in the replicaset but with the name of the replicaset as a prefix.
For example:
mongo --host my_mongo_server1
will connect to my_mongo_server1, it may just be yet another SECONDARY node.
But:
mongo --host my_repl_set_name/my_mongo_server1
will always connect to the PRIMARY node in the replica set, even if it's not my_mongo_server1.
Why? The answer is "replica set monitor".
In the example above, mongo shell would connect to the specified node, start a new replica set monitor for the replica set and will use the specified node just to seed it. From there, the monitor will figure out all nodes in the replica set and will switch the connection to the PRIMARY node.
Hope that helped.
You can use the "name/seed1,seed2,..." format:
> conn = new Mongo("myReplicaSet/A:27017,B:27017,C:27017")
> db = conn.getDB("test")
This should give you a connection to whichever node is currently primary and handle failover okay. You can specify one or more seeds and it'll find the rest.
Note that (AFAIK) the shell does not allow you to route reads to secondaries with a replica set connection.
To the best of my knowledge, the mongo command line client will not accept seeds to forward you to the master node, because you may often want to actually operate on the secondary node rather than being forwarded.
However, once connected to any node in the RS, you can discover the RS topology via rs.config() or db.isMaster(). You could then use this information to reconnect to the primary node. Depending on your shell, you might be able to use mongo --eval "db.isMaster()['primary']" to automatically connect to the master.
In the shell, you can first use:
mongo --nodb
to open a mongo session without connecting to mongo replicaset
Then, like kristina said, then you should be able to use
conn = new Mongo("myReplicaSet/A:27017,B:27017,C:27017")
to connect to a replicaset.
Or eventually put
conn = new Mongo("myReplicaSet/A:27017,B:27017,C:27017")
in your js file and
mongo --nodb yourcode.js
You can use the --host param to specify the replSet name and seed list, then mongo will automatically connect to the current primary host.
example:
mongo --host rs0/1.example.com:27017,2.example.com:27017,3.example.com:27017 [dbname]
Building on the answer by Chris Heald these two bash aliases let me connect to the master with one command (where db1.test.test is one member of the replica set, acme is the database name, mreppy is my account etc) It will fail of course if db1 is down, but it's still handy.
alias whichprimary='mongo db1.test.test/acme --username mreppy --password testtest --quiet --eval "db.isMaster()['"'primary'"']"'
alias connectprimary='mongo -u mreppy -p testtest `whichprimary`/acme'
The quoting in the eval alias is hard, I used How to escape single-quotes within single-quoted strings? for help :-)
I am using v3.4. Also new to mongodb stuff...
Although the help info from "man mongo" suggests to use "--host replicaSet/host:port,host:port" url, it does not work for me.
However, I can connect to my replicaSet according to official document, as below:
$ mongo "mongodb://c1m,c2m,c3m/?replicaSet=rs0"
MongoDB shell version v3.4.1
connecting to: mongodb://c1m,c2m,c3m/?replicaSet=rs0
2017-02-08T14:46:43.818+0800 I NETWORK [main] Starting new replica set monitor for rs0/c1m:27017,c2m:27017,c3m:27017
MongoDB server version: 3.4.1
Server has startup warnings:
2017-02-08T13:31:14.672+0800 I CONTROL [initandlisten]
2017-02-08T13:31:14.672+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-02-08T13:31:14.672+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2017-02-08T13:31:14.672+0800 I CONTROL [initandlisten]
rs0:PRIMARY>
So I guess the man page of my mongo is outdated (I am using CentOS 7.3).
mongodb://< dbuser >:< dbpassword >#example.com:< port >,example2.com:< port >/< dbname >?replicaSet=setname