MongoDB Configuration File not working - node.js

I am on Windows 10 environment.
By default, My mongod.cfg file has a content which is as follows i.e
# mongod.conf
systemLog:
destination: file
path: c:\data\log\mongod.log
storage:
dbPath: c:\data\db
By this when I will start MongoDB through CMD command i.e
net start mongodb
It will be running.
Now I have to use replica set and for that, I have to make some changes to configuration file i.e I have to add some piece of code for accessing the Replication i.e
# mongod.conf
systemLog:
destination: file
path: c:\data\log\mongod.log
storage:
dbPath: c:\data\db
net:
bindIp: 0.0.0.0
port: 27017
replication:
replSetName: test
after that when I start the service through command net start MongoDB it will return the error i.e
The service is not responding to the control function.
More help is available by typing NET HELPMSG 2186.
I am not sure what is this? If anyone has any Idea about that? How can I fix this? Any Help is appreciated.

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

MongoDB seeding and replication

I have this small project i am working on it but i got stuck with mongodb replicaset and reverse proxy. I explain myself better.
I have a Web application running one instance(just a simple web page). And this application by default connects to port 3000. I used nginx reverse proxy to use port 80 instead of 3000 (and this works)
This application is a nodejs app, and in its configuration files, it should connect to mongodb on port on
mongodb://10.0.1.100:27017/posts (value exported by code into my instance)
to display some random posts.
On my mongod.conf I bind my IP to 0.0.0.0 and set port to 27017
but when i log on my app instance and seed my mongoDB (it get seeded succesfully) but when i run
npm start
I got an error that the port 3000 is already used by another service.
So, my point is, i want to connect to my /posts from my web page, without having to type :/posts
this is my mongod.conf:
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: /var/lib/mongodb
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# network interfaces
net:
port: <%= #port %>
bindIp: <%= #bind_ip %>
#processManagement:
#security:
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options:
#auditLog:
#snmp:
and in the attribute i set:
default['mongodb']['port'] = 27017
default['mongodb']['bind_ip'] = '0.0.0.0'
Question Bonus:
mongodb://10.0.1.100:27017/posts
This value has been exported in the envroment variables, but as i need to set a replication over 2 more mongodb, i tried to insert in line separated but commas, but i get an error. is there a way how to export multiple env value under one variable name?
I hope i explained myself clearly and thank you in advance guys.

