As per mongodb documentation, we can use the mongo command to access the database using below command –
Connect to mongodb database locally
$ mongo -u user1 -p password123 –authenticationDatabase admin
mongo -u user 127.0.0.1/admin
Connect to mongodb database Remotely
To connect to mongo database remotely we need below steps –
1. you should have your host ip name in /etc/mongod.conf under bind ip –
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1, 178.45.55.88
2. Open above port to have access from outside
– You can either try using firewall-cmd on redhat linux as follows
$ sudo firewall-cmd –zone=public –add-port=27017/tcp –permanent
$ sudo firewall-cmd –reload
You can test it using telnet host_ip port
3. restart mongod service
sudo systemctl restart mongod
4. Now connect using the string provided example –
mongo -u <user-name> -p <user-password> <remote-server-IP-address>:<mongo-server-port>
tags : Mongodb connect remotely, access mongo database locally or remotely, mongodb connection refused, mongodb, mongodb connection issue,