一、Redis 密码设置和查看密码redis没有实现访问控制这个功能,但是它提供了一个轻量级的认证方式,可以编辑redis.conf配置来启用认证。1、初始化Redis密码:在配置文件中有个参数:requirepass,这个就是配置redis访问密码的参数;比如 requirepass password(Ps:需重启Redis才能生效) redis的查询速度是非常快的,外部用户一秒内可以尝试多大150K个密码;所以密码要尽量长(对于DBA 没有必要必须记住密码);
2、不重启Redis设置密码:在配置文件中配置requirepass的密码(当redis重启时密码依然有效)。redis 127.0.0.1:6379> config set requirepass password查询密码:redis 127.0.0.1:6379> config get requirepass(error) ERR operation not permitted
密码验证:redis 127.0.0.1:6379> auth passwordOK
再次查询:redis 127.0.0.1:6379> config get requirepass1) "requirepass"2) "password"PS:如果配置文件中没添加密码 那么redis重启后,密码失效;3、登陆有密码的Redis:在登录的时候的时候输入密码:redis-cli -p 6379 -a password先登陆后验证:redis-cli -p 6379redis 127.0.0.1:6379> auth passwordOKAUTH命令跟其他redis命令一样,是没有加密的;阻止不了攻击者在网络上窃取你的密码;认证层的目标是提供多一层的保护。如果防火墙或者用来保护redis的系统防御外部攻击失败的话,外部用户如果没有通过密码认证还是无法访问redis的。
二、启动、停止redis:启动redis:node02执行以下命令启动rediscd /export/servers/redis-3.2.8/src/redis-server redis.conf停止redis:redis-cli -h node02 -p 6379 shutdown
或者直接kill -9 redis进程号
三、连接redis客户端:node01执行以下命令连接redis客户端cd /export/servers/redis-3.2.8src/redis-cli -h node01 -p 6379 -a password
四、批量删除keys:1.未登录客户端的情况下:redis为默认端口号6379,无连接密码,删除命令如下:redis-cli keys "key*" | xargs redis-cli del
redis不为默认端口号6379,连接密码为"password",删除命令如下:redis-cli -h node02 -p 6379 -a password keys "itcast*" | xargs redis-cli -h node02 -p 6379 -a password del但是会报错:(error) ERR wrong number of arguments for 'del' command
2.登录redis客户端后,也可以批量删除,如下:DEL key1 key2 #删除多个keyflushdb #删除当前数据库中的所有Keyflushall #删除所有数据库中的key
五、redis绑定多个IP:
bind 192.168.8.100 127.0.0.1 #多个IP之间用空格进行分隔
bind 0.0.0.0 #可以绑定任意IP
六、redis的主从复制架构需要密码验证:
redis master配置了密码,需要进行主从同步的话,要在所有的slave的redis.conf中配置验证:masterauth 123456
否则查看redis.log,可以发现报错信息如下:
5933:S 05 Sep 20:46:09.100 * Connecting to MASTER 192.168.8.100:63795933:S 05 Sep 20:46:09.101 * MASTER <-> SLAVE sync started5933:S 05 Sep 20:46:09.101 * Non blocking connect for SYNC fired the event.5933:S 05 Sep 20:46:09.103 * Master replied to PING, replication can continue...5933:S 05 Sep 20:46:09.103 * (Non critical) Master does not understand REPLCONF listening-port: -NOAUTH Authentication required.5933:S 05 Sep 20:46:09.104 * (Non critical) Master does not understand REPLCONF capa: -NOAUTH Authentication required.5933:S 05 Sep 20:46:09.104 * Partial resynchronization not possible (no cached master)5933:S 05 Sep 20:46:09.105 # Unexpected reply to PSYNC from master: -NOAUTH Authentication required.5933:S 05 Sep 20:46:09.105 * Retrying with SYNC...5933:S 05 Sep 20:46:09.106 # MASTER aborted replication with an error: NOAUTH Authentication required.
七、哨兵模式,sentinel和redis身份验证配置:
当一个master配置为需要密码才能连接时,客户端和slave在连接时都需要提供密码。
redis.conf文件中:
(1)master通过requirepass设置自身的密码,不提供密码无法连接到这个master。(2)slave通过masterauth来设置访问master时的密码。但是当使用了sentinel时,由于一个master可能会变成一个slave,一个slave也可能会变成master,所以需要同时设置上述两个配置项。
sentinel.conf文件中,需要配置:
sentinel monitor mymaster 192.168.8.100 6379 2
sentinel auth-pass mymaster 123456 # Set the password to use to authenticate with the master and slaves.
protected-mode no(设置成:protected-mode no;保护模式关闭,如果你不关闭保护模式,启动哨兵的时候,无法正常运行)
注意:(1)如果redis的master和slave设置了密码,则必须要设置protected-mode no,否则master的redis-server进程意外停止时,redis.log日志文件中会打印错误信息:
3936:S 05 Sep 22:40:38.745 * Connecting to MASTER 192.168.8.100:63793936:S 05 Sep 22:40:38.746 * MASTER <-> SLAVE sync started3936:S 05 Sep 22:40:38.747 # Error condition on socket for SYNC: Connection refused
(2)可能需要master和slave的密码设置相同,不过没有验证过设置不同密码的话哨兵模式会不会失败。
八、
转载于:https://www.cnblogs.com/mediocreWorld/p/11462429.html
相关资源:JAVA上百实例源码以及开源项目