1. 安装部署
Redis 官方不支持 Windows 平台,Microsoft Open Tech Group 对其进行了跨平台移植,但是仅更新到 Redis 3.0.504,下载地址为 https://github.com/MicrosoftArchive/redis/releases。
wget http://download.redis.io/releases/redis-5.0.5.tar.gz # 获取源码
sudo apt-get install tcl # 安装依赖
sudo make # 编译源码
sudo make install # 编译安装
redis-server /path/to/redis.conf # 启动服务器进程
redis-cli -h 127.0.0.1 -p 6379 -a password # 连接到 Redis
2. 处理警告
Redis 首次启动时,一般会出现三个警告:
The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add ‘vm.overcommit_memory = 1’ to /etc/sysctl.conf and then reboot or run the command ‘sysctl vm.overcommit_memory=1’ for this to take effect.
you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix thisissue run the command ‘echo never > /sys/kernel/mm/transparent_hugepage/enabled’ as root, and add it to your /etc/rc.local in order to retain thesetting after a reboot. Redis must be restarted after THP is disabled.
上述警告关乎 Redis 运行性能,处理方法如下:
- 第一个警告:将
net.core.somaxconn = 1024
添加到/etc/sysctl.conf
中,执行sudo sysctl -p
生效; - 第二个警告:将
vm.overcommit_memory = 1
添加到/etc/sysctl.conf
中,执行sudo sysctl -p
生效; - 第三个警告:将
echo never > /sys/kernel/mm/transparent_hugepage/enabled
添加到/etc/rc.local
中,执行sudo source /etc/rc.local
生效。
3. 基本配置
Redis 配置文件位于安装目录下,文件名为 redis.conf(Windows 上为 redis.windows.conf),使用 redis-server /to/path/redis.conf
加载特定的配置文件,启动服务器进程。
在 Redis 交互命令行下,可以通过 CONFIG
查看或修改配置项:
> CONFIG GET * # 查看所有配置信息
> CONFIG GET loglevel # 查看日志级别
> CONFIG SET loglevel "notice" # 修改日志级别
redis.conf
常用配置如下:
port 6379 # 监听端口
bind 127.0.0.1 # 绑定的主机地址
daemonize yes # 是否启动 Redis 为守护进程(默认为 no)
pidfile /var/run/redis_6379.pid # 当 Redis 以守护进程运行时,自动将 PID 写入该文件,防止重复启动
loglevel notice # 日志记录级别(debug、verbose、notice、warning)
logfile /var/log/redis_6379.log # 日志记录方式(默认为标准输出)
dbfilename dump.rdb # 本地数据库文件名
dir /opt/redis/ # 本地数据库存放目录
timeout 0 # 客户端闲置多长时间后关闭连接(默认为 0,表示永不关闭)
/var/run/xxx.pid
文件,用于记录进程 ID,防止启动多个进程副本。进程启动后,自动给xxx.pid
加文件锁,只有获得该锁的进程才有写权限,把自身 PID 写入文件,未能成功获取该锁的进程自动退出。注意,文件锁的状态不会被子进程继承。
4. 支持远程连接
Redis 默认仅支持本地连接,修改配置以支持远程连接,主要包括:配置监听地址、关闭保护模式、设置访问密码等。
# bind 127.0.0.1 # 绑定的主机地址(如果启用该设置,则只允许来自指定网卡的请求。如果没有指定,则可以接受来自任意一个网卡的请求)
requirepass foobared # 连接密码
protected-mode no # 关闭保护模式,允许远程连接