Confusion with port number in MongoDB used by client connections - mongodb

When I start MongoDB using mongod.exe it displays a message like "waiting for connections on port 27017", but when I execute mongo.exe the log message is "connection accepted from 127.0.0.1:60501".
Now, when I use PyMongo to connect to the DB I assumed that the port number to be used is "60501" since this is the port where the connection was established. Surprisingly I got an error saying "connection refused". I thought there was some problem with my MongoDB installation, config file placement, etc.
When nothing fixed it, I just played around by changing the port number to 27017, and this actually fixed the problem. Can someone explain how this worked out?

This behaviour is not specific to MongoDB. As part of the TCP/IP client/server protocol, services listen on configured ports (typically with well known defaults like 27017 for mongod). Clients connect to a server port and also establish their own ephemeral/dynamic port to use for the duration of a client/server session.
Each active session from the same client IP will use a distinct port on the client. The ephemeral port is logged to identify different client connections as they are established.
The logging in your case might be slightly confusing since you are connecting from localhost (127.0.0.1) where the server is also running, but the correct port to connect to is the mongod server port (default: 27017).

Related

Having problem in connecting to remote MongoDB Server

I tried to connect a remote MongoDB Server running on Ubuntu using MongoDB Compass on Windows. But I have problems connecting always as the IP of the Windows machine changes every day.
I did the following things to connect to the remote server-
Got the IP of the Client Machine, then allowed that IP on the firewall of the server machine on port 27017.
sudo ufw allow from client_machine_ip to any port 27017
Note: The ufw status looked okay.
Got the IP of the Server Machine, then on the MongoDB configuration file on the server, I modified the bindIp.
bindIp: 127.0.0.1,server_machine_ip
Note: I restarted mongod and it was okay too.
I was able to connect the remote MongoDB Server using MongoDB Compass successfully for the first time. Then I saw, the IP of the client machine was changing every day. So, every time, the client IP changes, I need to allow that IP on the firewall of the server machine (in which I am using the MongoDB Server) on port 27017. Could you help to solve this? Thanks in advance.
You can update the firewall for port 27017 to allow from anywhere since client machine IP is not static.
sudo ufw allow 27017 #(this will allow from any IP)

MongoDB: Connect to localhost from another computer in same network

I'm running MongoDB on one computer and want to connect to the server instance from another computer on the same network.
Going into Mongo shell gives me the following output for both computers: connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb with a different instance: Implicit session: session { "id" : UUID(...) }.
I tried the proposed solutions from here and here to no avail. Thought that binding the IP to 0.0.0.0 would help, but it didn't.
Any additional suggestions from someone who dealt with a similar issue?
0.0.0.0 is an address, not a port. You need to configure the server to listen on all addresses or explicitly on loopback (127.0.0.1) and the network address (whatever it is), then configure the client to connect to the server's address.
Use netstat on the server side to verify the server is listening on your configured addresses.

Not able to connect on Mongodb Atlas port

I'm using an M0 GCP instance.
I can connect to the cluster using this string:
'mongodb+srv://my_user:my_pass#my_cluster-mbsnz.gcp.mongodb.net/db?retryWrites=true&w=majority'
I'm trying to use another client where I need to pass host and port, but I can't connect.
I tried telnet to the port 27017, but for some reason I'm not able to connect directly on the port.
curl http://my_cluster-mbsnz.gcp.mongodb.net:27017
curl: (7) Failed to connect to my_cluster-mbsnz.gcp.mongodb.net port 27017: Connection timed out
or
telnet my_cluster-mbsnz.gcp.mongodb.net 27017
Trying 185.82.212.199...
^C -> After a long time waiting
What might be wrong ?
+srv urls use a DNS seed. On atlas, you can click into the cluster and you should be able to see the urls for your primary & your secondaries and use those urls to connect. You should also be able to use nslookup to get that info using part of that connection string, but it's probably simpler to just look up the urls through the UI.
https://docs.mongodb.com/manual/reference/connection-string/
In order to leverage the DNS seedlist, use a connection string prefix of mongodb+srv: rather than the standard mongodb:. The +srv indicates to the client that the hostname that follows corresponds to a DNS SRV record. The driver or mongo shell will then query the DNS for the record to determine which hosts are running the mongod instances.

