1. 程式人生 > >Redis無法寫入故障排查過程

Redis無法寫入故障排查過程

redis

一、故障表現
1、新增一個key失敗
127.0.0.1:6379> set lion 2
(error) 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.

2、無法關閉服務
service redis-server restart
service redis-server stop

兩個命令都是一直卡在stop那一步,一直無法關閉

kill ps aux|grep ‘redis-server ‘|grep -v grep|awk ‘{print $2}‘ #無法殺掉
kill -9 ps aux|grep ‘redis-server ‘|grep -v grep|awk ‘{print $2}‘ #強行殺掉
強行殺掉後無法啟動,原來還有個pid的文件沒有刪除
/run/redis/redis-server.pid
rm /run/redis/redis-server.pid #手動刪除它之後才能啟動Redis

補充:
下面是無法關閉redis-server的原因
8576:signal-handler (1527833505) Received SIGTERM scheduling shutdown...

8576:M 01 Jun 14:11:45.180 # User requested shutdown...
8576:M 01 Jun 14:11:45.180 * Saving the final RDB snapshot before exiting.
8576:M 01 Jun 14:11:45.180 # Failed opening .rdb for saving: Permission denied
8576:M 01 Jun 14:11:45.180 # Error trying to save the DB, can‘t exit.
8576:M 01 Jun 14:11:45.180 # SIGTERM received but errors trying to shut down the server, check the logs for more information

二、搜索解決方法
1、關閉”當持久化出錯時停止寫入硬盤“
127.0.0.1:6379> config set stop-writes-on-bgsave-error no
2、修改目錄及持久化權限為755
本來該目錄就是755,所以與此無關。
看了幾篇基本上和這兩個相關。

三、老老實實看日誌解決辦法
1、在調試過程中一直監控日誌
tail -f /var/log/redis/redis-server.log #文件目錄及文件名請按自己配置文件,有兩行有用的信息
27915:C 01 Jun 14:12:14.038 # Failed opening .rdb for saving: Permission denied
8576:M 01 Jun 14:12:14.138 # Background saving error
2、將dump.rdb所在目錄權限修改為777,並將原來的dump.rdb刪除,殺掉redis-server並刪除redis-server.pid後啟動redis-server,奇跡出現了。
ll
-rw-rw---- 1 redis redis 41 Jun 1 14:42 dump.rdb
看到亮點了吧,用戶和用戶組都是redis,為啥?

3、再來看服務啟動的腳本
vi /etc/init.d/redis-server
其中有一段
Run_parts () {
if [ -d /etc/redis/${NAME}.${1}.d ]
then
su redis -s /bin/sh -c "run-parts --exit-on-error /etc/redis/${NAME}.${1}.d"
fi
}

su redis 就在這。

四、最終解決
chown redis:redis -R dir #dir 為redis配置文件中dir的目錄
chmod 755 dir #dir 為redis配置文件中dir的目錄,最後要將前面改為777的權限恢復回來。

五、總結
在這個事件中本來是很簡單的問題,但在此過程中花費了挺長時間,前前後後花了半天。究其原因有二,一是心急,沒有好好看日誌;二是意外,沒有想到會使用redis這個用戶來啟動,因為啟動的時候是在root權限下。

Redis無法寫入故障排查過程