一、問題描述
已部署好 Redis 主從服務器,實現了數據的同步。
Redis 主服務器(master server)具有讀寫的權限,而 從服務器(slave master)默認 只具有 讀 的權限。如果強行在從服務器中接入數據,則報錯提示“(error) READONLY You can't write against a read only slave”。如下所示:
127.0.0.1:6280> set str hello (error) READONLY You can't write against a read only slave. 127.0.0.1:6280>
二、解決辦法
解決 Redis 從服務器(slave ) 讀寫 權限的方法主要有以下兩種:
方法1:修改 redis.conf 配置文件(永久生效)
修改 redis.conf 配置文件中的參數 slave-read-only yes ,將 yes 修改為 no ;
然后保存並重啟 redis 服務,此刻從服務器就具備了 讀寫權限。
(注意:此方法必須重啟 Redis 服務,才能使配置生效。)
方法2:redis-cli 命令行中使用 config set 命令修改
在從Redis從服務器客戶端命令行中 (redis-cli),通過 config set slave-read-only no 進行設置,立即生效,不需要重啟 Redis 服務。
(注意:若Redis從服務器重新啟動,之前的設置參數就會失效,又會出現 redis 從服務器只有讀權限。)
127.0.0.1:6280> config get slave-read-only 1) "slave-read-only" 2) "yes" 127.0.0.1:6280> config set slave-read-only no OK 127.0.0.1:6280> config get slave-read-only 1) "slave-read-only" 2) "no" 127.0.0.1:6280>
測試設置(讀寫權限)是否生效:
127.0.0.1:6280> set str hello OK 127.0.0.1:6280> get str "hello" 127.0.0.1:6280>