How to connect to clients in MongoDB

I am writing an application using mongo as my db. I want to print clients connected to db, for example, print their ip. How can I get this information?

I tried to use

db.serverStatus().connections

But this gives me the number of computers accessing my db.

+4
source share
3 answers

You can use db.currentOp(true)and iterate over the array of the inprogresult set using the field client.

+6
source

You should be able to run this command and get a list of connected IP addresses:

db.currentOp(true).inprog.forEach(function(d){if (d.client)printjson(d.client)})

db.currentOp $cmd.sys.inprog, . , , db.currentOp mongo, :

> db.currentOp
function ( arg ){
    var q = {}
    if ( arg ) {
        if ( typeof( arg ) == "object" )
            Object.extend( q , arg );
        else if ( arg )
            q["$all"] = true;
    }
    return this.$cmd.sys.inprog.findOne( q );
}
+5

, netstat, db. (bash) script . login script (~/.bash_profile), ( ) MongoDB , TCP.

, .

function whoIsConnectedToPort () {
    PORT=$1
    netstat -an | grep ":${PORT}.*ESTAB" | awk '{print $4":"$5}' | cut -d: -f2- | grep "^${PORT}:" | cut -d: -f2 | grep -v '^127' | sort | uniq | xargs -n1 nslookup | grep 'name =' | awk {'print $NF'} | sed 's/.$//' | sort | uniq
}

.

whoIsConnectedToPort 27017

, .

. , :

whoIsConnectedToPort 22
whoIsConnectedToPort 3306
whoIsConnectedToPort 1521
0

Source: https://habr.com/ru/post/1541611/


All Articles