Problem:
Found MongoDB can not connect, view MongoDB log, there are a large number of errors as follows:
819
When the number of MONGODB connections reaches 819, it cannot be added, so it cannot be connected.
Analytical Solutions:
1. Maxconns Limit
By default, in a Linux system, MongoDB has a maximum number of connections of 819.
You can modify the maximum number of connections for MongoDB and modify its configuration file mongod.conf:
maxConns=20000 # officially specified, mongodb maximum connection number setting, can not exceed 20,000
Restart the MongoDB service for the configuration to take effect.
2. Ulimit Limit
If the Maxconns, or the too many open connections error, may also be related to the ulimit limit of the system.
Linux system default file handle for each process limit open files to 1024, this value is generally too small, need to be adjusted.
View all current limit information of the system.
# ulimit -a
Core file size (blocks, -c) 0
Data seg size (kbytes, -d) unlimited
Scheduling priority (-e) 0
File size (blocks, -f) unlimited
Pending signals (-i) 7672
Max locked memory (kbytes, -l) 64
Max memory size (kbytes, -m) unlimited
Open files (-n) 1024
Pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
Real-time priority (-r) 0
Stack size (kbytes, -s) 10240
Cpu time (seconds, -t) unlimited
Max user processes (-u) 1024
Virtual memory (kbytes, -v) unlimited
File locks (-x) unlimited
Use Ulimit-n to modify the open files restriction: (current session is in effect)
# ulimit -n 102400
Change system limits, modify/etc/security/limits.conf, add the following line: (permanent)
* soft nofile 102400
* hard nofile 102400
MongoDB Error: Connection refused because too many open connections:819