Connect to Mongo Atlas Secondary - mongodb-atlas

On Cloud Mongo (Mongo Atlas) Free tier, it has 3 members of servers. How can I connect to the Secondary host from Mongo shell? Their example only shows how to connect to Primary.
"members" : [
{
"_id" : 0,
"name" : "***-shard-00-00-***.mongodb.net:27017",
....
},
{
"_id" : 1,
"name" : "***-shard-00-01-***.mongodb.net:27017",
.....
},
{
"_id" : 2,
"name" : "***-shard-00-02-***.mongodb.net:27017",
.....
}
]

You need to use the --ssl flag and specify authSource.
Try:
mongo "mongodb://***-shard-00-02-***.mongodb.net:27017/?authSource=admin" --ssl
at the very minimum. Of course you can add options for username, password, database to connect, etc...
mongo "mongodb://<username>:<password>#***-shard-00-02-***.mongodb.net:27017/<database>?authSource=admin" --ssl
I hope this helps.

Related

how to output the result to a file in monogodb

I wan to list all database in Monogodb and output to a txt file, but it did not work.
mongo 127.0.0.1/test -eval 'var c= show databases;' >>db_list.txt
the error message is
MongoDB shell version: 2.6.12
connecting to: 127.0.0.1/test
2016-12-06T12:12:32.456-0700 SyntaxError: Unexpected identifier
anyone knows how to make this work. I appreciate any help.
To use eval and list databases directly on a shell, the following query should be helpful.
mongo test --eval "printjson(db.adminCommand('listDatabases'))"
MongoDB shell version: 3.2.10
connecting to: test
{
"databases" : [
{
"name" : "local",
"sizeOnDisk" : 73728,
"empty" : false
},
{
"name" : "m034",
"sizeOnDisk" : 11911168,
"empty" : false
},
{
"name" : "test",
"sizeOnDisk" : 536576,
"empty" : false
}
],
"totalSize" : 12521472,
"ok" : 1
}
This will list all the collection names in a particular DB.
mongo test --eval "printjson(db.getCollectionNames())"
MongoDB shell version: 3.2.10
connecting to: test
[
"aaa",
"areamodel",
"email",
"hex",
"key",
"mel",
"multi",
"ques",
"rich"
]
A sample execution for reference (screenshot)
Instead of test you can go simply,
mongo db_name query.js > out.json
here query.js contains any query like:
printjson( db.adminCommand('listDatabases') )

How to do custom mapping using mongo connector with elasticsearch

I wanna connect mongodb and elasticsearch. I used mongo connector to connect them. I followed instruction from below link to setup==>
http://vi3k6i5.blogspot.in/2014/12/using-elastic-search-with-mongodb.html
I am able to connect mongodb and elasticsearch. But by default mongo connector created indices in elasticsearch for all databases of mongodb.
I want to create only one index for my one database and I want to insert only selected field of documents. for example: in mongo shell==>
use hotels
db.restaurants.insert(
{
"address" : {
"street" : "2 Avenue",
"zipcode" : "10075",
"building" : "1480",
"coord" : [ -73.9557413, 40.7720266 ],
},
"borough" : "Manhattan",
"cuisine" : "Italian",
"grades" : [
{
"date" : ISODate("2014-10-01T00:00:00Z"),
"grade" : "A",
"score" : 11
},
{
"date" : ISODate("2014-01-16T00:00:00Z"),
"grade" : "B",
"score" : 17
}
],
"name" : "Vella",
"restaurant_id" : "41704620"
}
)
This will create database hotels and collection restaurants. Now I want to create index and I want to put only address field in elasticsearch for that index.
Below are the steps what I tried but thats not working :
First I start mongo connector like below :
Imomadmins-MacBook-Pro:~ jayant$ mongo-connector -m localhost:27017 -t localhost:9200 -d elastic_doc_manager --oplog-ts oplogstatus.txt
Logging to mongo-connector.log.
Then from new shell tab, I made command like :
curl -XPUT 'http://localhost:9200/hotels.restaurants/'
curl -XPUT "http://localhost:9200/hotels.restaurants/string/_mapping" - d'{
"string": {
"properties" : {
"address" : {"type" : "string"}
}
}
}'
But only index is created in elasticsearch named as hotels.restaurants. I can't see any document for index hotels.restaurants.
Please suggest me how to add document for hotels.restaurants
Well I got an answer to my question, while starting mongo connector we can specify collection name and the list of fields we are interested in. Please check below command ==>
$ mongo-connector -m localhost:27017 -t localhost:9200 -d elastic_doc_manager --oplog-ts oplogstatus.txt --namespace-set hotels.restaurants --fields address,grades,name

