MongoDB connecting to authenticationdatabase using shell command - mongodb

When i connect using from the OS shell,
mongo --port 27017 -u "testusr" -p "testpwd" --authenticationDatabase "testdb"
Instead of switching taking me to testdb, it always logs me into test database?
What I am missing here?
db.auth("testusr","testpwd") is working wihtout any authentication issues?
How do I redirect to the correct database?

The following works for me
mongo localhost:27017/testdb -u "user" -p "pass" --authenticationDatabase "testdb"
You can find help(i used in linux)
mongo --help
usage: mongo [options] [db address] [file names (ending in .js)]
db address can be:
foo foo database on local machine
192.169.0.5/foo foo database on 192.168.0.5 machine
192.169.0.5:9999/foo foo database on 192.168.0.5 machine on port 9999

Related

Connect the mongodb running on remote machine

MongoDB is running on remote machine which is accessible through my machine. I try to give the command in my machine
telnet "remote machine ip" 27017
It can able to connect.
But if I connect in the client by giving this command
mongo "machine ip" --port 27017 -u sample -p sample --authenticationDatabase admin
I am getting "Authentication failed" error.
But using that user name and password I can able to connect in remote machine. also in mongod.cfg file enabled the property "bindIpAll" as true.
is there any point I am missing here?
When you get an "Authentication failed" error, then you are already connected MongoDB.
You must put db address to the end or specify as option, i.e.
mongo --port 27017 -u sample -p sample --authenticationDatabase admin "machine ip"
or
mongo --host "machine ip" --port 27017 -u sample -p sample --authenticationDatabase admin
c:\>mongo -h
MongoDB shell version v4.2.0
usage: mongo [options] [db address] [file names (ending in .js)]
db address can be:
foo foo database on local machine
192.168.0.5/foo foo database on 192.168.0.5 machine
192.168.0.5:9999/foo foo database on 192.168.0.5 machine on port 9999
mongodb://192.168.0.5:9999/foo connection string URI can also be used

Dump remote database: Failed: error connecting to db server: no reachable servers

I have a database in www.myweb.io, which has SSL. The version of mongo is 2.6.12 in the server: the version of mongo in local is 3.4.1. I want to dump it into my local machine, modify it, and then restore back.
I tried
mongodump --host www.myweb.io --port 22 --username myname --password "mypassword"
and it gave me an error:
2017-11-20T20:57:07.775+0100 Failed: error connecting to db server: no reachable servers
Does anyone know what host and post I should set?
PS: in my localhost, I can use the following setting in Robo 3T to connect to the database:
1) under Connection, specify localhost:27017
2) under SSH, check Use SSH tunnel, specify xxx.xx.xx.xx:22 as SSH Address
In /etc/nginx/sites-enabled/myweb.io, there is listen 443 ssl.
you are not connecting via SSL or TLS to the database.
Use an ssh tunnel to establish a connection to your mongodb
ssh -N -L 27018:localhost:27017 user#www.myweb.io
you can specify -f to let ssh go in the background
Verify that the tunnel is working
Using mongodb
mongo --port 27018
Or telnet
telnet localhost 27018
dumping the database
mongodump --host 127.0.0.1 --port 27018 --db <dbName>
restore it somewhere, do your modifications
dump your local modifications
reapply the local modifications remote for example with mongorestore or mongoimport

mongodb database connection to my database

I connect to mongodb database like this
bin]$ ./mongo --host XXXXX-2.XXXXXXX-XXXX.1234.test.com
Connecting to test
mongo>use my_database
How do I connect to my_database directly instead of the default ?
Omit --host and append /<dbname> to the host name to provide a "db address":
bin]$ ./mongo XXXXX-2.XXXXXXX-XXXX.1234.test.com/my_database

Connect to a specific database by default in mongodb

I am running a mongodb on a linux box. So every time I connect to it from the console (typing mongo) I get something like this:
MongoDB shell version: 2.4.9
connecting to: test
And then I am doing use myDatabase (where myDatabase is 99% is the same). So basically I always do some unneeded type of work. Is there a way to configure mongo, so that it will connect to myDatabase by default?
Surprised that I don't find a duplicate of this. Okay, now we have content.
From the command line, just do this:
$ mongo myDatabase
This actually is covered in the documentation, albeit down the page somewhat. No direct link but search for <db address> and the same example is there.
Of course you could have done:
$ mongo --help
MongoDB shell version: 2.4.9
usage: mongo [options] [db address] [file names (ending in .js)]
db address can be:
foo foo database on local machine
192.169.0.5/foo foo database on 192.168.0.5 machine
192.169.0.5:9999/foo foo database on 192.168.0.5 machine on port 9999
Which shows the usage along with other options you can pass in.
Another thing, not quite a default connect but a shortcut is you can do this in the .mongorc.js file:
db=db.getSiblingDB("myDatabase")
Which assigns the variable db to that database so now:
db.collection.find()
Is acting on myDatabase.
As per latest mongodb drivers, we can provide default database name in connection string like this:
1. Connect using the mongoShell
mongo "mongodb+srv://sandbox.ununu.mongodb.net/mydatabase" --username user001
2. Connect using the drivers
mongodb+srv://user001:<password>#sandbox.ununu.mongodb.net/mydatabase?retryWrites=true&w=majority
Details of parametere:
Replace mydatabase with the name of the database that connections will use by default.
sandbox.ununu.mongodb.net is your cluster name
You will be prompted for the password for the Database User,user001.
user001 is username
For more details on passing ReplicaSet, other query string parameters, etc. refer mongodb official document. document link
Usually mongodb connects to default database test. It's very simple connect to any database, we just need to specify the new database name in the mongo shell
With authentication enabled and enter password on runtime (preferred mostly)
mongo --port 27017 -u USERNAME DATABASE_NAME --authenticationDatabase admin
With authentication enabled and password supplied on shell (not recommended)
mongo --port 27017 -u USERNAME -p PASSWORD DATABASE_NAME --authenticationDatabase admin
With authentication disabled
mongo --port 27017 -u USERNAME -p PASSWORD DATABASE_NAME
The above commands will directly connect to the database which we have specified.
Note: make sure the user has the necessary privilege to perform the operations
mongosh
use admin
db.createUser({ user: "user" , pwd: "1234", roles: [ "readWrite", "dbAdmin" ] })
use metadata
db.createCollection("product")
// mongodb://user:1234#localhost:27017/metadata

AWS RDS: How to Connect to Instance

I just set up an Amazon RDS instance. I have a separate application server and I am trying to figure out how to connect to the RDS instance from my EC2 application server. On the Instance page, I have
enbdpoint: mycompany.czdv3mj7ps25.us-west-2.rds.amazonaws.com:5432
I tried to login into to psql using that address but I got
$ psql -h mycompany.czdv3mj7ps25.us-west-2.rds.amazonaws.com:5432 -U myuser -d mydb
psql: could not translate host name "mycompany.czdv3mj7ps25.us-west-2.rds.amazonaws.com:5432" to address: Name or service not known
How do I connect to the instances database? I don't see any other ip addresses in the RDS console.
You have wrong syntax. The correct syntax is:
$ psql --host mycompany.czdv3mj7ps25.us-west-2.rds.amazonaws.com --port 5432 --username myuser --dbname mydb
You have specified port information wrong in you command. the port has to be specified using --port option and not hostname:port
This syntax worked for me in the psql command line:
\connect dbname username hostname port#
If the information is correct it will take a couple seconds to process then it will ask for your password
Also make sure the security group for your instance, the outbound/inbound is setup to allow access from your IP