compass refuse to connect to remoto mongodb - node.js

I am trying to connect with my MongoDB data base using compass or through my NodeJS app but it refuse to connect, It was working but suddenly it now not ,I am using windows 11
I have try to connect it from the PowerShell using this and it actually connected
mongo "mongodb+srv://<username>:<password>#cluster0.oeari.mongodb.net/dbname?authSource=admin"
I tried edit the security in config file in bin folder but nothing changed , I also have configure the IP address option in MongoDB to accept connect from anywhere but still the same
security:
authorization: enabled
net:
port: 27017
bindIp: 0.0.0.0

Related

Can not connect nodeJs version 19 to mongodb

I have a problem connecting my nodejs app to mongoDB driver. I have nodejs v19.6.0 working on my windows 10. I have tried connecting my app to mongo compass but no connection is working. I read on mongodb docs about the change in DNS resolution and the changes that happened since v18. I have tried to replace localhost with 127.0.0.1 in the url, and also set ipv6 = true in the mongodb config file but nothing is working. Kindly can someone help me resolve this. I'm frustrated since it's a week without finding a workable solution on the internet.screenshot of the error. nodemon is stack on starting my index.js file.
I tried changing the url ip address from var url = "mongodb://localhost:27017/mydb"; to var url = "mongodb://127.0.0.1:27017/mydb";
I also added the config file the net:
port: 27017
bindIp: 127.0.0.1
ipv6: true
bindIpAll: true

connection refused connecting to remote mongodb server

So we've accumulated enough applications in our network that use MongoDB to justify building a dedicated server specifically for MongoDB. Unfortunately, I'm pretty new to mongodb (coming from SQL/MySQL derivatives). I have followed several guides on installing and configuring mongodb for my environment. None are perfect, but I think I'm close... I've have managed to get to a point that I can connect to the db server from the local server using the following command:
mongo -u user 127.0.0.1/admin
However, I'm NOT able to connect to the server using this from either the local OR a remote computer using it's network address, IE:
mongo -u user 192.168.24.102/admin
I've tried both with authentication enabled and disabled, and I've tried setting the bindIP to 192.168.24.102 and 0.0.0.0 with no love. Thinking it was a Firewall issue, I disabled the firewall entirely... same. no love...
so what's the secret sauce? how do I connect to a MongoDB server remotely?
Some notes to know: This server is on a local network only. There will be some NAT shenanigans at some point directing public traffic to it from remote application servers, but only specific ports (we will NOT be using 27017 when that happens) and it will sit behind a pretty robust firewall appliance, so I'm not worried about securing the server as I about securing MongoDB itself.
This answer assume a setup where a Linux server is completely remote and has MongoDB already installed.
Steps:
1. Connect to your remote server over SSH.
ssh <userName>#<server-IP-address>
2. Start Mongo shell and add users to MongoDB.
Add the admin;
use admin
db.createUser(
{
user: "AdminSammy",
pwd: "AdminSammy'sSecurePassword",
roles: [
{"userAdminAnyDatabase",
"dbAdminAnyDatabase",
"readWriteAnyDatabase"}
]
}
)
Then add general user/users. Users are added to specific databases.
use some_db
db.createUser({
user: 'userName',
pwd: 'secretPassword',
roles: [{ role: 'readWrite', db:'some_db'}]
})
3. Edit your MongoDB config file, mongod.conf, that is found in etc directory.
sudo vim /etc/mongod.conf
Scroll down to the #security: section and add the following line. Make sure to un-comment the security: line.
security:
authorization: 'enabled'
After authorization has been enabled only those authenticated with password will access the database. In this case these are the ones added in step 2 above.
Note: Visual Studio code can also be used over SSH to edit the mongo.conf file.
4. Add remote server's IP address to mongod.conf file.
Look for the net line and add the IP address of the server that is hosting this MongoDB installation, example 178.45.55.88
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1, 178.45.55.88
5. Open port 27017 on your server instance.
This allows access to your MongoDB server from anywhere in the world to anyone who knows your remote server IP address. This is one reason to have authenticated users. More robust ways of handling security are really important! Consult MongoDB manual for that.
Check firewall status using ufw.
sudo ufw status
If its not active, activate it.
sudo ufw enable
Then,
sudo ufw allow 27017
Important: You also need to allow port 22 for your SSH communication with your remote server. Otherwise you will be locked out from your remote server. Assumption here is that SSH uses port 22 for communication, the default.
sudo ufw allow 22
6. Restart Mongo daemon (mongod)
sudo systemctl restart mongod
7. Connect to remote Mongo server using Mongo shell
You can now connect to the remote MongoDB server using the following command.
mongo -u <user-name> -p <user-password> <remote-server-IP-address>:<mongo-server-port>
You can also connect to the remote MongoDB server with authentication:
mongo -u <user-name> -p <user-password> <remote-server-IP-address>:<mongo-server-port> --authenticationDatabase <auth-db-name>
You can also connect to a specific remote MongoDB database with authentication:
mongo -u <user-name> -p <user-password> <remote-server-IP-address>:<mongo-server-port>/<db-name> --authenticationDatabase <auth-db-name>
At this moment you can read and write within the some_db database from your local computer without ssh.
Important: Put into consideration the standard security measures for any database. Local security practices should guide what to do at any of the above steps.