exception: hosts cannot switch between localhost and hostname

I created a replication set.
I added localhost in the set in the beginning, but when I try to edit the member with the actual hostname. I get error "exception: hosts cannot switch between localhost and hostname"
I need to get rid of localhost:27017 because, otherwise, it doesn't let me enter any other member as hostname (i.e. non-localhost address)
my-rs0:PRIMARY> cfg=rs.conf();
{
"_id" : "my-rs0",
"version" : 1,
"members" : [
{
"_id" : 0,
"host" : "localhost:27017"
}
]
}
my-rs0:PRIMARY> cfg.members[0].host="my-server04:27017"
my-rs0:PRIMARY> cfg
{
"_id" : "my-rs0",
"version" : 1,
"members" : [
{
"_id" : 0,
"host" : "my-server04:27017"
}
]
}
using rs.reconfig(cfg);
my-rs0:PRIMARY> rs.reconfig(cfg);
{
"errmsg" : "exception: hosts cannot switch between localhost and hostname",
"code" : 13645,
"ok" : 0
}
no luck with rs.add("my-server04:27017") or rs.remove("localhost:27017") as well.
my-rs0:PRIMARY> rs.add("my-server04:27017");
{
"errmsg" : "exception: can't use localhost in repl set member names except when using it for all members",
"code" : 13393,
"ok" : 0
}
I have tried all the reconfiguration methods mentioned here Replica Set Reconfig steps
But, none fixing above issue. Already spent hours, I am really frustrated.
I had the same problem and I fixed it without dropping any database. Just edited the host field of the member in the local.system.replset collection to match the local ip and then restarted mongod. Everything worked perfect.
It looks like you'll need to scrap your replicaset and start over.
I believe that when you initiated your Replica Set, you explicitly passed it a config document that references your MongoDB instance using localhost.
As I was investigating this, I brought up a replica set. When I initiated the replica set using rs.initiate() (without passing a config document) it used host name by default.
rs.initiate()
rs.conf()
{
"_id" : "stack1",
"version" : 1,
"members" : [
{
"_id" : 0,
"host" : "MY-HOSTNAME:28001"
}
]
}
This post describes the need to complete clear out your database files to create a fresh replica set.
Once I did this, I initiated a new replica set in the by passing a configuration document:
cfg = {
"_id" : "stack1",
"version" : 1,
"members" : [
{
"_id" : 0,
"host" : "localhost:28001"
}
]
}
rs.initiate(cfg)
rs.conf()
{
"_id" : "stack1",
"version" : 1,
"members" : [
{
"_id" : 0,
"host" : "localhost:28001"
}
]
}
Long story short, you'll need to delete all of the files in your --dbpath directory and re-create the replica set, without explicitly specifying "localhost" as your hostname.
I did according to the docs:
Restarted MongDB on another port (e.g. 37107) to prevent user connections to it.
Then started a shell on it:
$ mongo --port 37017
Then updated the configuration:
use local
cfg = db.system.replset.findOne( { "_id": "my-rs0" } )
cfg.members[0].host = "my-server04:27017"
db.system.replset.update( { "_id": "my-rs0" } , cfg )
Then restarted MongoDB on the original port.

Replication set for MongoDB

