Mongodb and mLab connection - New to coding - - mongodb

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 !

Related

Need help in connecting to mongo db through mongo-connector

I'm trying to connect my local mongo db instance to my QA mongodb through mongo-connector. Below is the command i have used to connect. But after hitting the command its thinking for few seconds and coming out. I'm not seeing any error. Am i making any mistake in connecting to target mongodb instance?
C:\Dev\mongodb\mongo-connector>mongo-connector -m localhost:27017 -t mongodb://username:password#dbhost:27017/dbname -d mongo_doc_manager --oplog-ts oplog.txt
Logging to mongo-connector.log.

Unable to connect to mongolab host

I am trying to connect to mongolab from terminal via below command
mongo ds061158.mongolab.com:61158/order_it -u <dbuser> -p <dbpassword>
I am getting the below error.
MongoDB shell version: 2.6.3
connecting to: ds061158.mongolab.com:61158/order_it
2014-07-09T13:52:44.890+0530 Error: couldn't connect to server ds061158.mongolab.com:61158 (23.22.170.205), connection attempt failed at src/mongo/shell/mongo.js:148
exception: connect failed
What has to be done in this case?
Thanks in Advance.
It looks like your network is blocking access to that port. I'd recommend contacting your network administrator or trying from a different network.
To test your network connectivity alone (no credentials necessary) you can run this command. This example was run from my unprivileged laptop just now and demonstrates a successful test.
% mongo ds061158.mongolab.com:61158
MongoDB shell version: 2.6.1
connecting to: ds061158.mongolab.com:61158/test
rs-ds061158:PRIMARY> db.runCommand({ping:1});
{ "ok" : 1 }
rs-ds061158:PRIMARY> exit
bye
Our full connectivity troubleshooting guide is here: http://docs.mongolab.com/connecting/#help
Also, feel free to contact us as support#mongolab.com if you'd like us to dig into the specifics of your server or code. We're always happy to help!
Regards,
Jared
I know this question is old, but in case someone still faces a similar issue, this is what helped me:
sudo rm /var/lib/mongodb/mongod.lock
sudo service mongodb restart
You should try to explicitly specify the port you want to use with the --port option:
mongo ds061158.mongolab.com/order_it --port 61158 -u <dbuser> -p <dbpassword>
From the mongo man pages:
--port <port>
Specifies the port where the mongod or mongos instance is listening. Unless specified mongo connects to mongod instances on port 27017, which is the default mongod port.

Cannot connect to mongod running in Ubuntu machine

I would like to connect with mongo from a cmd shell in windows to a mongod database running in a Ubuntu virtual machine.
mongo is running fine in the Ubuntu terminal and from a putty shell
When I use mongo from a windows cmd shell, I got this error:
mongo.exe --host 192.168.1.6 --port 27017
MongoDB shell version: 2.4.6
connecting to: 192.168.1.6:27017/test
Sat Feb 01 14:45:32.181 Error: couldn't connect to server 192.168.1.6:27017 at src/mongo/shell/mongo.js:147
exception: connect failed
What should I do to be able to connect?
My goal is to use MongoVue to connect to the mongod database in the Ubuntu machine (by the way, MongoVue is not connecting even using its SSH options).
I am trying to connect to the mongod instance of a meteorjs application.
The meteor application is up and running and I can connect to the mongod instance running on the Ubuntu machine at port 3002, both in the Ubuntu terminal and with a putty shell.
stefano#MeteorDeploy:~$ mongo --port 3002
MongoDB shell version: 2.0.4
connecting to: 127.0.0.1:3002/test
PRIMARY> show dbs
local 0.0625GB
meteor 0.0625GB
I would like to connect to the mongod instance using MongoVue as alternative of the putty shell.
I did as in the docs.mongodb.org/manual/tutorial/configure-linux-iptables-firewall/ but without success.
Meteor runs it's own instance of mongo per app. As you note since your edit, when you ssh into your VM you use --port 3002 to connect.
Now you could add that port to your mongo shell launch except for one problem
ps -ef | grep mongo
on your VM will show you the running instance of mongo along with it's startup options. By default this will be bound to 127.0.0.1 which is the loopback adaptor and not accessible outside of the VM.
So what you need to do is either change the startup options in your project, or use another instance on mongo installed on the local machine.
export MONGO_URL=mongodb://localhost:27017/your_db
By default mongod insllation on Ubuntu only listen to localhost, so you can't connect from Windows.
Edit /etc/mongodb.conf and change the bind_ip line (add your windows IP adress on the local network) so it will accept connection.
Be aware that by default mongod does not require authentification so you would maybe want to settup one.
Doc is here :
http://docs.mongodb.org/manual/reference/configuration-options/#bind_ip
For quick and dirty solution (not for Production):
Edit /etc/mongodb.conf and change the bind_ip to 0.0.0.0

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

How to identify the port of minimongo running inside meteor?

I am novice in meteor. I was developing an application which leverages the mongo db which comes with the meteor package. The meteor is running under the port 3000, but i want to know under what port the mini mongo runs. I am using a tool MongoVUE to view all the collections that is under mongo running in a port. I was able to view the mongo collections when i run the standalone mongo since i know the port for that but for this mini mongo running under meteor i am not able to know the port. Any help is appreciated.
Mingomongo runs in the browser, it's a client-side Javascript library.. it runs in memory, it doesn't have a port.. it enables you to query data published by the server in your client application, using MongoAPI style syntax..
to get the connection string of the Mongo instance running on the server (say, if you want to connect from a different client, or just see the port number) run
meteor mongo -U
Using ps auxww|grep mongo|grep meteor on my Fedora system I get:
jwulf 20635 0.3 2.6 150800 41336 pts/1 Sl+ 17:07 0:01 /usr/lib/meteor/mongodb/bin/mongod --bind_ip 127.0.0.1 --smallfiles --port 3002 --dbpath /home/jwulf/tools/leaderboard/.meteor/local/db
So it is running on port 3002 on my system. Starting mongo with --port 3002 allows me to connect to the Meteor mongo server and copy data into the database.
You can also connect to the local instance by running meteor mongo in your Meteor app directory.
The database itself is at <app-name>/.meteor/local/db on my Fedora system.
In meteor 0.8.3 it's using port 3001
so I run:
mongo localhost:3001
use use meteor
and I'm good to go...thanks to Sitapati Das for the command above...