Connect the mongodb running on remote machine - mongodb

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

Related

How to fix : Error: Authentication failed

I'm trying to connect remotely on a mongodb server throw my local machine, but i'm having some issues.
On the remote server i modified the 'mongod.cfg' file and i changed the bindIp from 127.0.0.1 to 0.0.0.0 to allow access. In the same file i changed the security by adding Authentication: 'enabled'.
I created an admin user:
> use admin
> db.createUser({user: "root", pwd: "root", roles:["root"]})
I started mongodb with --auth flag
> mongod --auth --port 27017
Once the server was up, i connect to it as administrator
mongo 127.0.0.1:27017 -u "root" -p "root" --authenticationDatabase "admin"
Once i was connected, i created a normal user
> use base
> db.createUser({user: "base", pwd: "base", roles:["dbOwner"]})
Then i disconnected from mongo shell and reconnected with new user credentials
> mongo <127.0.0.1:27017>/base -u "base" -p "base"
It worked properly on the remote server.
On the local machine i tried the same command on my local machine and it failed
> mongo 127.0.0.1:27017/base -u "base" -p "base"
[js] Error: Authentication failed. :
connect#src/mongo/shell/mongo.js:344:17
#(connect):2:6
exception: connect failed
127.0.0.1 is making reference to the localhost of the host machine, you need to set the user in both machines if you are going to use one, if you want to use a db that share the data in the cloud you can use Mlab or if both ip are in the same network you can make reference from one to another by the ip address
mongo <ip_address>:27017/base -u "base" -p "base"

Unable to export MongoDB database from a remote server

mongoexport --host=hostname --port=27017 --db=dbname --collection=
collection_name --out=path/data.json
Here is the error which I'm getting
error connecting to db server: no reachable servers
you need to fill a remote hostname or a remote ip, username and password if required.
mongoexport --host hostname --port 27017 --username user --password "pass" --collection collectionName --out=path/data.json
try read the official mongodb docs

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 connecting to authenticationdatabase using shell command

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

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