Redis:ERR max number of clients reached


環境

  1. redis 2.7

原因

在項目啟動的過程中,redis 突然報錯,提示:ERR max number of clients reached。

分析

根據錯誤提示,可知是 redis 的連接客戶端達到了最大數量。

客戶端連接數

首先,登錄 redis 服務器,使用 redis-cli 連接上 redis,下面的 xxx.xxx.xxx.xxx 表示服務器的 IP,如果沒有默認為 127.0.0.1。

./redis-cli -h xxx.xxx.xxx.xxx

# 如果不需要密碼,可以不用認證
AUTH password

連接上 redis 后,使用 INFO clients 查看客戶端連接數量:

# Clients
connected_clients:300
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0

然后使用命令 CONFIG GET maxclients 查看配置的最大客戶端連接數量:

1) "maxclients"
2) "3984"

如果連接的數量達到了最大,可以將 redis 中的最大客戶端數量加大。以下是配置文件中的說明:

# Set the max number of connected clients at the same time. By default
# this limit is set to 10000 clients, however if the Redis server is not
# able to configure the process file limit to allow for the specified limit
# the max number of allowed clients is set to the current file limit
# minus 32 (as Redis reserves a few file descriptors for internal uses).
#
# Once the limit is reached Redis will close all the new connections sending
# an error 'max number of clients reached'.
#
 maxclients 10000

連接數量

總連接數

有時候,需要排查到底是哪些 IP 占用的連接數過多,可以通過命令 netstat -an|grep 26379|wc -l 來統計連接的數量。

根據 IP 統計

通過命令

netstat -n |grep 26379 |awk '/^tcp/ {print $5}'| awk -F: '{print $1}'|sort | uniq -c | sort -rn

來統計每個 IP 的具體連接數量,就可以找出來占用連接數最多的 IP(grep 過濾根據實際情況修改)。

linux 系統限制

ulimit -a

[app_rdu@vm-kvm11286-app ~]$ ulimit -a
core file size          (blocks, -c) unlimited
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 62776
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) 4096
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

open file 限制

客戶端的連接數量,除了配置文件中配置的數量限制外,也會受到 linux 系統打開文件數量的限制。
在 redis 啟動時,有時候會看到如下的提示:

You requested maxclients of 10000 requiring at least 10032 max file descriptors.
Redis can’t set maximum open files to 10032 because of OS error: Operation not permitted.
Current maximum open files is 4096. Maxclients has been reduced to 4064 to compensate for low ulimit. If you need higher maxclients increase ‘ulimit –n’.

上面的提示說明最大連接數被 linux 系統限制了。可以通過命令 sudo sh -c "ulimit -n 65535 && exec su $LOGNAME" 來修改。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM