How to connect to existing mongodb database - mongodb

I have mongodb server on my system. For testing purpose i created different database using different port. E.g.
dbname1 - SomepathIndrive -- Port
dbname2 - SomepathIndrive -- Port
dbname3 - SomepathIndrive -- Port
Now how do connect/start dbname2 or dbname 3 ?
When I use mongo command , it connects to default port and default database

As you said
mongo
command will connect to the server on the default port, if you want to connect to the server on different port use
mongo --port portnumber
When you are connected to mongo shell, command
show dbs
will result in list of all available databasses and to start using one of these use command
use databasename
Here's the link to the mongodb documentation about mongo Shell:
https://docs.mongodb.com/manual/mongo/

Related

how to connect local mongo DB instead of docker from sails JS application

My sails JS application looking for mongo db(Running on docker) , though local mongo db server is available.
If docker is up with mongo db server then application connecting to docker based mongo-DB
{ name: 'MongoError',
message: 'failed to connect to server [192.168.99.100:27017] on first connect'
}
In order to make the container connect to a local mongodb you need to get the ip of your host in linux you can type the following command on the host
ifconfig docker0 | awk '/inet / { print $2 }'
The result will be an ip that can be used to call it from any container followed by the service port which will be 27017 in your case. and make sure that the mongodb is not listening to 127.0.0.1 only.

Connect mongodb server via robomongo from another PC

I am using mongodb database for my meteor app. I want to access it from another pc. I have mounted my local as a virtual drive on other PC using ssh. Now I want to connect to mongodb via robomongo. I have given the address as 192.168.1.2:4001 (ip addr of local : port on which meteor is running +1). But its giving an error 'Unable to connect to mongodb'. How to proceed?
The other way around is to start your meteor on a regular mongo server with this command :
MONGO_URL=mongodb://localhost:27017/nameOfDatabase meteor
Be sure to have a running mongo on localhost and to change the nameOfDatabase.
Now it's just a regular mongoDB server to connect. Also you might need to add login and password to that mongo url and the debug parameter after meteor if you use packages like meteor toys.
Please check mongodb's config file /etc/mongod.conf and comment out bind ip
net:
#bindIp: 127.0.0.1
port: 4001
Restart mongodb service. This will allow mongodb to bind to ip's other than localhost.

JDBC connection to mongodb running on Meteor server on Cloud 9

I have a Meteor app running on Cloud 9 and I would like to connect to MongoDB from a window app that I am in trial period (DBSCHEMA: http://www.dbschema.com/).
Cloud 9 guys told me that I need my DB to listen to 0.0.0.0:8082.
In cloud9 I started my app using command: $ meteor --port $IP:$PORT.
I also created a db and user that has role "userAdmin" to this database.
In another terminal, at c9, "Meteor mongo" command give me connecting to: 127.0.0.1:8081/meteor
In DBSchema ping to the server is succesfull, but connection is refused.
So, I am trying to change to 0.0.0.0:8082 but I cant figure out how and not sure its going to work.
Any suggestions please?
use environment variable MONGO_URL=your-mongo-server-ip-or-hostname:8082 in meteor and start mongo with port 8082 on 0.0.0.0 with the config file
http://docs.mongodb.org/manual/reference/configuration-options/
Ideally, you should not expose you dB to the outside (so not bind to 0.0.0.0 which exposes the DB on all interfaces including the public IP).
If you want to access it with Robomongo, bind to localhost, and then you should create a ssh tunnel to the server from you local terminal with the command
ssh -L 8082:localhost:8082 your-host
then connect to it with robomongo at localhost:8082 –

How to connect mongodb clients to local Meteor MongoDB

How can I connect Robomongo (or any other mongodb client) to the mongodb instance that is created by my local Meteor application?
Ensure Meteor is running on localhost. Open a terminal window and run meteor command. It will start running on localhost:3000 if you have not changed to port.
While it is running, open a separate terminal window and run meteor mongo command. This will open up a MongoDB shell and tell you what port it is connecting to This is normally 3001 as of version 0.7.1.1 or 3002 if earlier. It will say something like 127.0.0.1:3001/meteor
Go to Robomongo (or your favorite mongodb client software) and create a new connection, making sure to change the connection address to localhost and the given the port number. No need to additionally define /meteor if your client does not insist on a default database.
Also as pointed out in https://stackoverflow.com/a/22023284/1064151 some drivers may need specific line endings, delimeters or other character flow. For example, ObjCMongoDB a C based driver wants the url to be 127.0.0.1:3001/ with that extra / at the end, or it won't work. So make sure you check the documentation for your driver/client.
Easiest way to get the current configuration details is to use the following command
meteor mongo -U
This will give you the connection string
From terminal run following command
meteor mongo -U
That will show you the local host IP address and in which port you application is running. Now run the Robomongo and configure as following two field as you got by running the previous command
Use SSH tunneling by the following command :
ssh -L 3001:localhost:3001 user-name#host
It forwards connections from your local port 3001 to localhost:3001 on your server. Now we can simply connect to our database.
Create a Robomongo connection on your localhost and hit Test (Out of two checks, Authentication may fail) :
I'm using ObjCMongoDB, a C based mongoDB driver. With the new update instead of using the previous 127.0.0.1:3002 to connect to my localhost running meteor's mongodb, I now need to use 127.0.0.1:3001/ with the collection name still being meteor.collection. The important change is the port from :3002 to :3001/. Remember the /, it is critical for the connection.
This worked for me,Before connecting make sure meteor is running.
I am using Robomongo to connect. Create new connection and add
Address as : localhost;
port as: 3001
I'm too using Robomongo and before the latest update V0.7.1,i used port 3002 to connect,as #Serkan Durusoy suggest's for the latest update it is working for 3001 port
#imal365 answer is perfect. Just to add my insight on it:
I realized that the default Meteor Mongo port number is the port number of the application with 1 added to it (as of version 0.7.1.1). In my case, I was running Meteor on port 1337 with the command meteor --port 1337 and my Meteor Mongo port was 1338.

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