Is there any issue using the same postgresql database for two different servers?

I am using postgres database which is accessed by jboss and tomcat server.
My server accidentally restarted and then postgres service was not showing in the services list.
When I tried to open postgres local host server using pgadminIII it showed following error:
Server doesn't listen
The server doesn't accept connections: the connection library reports
could not connect to server: Connection refused (0x0000274D/10061) Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? could not connect to server: Connection refused (0x0000274D/10061) Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432?
If you encounter this message, please check if the server you're trying to contact is actually running PostgreSQL on the given port. Test if you have network connectivity from your client to the server host using ping or equivalent tools. Is your network / VPN / SSH tunnel / firewall configured correctly?
For security reasons, PostgreSQL does not listen on all available IP addresses on the server machine initially. In order to access the server over the network, you need to enable listening on the address first.
For PostgreSQL servers starting with version 8.0, this is controlled using the "listen_addresses" parameter in the postgresql.conf file. Here, you can enter a list of IP addresses the server should listen on, or simply use '*' to listen on all available IP addresses. For earlier servers (Version 7.3 or 7.4), you'll need to set the "tcpip_socket" parameter to 'true'.
You can use the postgresql.conf editor that is built into pgAdmin III to edit the postgresql.conf configuration file. After changing this file, you need to restart the server process to make the setting effective.
If you double-checked your configuration but still get this error message, it's still unlikely that you encounter a fatal PostgreSQL misbehaviour. You probably have some low level network connectivity problems (e.g. firewall configuration). Please check this thoroughly before reporting a bug to the PostgreSQL community.
Does this problem arise due to accessing same DB by two different servers?
My server accidentally restarted and then postgres service was not
showing in the services list.
You can't connect because it's not running. You can have as many clients connecting to a PostgreSQL server as your hardware can support - there is no problem with that.
Restart your PostgreSQL service. If it won't, check the logs for why. If you don't know where your PostgreSQL logs are now is a good time to find out. I can't tell you how to restart the service because you haven't said what OS you are running or how you installed PG. Check your documentation.

Issues with setting bind_ip in mongo on centos, pymongo

Running Mongo 3.0.3 on Centos6.5. Running app with pymongo (python3.4) from a different server.
Can only get connection from remote server to work by commenting out bind_ip completely, which I'm a bit worried about. How can bind_ip be set to accept connections from specific IP of remote server and from localhost?
I tried:
bind_ip = 127.0.0.1 # only local works
bind_ip = 127.0.0.1, <remote server IP> # all traffic stops
The bind_ip configuration option in mongodb is for attaching mongos or mongod process to listen to specific network interface. As you may know a server may have multiple network interfaces, you may choose on which interface you want mongos/mongod to listen for connections.
If I put it simply, bind_ip configuration allows attaching mongos/mongod process to one or multiple ips that are available on a server so that it will listen for connection requests coming on the specific ip/ips. bind_ip sometimes confuses beginers. They may feel that bind_ip is for allowing an ip FROM where connections to the mongodb are allowed. But this is not true.
For example, suppose you have a server that has 2 network interface i.e x.x.x.x and y.y.y.y
Now if you run mongod process on this server and use x.x.x.x in bind_ip configuration then you can connect to this mongod from anywhere if you use x.x.x.x:27017 in your connection string. Though the other ip is also on the same server but you will not be able to connect to this mongod using y.y.y.y:27017 in your connection string. Similarly, if you bind your mongos/mongod to 127.0.0.1 then you will only be able to connect to it from the server itself.
Hope this helps.