Connect and allow multiple ip's for mongodb accepting connections

I have a mongodb config file where bind ip is 127.0.0.1
Now mongodb is hosted on one instance and nodejs server is hosted on another instance which ip is (dummy ip) 184.54.125.132
On my mongodb Config file is
net:
port: 27017
bindIp: 0.0.0.0
By this my node server connect because it allows all (which is wrong)
How I add localhost ip of mongodb and my node server ip on bindIp so that mongodb server will listen only through node server and mongodb local.
I tried
bindIP: [127.0.0.1,184.54.125.132] //Not work
I tried
bindIp: 127.0.0.1,184.54.125.132 //Not work
I tried
bind_ip: [127.0.0.1,184.54.125.132] //Not work
I tried
bind_ip: 127.0.0.1,184.54.125.132 //Not work
Anyone suggest me where I am doing mistake. I also enabled authentication
It should be :
bindIp : 127.0.0.1,184.54.125.132
And dont forget to restart your mongod, after changing the config file, otherwise it wont work.
sudo service mongod restart
or
sudo systemctl mongod restart
.
Also, dont forget to open the port so that outside network can access that port(27017 or whatever port your mongodb is hosted on).
If you are using AWS EC2 to host your mongodb, you need to add that port to your inbound security rules, and that specific IP wherever you want to access it from

how to connect same mongodb (in compass using hostname: localhost and port : 27017) local in 2 different PC's [duplicate]

I follow this mongoose document enter link description here
mongoose.connect('mongodb://localhost/waterDB');
Using This, I can connect local machine waterDBmongoDB DataBase
My Personal Machine Local IP : 192.168.1.5
My Server Machine Local IP : 192.168.1.100
Both machine have waterDB DataBase . There is no username and password for both DB
I wanted to connect Server Machine waterDB Inside My Personal Machine.
According To This : mongoose.connect('mongodb://username:password#host:port/database?options...');
I try : mongoose.connect('mongodb://192.168.1.100:27017/waterDB');
But,
MongoError: failed to connect to server [192.168.1.100:27017] on first connect
at null.<anonymous> (/home/water/node_modules/mongodb-core/lib/topologies/server.js:313:35)
at emitOne (events.js:77:13)
at emit (events.js:169:7)
..........
Any solution for err ?
Thank (#_#)
It might be a problem with your MongoDB instance listening on localhost only.
You can change the bind address in MongoDB's configuration file. The config file may be located in /etc/mongodb.conf or /etc/mongod.conf. There are also 2 config file formats:
Old format (still supported):
bind_ip = 0.0.0.0
YAML (version 2.6+):
net:
bindIp: 0.0.0.0
After changing the config file you have to restart the MongoDB server.
Try without the Port no.
mongoose.connect('mongodb://192.168.1.100/waterDB');
this should work for you.
But make sure both are connected on the same network, if you are connected on other network than your server is, then it wont work
You will have to use SSH tunnel in this case. Refer to the following link which shows how you can create SSH tunnel.
Node.js SSH Tunneling to MongoDB using Mongoose
For me (using windows and mongo version 6, and remotely connecting from Mac), after changing config file to
net:
port: 27017
bindIp: "*" # OR 0.0.0.0
Then starting server with: mongod, it was still binding to 127.0.0.1 (localhost). I had to start the server with: mongod --bind_ip_all

MongoDB server not accessible in local Network despite binding ip

I am using MongoDB v3.6.3 on macOS, installed via home-brew. I have used almost every possible way to make my MongoDB server accessible on local network, but to no avail. I have:
Turned off firewall on both machines
Edited \etc\mongod.conf file to add bindIP.
used options-> bind_ip, bind_ip_all (127.0.0.1, 0.0.0.0, other machine's address)
As a matter of fact, also, my redis-server is not accessible on network.
I believe there is a common issue in between this.
I have tried the above mentioned solutions on macOS High Sierra, Ubuntu 16, Windows 10.
I have been facing the same error.
You could allow external access using the following parameters when starting your server:
mongod --bind_ip_all // Allow any ip
or
mongod --bind_ip <your_ip_here>
Open MongoDB config file.
Add the below command in that file(mongod.conf) and save it.
Mac config file location is : /usr/local/etc/mongod.conf
net:
bindIp: 0.0.0.0
Important Note: Once saved please restart the MongoDB services then only it will work.

Resources