I am trying to follow the instructions for setting up a replication set for a MongoDB database with Azure. The original instructions are at http://www.mongodb.org/display/DOCS/MongoDB+on+Azure+VM+-+Linux+Tutorial. I connect the script interpreter with 'mongo --host bsicentos.cloudapp.net --port 27018' (somehting the instructions didn't tell me). The final step instructions me to enter:
> conf = {
id = “mongors”,
members : [
\{id:0, host:”mongodbrs.cloudapp.net:27018\},
\{id:0, host:”mongodbrs.cloudapp.net:27019\},
\{id:0, host:”mongodbrs.cloudapp.net:27020\}]}
>rs.initiate(conf)
If I don't type this exactly as specified and I modify it slightly to fit my host (missing closing quotes, not escapes, and id numbers all zero) I finally get an accepted command:
mongors:PRIMARY> conf = {
... _id:"mongors",
... members:[
... {_id:0,host:"bsicentos.cloudapp.net:27018"},
... {_id:1,host:"bsicentos.cloudapp.net:27019"},
... {_id:2,host:"bsicentos.cloudapp.net:27020"}]}
{
"_id" : "mongors",
"members" : [
{
"_id" : 0,
"host" : "bsicentos.cloudapp.net:27018"
},
{
"_id" : 1,
"host" : "bsicentos.cloudapp.net:27019"
},
{
"_id" : 2,
"host" : "bsicentos.cloudapp.net:27020"
}
]
}
But I get and error:
mongors:PRIMARY> rs.initiate(conf)
{
"info" : "try querying local.system.replset to see current configuration",
"errmsg" : "already initialized",
"ok" : 0
}
Ideas?

How to Verify Sharding?

I am trying to shard MongoDB. I am done with Sharding configuration, but I am not sure how to verify if sharding is functional.
How do i check whether my data is get sharded? Is there a query to verify/validate the shards?
You can also execute a simple command on your mongos router :
> use admin
> db.printShardingStatus();
which should output informations about your shards, your sharded dbs and your sharded collection as mentioned in the mongodb documentation
sharding version: { "_id" : 1, "version" : 2 }
shards:
{ "_id" : ObjectId("4bd9ae3e0a2e26420e556876"), "host" : "localhost:30001" }
{ "_id" : ObjectId("4bd9ae420a2e26420e556877"), "host" : "localhost:30002" }
{ "_id" : ObjectId("4bd9ae460a2e26420e556878"), "host" : "localhost:30003" }
databases:
{ "name" : "admin", "partitioned" : false,
"primary" : "localhost:20001",
"_id" : ObjectId("4bd9add2c0302e394c6844b6") }
my chunks
{ "name" : "foo", "partitioned" : true,
"primary" : "localhost:30002",
"sharded" : { "foo.foo" : { "key" : { "_id" : 1 }, "unique" : false } },
"_id" : ObjectId("4bd9ae60c0302e394c6844b7") }
my chunks
foo.foo { "_id" : { $minKey : 1 } } -->> { "_id" : { $maxKey : 1 } }
on : localhost:30002 { "t" : 1272557259000, "i" : 1 }
MongoDB has detailed documentation on Sharding here ...
http://www.mongodb.org/display/DOCS/Sharding+Introduction
To anwser you question (I think), see the portion on the config Servers ...
Each config server has a complete copy
of all chunk information. A two-phase
commit is used to ensure the
consistency of the configuration data
among the config servers.
Basically, it is the config server's job to make sure everything get sharded ... correctly.
Also, there are system collections you can query ...
db.runCommand( { listshards : 1 } );
Lots of help in the prez below too ...
http://www.slideshare.net/mongodb/mongodb-sharding-internals
http://www.10gen.com/video/mongosv2010/sharding
If you just want to check whether you are conencted to a sharded cluster or not:
db.isMaster() can be used to detect that you are connected to a sharding router (mongos).
If db.isMaster().msg is "isdbgrid", you are connected to a sharded instance.
db.isMaster() can be run without authentication.
For checking the details of the shards, sh.status() also works, which has the same output as db.printShardingStatus(); works.