When I check my MongoDB logs (/var/log/mongodb/mongodb.log), it shows something like:
end connection 127.0.0.1:57132 (**651 connections now open**)
But I am not running so many processes. How are so many connections open? Is there a way to close all these connections?
I do run a cron job (from shell script) to query mongodb every minute. Is that what is causing so many open connections? Should I close the connection every time after querying?
When I run db.serverStatus().connections, I get:
{ "current" : 500, "available" : 319, "totalCreated" : NumberLong(1328754) }
I think that's too big a number. How do I handle this?
Mongo does not usually log information about connections unless they are somehow exceptional (in size of the results or time to perform them for instance). Is this a cluster? If so these connections are probably the normal result of the mongo boxes syncing to each other. If not try
netstat -ln|grep 27017
to see who's using your mongo.
Two reasons I have seen for high connection counts:
When using replication
The Java driver didn't close any connections one it had opened them. Could be controlled with a property
Related
I use mongodb client to connect Database like here. Notice, i only use one connect to DB, but when i start my application and check Mongodb connection by command.
db.serverStatus().connections
Before start app at 8 connection, after it increase more 20 connection. I miss something, any one help me to explain ?
I have a 3 member replica set. Read preference is set as "Secondary Preferred" How to check application is reading from Secondary Node in MongodB? Please suggest.
Firstly you can configure profiling. For that you need to start your mongodb servers with option --profile 2 and configure log file. It'll log all queries.
After that you can read log for each instance db. Simple example:
db.your_collection.profile.find({ns: "name_your_db.your_collection"})
Secondly you can use mongotop. You need to start it for each mongodb server.
For example:
mongotop -h server_ip:server_port seconds
mongotop -h 127.0.0.1:27017 5
It'll print every specified period of time log, where you can read how much time for read or write is taken for each collection.
Other means of determining whether queries are sent to secondaries:
Enable command logging in the driver, which should tell you which server each command was sent to.
Inspect server logs for connections being made or not made, then set minimum connection pool size to 0 so that a connection is only made when needed for a query, then run some queries.
I am using MongoDb as the Database for our WebApplication
Currently we are facing a issue in mongoDb which is "max connections 8000 exceeded"
To solve this problem we have decided to increase the connections to 20000
But not sure how to increase the connections in MongoDB .
Currently we have only this property called maxConnections whose vaue is 800 in our code
This is the stats collected from mongodb server
db.serverStatus().connections
{ "current" : 7000, "available" : 1000 }
We start the mongodb this way
mongod --config /etc/mongodb.conf
and inside the mongodb.conf we have
replSet = test
fork = true
port = 27017
I am not sure why the max conection is showing 8000 ?? where it is hardcoded ??
Could any body please tell me how to find why the connection size is 8000 in my case ??
Do you actually have over 8000 threads access this database concurrently? Perhaps you are instead just spinning up a ton of connections you don't actually need?
That said, the 8000 limit is probably coming from your system limits. ulimit will restrict the number of usable sockets, which will restrict the number of connections. See the mongod documentation for a complete explanation.
Your approach here, though, should probably be to make sure you're reusing connections rather than just creating tons of them, since it's extremely doubtful that you are actually running 8000 concurrent clients.
I can determine the current number of connections by
db.serverStatus().connections
but all that gives me is the current number of connections. Is there anywhay to determine which ips are connected and which connection number they have been assigned to?
From mongo shell, this will print client IP:port, along with connection ID:
db.currentOp(true).inprog.forEach(function(d){if(d.client)print(d.client, d.connectionId)})
Note: passing true to db.currentOp() shows all connections (including idle). The docs have more examples on filtering connections, see:
db.currentOp reference and currentOp output fileds with descriptions.
From mongo shell run db.currentOp() to show all active connections or db.currentOp(true) to show all connections.
It depends on your db engine, but one simple way you can do it with netstat, checking the port your database allows to connect, and if you have security concerns you can limit the ip addresses that connect in the configuration file. Most databases by default only allow localhost to connect to them.
yesterday appeared a strange behaviour:
on small load all queries take long time and then site return error
"Connection to MongoDB failed. Operation now in progress"
At mongostat we see about 10-30 connections (very small, because we
usualy work with 400-500)
But when I type "netstat -na | grep 27017" I see very big number of
TCP connections (> 150):
http://pastebin.com/3ghtwkVd
Why mongodb closes connection but TCP still open?
We doesn't use persistent connections and always doing Mongo:close()
at the end of scripts.
Site work on cloud system like Amazon EC2 (we doesn't observe any
network issues)
10.1.1.16 - MongoDB
10.1.1.7 - Apache
1Gbit/s between servers
OS: Debian 6 Squeeze
MongoDB: 1.8.2 (with 1.6.6 we have the same problem)
Apache 2
PHP 5.3.6
PHP mongo driver 1.1.0 (connection pooling in 1.2.x is very bad for
us)
Looks like your driver (e.g. PHP) does not actually close the TCP connection even when you close it using the method.
If you're using PHP as a module, try graceful apache reload to make PHP module unloaded and loaded again. This way, destructors are called and connections are closed.
If you use PHP as fastcgi app, restart (kill/exec) it and invoke again.
File a bug if necessary.