Skip to main content

Redis 使用技巧

文档

一定要去官网看文档,国内的翻译非常滞后而且很乱。 https://redis.io/

安装

Install Redis on Linux

启动服务端

redis-server

查看监听的端口是否正确:

netstat -tunple | grep 6379
root@SERVER:/tmp# netstat -tunple | grep 6379
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 112 307522147 8851/redis-server *
tcp6 0 0 :::6379 :::* LISTEN 112 307522148 8851/redis-server *

停止 Redis 服务

正确方式是通过 cli 客户端发送命令给 redis-server 让其停止。

redis-cli -h 127.0.0.1 shutdown

不加 -h 可能会 shutdown 不成功。

安全性

https://redis.io/docs/manual/security/

非 root 用户运行

Redis 不需要 root 权限即可运行。建议以仅用于此目的的非特权redis用户身份运行它。

密码验证

配 /etc/redis/redis.conf 的 requirepass "xxxxxxxxx"。

使用密码连接 redis-server:redis-cli -p "xxxxx"

但直接通过命令参数去传密码,不安全,官方建议是通过设置 REDISCLI_AUTH 这个环境变量。

保证 Redis 在内存不足的情况下正常运行

sudo sysctl vm.overcommit_memory=1

Redis Sentinel 哨兵

https://www.redis.com.cn/topics/sentinel.html

Redis 主从复制