redis设置密码
因為沒有密碼認證會不安全,這里我們加上密碼認證。先介紹一下密碼認證的兩個配置。主要是兩個參數(shù):
requirepass:外面服務、客戶端來連接redis的密碼。
masterauth:redis從去連接redis主使用的密碼。這個意思是說,如果你在主上設(shè)置了requirepass參數(shù),你就需要再從上設(shè)置masterauth參數(shù),并和主密碼指定成一樣的。這樣從才能繼續(xù)去同步主的數(shù)據(jù)。
并且,如果你是哨兵模式,還要在/etc/redis-sentinel.conf添加下面一行配置,這里lenovo20!&是那個配置的密碼,你可以改成你的密碼。否則,哨兵會認為redis主機失聯(lián)。
sentinel auth-pass mymaster lenovo20!&
下面介紹下,如何添加這個密碼。對于redis來說,有兩種方式:
一種是在/etc/redis.conf添加下面2個字段,之后重啟redis。兩個redis主從密碼要設(shè)置的一樣。
requirepass "lenovo20!&"
masterauth "lenovo20!&"
另外一種是登錄到redis數(shù)據(jù)庫里面,設(shè)置密碼,這個不用重啟服務會立即生效,不過重啟服務之后會失效。如果想永久生效,需要繼續(xù)執(zhí)行一條命令 config rewrite 。
config set masterauth "lenovo20!&"
config set requirepass "lenovo20!&"
config rewrite
你執(zhí)行上面命令,會發(fā)現(xiàn)馬上生效了。這時你輸入其他命令會報錯,這時候你要輸入密碼來證明你的身份。
對于redis-sentinel,修改cat /etc/redis-sentinel.conf文件,增加下面字段。之后重啟服務。
sentinel auth-pass mymaster lenovo20!&
6.上面就是添加密碼認證的過程了,下面給出一些常用的reids命令,以及配置。
登錄 redis 數(shù)據(jù)庫
redis-cli -h host -p port
查看redis 數(shù)據(jù)庫狀態(tài)
Info
指定主庫
slaveof host post
解除主從關(guān)系(慎用)
slaveof no one
設(shè)置向redis主同步的密碼
config set masterauth "lenovo20!&"
設(shè)置遠程連接的密碼
config set requirepass "lenovo20!&"
將配置寫到配置文件里
config rewrite
查詢key
get key
新建key
set key value
刪除key
del key
7.配置文件:
/etc/redis.conf
bind 172.16.51.140
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
supervised systemd
pidfile "/var/run/redis_6379.pid"
loglevel notice
logfile "/var/log/redis/redis.log"
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
dir "/opt/webapps/redis"
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
# Generated by CONFIG REWRITE
masterauth "lenovo20!&"
requirepass "lenovo20!&"
slaveof 172.16.51.141 6379
/etc/redis-sentinel.conf
dir "/opt/webapps/redis"
logfile "/var/log/redis/sentinel.log"
sentinel myid e85b094ccd8da12305652c84aa9cdbdd0a0caec8
sentinel monitor mymaster 172.16.51.141 6379 1
sentinel down-after-milliseconds mymaster 15000
sentinel failover-timeout mymaster 10000
sentinel auth-pass mymaster lenovo20!&
# Generated by CONFIG REWRITE
port 26379
supervised systemd
sentinel config-epoch mymaster 3
sentinel leader-epoch mymaster 3
sentinel known-slave mymaster 172.16.51.140 6379
sentinel current-epoch 3
總結(jié)
- 上一篇: 拉菲红酒图片(传说中的“拉菲红酒”)
- 下一篇: 鼠标dpi是什么意思(鼠标dpi是高还是