一、報錯:
MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. Commands that may modify the data set are disabled. Please check Redis logs for details about the error.
Redis被配置為保存數據庫快照,但它目前不能持久化到硬盤。用來修改集合數據的命令不能用。請查看Redis日志的詳細錯誤信息。
二、搜索網上的解決方案,大概有三種:
1. 直接忽略形
將stop-writes-on-bgsave-error設置為no
# 臨時生效 127.0.0.1:6379> config set stop-writes-on-bgsave-error no # 永久生效 修改配置文件,重啟服務 vim /etc/redis.conf xxx
簡單粗暴,但不推薦
2. 解決問題形
查日志 不能保存到硬盤?為什么?
1.權限不足? chown授權
#tail /var/log/redis/redis.log 12402:C 26 Nov 18:06:13.077 # Failed opening the RDB file root (in server root dir /var/spool/cron) for saving: Permission denied 30640:M 26 Nov 18:06:13.177 # Background saving error # chmod 777 /var/log/redis/redis.log
2.磁盤滿了? df -h
3.內存不足了? free -m
原因: 強制關閉Redis快照導致不能持久化
這種方式是比較正常的套路,通過查日志來確定內存、磁盤問題、權限問題
3. 內存型問題處理:
修改系統內核相關內存分配策略:sysctl vm.overcommit_memory=1
這個參數是vm.overcommit_memory 是linux系統在應用申請內存使用的一個策略。
該策略有三個值:0、1、2 。0為默認值
在日志中看到有相關的日志: Can’t save in background: fork: Cannot allocate memory 不能保存,fork進程沒有足夠內存。但查看系統內存還有兩三個G,這個時候就懵逼了。 原來默認情況下系統對於內存的分配特別是dump內存中數據到磁盤上時會都需要fork一份,然后保存在磁盤上。而我們的redis內存應使用了8個G,持久化時候會額外增加一部分,而系統中的內存只有3G,不夠。 可以修改overcommit_memory的值來改善這個問題。
三、排查具體情況:
3.1 開發反饋redis異常、日志有報錯
redis.exceptions.ResponseError: MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. Commands that may modify the data set are disabled. Please che ck Redis logs for details about the error. During handling of the above exception, another exception occurred:
3.2 查看系統情況:
[root@hn-xx-xx celery]# free -m total used free shared buff/cache available Mem: 3790 1826 1363 0 600 1728 Swap: 0 0 0 [root@hn-xx-xx celery]# ps -ef |grep redis root 6496 3536 0 21:59 pts/0 00:00:00 grep --color=auto redis redis 9978 1 0 2018 ? 03:27:01 /usr/bin/redis-server 172.16.1.19:6379 [root@hn-xx-xx celery]# ll -d /var/lib/redis/ drwxr-x--- 2 redis redis 4096 Jul 18 21:46 /var/lib/redis/ [root@hn-xx-xx celery]# df -H Filesystem Size Used Avail Use% Mounted on /dev/vda1 43G 11G 30G 26% / devtmpfs 2.0G 0 2.0G 0% /dev tmpfs 2.0G 0 2.0G 0% /dev/shm tmpfs 2.0G 537k 2.0G 1% /run tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup /dev/mapper/vdb--vg-vdb--lv 106G 79M 106G 1% /data tmpfs 398M 0 398M 0% /run/user/0 tmpfs 398M 0 398M 0% /run/user/1004 tmpfs 398M 0 398M 0% /run/user/1002
3.3 查redis日志
tail /var/log/redis/redis.log
3.4查監控 分布式監控系統Zabbix--使用Grafana進行圖形展示

3.5 查看redis的使用情況
[root@hn-xx-xx celery]# redis-cli -h 127.0.0.1 127.0.0.1:6379> info Memory # Memory used_memory:1129160 used_memory_human:1.08M used_memory_rss:5255168 used_memory_rss_human:5.01M used_memory_peak:765978920 used_memory_peak_human:730.49M total_system_memory:3974848512 total_system_memory_human:3.70G used_memory_lua:37888 used_memory_lua_human:37.00K maxmemory:0 maxmemory_human:0B maxmemory_policy:noeviction mem_fragmentation_ratio:4.65 mem_allocator:jemalloc-3.6.0
