8

I know that default mongo port is 27017 and I can connect to this instance on such port.

But I tried to figure it out by other means.

I found that mongo is running by ps aux | grep mongo with it I can see /usr/bin/mongod --config /etc/mongod.conf

At /etc/mongod.conf port is 27018

Netstat netstat -tulnp | grep 2701 gives me

tcp        0      0 0.0.0.0:27017           0.0.0.0:*               LISTEN      -               
tcp        0      0 0.0.0.0:27018           0.0.0.0:*               LISTEN      -

I don't know why I don't see name of the process. Is there any other way to be sure that mongo works on 27017?

4 Answers4

9
lsof -i | grep mongo

This will list all open files (lsof) that are listening on "Internet addresses" (-i). The output of this is piped to grep, which is used to filter by text. In this case, we're looking for all files open, related to the Internet, that match the word mongo.

earthmeLon
  • 11,247
  • That will work only if there is already a client accessing to the database. I may suggest start "mongostat" for example on a different terminal and let it run while executing lsof -n -i | grep mongo (-n is faster because it does not try to resolve names) – Fran Marzoa Feb 27 '18 at 18:27
5

Actually you can use:

lsof -i -ac mongod

lsof can do more than just look up network connections. the "-a" is for "And" so it will have to match both criteria. Otherwise it defaults to "Or". The "-c" is for the command. You can also specify user name or PID or lots of other stuff.

4

You could use nmap to scan ports in range from 1 to 65535 with -p- flag

For instance,

Downloads:$ nmap -p- localhost

Starting Nmap 6.40 ( http://nmap.org ) at 2015-11-10 12:07 MST
Nmap scan report for localhost (127.0.0.1)
Host is up (0.00036s latency).
Not shown: 65529 closed ports
PORT      STATE SERVICE
21/tcp    open  ftp
22/tcp    open  ssh
25/tcp    open  smtp
631/tcp   open  ipp
37818/tcp open  unknown
46400/tcp open  unknown
Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
1

From the system shell you can use lsof or netstat -an as you mentioned to view what a process is doing in terms of open ports. As an alternative you can run the db.getCmdLineOpts() command from the mongo shell. That output will give you all the arguments passed on the command line (argv) to the server and the ones from the config file (parsed) and you can infer the ports mongod is listening based on that information. Here's an example:

{
        "argv" : [
               "/usr/bin/mongod",
               "--config",
               "/etc/mongod.conf",
               "--fork"
        ],
        "parsed" : {
               "bind_ip" : "127.0.0.1",
               "config" : "/etc/mongodb/mongodb.conf",
               "dbpath" : "/srv/mongodb",
               "fork" : true,
               "logappend" : "true",
               "logpath" : "/var/log/mongodb/mongod.log",
               "quiet" : "true",
               "port" : 30001
        },
        "ok" : 1
}

If you have not passed specific port options like the one above, then the mongod will be listening on 27017 (normal) and 28017 (http status interface) by default.

Note: there are a couple of other arguments that can alter ports without being explicit, see here:

https://docs.mongodb.org/manual/reference/configuration-options/#sharding.clusterRole

I mention this because your lsof output also shows port 27018 open, which is the default if you have the shardsvr option set. Hence, your output suggests that you may have a mongos on 27017 and a (shard) mongod on 27018. If you connect to each and run the getCmdLineOpts() you should be able to figure out how things are configured.

Adam C
  • 1,929