1. 程式人生 > 實用技巧 >Redis 無法持久化到硬碟錯誤:not able to persist on disk

Redis 無法持久化到硬碟錯誤:not able to persist on disk

一、報錯:
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查監控

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