MongoDB seeding and replication - node.js

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.

Related

compass refuse to connect to remoto mongodb

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

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

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

MongoDB Configuration File not working

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.

My websites running in docker containers, how to implement virtual host?

I am running two websites in two docker containers respectively in a vps.
e.g. www.myblog.com and www.mybusiness.com
How can I implement virtualhost in the vps so that the two websites can both use port 80.
I asked this question somewhere else, and was suggested to take a look at: https://github.com/hipache/hipache and https://www.tutum.co/
They look a bit curving. I am trying to find if there is a straightforward way to achieve that. Thanks!
In addition, forgot to mention my vps is a Ubuntu 14.04 box.
Take a look at jwilder/nginx-proxy project.
Automated nginx proxy for Docker containers using docker-gen
It's the easiest way to proxy your docker containers. You don't need to edit the proxy config file every time you restart a container or start a new one. It all happens automatically for you by docker-gen which generates reverse proxy configs for nginx and reloads nginx when containers are started and stopped.
Usage
To run it:
$ docker run -d -p 80:80 -v /var/run/docker.sock:/tmp/docker.sock \
jwilder/nginx-proxy
Then start any containers you want proxied with an env var VIRTUAL_HOST=subdomain.youdomain.com
$ docker run -e VIRTUAL_HOST=foo.bar.com ...
Provided your DNS is setup to forward foo.bar.com to the a host running nginx-proxy, the request will be routed to a container with the VIRTUAL_HOST env var set.
Multiple Ports
If your container exposes multiple ports, nginx-proxy will default to the service running on port 80. If you need to specify a different port, you can set a VIRTUAL_PORT env var to select a different one. If your container only exposes one port and it has a VIRTUAL_HOST env var set, that port will be selected.
You need a reverse proxy. We use nginx and haproxy. They both work well, and are easy to run from a docker container. A nice way to run the entire setup would be to use docker-compose (formerly fig) to create the two website containers with no externally visible ports, and use a, say, haproxy container with links to both website containers. Then the entire combination exposes exactly one port (80) to the network, and the haproxy container forwards traffic to one or the other container based on the hostname of the request.
---
proxy:
build: proxy
ports:
- "80:80"
links:
- blog
- work
blog:
build: blog
work:
build: work
Then a haproxy config such as,
global
log 127.0.0.1 local0
maxconn 2000
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
user haproxy
group haproxy
daemon
stats socket /var/lib/haproxy/stats
defaults
log global
option dontlognull
option redispatch
retries 3
timeout connect 5000s
timeout client 1200000s
timeout server 1200000s
### HTTP frontend
frontend http_proxy
mode http
bind *:80
option forwardfor except 127.0.0.0/8
option httplog
option http-server-close
acl blog_url hdr_beg(host) myblog
use_backend blog if blog_url
acl work_url hdr_beg(host) mybusiness
use_backend work if work_url
### HTTP backends
backend blog
mode http
server blog1 blog:80 check
backend work
mode http
server work1 work:80 check

Resources