安装:将下载的.tar.gz文件移动到需要路径下,然后解压 解压命令:tar zxvf redis-3.2.6.tar.gz 切换至解压目录:cd redis-3.2.6 安装:make && make instal
修改配置文件 配置文件示例:
# Redis configuration file example.## Note that in order to read the configuration file, Redis must be# started with the file path as first argument:## ./redis-server /path/to/redis.conf# 当你需要为某个配置项指定内存大小的时候,必须要带上单位,# 通常的格式就是 1k 5gb 4m 等:## 1k => 1000 bytes# 1kb => 1024 bytes# 1m => 1000000 bytes# 1mb => 1024*1024 bytes# 1g => 1000000000 bytes# 1gb => 1024*1024*1024 bytes## 单位是不区分大小写的,你写 1K 5GB 4M 也行################################## INCLUDES #################################### 假如说你有一个可用于所有的 redis server 的标准配置模板,# 但针对某些 server 又需要一些个性化的设置,# 你可以使用 include 来包含一些其他的配置文件,这对你来说是非常有用的。## 但是要注意哦,include 是不能被 config rewrite 命令改写的# 由于 redis 总是以最后的加工线作为一个配置指令值,所以你最好是把 include 放在这个文件的最前面,# 以避免在运行时覆盖配置的改变,相反,你就把它放在后面。## include /path/to/local.conf# include /path/to/other.conf################################## NETWORK ###################################### 默认情况下,redis 在 server上所有有效的网络接口上监听客户端连接。# 你如果只想让它在一个网络接口上监听,那你就绑定一个IP或者多个IP。#该处说明bind的是interface,也就是说是网络接口(网卡)。#打个比方说机器上有两个网卡,分别为192.168.205.5 和192.168.205.6,#如果bind 192.168.205.5,那么只有该网卡地址接受外部请求,如果不绑定,则两个网卡口都接受请求。## 示例,多个IP用空格隔开:# bind 192.168.1.100 10.0.0.1
bind 127.0.0.1# Protected mode is a layer of security protection, in order to avoid that# Redis instances left open on the internet are accessed and exploited.## When protected mode is on and if:## 1) The server is not binding explicitly to a set of addresses using the# "bind" directive.# 2) No password is configured.## The server only accepts connections from clients connecting from the# IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain# sockets.## By default protected mode is enabled. You should disable it only if# you are sure you want clients from other hosts to connect to Redis# even if no authentication is configured, nor a specific set of interfaces# are explicitly listed using the "bind" directive.protected-mode no
# 监听端口号,默认为 6379,如果你设为 0 ,redis 将不在 socket 上监听任何客户端连接
port 6379# TCP 监听的最大容纳数量## 在高并发的环境下,你需要把这个值调高以避免客户端连接缓慢的问题。# Linux 内核会一声不响的把这个值缩小成 /proc/sys/net/core/somaxconn 对应的值,# 所以你要修改这两个值才能达到你的预期。
tcp-backlog 511# 指定 unix socket 的路径。## unixsocket /tmp/redis.sock# unixsocketperm 700# 指定在一个 client 空闲多少秒之后关闭连接(0 就是不管它)
timeout 0# tcp 心跳包。## 如果设置为非零,则在与客户端缺乏通讯的时候使用 SO_KEEPALIVE 发送 tcp acks 给客户端。# 这个之所有有用,主要由两个原因:## 1) 防止死的 peers# 2) Take the connection alive from the point of view of network# equipment in the middle.## On Linux, the specified value (in seconds) is the period used to send ACKs.# Note that to close the connection the double of the time is needed.# On other kernels the period depends on the kernel configuration.## A reasonable value for this option is 60 seconds.# 推荐一个合理的值就是60秒
tcp-keepalive 300################################# 通用 ###################################### 默认情况下 redis 不是作为守护进程运行的,如果你想让它在后台运行,你就把它改成 yes。# 当redis作为守护进程运行的时候,它会写一个 pid 到 /var/run/redis.pid 文件里面。
daemonize yes
# If you run Redis from upstart or systemd, Redis can interact with your# supervision tree. Options:# supervised no - no supervision interaction# supervised upstart - signal upstart by putting Redis into SIGSTOP mode# supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET# supervised auto - detect upstart or systemd method based on# UPSTART_JOB or NOTIFY_SOCKET environment variables# Note: these supervision methods only signal "process is ready."# They do not enable continuous liveness pings back to your supervisor.
supervised no
# 当redis作为守护进程运行的时候,它会把 pid 默认写到 /var/run/redis.pid 文件里面,# 但是你可以在这里自己制定它的文件位置。
pidfile "/var/run/redis_6379.pid"# 定义日志级别。# 可以是下面的这些值:# debug (适用于开发或测试阶段)# verbose (many rarely useful info, but not a mess like the debug level)# notice (适用于生产环境)# warning (仅仅一些重要的消息被记录)
loglevel notice
# 指定日志文件的位置
logfile "/home/redis-3.2.6/data/logs/redis.log"# 要想把日志记录到系统日志,就把它改成 yes,# 也可以可选择性的更新其他的syslog 参数以达到你的要求# syslog-enabled no# 设置 syslog 的 identity。# syslog-ident redis# 设置 syslog 的 facility,必须是 USER 或者是 LOCAL0-LOCAL7 之间的值# syslog-facility local0# 设置数据库的数目。# 默认数据库是 DB 0,你可以在每个连接上使用 select <dbid> 命令选择一个不同的数据库,# 但是 dbid 必须是一个介于 0 到 databasees - 1 之间的值
databases 16################################ SNAPSHOTTING ################################## 存 DB 到磁盘:## 格式:save <间隔时间(秒)> <写入次数>## 根据给定的时间间隔和写入次数将数据保存到磁盘## 下面的例子的意思是:# 900 秒内如果至少有 1 个 key 的值变化,则保存# 300 秒内如果至少有 10 个 key 的值变化,则保存# 60 秒内如果至少有 10000 个 key 的值变化,则保存# # 注意:你可以注释掉所有的 save 行来停用保存功能。# 也可以直接一个空字符串来实现停用:# save ""
save 9001
save 30010
save 6010000# 默认情况下,如果 redis 最后一次的后台保存失败,redis 将停止接受写操作,# 这样以一种强硬的方式让用户知道数据不能正确的持久化到磁盘,# 否则就会没人注意到灾难的发生。## 如果后台保存进程重新启动工作了,redis 也将自动的允许写操作。## 然而你要是安装了靠谱的监控,你可能不希望 redis 这样做,那你就改成 no 好了。
stop-writes-on-bgsave-error yes
# 是否在 dump .rdb 数据库的时候使用 LZF 压缩字符串# 默认都设为 yes# 如果你希望保存子进程节省点 cpu ,你就设置它为 no ,# 不过这个数据集可能就会比较大
rdbcompression yes
# 是否校验rdb文件.
rdbchecksum yes
# 设置 dump 的文件位置
dbfilename "dump.rdb"# 工作目录# 例如上面的 dbfilename 只指定了文件名,# 但是它会写入到这个目录下。这个配置项一定是个目录,而不能是文件名。
dir "/home/redis-3.2.6/data/redis-6379"################################# 主从复制 ################################## 主从复制。使用 slaveof 来让一个 redis 实例成为另一个reids 实例的副本。# 注意这个只需要在 slave 上配置。## slaveof <masterip> <masterport># 如果 master 需要密码认证,就在这里设置
masterauth "123456"# 当一个 slave 与 master 失去联系,或者复制正在进行的时候,# slave 可能会有两种表现:## 1) 如果为 yes ,slave 仍然会应答客户端请求,但返回的数据可能是过时,# 或者数据可能是空的在第一次同步的时候## 2) 如果为 no ,在你执行除了 info he salveof 之外的其他命令时,# slave 都将返回一个 "SYNC with master in progress" 的错误,#
slave-serve-stale-data yes
# 你可以配置一个 slave 实体是否接受写入操作。# 通过写入操作来存储一些短暂的数据对于一个 slave 实例来说可能是有用的,# 因为相对从 master 重新同步数而言,据数据写入到 slave 会更容易被删除。# 但是如果客户端因为一个错误的配置写入,也可能会导致一些问题。## 从 redis 2.6 版起,默认 slaves 都是只读的。## Note: read only slaves are not designed to be exposed to untrusted clients# on the internet. It's just a protection layer against misuse of the instance.# Still a read only slave exports by default all the administrative commands# such as CONFIG, DEBUG, and so forth. To a limited extent you can improve# security of read only slaves using 'rename-command' to shadow all the# administrative / dangerous commands.# slave是否为"只读",强烈建议为"yes"
slave-read-only yes
# Replication SYNC strategy: disk or socket.## -------------------------------------------------------# WARNING: DISKLESS REPLICATION IS EXPERIMENTAL CURRENTLY# -------------------------------------------------------## New slaves and reconnecting slaves that are not able to continue the replication# process just receiving differences, need to do what is called a "full# synchronization". An RDB file is transmitted from the master to the slaves.# The transmission can happen in two different ways:## 1) Disk-backed: The Redis master creates a new process that writes the RDB# file on disk. Later the file is transferred by the parent# process