How to find out on which port mongo is?

Solution 1:

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.

Solution 2:

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

Solution 3:

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.

Solution 4:

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.