I have an issue with establishing a remote connection to MongoDB running on a VPS. I have followed instructions on editing the mongod.conf to bind my IP.
The network section looks like this:
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1,xx.xx.xxx.xxx
The latter IP is my local machine which I am trying to access MongoDB on using Compass.
I have also tried surrounding the list in []
, but it does not work. I am restarting the mongo service after each change like so:
sudo systemctl restart mongod
When I try to run mongo
after adding the new comma-separated IP to the bindIp
I receive the following error.
MongoDB shell version v4.4.9
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:374:17
@(connect):2:6
exception: connect failed
exiting with code 1
When I edit the mongo.conf
to remove the second IP, mongo
works and I can use the shell.
I have also created a new user in the MongoDB admin to use as credentials in Compass which I am using to try to connect in Compass.
This is what the user looks like in the admin system.users
collection.
{
"_id" : "admin.newAdmin",
"userId" : UUID("9b5c9a51-de6b-4e55-a2bc-3ae92d89993c"),
"user" : "newAdmin",
"db" : "admin",
"credentials" : { ... },
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
},
{
"role" : "readWriteAnyDatabase",
"db" : "admin"
}
]
}
The connection string in Compass:
mongodb://username:password@<vps-ip>:27017/?authSource=admin&readPreference=primary&ssl=false
I have seen that replacing bindIp
with 0.0.0.0
works, but I am not comfortable with that from a security point of view.
If anyone can help with a solution to securely and easily establishing a remote connection I would much appreciate it.