How do I set the default connection URL for mongo CLI? - mongodb

I cannot find anywhere to set the default connection URL for the mongo CLI. I want to be able to specify a particular username/password/hostname/database, which can all be specified as a URL. However, I want to just be able to type mongo instead of mongo "mongodb://…" in the shell.
I also don’t want the password to show up in /proc/«PID»/cmdline, but having it in a dotfile in my home directory is perfectly fine with me.
It doesn’t appear that ~/.mongorc.js allows specifying the default connection string. But I would expect such an option to be available because the mysql CLI supports ~/.my.cnf which allows you to specify username/password/hostname/database. So, where is this for mongodb?
EDIT: The solution has to work even if the mongodb at localhost is fully secured (no insecure local test database should be required—I thought this went without saying).

You can run mongo shell commands in ~/.mongorc.js. So you can just run the connect method in your ~/.mongorc.js:
db = connect("mongodb://username:password#host:27017/myDatabase")
When you run mongo it will first connect to localhost, then connect to the database passed to the connect method.
A MongoDB server must be running on localhost, otherwise the mongo shell will error and exit before trying to connect to the other DB.
Run mongo --nodb to get around needing to connect on localhost before connecting to whatever is defined in ~/.mongorc.js.

To directly launch mongo against a particular hostname/database/username/password without prompting for a password, without passing the password on the CLI, and without requiring an insecure database at mongodb://localhost/test, you can use this technique. Write a script file which does the connection for you and then launch mongo with the appropriate arguments. For example:
~/.mongo-do-auth.js
// https://docs.mongodb.com/manual/reference/program/mongo/#cmdoption-nodb
// https://docs.mongodb.com/manual/tutorial/write-scripts-for-the-mongo-shell/#mongo-shell-new-connections
//
// Run this with:
//
// mongo --nodb --shell ~/.mongo-do-auth.js
db = connect('localhost/admin', 'root', 'asdf123');
And then, simply launch it from the CLI to get an interactive session:
sam ~ # mongo --nodb --shell ~/.mongo-do-auth.js
MongoDB shell version: 2.6.12
type "help" for help
connecting to: localhost/admin
> quit()
Or, to run a script noninteractively, simply remove --shell and append your script. Consider a script:
s2.js:
print('connected to database ' + db);
To run:
sam ~ # mongo --nodb ~/.mongo-do-auth.js ~/s2.js
MongoDB shell version: 2.6.12
loading file: /root/.mongo-do-auth.js
connecting to: localhost/admin
loading file: /root/s2.js
connected to database admin
However, this requires a lot of work and invoking mongo with arguments. It appears that this is a limitation of mongo because no one has suggested any alternative which works as smoothly as the mysql CLI client’s ~/.my.cnf. This means that all shell scripts which directly invoke mongo will need to allow the the user to pass through --nodb and a path to a connection script instead of being able to rely on implicit per-user configuration.

Related

How to check meteor production database [duplicate]

It seems the answer in this thread (Accessing Meteor production database) does not work anymore when you want to access a meteor production database in 2016. I want to access a meteor production database blah.meteor.com using
meteor mongo blah.meteor.com
instead what I get is:
connecting to: sg-mother1-6243.servers.mongodirector.com:27017/blah_meteor_com
2016-01-18T15:21:49.884+0200 Error: 18 { ok: 0.0, errmsg: "auth failed", code: 18 } at src/mongo/shell/db.js:1210
exception: login failed
Then I tried
meteor mongo --url blah.meteor.com
I get username cursor. I enter my meteor site username and press enter and then get password cursor. I enter password for the above username and press enter. I get presented with the following url:
mongodb://client-2ee8c14d:c1546ca8-4e7e-5883-0214-150b309fb4fb#SG-mother1-6242.servers.mongodirector.com:27017/blah_meteor_com
Then every time I re-enter
meteor mongo --url blah.meteor.com
I am assumed to have logged on already, and I just get presented with a similar url to the one I was presented with just above.
I read the "meteor mongo command" documentation by entering:
meteor mongo --help
In the documentation I read the following line:
Instead of opening a shell, specifying --url (-U) will return a URL
suitable for an external program to connect to the database. For remote
databases on deployed applications, the URL is valid for one minute.
For the meaning, I went back to the thread (stackoverflow.com/questions/11801278/accessing-meteor-production-database) I mentioned in the beggining and read:
"So what it's saying is, the url provided by running the command with the --url option is for connecting to the database by some external application, i.e. other than meteor."
I don't know what other application can help me connect to meteor production database other than what I used to do in 2015, which is:
meteor mongo blah.meteor.com
I read somewhere that I can use the mongo shell intead but I don't know how to open it and I don't know the mongo installation directory when it is installed with meteor. I am using linux (fedora) OS.
How do I access meteor production database in 2016? Are there upgrades that happened that make me not to be able to access meteor production database as easily as I did in 2015?
You are trying to connect to a database version 3.0 while your meteor mongo command still use the version 2.6.7 of mongo
Try this workaround :
Install Mongo version (3.x) directly on your machine.
Then run this command (should work on osx, linux and windows when sed is installed):
mongo `meteor mongo --url XXX.meteor.com | sed 's/mongodb:\/\//-u /' | sed 's/:/ -p /' | sed 's/#/ /'`
Source: https://forums.meteor.com/t/meteor-mongo-xxx-meteor-com-giving-exception-login-failed-workaround/15289
Since Meteor stopped supporting the use of .meteor domains and every developer needs to get his hosting by himself, I found a way accessing the remote's database by using mup or mupx. I wrote it in this post: https://stackoverflow.com/a/37439315/2908071
I hope this will help future people.

