Why cant I connect to replica set using mongo shell? - mongodb

I can connect to replica set using following command from mongo shell version 3.2.11. But the same does not works with mongo shell v3.4.1.
mongo --host \
"replicaSet1/10.10.10.15:27001,10.10.10.16:27002,10.10.10.17:27000" mydbname \
-u root -p root \
--authenticationDatabase admin
[main] Error: Failed to parse mongodb:// URL: mongodb://replicaSet1/10.10.10.15:27001,10.10.10.16:27002,10.10.10.17:27000/mydbname :
I have read here that replica set address format has not changed since v 3.4.1 release.
Why cant I connect to db ? What is the parse error, as per new format(if its there).

This is a known regression in MongoDB 3.4.0/3.4.1: SERVER-27289: mongo --host replSet/Host:Port no longer works. A fix has been committed for the upcoming MongoDB 3.4.2 release.
You can work around this in an affected 3.4.x mongo shell by using the standard MongoDB Connection String URI format instead:
mongo --host mongodb://10.10.10.15:27001,10.10.10.16:27002,10.10.10.17:27000/mydbname?replicaSet=replicaSet1
You can also use a standard MongoDB Connection String as a plain argument (i.e. without the --host parameter):
mongo mongodb://10.10.10.15:27001,10.10.10.16:27002,10.10.10.17:27000/mydbname?replicaSet=replicaSet1
I have read here that replica set address format has not changed since v 3.4.1 release.
Support for using the standard MongoDB Connecting String format in the --host parameter was added in MongoDB 3.4 in order to align the mongo shell connection string syntax with the format used by all official drivers.
This change is currently not noted in the MongoDB 3.4 manual, so I've raised DOCS-9808 to clarify.

Related

Mongoexport "ERROR PARSING URI" error - MONGODB

Facing below error while executing mongoexport command.Connection string below.
MongoDB shell version v4.2.0
OS - Mac OS Catalina
mongoexport --uri="mongodb+srv://m001-student:m001-****#sandbox.*****.mongodb.net/sample_supplies" --collection=sales --out=sales.json
Error:
2021-01-14T20:27:59.584+0000 error parsing command line options: error parsing uri: lookup _mongodb._tcp.sandbox.*****.mongodb.net on 192.#.#.#:#:# no such host <br/>
2021-01-14T20:27:59.585+0000 try 'mongoexport --help' for more information
I have provided ACCESS FROM ANYWHERE IP details in network access tab too. In that case, there should not be any issues while connecting. I have installed home-brew in my MacOS and installed mongodb database tools, since my terminal didn't recognise mongoexport command initially. Please let me know if I'm missing any detail in connection string.
delete "+srv" from uri...
If you want to take a backup from a remote database and restore it on your local machine.
Get your local machine IP by opening the terminal and running this command dig +short myip.opendns.com #resolver1.opendns.com
Add your machine IP address in the mongo atlas and provide access for this IP to limited hours only. In-network access tab.
Run this command mongodump --uri 'PUT_DB_HERE_URI' --out $(pwd) which takes a backup from your remote db you will find collection files with extension .bson.
To restore this data in your local database run this command mongorestore --host 127.0.0.1 --port 27017 -d database_name ./exported_folder_which_contains_.bson files/ .
As a reference, you can find the details here
I had the same issue with mongorestore and turns out it was just a mongorestore version problem, just updgraded it and everything works fine now

Connect to Mongo DB cluster

I can execute Mongo in Command but I can`t execute the command to connect to the mongoDB cluster and
with mongoshell
what`s the problem.....
Mongo atlas⇒Clusters connect ⇒connect with mongo shell⇒ mongo shell version is correct
⇒Run your connection string in your command line⇒
⇒ mongo "mongodb+srv://xxxx.xxxxxxx/<dbname>" --username xxxxx
Then I have error...
what's the <dbname>? do I have to make a folder? where?
FailedToParse: Database name cannot have reserved characters for mongodb:// URL: mongodb+srv://xxxxxxx/
try 'mongo --help' for more information

Format for MONGO_URL when connecting MongoDB Atlas to Galaxy

Revising this question following more attempts:
I'm deploying a Meteor app on Galaxy for the first time and cannot connect to MongoDB Atlas.
I can connect to MongoDb through Mongo Shell using:
mongo "mongodb://host1:27017,host2:27017,host3:27017/<db_name>?replicaSet=<replica_set_name>" --ssl --authenticationDatabase admin --username <username> --password <password>
I tried an equivalent for the Mongo URL in my settings.json:
mongodb://<username>:<password>#host1:27017,host2:27017,host3:27017/<db_name>?authSource=admin&replicaSet=<replace_set_name>
But when I deploy my app, I get:
Error: Could not locate any valid servers in initial seed list.
I've also tried using the connection string provided by Atlas:
mongodb+srv://<username>:<password>#<host>/<db_name>?retryWrites=true&w=majority
But this just returns:
Error: URL must be in the format mongodb://user:pass#host:port/dbname
I've tried every combination I can come up with but no luck.
RESOLVED, WITH HELP FROM MONGODB ATLAS SUPPORT:
"MONGO_URL": "mongodb://:#host1:27017,host2:27017,host3:27017/?ssl=true&replicaSet=&authSource=admin&retryWrites=true&w=majority",

Mongodb and mLab connection - New to coding -

I have been trying to set up my first database for my python program. I installed MongoDb as well as mLab. When I start mongod and mongo it seems to be connecting correctly to the test database.
However, when I insert my user and pass to start the mongo shell, it shows me this:
Vincents-MacBook-Pro:~ vincentfortin$ mongo ds029735.mlab.com:29735/vinc_personnal -u xxxxx -p xxxx
MongoDB shell version: 3.2.8
connecting to: ds029735.mlab.com:29735/vinc_personnal
rs-ds129735:PRIMARY>
While in my other terminal window with mongod, it shows this :
[initandlisten] waiting for connections on port 27017
Am I missing something ?
Thanks.
Also, if anyone know of any ressources for mongodb with python, it would help alot as most of them don't show the very basics !

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