Unable to connect to remote host from Mongo Shell - mongodb

i am trying to connect to a remove Mongo server from Mongo shell but when i run the mongo shell it keeps connecting to the localhost by default, if i enter exit to connect to another mongo server, the shell closes
i tried changing the mongod.cfg file and set the remote server ip and port but it did not work
i tried to connect to the remote server from the windows CMD but i keep getting this error SASL mechanism 'PLAIN' is not supported
how can i change the default connection of the mongo shell to connect to the remote mongo server?

mongod.cfg is for your local mongo server. Changes in this file have no effect on the client/shell.
The authenticationMechanism parameter in your command should agree with remote server configuration and authenticationDatabase should be the one where the user is registered on the remote server.
There are good chances authenticationMechanism is default SCRAM and the database is "admin".
Try mongo -u 'username' -p 'password' --authenticationDatabase admin host:port/db

Related

How to connect mongodb with url

I am new to mongodb.
I have db url, and I want to connect to that server from my ubuntu terminal and access data on terminal.
can anyone please help me how to connect?
Assuming you have the URI and the mongo shell installed, you can connect using the following command:
mongo <uri>
For example, if you want to log in as admin with the password mypassword through example.com on the port 27017, you can do:
mongo mongodb://admin:mypassword#example.com:27017/?authSource=admin
See:
how can I connect to a remote mongo server from Mac OS terminal
If mongodb version is 5.x.x
mongosh --host ip

Use Robo 3T to connect to remote MongoDB

I'm trying to use Robomongo (or Robo 3T) under Mac to control my mongodb in the remote Ubuntu & Nginx server.
Normally, I need to ssh xxx.xx.xx.xx in a terminal with a username and a password to connect to the server. in /etc/nginx/sites-enabled/myweb.io, there is listen 443 ssl.
In Robo 3T, I tried to set up the connection with Use SSH tunnel. I tried the port number 443 or 80. But it gave me an error: Error: Resource temporarily unavailable. Error when starting up SSH session: -13. (Error #35)
Does anyone know how to fix this?
The correct setting is
1) under SSH, check User SSH tunnel, use port 22
2) and under Connection, write 127.0.0.1:27017
I've done few configurations on my Ubuntu 18 Vagrant box in order to successfully connect MongoDB remotely using Robo 3T GUI. I've explained in the following steps.
On Ubuntu server, to open mongo shell run:
$ mongo
Inside mongo shell, type following command to create new a admin user.
> use admin;
> db.createUser({user:"admin", pwd:"password", roles:[{ role: "root", db: "admin" }]});
By default mongodb is configured to allow connections only from localhost(IP 127.0.0.1). We need to allow remote connections from any ip address. The following change should only be done in your development server. Open up etc/mongod.conf file and do the following change.
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0 #default value is 127.0.0.1
Also in the same mongod.conf file uncomment security option and add authorization option as shown below.
security:
authorization: enabled
Save and exit the mongod.conf file and restart mongodb server.
$ sudo servcie mongod restart
Download and install Robo 3T GUI tool.
On Robo 3T GUI, in the connection settings, you need to do few changes as shown on
below screen shots.
Enter mongodb admin database username and password which you have created earlier.
Here, I have entered my Ubuntu 18 Vagrant box ssh credentials.
Save the changes and press connect icon to see if the connection is working fine.
Connect to remote MongoDB using Roto3t
Firstly, we should check the standard URI connection scheme
for mongodb
mongodb://[username:password#]host1[:port1][,...hostN[:portN]]][/[database][?options]]
We make a SSH Connection to the remote MongoDB on Cloudfoundry
in my case
cf ssh -L port_forwarding:HOST_NAME:Port NAME_OF_APP
(1) In the Connection tab in Robo3T we choose
Type: direct connection
Name: choose_any_connection_name
Address: localhost and port is 'port_forwarding' we choose in the SSH connection we made.
(2) in the Authentication tab, we should add database, username, password, and Auth Machanism: SCRAM-SHA-1
(3) in the SSH Tab we will uncheck use SSH tunnel as we made it manually.
Now, if you click test the connection should be successfully done.
Note: If we use Studio 3T it will parse the URI and fill all these fields for us.

JDBC connection to mongodb running on Meteor server on Cloud 9

I have a Meteor app running on Cloud 9 and I would like to connect to MongoDB from a window app that I am in trial period (DBSCHEMA: http://www.dbschema.com/).
Cloud 9 guys told me that I need my DB to listen to 0.0.0.0:8082.
In cloud9 I started my app using command: $ meteor --port $IP:$PORT.
I also created a db and user that has role "userAdmin" to this database.
In another terminal, at c9, "Meteor mongo" command give me connecting to: 127.0.0.1:8081/meteor
In DBSchema ping to the server is succesfull, but connection is refused.
So, I am trying to change to 0.0.0.0:8082 but I cant figure out how and not sure its going to work.
Any suggestions please?
use environment variable MONGO_URL=your-mongo-server-ip-or-hostname:8082 in meteor and start mongo with port 8082 on 0.0.0.0 with the config file
http://docs.mongodb.org/manual/reference/configuration-options/
Ideally, you should not expose you dB to the outside (so not bind to 0.0.0.0 which exposes the DB on all interfaces including the public IP).
If you want to access it with Robomongo, bind to localhost, and then you should create a ssh tunnel to the server from you local terminal with the command
ssh -L 8082:localhost:8082 your-host
then connect to it with robomongo at localhost:8082 –

Unable to connect to remote Mongo Database (v3.0.3)

On the Server
I have the Mongod instance running on my server.
I created a User on admin, using which I created a user with readWrite role on the database that I wanted to access.
Updated the mongo /etc/mongod.conf file:
commented out the bind ip
uncommented auth=yes
Restarted Mongod using sudo service mongod restart
On the Client:
The db.auth(user,pass) command works fine on the remote server, but shows auth failure on the client (my home system).
I made sure that I was using the correct database with the use myDb command.
PS. I am able to connect to the mongo server using the mongo xx.xx.xx.xx:27017 command. But unable to Authenticate.
Please help. Mongo v3.0.3.

how to read data from remote mongo database server by using shell script via ssh connection

I would like to read data from remote mongo database server. I tried by below ways.
ssh username#hostname
"mongo databaseName --eval 'db.collection.find({\"params1\":\"2\",\"params2\":\"2\"})'"
or
ssh username#hostname
"mongo databaseName --eval 'printjson(db.collection.find({\"params1\":\"2\", \"params2\":\"2\"}))'"
I used both ways, but I can't get the correct result output from database server.
How should I do ?
The following format should work, note that I specifically included the full path of the mongo executable on the remote database server. If this command does not work, you should ensure that SSH is running on the server and check to see if there is any authentication settings on your database.
ssh username#hostname "/usr/local/mongodb/bin/mongo --norc --eval 'db.collection.find().forEach(printjson)'"