redis遠端連線與密碼驗證
阿新 • • 發佈:2019-01-06
redis預設只允許本地訪問,要使redis可以遠端訪問可以修改redis.conf
開啟redis.conf檔案在NETWORK部分有說明
解決辦法:註釋掉bind 127.0.0.1可以使所有的ip訪問redis
若是想指定多個ip訪問,但並不是全部的ip訪問,可以bind
注意 下面還有個說明 在redis3.2之後,redis增加了protected-mode,在這個模式下,即使註釋掉了bind 127.0.0.1,再訪問redisd時候還是報錯,如下
修改辦法:protected-mode no
1.通過配置檔案進行配置
yum方式安裝的redis配置檔案通常在/etc/redis.conf中,開啟配置檔案找到
使用第一步中配置檔案中配置的老密碼登入redis,會發現原來的密碼已不可用,操作被拒絕
除了在登入時通過 -a 引數制定密碼外,還可以登入時不指定密碼,而在執行操作前進行認證。
slave中配置檔案內找到如下行,移除註釋,修改密碼即可
注意 下面還有個說明 在redis3.2之後,redis增加了protected-mode,在這個模式下,即使註釋掉了bind 127.0.0.1,再訪問redisd時候還是報錯,如下
(error) DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside. |
redis配置密碼
1.通過配置檔案進行配置
yum方式安裝的redis配置檔案通常在/etc/redis.conf中,開啟配置檔案找到
- #requirepass foobared
- requirepass myRedis
- sudo service redis restart
- #或者
- sudo service redis stop
- sudo redis-server /etc/redis.conf
- redis-cli -h 127.0.0.1 -p 6379
- redis 127.0.0.1:6379>
- redis 127.0.0.1:6379> keys *
- (error) ERR operation not permitted
- redis 127.0.0.1:6379> select 1
- (error) ERR operation not permitted
- redis 127.0.0.1:6379[1]>
- redis-cli -h 127.0.0.1 -p 6379 -a myRedis
- redis 127.0.0.1:6379> keys *
- 1) "myset"
- 2) "mysortset"
- redis 127.0.0.1:6379> select 1
- OK
- redis 127.0.0.1:6379[1]> config get requirepass
- 1) "requirepass"
- 2) "myRedis"
2.通過命令列進行配置
- redis 127.0.0.1:6379[1]> config set requirepass my_redis
- OK
- redis 127.0.0.1:6379[1]> config get requirepass
- 1) "requirepass"
- 2) "my_redis"
使用第一步中配置檔案中配置的老密碼登入redis,會發現原來的密碼已不可用,操作被拒絕
- redis-cli -h 127.0.0.1 -p 6379 -a myRedis
- redis 127.0.0.1:6379> config get requirepass
- (error) ERR operation not permitted
- redis-cli -h 127.0.0.1 -p 6379 -a my_redis
- redis 127.0.0.1:6379> config get requirepass
- 1) "requirepass"
- 2) "my_redis
- sudo service redis restart
- Stopping redis-server: [ OK ]
- Starting redis-server: [ OK ]
- redis-cli -h 127.0.0.1 -p 6379 -a my_redis
- redis 127.0.0.1:6379> config get requirepass
- (error) ERR operation not permitted
- redis-cli -h 127.0.0.1 -p 6379 -a myRedis
- redis 127.0.0.1:6379> config get requirepass
- 1) "requirepass"
- 2) "myRedis"
除了在登入時通過 -a 引數制定密碼外,還可以登入時不指定密碼,而在執行操作前進行認證。
- redis-cli -h 127.0.0.1 -p 6379
- redis 127.0.0.1:6379> config get requirepass
- (error) ERR operation not permitted
- redis 127.0.0.1:6379> auth myRedis
- OK
- redis 127.0.0.1:6379> config get requirepass
- 1) "requirepass"
- 2) "myRedis"
3.master配置了密碼,slave如何配置
slave中配置檔案內找到如下行,移除註釋,修改密碼即可
- #masterauth mstpassword