How to connect to MongoDB on mLab through mongo shell? - mongodb

I'm trying to connect to my database cluster on mLab through mongo shell using the commands as mentioned by the service providers but I keep getting the same error mentioning missing ";" statement. I can access the same database through mongoose client without any problem.
Any clue where I am messing up:
mongo ds117956.mlab.com:17956/image_search -u <dbuser> -p <dbpassword>
2017-11-29T03:04:33.139+0530 E QUERY [thread1] SyntaxError: missing ; before statement #(shell):1:6

you need to run from mongodb/bin
and then enter the following command: mongo ds117956.mlab.com:17956/image_search -u -p
not from mongo itself
mongo ds117956.mlab.com:17956/image_search -u -p

Related

Unable to connect to MongoDB Atlas Cluster from Mongosh terminal

I am trying to connect to the Mongo Atlas Cluster through the Mongosh terminal using mongosh "mongodb+srv://cluster0.sdwcf.mongodb.net/myFirstDatabase" --username <username>. But I am unable to connect the error being Error: queryTxt ETIMEOUT cluster0.sdwcf.mongodb.net. I found the same issue at https://github.com/metabase/metabase/issues/9867 but issue has been closed without any solution.
One the other hand if I try to connect using the mongo 3.4 or earlier command mongo "mongodb://cluster0-shard-00-00.sdwcf.mongodb.net:27017,cluster0-shard-00-01.sdwcf.mongodb.net:27017,cluster0-shard-00-02.sdwcf.mongodb.net:27017/myFirstDatabase?replicaSet=atlas-10n7hz-shard-0" --ssl --authenticationDatabase admin --username <username> --password <password> it works just fine. But given that the Mongosh terminal has superseded the Mongo terminal, I would really like to know if there is any fix for this..

error in connection mongodb atlas to shell

mongo "mongodb+srv://sandbox-hoj54.mongodb.net/test" --authenticationDatabase admin --username m001-student --password m001-mongodb-basics
2020-06-19T14:18:02.553+0530 E QUERY [js] uncaught exception: SyntaxError: unexpected token: string literal :
#(shell):1:6
I think the problem is that you are running this command:
mongo "mongodb+srv://cluster0-jxrfu.mongodb.net/<dbname>" --username <password> on the MongoDB shell you downloaded in your <your mongo shell's download directory>/bin directory.
Instead, Open up a new command line and run the command. It worked for me.
There are two options to connect to db. Connect with the mongo shell or connect your application. It seems that you are trying to connect by the shell. You can see your connection string in the window:
MongoDB Atlas Panel > Clusters > 'Connect' button.
Of course you have to replace <dbname> and <password> as in the description.

Issues with mongoRestore [listCollections requires authentication]

I am trying to restore a MongoDB on an EC2 instance. I am currently running Mongo 4.0. I am restoring a .tgz, which I then unzip, and it contains a directory with all of my files. I previously used this command:
sudo mongorestore --db newDB mongoDump-2018-07-25-0200/viboDB/
Now that I am trying to update our database, I am getting the following error.
building a list of collections to restore from mongoDump-2018-07-25/0200 dir
Failed: viboBI2.Songs: error reading database: command listCollections requires authentication
I have logged into the mongo shell, and used db.auth() to authenticate as an admin. I have tried restarting mongo as well. Any help would be appreciated!
For restoring the Database you need to provide authentication.
mongorestore -u USERNAME -p PASSWORD --authenticationDatabase admin -d dbNAME PATH/TO/DIRECTORY
you can also provide host and port by adding -h and --port

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.

How do I create a new MongoDB from the command line with auth turned on?

For the purpose of wanting to create a MongoDB database in a Capistrano task, I need to figure out how to create a new database via the MongoDB shell when MongoDB is running with auth turned on.
With auth turned on the MongoDB shell can't do anything without first authenticating, but authenticating has to happen while using the admin database as far as I'm aware. So I've been connecting to it for starters like:
sudo -u mongodb mongo admin --eval "db.auth(username, password)"
And that authenticates me for further action but...at that point I need to create the database, and a user for it in the same shell session. I can't however:
sudo -u mongodb mongo admin --eval "db.auth(username, password);use new_database"
Because use database can't be used in eval. So I've tried instead:
sudo -u mongodb mongo admin --eval "db.auth(username, password);db = connect('localhost:27017/new_database')"
And that will actually get me an instance of the new database I want created but if I try to:
sudo -u mongodb mongo admin --eval "db.auth(username, password);connect('localhost:27017/new_database').addUser(new_user, new_password)"
I get an authentication error, so apparently using the connect() command forgets that I've authenticated before.
So this is where I'm stuck, trying to create a database in one line with MongoDB using auth and an admin user.
Any suggestions on how I can do this?
You can use db.getSiblingDB(name) to reference the new database and add a new account, eg:
mongo -u username -p password admin --eval "db.getSiblingDB('new_database').addUser('new_user', 'new_password');"
Now you should be able to authenticate from the command line using -u and -p:
mongo -u new_user -p new_password new_database
Beware of the fact that addUser function got deprecated since version 2.6.
Use db.createUser() and db.updateUser() instead of db.addUser() to add users to MongoDB.
Your code should be like below :
mongo -u username -p password admin --eval "db.getSiblingDB('new_database').createUser({ user : 'new_user', pwd : 'new_password', roles : [{ role: 'readWrite', db: 'new_database'}]});"