天天看點

Redis連接配接報錯:ERR Client sent AUTH, but no password is set

今天在啟動項目時,用到了Redis緩存資料庫,但是卻出現了報錯資訊:ERR Client sent AUTH, but no password is set。

Caused by: io.lettuce.core.RedisCommandExecutionException: ERR Client sent AUTH, but no password is set
           
Redis連接配接報錯:ERR Client sent AUTH, but no password is set

原因

産生這個問題的原因異常資訊裡已經說明,就是Redis伺服器沒有設定密碼,但用戶端向其發送了AUTH(authentication,身份驗證)請求攜帶着密碼,導緻報錯。既然是沒有設定密碼導緻的報錯,那我們就把Redis伺服器給設定上密碼就好了。一共有2種方式設定密碼:

一、指令行方式

1、先進入Redis伺服器

C:\Program Files (x86)\Redis-x64-3.2.100>redis-cli.exe
           

2、檢視是否設定了密碼

127.0.0.1:6379> auth 123456
(error) ERR Client sent AUTH, but no password is set
           

3、報錯,說明沒有設定密碼,然後再執行配置指令

127.0.0.1:6379> config set requirepass root

OK
           

4、傳回OK後即代表配置成功,這個時候再執行檢視密碼指令

redis 127.0.0.1:6379> AUTH 123456
Ok
           

傳回OK,就說明已經配置成功了。

這種配置方式存在一個很嚴重的問題,就是當我們将Redis伺服器關掉之後,這些配置就會失效,下次再啟動伺服器,需要重新設定!

二、修改配置檔案

還有一種方式就是一勞永逸的方式,就是直接修改配置檔案裡的參數。在redis.windows.conf(我的是這個配置檔案)或者redis.conf(我看網上有說是這個配置檔案的)的配置檔案中找到requirepass這個參數,設定參數密碼,然後儲存配置檔案,重新開機Redis。

# requirepass foobared
requirepass 123456 //123456是設定的密碼
           

本來這種方式非常簡單,但是在實際過程中,卻遇見了一些問題,那就是配置不生效,明明配置檔案裡都已經配置了密碼,但是還會報錯,後來在Redis啟動時發現,Redis報錯了:

Warning: no config file specified, using the default config. 
 In order to specify a config file use C:\Program Files (x86)\Redis-x64-3.2.100\redis-server.exe
 /path/to/redis.conf
           
Redis連接配接報錯:ERR Client sent AUTH, but no password is set

後來查閱之後才知道,原來Redis啟動時需要指定配置檔案,否則還會使用預設配置,而我在Windows裡啟動.exe應用程式時,還是習慣性的輕按兩下應用程式啟動,導緻Redis一直使用的是預設配置。

這樣我們就需要在指令行視窗通過指令行的方式來啟動并指定配置檔案:

C:\Program Files (x86)\Redis-x64-3.2.100>redis-server.exe redis.windows.conf
           
Redis連接配接報錯:ERR Client sent AUTH, but no password is set

這樣,我們的Redis伺服器的密碼就正式配置完成了。