Replication failed to connect to server [IP:27017] on first connect [MongoError: getaddrinfo ENOTFOUND

I have setup Replication and trying to connect
URI:
mongodb://[userName:password]#IP1:27017,
[userName:password]#IP2:27017/dbName?
authSource=admin&w=1&replicaSet=replicaqa
but I am getting below error:
{ MongoError: failed to connect to server [host_name_ip1:27017] on first connect
[MongoError: getaddrinfo ENOTFOUND [host_name_ArbiterIP] [host_name_ArbiterIP]:27017]
When I am individually trying to connect [IP1:27017] without replication it works.
Here is my Mongod.conf
systemLog:
destination: file
logAppend: true
path: /var/mongodb/mongod.log
# Where and how to store data.
storage:
dbPath: /data/var/lib/mongo
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0
security:
authorization: "disabled"
#operationProfiling:
replication:
replSetName: "replicaqa"
Am I missing something while configuring replication?
I can see mongod instance is started with replication where I can see (Primary>, Secondary> and Arbitary> And each instance is on remote location.
Found mistake,
When we configure replication with rs.conf() we give domain name and update DNS in etc/hosts file.
In my case, I have updated it in mongo instance(etc/hosts) but not updated where my server is hosted (server is hosted on another instance).
Updated hosts file where Node service is running and now it's working for me.
when I connect without replication I was able to connect, but the issue is when I am trying to connect with Replication.
(No matter even if I have given IP address in my DB connection string URL.
like
mongodb://[userName:password]#IP1:27017,[userName:password]#IP2:27017/dbName?
authSource=admin&w=1&replicaSet=replicaqa
It will try to resolve the domain name. Because we have configured in rs.conf(), that's what happened with me )
Path of hosts file in windows
C:\Windows\System32\drivers\etc\hosts file
A path for hosts file in Linux/ CentOS
/etc/hosts
DNS
10.XX.XX.XX viavi.local
10.XX.XX.XXX domain_name_configured_while_replication

How to connect via mongoose and a ssh tunnel

I have setup my mongod.conf as follows so it only allows localhost connection.
storage:
dbPath: /var/lib/mongodb
journal:
enabled: true
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
net:
port: 27017
bindIp: 127.0.0.1
I then want my site to ssh into the mongodb so the port has to be converted to localhost.
However how can I integrate this with mongoose's connect function?
mongoose.connect(configDB.url, function(err){
if (err){
console.log('Error connecting to mongodb: ' + err)
}
});
I have found the following command but I am not sure if this is what I need:
ssh -L 4321:localhost:27017 -i ~/.ssh/ssh_key user#ip-adress
This should ssh me via port 4321 to the localhost right? So I think I need something like this in the nodejs mongoose's connect function. I've tried to read up on this on the mongodb security tutorials but I cannot link their instructions to nodejs at all. Anyone who has experience with this?
You're nearly there. Set up the tunnel independent of node:
ssh -Nf -p [db_server_ssh_port] [mongo_user]#[mongo_domain] -L \
[local_db_port]:localhost:[remote_db_port]
And then within node, connect to mongo using [local_db_port]:
mongoose.connect(
"mongodb://localhost:[local_db_port]/[db_name]",
{"pass":"[db_pwd]"}
)
All the traffic sent to [local_db_port] on the web server will be sent through the tunnel to port [remote_db_port] on [mongo_domain]. The following post gives more info. It's connecting to a MySQL database, but the principle is the same.
Connect to MySQL using SSH Tunneling in node-mysql
Set up the tunnel independent of node:
ssh -L [your given port]:localhost:27017 [username of ssh]#[ip address of ssh matchine] -f -N
after that you have include your given port for mongo database.
In the nodejs you have to setup for mongoose connection like this
'mongodb://localhost:[your given port number]/[database name]'
enjoy it

Connection attempt failed when connecting to MongoDB deployment from mongo shell

first question and complete beginner so apologies in advanced for any silly mistakes.
I have created a server on Amazon Web Services and then linked that through the MongoDB Cloud Manager where I made a replica set.
I have been following the tutorial on the mongoDB cloud documentation but have become stuck on the final part - "Connect to a MongoDB Process".
It says "Cloud Manager provides a mongo shell command that you can use to connect to the MongoDB process if you are connecting from the system where the deployment runs" - Can I not do this because the deployment is running on the Amazon Server?
When I enter the mongo shell command this is what it reads:
MongoDB shell version: 3.0.4
connecting to: AM-0.amigodb.0813.mongodbdns.com:27001/AmigoMain_1
2015-08-07T18:41:56.806+0100 W NETWORK
Failed to connect to 52.18.23.14:27001 after 5000 milliseconds, giving up.
2015-08-07T18:41:56.809+0100 E QUERY
Error: couldn't connect to server AM-0.amigodb.0813.mongodbdns.com:27001 (52.18.23.14), connection attempt failed
at connect (src/mongo/shell/mongo.js:181:14)
at (connect):1:6 at src/mongo/shell/mongo.js:181
exception: connect failed
I followed the instructions for the security settings on the Amazon Web Service but thinking that I may well have made a mistake.
Would greatly appreciate any help or where to go for answers.
Thanks,
Louis
Mongo by default only listens to connections on localhost. You'll need to edit mongo.conf and add your IP to the "bind ip" setting.
This is my mongod.conf file:
processManagement:
fork: true
net:
bindIp: 127.0.0.1
port: 27017
storage:
dbPath: "/data/db"
systemLog:
destination: file
path: "/var/log/mongod.log"
logAppend: true
storage:
journal:
enabled: true
It also gave me the same error message when I changed my bindIp to 0.0.0.0

Resources