In MongoDB, how do I change the name of the server/daemon, or create a new server with a name other than localhost? The docs specify a way to connect to a server with a specific name, such as localhost (the --host option on the mongo command), as well as a way to specify the names of each server in a replication set, which I forget at the moment, but not a method to say the name of the server to create an instance of for the mongod command. It seems to have to be localhost.
I have node.js and apache, so I can use virtual hosting, and I obviously have a hosts file (/etc/hosts on Unix-like OS's and C:\WINDOWS\System32\drivers\etc\hosts on Windows), so I can presumably create a server with a different name from localhost, but the question is can I do this with the built-in commands like mongod, or with a configuration file?
Unlike the HTTP protocol (as used by your web apps running in a web server like Apache), the MongoDB Wire Protocol does not use hostnames when determining how to handle an incoming request. That means there is no equivalent of a host name or virtualhost directive for your MongoDB server.
The MongoDB server configuration file allows you to bind to one or more IP addresses. The bind IP configuration option specifies which IP address(es) the server listens to for incoming connections. For example, this might be: 127.0.0.1 (aka localhost), a comma-delimited list of IP addresses, or all network interfaces if you don't specify a bind IP restriction.
When you connect to MongoDB and specify a hostname in your command line or driver options, this name just has to be resolvable from the external point of view. You can use whatever form of resolvable hostname you want to create outside of MongoDB (for example, in your /etc/hosts file or DNS). As long as the MongoDB server is listening to the IP your hostname resolves to (and there are no firewalls or network connectivity issues), you should be able to connect irrespective of the hostname used.
For example, while localhost is the default name available for the loopback IP address (127.0.0.1) and should always be defined .. normally your computer will also have a unique hostname defined. You can check what your default hostname resolves to in the mongo shell with getHostName():
> getHostName()
Webscale.local
Assuming no bind IP restriction and a hostname of Webscale.local with an IP address of 192.168.1.1, you would be able to connect with any of:
Webscale.local
192.168.1.1
localhost
127.0.0.1
Make use of Docker Environment, create two containers, one for your Mongo Server and another for your App, run both containers inside one docker-compose.yml file, specify that your app container would depend_on your mongo container and then replace "localhost" the connection string in your app with the name of your mongo container. Take reference from the two snippets as below:
docker-compose.yml :
version: '3'
services:
img_flaskapp:
build: ./Flask/gie
container_name: flaskapp
ports:
- "8000:8000"
depends_on:
- img_mongodb
img_mongodb:
image: mongo
container_name: mongodb
ports:
- "27017:27017"
app code:
connect (host="mongodb://mongodb:27017/nameofyourdb")
Related
I have running widlfy docker and the postgres docker and also nginx docker, I want to connect all the three docker internally so my widlfy can connect to postgres and nginx and I have my url running. I have added the parameter of networking in the docker-compose.yml file but still not success.
Can you suggest how can I perform this activity of interconnection.
If you can't have all the containers in the same docker-compose.yml, then use the next.
The containers have assigned an internal port and external port, internal is inside the every docker's network and the external is where you can access using the computer's IP or localhost. In this case you need use your computer's IP, If the IP is 192.168.1.20 and the externals ports for the containers are:
Widlfy: 8080
Postgresql: 5432
ngnx: 80
Then you can connect them using this information:
Widlfy: 192.168.1.20:8080
Postgresql: 192.168.1.20:5432
ngnx: 192.168.1.20:80
The computer's IP use to change in a period of time, it depends of your modem, so check that your IP be the right when you use this option.
I have Windows 10 machine where MongoDB is installed. I can connect it from a command line. I run NodeJS app with sam local. When I use a production environment, the app can access Mongo Atlas cloud instance. But when I switch to a dev environment with localhost MongoDB it fails to connect.
The sam command starts Docker so it is clear why it cannot connect Mongo running on windows localhost. I found relevant question: From inside of a Docker container, how do I connect to the localhost of the machine?. The problem is that I still cannot connect my local MongoDB, even if I try:
"MONGODB_URI": "mongodb://docker.for.win.localhost:27018/bud?retryWrites=true&w=majority"
or
"MONGODB_URI": "mongodb://host.docker.internal:27018/bud?retryWrites=true&w=majority"
Error:
Request failed { MongoNetworkError: failed to connect to server [docker.for.win.localhost:27018] on first connect [MongoNetworkError: connect ECONNREFUSED 192.168.65.2:27018]
Has anybody faced this issue as well and overcome it? Mongo is installed directly to windows, not in Docker.
If MongoDB is installed and running directly from windows, it should be accessible via localhost:27017. Default port for mongod is 27017, as described in mongoDB documentation page.
Try using:
"MONGODB_URI": "mongodb://localhost:27017/bud?retryWrites=true&w=majority"
If you are using NETWORKS_DRIVER other than bridge for your NodeJS docker container, which is set by default. Refer to Docker Network drivers
Other cases:
The default port for mongod is 27018 when running with --shardsvr command-line option or the shardsvr value for the clusterRole setting in a configuration file.
The default port for mongod is 27019 when running with --configsvr command-line option or the configsvr value for the clusterRole setting in a configuration file.
Remember, that localhost (or any name) is just for your convinience. Tcp stack works on ip addresses. If you configure dns service (e.g. via hosts file) to resolve name to 127.0.0.1 for container it doesn't mean your host, but 127.0.0.1 points to the container, always.
You could make mongo service to listen on your main ip and use it for docker app, but you can also leverage hyper-v virtual network cards and setup mongo to listen not only on host's loopback interface, but also on the virtual one and give docker app ip of that interface. It remains on your virtual lan, therefore it's not exposed to public. However, windows firewall might block it, so make sure you set it up as private network (it will be marked as unidentified and by default is public, which usually has stuff blocked).
I have two computers within the same network. One of them is a server, which serves a publicly accessible website; the other one is for my daily use.
Both computers are using Microsoft technologies. (Windows Server 2019/Windows 10)
I have a MongoDB instance running on the server, and I want to connect it using my daily PC.
However, if I try to connect to the MongoDB instance (mongodb://192.168.50.33:27017, 192.168.50.33 is the local IP address of the server PC, 27017 is the port for the instance), it always timeout using
Google Chrome: If the connection works, I should see a message saying It looks like you are trying to access MongoDB over HTTP on the native driver port..
MongoDB Compass: If the connection works, I should be able to see the content inside the database.
I have tried/setup the following:
Setup an inbound firewall rule with the following properties:
Allow connections
Protocol: TCP
Ports:
Local: All ports
Remote: 27017
Scope: Any Local/Remote IP addresses
Apply to Domain, Private, Public
Checked the firewall log. Saw that the connection from my daily PC is allowed through the firewall.
Set up a port forwarding on the router (ASUS RT-AC58U) which connects to both of my computers with the following properties:
Port Range: 27017
Local Port: 27017
Local IP: 192.168.50.33
Set the net.bindIp of the configuration of the MongoDB instance to be 0.0.0.0
Any ideas?
bindIp value in mongodb config file should have the address of your server/lan as well to access.
net.bindIp=0.0.0.0,192.168.50.33
for more info you can visit the below link:
https://mkyong.com/mongodb/mongodb-allow-remote-access/
Download the app from the below link and also check if you can ping to mongodb server from 27017:
https://www.elifulkerson.com/projects/tcping.php
tcping.exe 192.168.50.33 27017
And use mongo client to check if you can connect, below is the command:
mongo --host 192.168.50.33 --port 27017
If this also does not help try starting the mongodb using the below commands and then try to access them:
mongod --bind_ip=192.168.50.33 --port=27017
mongod --bind_ip_all --port=27017 (works only if mongodb is 3.6 or greater)
Let me know the what happens.
I've successfully installed MongoDB on Windows (on a local machine) as a service, but now I want to move MongoDb to a separate server. So I extracted the tarball to a virtual server on network (running linux).
When I connected to the server ("testmongoserver") using PuTTY from my local machine, I started the mongod server and it told me that it was listening to the default 28017 port. The mongo console is also working and allowed me to create a new database (testdb) and add users to it.
However, I could not access the server from remote. When I type testmongoserver:28017 it doesn't open the HTTP console as localhost:28017 on my local machine does. I also can't connect using official drivers and providing a connectionstring.
What are the neccesarry steps to install MongoDB on Linux, so that I could access it from a remote machine with a connectionstring and use its HTTP console via testmongoserver:28017
Thanks!
1. Bind IP option
Bind IP is a MongoDB option that restricts connections to specifics IPs.
Have a look at your mongod configuration file, most of the time bind_ip is set to 127.0.0.1 for obvious security reasons. You can:
Add your desired IP by concatenating a list of comma separated values to bind MongoDB to multiple IP addresses.
Remove or comment (with # character) the bind_ip line. But be aware that all remote connection will be able to connect your MongoDB server!
More about bind_ip configuration option: https://docs.mongodb.com/manual/reference/configuration-options/#net.bindIp
Bind IP can also be set as a command argument: http://docs.mongodb.org/manual/reference/program/mongod/#cmdoption--bind_ip
2. Firewall
Check that you are not running behind a firewall
Make sure in your /etc/mongodb.conf file you have the following line,
bind_ip = 0.0.0.0
http://jitu-blog.blogspot.com.br/2013/06/allow-mongo-to-connect-from-remote-ip.html
Run netstat -a on mongo server and check a port.
Check DNS settings and check that linux server allows external connections.
Check that mongodb can accept external/remote connection.
Default port for mongo is 27017.
28017 - port for webstats.
See http://www.mongodb.org/display/DOCS/Security+and+Authentication#SecurityandAuthentication-Ports
Just had this issue and this fixed it:
Edit /etc/mongod.conf with sudo nano /etc/mongod.conf ensure that the net section looks like below (localhost binding by default doesn't allow for remote access):
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0
Make sure to restart mongod when you are done with above with below (assuming systemd ubuntu 16.04+ etc.):
sudo service mongod restart
Obviously from a security perspective if you are going to be opening up mongo to your network/the world be aware of the implications of this (if any)
Another problem may be that the mongodb port is not enabled. Check, from another host, the ports enabled on your server. For that you can use the command:
sudo nmap -P0 your_server_ip
You can get an answer like this:
Host is up (0.052s latency).
Not shown: 997 filtered ports
PORT STATE SERVICE
22/tcp open ssh
80/tcp open http
443/tcp closed https
If you use a virtual server in the cloud, as AWS, you need to add a new rule to add mongodb port (27017 by default).
Important: Note that with this configuration anyone can have access to your database
I fixed by below reference :
https://medium.com/founding-ithaka/setting-up-and-connecting-to-a-remote-mongodb-database-5df754a4da89
Actually, first i changed my bindIp from 127.0.0.1 to 0.0.0.0 in mongod.conf,
and enable security:
security:
authorization: "enabled"
then i restarted mongod using sudo service mongod restart.(because of new changes in mongod.conf), after that set firewall to open mongod running port (by iptables) and create a new user in admin db with new access (based on this link : https://medium.com/mongoaudit/how-to-enable-authentication-on-mongodb-b9e8a924efac), finally test open ports in my server from outside with (https://www.yougetsignal.com/tools/open-ports/) and connected successfully to remote mongod using mongocompass.exe.
I've successfully installed MongoDB on Windows (on a local machine) as a service, but now I want to move MongoDb to a separate server. So I extracted the tarball to a virtual server on network (running linux).
When I connected to the server ("testmongoserver") using PuTTY from my local machine, I started the mongod server and it told me that it was listening to the default 28017 port. The mongo console is also working and allowed me to create a new database (testdb) and add users to it.
However, I could not access the server from remote. When I type testmongoserver:28017 it doesn't open the HTTP console as localhost:28017 on my local machine does. I also can't connect using official drivers and providing a connectionstring.
What are the neccesarry steps to install MongoDB on Linux, so that I could access it from a remote machine with a connectionstring and use its HTTP console via testmongoserver:28017
Thanks!
1. Bind IP option
Bind IP is a MongoDB option that restricts connections to specifics IPs.
Have a look at your mongod configuration file, most of the time bind_ip is set to 127.0.0.1 for obvious security reasons. You can:
Add your desired IP by concatenating a list of comma separated values to bind MongoDB to multiple IP addresses.
Remove or comment (with # character) the bind_ip line. But be aware that all remote connection will be able to connect your MongoDB server!
More about bind_ip configuration option: https://docs.mongodb.com/manual/reference/configuration-options/#net.bindIp
Bind IP can also be set as a command argument: http://docs.mongodb.org/manual/reference/program/mongod/#cmdoption--bind_ip
2. Firewall
Check that you are not running behind a firewall
Make sure in your /etc/mongodb.conf file you have the following line,
bind_ip = 0.0.0.0
http://jitu-blog.blogspot.com.br/2013/06/allow-mongo-to-connect-from-remote-ip.html
Run netstat -a on mongo server and check a port.
Check DNS settings and check that linux server allows external connections.
Check that mongodb can accept external/remote connection.
Default port for mongo is 27017.
28017 - port for webstats.
See http://www.mongodb.org/display/DOCS/Security+and+Authentication#SecurityandAuthentication-Ports
Just had this issue and this fixed it:
Edit /etc/mongod.conf with sudo nano /etc/mongod.conf ensure that the net section looks like below (localhost binding by default doesn't allow for remote access):
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0
Make sure to restart mongod when you are done with above with below (assuming systemd ubuntu 16.04+ etc.):
sudo service mongod restart
Obviously from a security perspective if you are going to be opening up mongo to your network/the world be aware of the implications of this (if any)
Another problem may be that the mongodb port is not enabled. Check, from another host, the ports enabled on your server. For that you can use the command:
sudo nmap -P0 your_server_ip
You can get an answer like this:
Host is up (0.052s latency).
Not shown: 997 filtered ports
PORT STATE SERVICE
22/tcp open ssh
80/tcp open http
443/tcp closed https
If you use a virtual server in the cloud, as AWS, you need to add a new rule to add mongodb port (27017 by default).
Important: Note that with this configuration anyone can have access to your database
I fixed by below reference :
https://medium.com/founding-ithaka/setting-up-and-connecting-to-a-remote-mongodb-database-5df754a4da89
Actually, first i changed my bindIp from 127.0.0.1 to 0.0.0.0 in mongod.conf,
and enable security:
security:
authorization: "enabled"
then i restarted mongod using sudo service mongod restart.(because of new changes in mongod.conf), after that set firewall to open mongod running port (by iptables) and create a new user in admin db with new access (based on this link : https://medium.com/mongoaudit/how-to-enable-authentication-on-mongodb-b9e8a924efac), finally test open ports in my server from outside with (https://www.yougetsignal.com/tools/open-ports/) and connected successfully to remote mongod using mongocompass.exe.