Unable to get run js from command to query mongodb

Hi I am trying to run a simple JavaScript program to retrieve data from mongodb. collection named news.
var doc=db.news.findone();
printjson(doc);
I have mongodb running in my machine. when I try to run it from my command prompt, I am getting the below result.
MongoDB shell version:2.4.15-pre-
connecting to: test
I have no idea why it is connecting to test. Someone please help.
I assume, the collection, "news" resides on some mongo logical database, which need to be specified in script. For exmaple
db = db.getSiblingDB('db_name')
doc = db.news.findOne()
printjson(doc)
Then evaluate the script using
mongo --host ${host} --port ${port} script.js
If you don't specify a database in your connection string like for example,
//Connect to foo
${host}:${port}/foo
mongo will always try to connect to "test" database.

how can I connect to a remote mongo server from Mac OS terminal

I would like to drop into the mongo shell in the terminal on my MacBook. However, I'm interested in connecting to a Mongo instance that is running in the cloud (compose.io instance via Heroku addon). I have the name, password, host, port, and database name from the MongoDB URI:
mongodb://username:password#somewhere.mongolayer.com:10011/my_database
I have installed mongodb on my MacBook using Homebrew not because I want Mongo running on my Mac, but just to get access to the mongo shell program in order to connect to this remote database.
However, I can't find the right command to get me the full shell access I would like. Using instructions found here http://docs.mongodb.org/manual/reference/program/mongo/ (search for "remote") I am able to get what looks like a connection, but without giving my username or password I am not fully connected. Running db.auth(username, password) returns 1 (as opposed to "auth fails" when I provide incorrect username and password), but I continue to get an "unauthorized" error message when issuing the show dbs command.
You are probably connecting fine but don't have sufficient privileges to run show dbs.
You don't need to run the db.auth if you pass the auth in the command line:
mongo somewhere.mongolayer.com:10011/my_database -u username -p password
Once you connect are you able to see collections?
> show collections
If so all is well and you just don't have admin privileges to the database and can't run the show dbs
With Mongo 3.2 and higher just use your connection string as is:
mongo mongodb://username:password#somewhere.mongolayer.com:10011/my_database
Another way to do this is:
mongo mongodb://mongoDbIPorDomain:port

can't make basic mongo shell script with authentication

I have a really complicated issue that i think i can solve by writing a mongo shell script but i can't even manage to make a simple connection. I have a local mongo database which is requires a username/password that i normally access like this:
mongo admin -u <username> -p
at which point I enter the password and hooray! i have a shell. but that won't work for my issue. As a test, I created a file called test.js and all it has in it is this:
var conn = new Mongo()
db = conn.getDB("test");
db.cust.find();
I then run the script from the command line like so:
mongo test.js
at which point i get this:
MongoDB shell version: 2.4.10
connecting to: test
Why am i getting no results?
I finally made this work. This is how i ended up doing it:
First I made a file called test.js with the following in it:
db = connect("localhost:27017/admin");
db.auth('username','password');
db = db.getSiblingDB('test');
var cursor = db.cust.find();
while (cursor.hasNext()) {
printjson(cursor.next());
}
I then ran this command from the command line:
mongo test.js
I also want to point out a few things that i learned while trying to do this to any other developer who is having issues.
1) if you add a new database, and your are running mongo with authentication you either need to log into the authentication database first and then switch to the desired database (as my example shows) or you need to add a user/password to the desired database (as i probably should have done in the first place)
2) When running a javascript file via mongo, don't expect to use the same "javascript" functions that you are used to. I just learned a hard lesson that not all javascript is the same. for example, you can not use Console.log() in a javascript file that is run via mongo because console.log is not actually core javascript but rather a function specific to browser and node implementations.

Cannot use commands write mode error, Degrading to compatibility mode

I just play with mongo shell and came across with Cannot use commands write mode, degrading to compatibility mode.
I connected to remote mongo server (mongolab) and tried to insert new record to collection by my simple script:
// script.js
db = connect(host + ":" + port +"/" + dbName);
db.auth(username, password);
db.test2.insert({ item: "card", qty: 15 });
I run script by mongo script.js and got:
MongoDB shell version: 2.6.3
connecting to: test
connecting to: my.mongolab.com:port/DBname
Cannot use commands write mode, degrading to compatibility mode
What is wrong? Additionally when I executed similar query after connected via mongo my.mongolab.com:port/DBname -u <dbuser> -p <dbpassword> everything is OK.
You are using a shell that's newer than the server it's communicating with.
You can check the server version from the shell via db.version() - to check shell version you use version()
Starting with 2.6 the mongod server started using new write commands which are different than the previously use insert/update/remove op code (this is all described in the MongoDB Wire Protocol).
This is a harmless "informational" warning. It's a good idea to use the same version shell as the server to avoid wondering about such things though.