redis应用之安装配置介绍

1、redis介绍:php

一、redis定义:html

 Redis是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。从2010年3月15日起,Redis的开发工做由VMware主持。redis是一个key-value存储系统。和Memcached相似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set --有序集合)和hash(哈希类型)。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操做,并且这些操做都是原子性的。在此基础上,redis支持各类不一样方式的排序。与memcached同样,为了保证效率,数据都是缓存在内存中。区别是redis会周期性的把更新的数据写入磁盘或者把修改操做写入追加的记录文件,而且在此基础上实现了master-slave(主从)同步。Redis 是一个高性能的key-value数据库。redis的出现,很大程度补偿了memcached这类key/value存储的不足,在部分场合能够对关系数据库起到很好的补充做用,它提供了Python,Ruby,Erlang,PHP客户端,使用很方便。node

二、redis存储介绍c++

 redis使用了两种文件格式:全量数据和增量请求。全量数据格式是把内存中的数据写入磁盘,便于下次读取文件进行加载;增量请求文件则是把内存中的数据序列化为操做请求,用于读取文件进行replay获得数据,序列化的操做包括SET、RPUSH、SADD、ZADD。git

三、redis性能:github

下面是官方的bench-mark数据:web

测试完成了50个并发执行100000个请求。redis

设置和获取的值是一个256字节字符串。算法

Linux box是运行Linux 2.6,这是X3320 Xeon 2.5 ghz。数据库

文本执行使用loopback接口(127.0.0.1)。

结果:写的速度是110000次/s,读的速度是81000次/s 。

2、redis单台服务的安装配置

环境:centos 6.6  x86_64

注:有2种安装方式,第一种是直接yum安装;第二种是编译安装

一、使用yum安装(版本偏低2.4.10)

配置epel源
#rpm -ivh http://dl.Fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
#yum install redis -y
启动redis
#service redis start
#chkconfig --add redis
#chkconfig redis on
查看进程
#ps aux | grep redis
redis     4393  0.0  0.3  39936  7092 ?        Ssl  15:01   0:00 /usr/sbin/redis-server /etc/redis.conf
查看版本等信息:
#/usr/bin/redis-cli info
redis_version:2.4.10
redis_git_sha1:00000000
redis_git_dirty:0
arch_bits:64
multiplexing_api:epoll
gcc_version:4.4.6
……………………
查看安装了哪些文件:
#rpm -ql redis
/etc/logrotate.d/redis
/etc/rc.d/init.d/redis
/etc/redis.conf
/usr/bin/redis-benchmark
/usr/bin/redis-check-aof
/usr/bin/redis-check-dump
/usr/bin/redis-cli
/usr/sbin/redis-server
/usr/share/doc/redis-2.4.10
/usr/share/doc/redis-2.4.10/00-RELEASENOTES
/usr/share/doc/redis-2.4.10/BUGS
/usr/share/doc/redis-2.4.10/CONTRIBUTING
/usr/share/doc/redis-2.4.10/COPYING
/usr/share/doc/redis-2.4.10/README
/usr/share/doc/redis-2.4.10/TODO
/var/lib/redis
/var/log/redis
/var/run/redis
查看默认配置文件
#cat /etc/redis.conf   | egrep -v "^#|^$"
daemonize yes    
pidfile /var/run/redis/redis.pid
port 6379
bind 127.0.0.1
timeout 0
loglevel notice
logfile /var/log/redis/redis.log
databases 16
save 900 1
save 300 10
save 60 10000
rdbcompression yes
dbfilename dump.rdb
dir /var/lib/redis/
slave-serve-stale-data yes
appendonly yes
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
slowlog-log-slower-than 10000
slowlog-max-len 1024
vm-enabled no
vm-swap-file /tmp/redis.swap
vm-max-memory 0
vm-page-size 32
vm-pages 134217728
vm-max-threads 4
hash-max-zipmap-entries 512
hash-max-zipmap-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
activerehashing yes

二、使用编译方式安装redis

注:编译安装能够安装比较稳定的高版本,截止到20161010,最新版本是3.2.4 官网http://redis.io/

安装依赖包

#yum install gcc gcc-c++ tcl -y

PS:遇到的问题

a、若是不安装tcl,后面make test的时候回报错,以下:

cd src && make test
make[1]: Entering directory `/root/redis-3.0.3/src'
You need tcl 8.5 or newer in order to run the Redis test
make[1]: *** [test] Error 1
make[1]: Leaving directory `/root/redis-3.0.3/src'
make: *** [test] Error 2

b、make的时候报错(版本3.0.3)

#make
cd src && make all
make[1]: Entering directory `/root/redis-3.0.3/src'
    CC adlist.o
In file included from adlist.c:34:
zmalloc.h:50:31: error: jemalloc/jemalloc.h: No such file or directory
zmalloc.h:55:2: error: #error "Newer version of jemalloc required"
make[1]: *** [adlist.o] Error 1
make[1]: Leaving directory `/root/redis-3.0.3/src'
make: *** [all] Error 2
在README 有这个一段话。
Allocator 
--------- 
Selecting a non-default memory allocator when building Redis is done by setting 
the `MALLOC` environment variable. Redis is compiled and linked against libc 
malloc by default, with the exception of jemalloc being the default on Linux 
systems. This default was picked because jemalloc has proven to have fewer 
fragmentation problems than libc malloc. 
To force compiling against libc malloc, use: 
 % make MALLOC=libc 
To compile against jemalloc on Mac OS X systems, use: 
 % make MALLOC=jemalloc
大概意思是说关于分配器allocator, 若是有MALLOC 这个 环境变量, 会有用这个环境变量的 去创建Redis。
并且libc 并非默认的 分配器, 默认的是 jemalloc, 由于 jemalloc 被证实有更少的fragmentation problems 比libc。
可是若是你又没有jemalloc 而只有 libc 固然 make 出错,因此加这么一个参数。
解决办法:
#make MALLOC=libc   #添加一个参数
………………
Hint: It's a good idea to run 'make test' ;)
make[1]: Leaving directory `/root/redis-3.0.3/src'

开始编译安装redis-3.0.7

#tar xf redis-3.0.7.tar.gz
#cd redis-3.0.7
#make  
…………
Hint: It's a good idea to run 'make test' ;)
make[1]: Leaving directory `/root/redis-3.0.7/src'
#make test
………………
\o/ All tests passed without errors!
Cleanup: may take some time... OK
make[1]: Leaving directory `/root/redis-3.0.7/src'

到这里已经编译安装OK了!

我这里自定义路径,把redis安装到/usr/local/redis/目录下,操做以下:

#mkdir -pv /usr/local/redis/bin
#cd /root/redis-3.0.7/src
#cp  redis-benchmark /usr/local/redis/bin
#cp  redis-check-aof /usr/local/redis/bin
#cp  redis-check-dump /usr/local/redis/bin
#cp  redis-cli /usr/local/redis/bin
#cp  redis-server /usr/local/redis/bin
#cp redis-sentinel /usr/local/redis/bin/
#cp redis-trib.rb /usr/local/redis/bin/
设置环境变量:
#vim /etc/profile
PATH=$PATH:/usr/local/redis/bin
#source /etc/profile
建立conf目录:
#mkdir /usr/local/redis/conf
建立log目录
#mkdir -pv /usr/local/redis/log/
建立配置文件:
#cp /root/redis-3.0.7/redis.conf  /usr/local/redis/conf
#cp /root/redis-3.0.7/sentinel.conf  /usr/local/redis/conf

配置文件根据需求修改成以下:

logfile "/usr/local/redis/log/redis.log"
daemonize no  --改成yes,让redis在后台运行
pidfile /var/run/redis.pid  
port 6379    --端口能够自定义,默认是6379
bind 10.0.18.145 127.0.0.1 --绑定地址,本机IP
dbfilename dump.rdb        --rdb名称
dir /usr/local/redis/      --数据存放路径
appendonly yes             --开启aof日志
databases 16               --数据库数目

其余参数根据须要修改!

注:appendonly选项就是负责是否开启AOF日志的开关.AOF日志,你能够简单理解为MySQL binlog同样的东西,做用就是记录每次的写操做,在遇到断电等问题时能够用它来恢复数据库状态。可是他不是bin的,而是text的.一行一行,写得很规范.若是你是一台redis,那你也能经过它恢复数据.

系统参数设置:

设置打开文件的最大数

#vim /etc/security/limits.conf 
* soft nofile 65535
* hard nofile 65535
* soft    nproc     10240         
* hard    nproc     10240

启动redis

#nohup /usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf &  #在后台运行
启动以后,查看是否运行正常 
#netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      4628/sshd           
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      1101/master                 
tcp        0      0 127.0.0.1:6379              0.0.0.0:*                   LISTEN      9514/redis-server 1 
tcp        0      0 10.0.18.145:6379            0.0.0.0:*                   LISTEN      9514/redis-server 1          
tcp        0      0 :::22                       :::*                        LISTEN      4628/sshd           
tcp        0      0 ::1:25                      :::*                        LISTEN      1101/master         
注:启动redis以后看到的日志:
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.
'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
 WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
从上述信息能够看出,应该是须要优化一些系统参数
#vi /etc/sysctl.conf    #添加或者修改参数
vm.overcommit_memory = 1
net.core.somaxconn = 511
vm.swappiness = 0
net.ipv4.neigh.default.gc_stale_time=120
net.ipv4.conf.all.rp_filter=0
net.ipv4.conf.default.rp_filter=0
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.all.arp_announce=2
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 1024    #默认值
net.ipv4.tcp_synack_retries = 2
net.ipv4.conf.lo.arp_announce=2
注:net.ipv4.tcp_max_syn_backlog 参数决定了SYN_RECV状态队列的数量,通常默认值为512或者1024,即超过这个数量,系统将再也不接受新的TCP链接请求,
必定程度上能够防止系统资源耗尽。可根据状况增长该值以接受更多的链接请求。centos系统默认是1024
查看修改以前
#cat /sys/kernel/mm/transparent_hugepage/enabled
[always] madvise never
#echo never > /sys/kernel/mm/transparent_hugepage/enabled  #修改成never
#cat  /sys/kernel/mm/transparent_hugepage/enabled
always madvise [never]
而后重启redis,就不会提示以前的WARNING了!

对redis进行一个简单的压力测试,以下:

#redis-benchmark -t set -c 20 -n 1000000 -r 100000000
====== SET ======
  1000000 requests completed in 17.69 seconds
  20 parallel clients
  3 bytes payload
  keep alive: 1

99.99% <= 1 milliseconds
100.00% <= 1 milliseconds
56516.33 requests per second

3、redis配置文件参数和经常使用命令介绍

一、配置文件参数,以下:

#cat 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 也行
# 假如说你有一个可用于全部的 redis server 的标准配置模板,
# 但针对某些 server 又须要一些个性化的设置,
# 你可使用 include 来包含一些其余的配置文件,这对你来讲是很是有用的。
# 可是要注意,include 是不能被 config rewrite 命令改写的
# 因为 redis 老是以最后的加工线做为一个配置指令值,因此你最好是把 include 放在这个文件的最前面,
# 以免在运行时覆盖配置的改变,相反,你就把它放在后面。
# include /path/to/local.conf
# include /path/to/other.conf
################################ 经常使用 #####################################
# 默认状况下 redis 不是做为守护进程运行的,若是你想让它在后台运行,你就把它改为 yes。
daemonize no
# 当redis做为守护进程运行的时候,它会把 pid 默认写到 /var/run/redis.pid 文件里面,
# 可是你能够在这里自定义它的文件位置。
pidfile /var/run/redis.pid
# 监听端口号,默认为 6379,若是你设为0,redis 将不在 socket 上监放任何客户端链接,也能够自定义端口
port 6379
# TCP 监听的最大容纳数量
# 在高并发的环境下,你须要把这个值调高以免客户端链接缓慢的问题。
# Linux 内核会一言不发的把这个值缩小成 /proc/sys/net/core/somaxconn 对应的值,
# 因此你要修改somaxconn和tcp_max_syn_backlog这两个值才能达到你的预期。
tcp-backlog 511
# 默认状况下,redis 在 server 上全部有效的网络接口上监听客户端链接。
# 你若是只想让它在一个网络接口上监听,那你就绑定一个IP或者多个IP。
# 示例,多个IP用空格隔开:
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1
# 指定 unix socket 的路径。
# unixsocket /tmp/redis.sock
# unixsocketperm 755
# 指定在一个 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 0
# 定义日志级别。
# 能够是下面的这些值:
# debug (适用于开发或测试阶段)
# verbose (many rarely useful info, but not a mess like the debug level)
# notice (适用于生产环境)
# warning (仅仅一些重要的消息被记录)
loglevel notice
# 指定日志文件的位置
logfile "/var/log/redis.log"
# 要想把日志记录到系统日志,就把它改为 yes,
# 也能够可选择性的更新其余的syslog 参数以达到你的要求
# syslog-enabled no
# 设置 syslog 的 identity。
# syslog-ident redis
# 设置 syslog 的 facility,必须是 USER 或者是 LOCAL0-LOCAL7 之间的值。
# syslog-facility local0
# 设置数据库的数目。
# 默认数据库是 DB 0,你能够在每一个链接上使用 select <dbid> 命令选择一个不一样的数据库,
# 可是 db id 必须是一个介于 0 到 databasees - 1 之间的值
databases 16
################################ 快照 ################################
#
# 存 DB 到磁盘:
#
#   格式:save <间隔时间(秒)> <写入次数>
#
#   根据给定的时间间隔和写入次数将数据保存到磁盘
#
#   下面的例子的意思是:
#   900 秒内若是至少有 1 个 key 的值变化,则保存
#   300 秒内若是至少有 10 个 key 的值变化,则保存
#   60 秒内若是至少有 10000 个 key 的值变化,则保存
#  
#   注意:你能够注释掉全部的 save 行来停用保存功能。
#   也能够直接一个空字符串来实现停用:
#   save ""
save 900 1
save 300 10
save 60 10000
# 默认状况下,若是 redis 最后一次的后台保存失败,redis 将中止接受写操做,
# 这样以一种强硬的方式让用户知道数据不能正确的持久化到磁盘,不然就会没人注意到灾难的发生。
# 若是后台保存进程从新启动工做了,redis也将自动的容许写操做。
补充:
redis的存储分为内存存储、磁盘存储和log文件三部分,配置文件中有三个参数对其进行配置。
由于redis自己同步数据文件是按上面的save条件来同步的,因此有的数据会在一段时间内只存在于内存中。开启的话每次写操做会记一条log,这会提升数据抗风险能力,但影响效率。

#然而若是你已经设置了对redis服务器的正确监视和持久性,你可能要禁用此功能,以便redis将继续正常工做
即便有磁盘、权限等问题,说白点就是有任何问题致使的bgsave失败都中止redis的对外服务。默认yes,能够设置为no
stop-writes-on-bgsave-error yes
# 是否在dump .rdb 数据库的时候使用 LZF 压缩字符串,默认都设为 yes
# 若是你但愿保存子进程节省点 cpu ,你就设置它为 no,不过这个数据集可能就会比较大
rdbcompression yes
# 是否校验rdb文件
rdbchecksum yes
# 设置dump的文件名
dbfilename dump.rdb
# 工做目录
# 例如上面的 dbfilename 只指定了文件名,
# 可是它会写入到这个目录下。这个配置项必定是个目录,而不能是文件名。
dir ./    #能够自定义,好比dir /usr/local/redis/
################################# 主从复制 #################################
# 主从复制。使用slaveof来让一个redis实例成为另外一个reids实例的副本。
关于Redis复制的几个事情
# 1)redis复制是异步的,可是若是它看起来没有与至少给定数量的slave链接,你能够将master配置为中止接受写入
# 2)若是复制连接丢失相对较少的时间,Redis从服务器可以与主设备执行部分从新同步。 
# 你可能须要根据你的须要使用合理的值配置复制积压大小(请参阅此文件的下一部分)
# 3)复制是自动的,不须要用户干预。网络分区后,从服务器自动尝试从新链接到主服务器并与其同步。
#
# 设置当本机为slave服务时,设置master服务的IP地址及端口,在Redis启动时,它会自动从master进行数据同步
# slaveof <masterip> <masterport>
# 若是 master 须要密码认证,就在这里设置
# masterauth <master-password>
# 当一个 slave 与 master 失去联系,或者复制正在进行的时候,
# slave 可能会有两种表现:
# 1) 若是slave-serve-stale-data设置为 yes ,slave仍然会应答客户端请求,但返回的数据多是过期,
# 或者数据多是空的在第一次同步的时候
#
# 2) 若是slave-serve-stale-data设置为 no ,在你执行除了info 和slaveof 以外的其余命令时,
#    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.
# 注意:只读的 slaves 没有被设计成在 internet 上暴露给不受信任的客户端。
# 它仅仅是一个针对误用实例的一个保护层。
# 从服务器只读设置,默认是yes
slave-read-only yes
# 复制SYNC策略:磁盘或套接字。
# 使用慢磁盘和快速(大带宽)网络,无盘复制的效果更好
repl-diskless-sync no
#当启用无磁盘复制时,能够配置服务器等待的延迟,以便生成将RDP经过套接字传输到从属设备的子节点。
#
#延迟时间以秒为单位,默认值为5秒。 要彻底禁用它只是设置为0秒,传输将尽快启动。
repl-diskless-sync-delay 5
#
# Slaves 在一个预约义的时间间隔内发送 ping 命令到 server 。
# 你能够改变这个时间间隔。默认为 10 秒。
# repl-ping-slave-period 10
# The following option sets the replication timeout for:
# 设置主从复制过时时间
# 1) Bulk transfer I/O during SYNC, from the point of view of slave.
# 2) Master timeout from the point of view of slaves (data, pings).
# 3) Slave timeout from the point of view of masters (REPLCONF ACK pings).
#
# It is important to make sure that this value is greater than the value
# specified for repl-ping-slave-period otherwise a timeout will be detected
# every time there is low traffic between the master and the slave.
# 这个值必定要比 repl-ping-slave-period 大
#
# repl-timeout 60
# Disable TCP_NODELAY on the slave socket after SYNC?
#
# If you select "yes" Redis will use a smaller number of TCP packets and
# less bandwidth to send data to slaves. But this can add a delay for
# the data to appear on the slave side, up to 40 milliseconds with
# Linux kernels using a default configuration.
#
# If you select "no" the delay for data to appear on the slave side will
# be reduced but more bandwidth will be used for replication.
#
# By default we optimize for low latency, but in very high traffic conditions
# or when the master and slaves are many hops away, turning this to "yes" may
# be a good idea.
默认状况下,咱们针对低延迟进行优化,但在很是高的流量条件下或当主设备和从设备有许多跳时,
将其转换为“yes”多是一个好主意。因此默认是no
repl-disable-tcp-nodelay no
# 设置主从复制容量大小。这个 backlog 是一个用来在 slaves 被断开链接时
# 存放 slave 数据的 buffer,因此当一个 slave 想要从新链接,一般不但愿所有从新同步,
# 只是部分同步就够了,仅仅传递 slave 在断开链接时丢失的这部分数据。
#
# The biggest the replication backlog, the longer the time the slave can be
# disconnected and later be able to perform a partial resynchronization.
# 这个值越大,salve 能够断开链接的时间就越长。
#
# The backlog is only allocated once there is at least a slave connected.
#
# repl-backlog-size 1mb
# After a master has no longer connected slaves for some time, the backlog
# will be freed. The following option configures the amount of seconds that
# need to elapse, starting from the time the last slave disconnected, for
# the backlog buffer to be freed.
# 在某些时候,master 再也不链接 slaves,backlog 将被释放。
#
# A value of 0 means to never release the backlog.
# 若是设置为 0 ,意味着毫不释放 backlog 。
#
# repl-backlog-ttl 3600
# The slave priority is an integer number published by Redis in the INFO output.
# It is used by Redis Sentinel in order to select a slave to promote into a
# master if the master is no longer working correctly.
# 从属优先级是Redis在INFO输出中发布的整数。若是主服务器再也不正确工做,则Redis Sentinel
# 使用它来选择从服务器升级为主服务器。
# A slave with a low priority number is considered better for promotion, so
# for instance if there are three slaves with priority 10, 100, 25 Sentinel will
# pick the one with priority 10, that is the lowest.
# 具备低优先级号的从服务器被认为更好地用于推广,因此例如,若是存在具备优先级10,100,25的三个从服务器
# 则哨兵将选择具备优先级10的那台,即最低的那个。
# However a special priority of 0 marks the slave as not able to perform the
# role of master, so a slave with priority of 0 will never be selected by
# Redis Sentinel for promotion.
# 当master不能正常工做的时候,Redis Sentinel 会从 slaves 中选出一个新的 master,
# 这个值越小,就越会被优先选中,可是若是是 0 ,那是意味着这个 slave 不可能被选中。
# 默认优先级为 100。
slave-priority 100
# It is possible for a master to stop accepting writes if there are less than
# N slaves connected, having a lag less or equal than M seconds.
# 若是链接少于N个slave服务器,具备小于或等于M秒的滞后,则master能够中止接受写入
# The N slaves need to be in "online" state.
# 这N个slave服务器必须是处于online状态
# The lag in seconds, that must be <= the specified value, is calculated from
# the last ping received from the slave, that is usually sent every second.
#
# This option does not GUARANTEES that N replicas will accept the write, but
# will limit the window of exposure for lost writes in case not enough slaves
# are available, to the specified number of seconds.
#
# 此选项不保证N个副本将接受写入,但将在没有足够的slave可用的状况下将丢失写入的曝光窗口限制为指定的秒数。
# For example to require at least 3 slaves with a lag <= 10 seconds use:
#
# min-slaves-to-write 3
# min-slaves-max-lag 10
# 也就是说若是启用以上2个选项,master只有当有N个slaves处于链接状态的状况下才接受写操做
# Setting one or the other to 0 disables the feature.
#
# By default min-slaves-to-write is set to 0 (feature disabled) and
# min-slaves-max-lag is set to 10.
# 默认状况下,min-slave-to-write设置为0(禁用功能),min-slaves-max-lag设置为10。
################################## 安全 ###################################
 
# Require clients to issue AUTH <PASSWORD> before processing any other
# commands.  This might be useful in environments in which you do not trust
# others with access to the host running redis-server.
#
# This should stay commented out for backward compatibility and because most
# people do not need auth (e.g. they run their own servers).
# 
# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
# 
# 设置认证密码,这里的密码是加密后的加密串!
# requirepass foobared
# Command renaming.
#
# It is possible to change the name of dangerous commands in a shared
# environment. For instance the CONFIG command may be renamed into something
# hard to guess so that it will still be available for internal-use tools
# but not available for general clients.
#能够在共享环境中更改危险命令的名称。 例如,CONFIG命令能够重命名为难以猜想的东西,
#以便它仍然可用于内部使用工具,但不能用于通常客户端。
# Example: 好比
#
# rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52
#
# It is also possible to completely kill a command by renaming it into
# an empty string:
也能够经过将命令重命名为空字符串来彻底杀死命令:
# rename-command CONFIG ""
#
# Please note that changing the name of commands that are logged into the
# AOF file or transmitted to slaves may cause problems.
################################### 限制 ####################################
# Set the max number of connected clients at the same time. By default
# this limit is set to 10000 clients, however if the Redis server is not
# able to configure the process file limit to allow for the specified limit
# the max number of allowed clients is set to the current file limit
# minus 32 (as Redis reserves a few file descriptors for internal uses).
#
# 一旦达到最大限制,redis 将关闭全部的新链接,并发送一个‘max number of clients reached’的错误。
# maxclients 10000
# 不要使用比指定的字节数更多的内存。
# 若是你设置了这个值,当缓存的数据容量达到这个值, redis 将根据你选择的
# eviction 策略来移除一些 keys。
#
# 若是 redis 不能根据策略移除 keys ,或者是策略被设置为 ‘noeviction’,
# redis 将开始响应错误给命令,如 set,lpush 等等,
# 并继续响应只读的命令,如 get
#
# This option is usually useful when using Redis as an LRU cache, or to set
# a hard memory limit for an instance (using the 'noeviction' policy).
#
# WARNING: If you have slaves attached to an instance with maxmemory on,
# the size of the output buffers needed to feed the slaves are subtracted
# from the used memory count, so that network problems / resyncs will
# not trigger a loop where keys are evicted, and in turn the output
# buffer of slaves is full with DELs of keys evicted triggering the deletion
# of more keys, and so forth until the database is completely emptied.
#
# In short... if you have slaves attached it is suggested that you set a lower
# limit for maxmemory so that there is some free RAM on the system for slave
# output buffers (but this is not needed if the policy is 'noeviction').
#
# 最大使用内存
# maxmemory <bytes> 
# maxmemory 3GB
# 最大内存策略,你有 5 个选择。
# 
# volatile-lru -> remove the key with an expire set using an LRU algorithm
# 使用 LRU 算法移除包含过时设置的 key,通常生产环境使用这个策略
# allkeys-lru -> remove any key accordingly to the LRU algorithm
# 根据 LRU 算法移除全部的 key 。
# volatile-random -> remove a random key with an expire set 
# 删除带有过时集的随机密钥
# allkeys-random -> remove a random key, any key
# 移除一个随机key,任意key
# volatile-ttl -> remove the key with the nearest expire time (minor TTL)
# 删除具备最近过时时间的密钥(次TTL)
# noeviction -> don't expire at all, just return an error on write operations
# 不让任何 key 过时,只是在写入操做时返回一个错误
# 
# Note: with any of the above policies, Redis will return an error on write
#       operations, when there are not suitable keys for eviction.
#
#       At the date of writing this commands are: set setnx setex append
#       incr decr rpush lpush rpushx lpushx linsert lset rpoplpush sadd
#       sinter sinterstore sunion sunionstore sdiff sdiffstore zadd zincrby
#       zunionstore zinterstore hset hsetnx hmset hincrby incrby decrby
#       getset mset msetnx exec sort
#
# The default is:
# 默认设置是  noeviction
# maxmemory-policy noeviction
能够根据状况自定义策略,好比
maxmemory-policy volatile-lru
# LRU and minimal TTL algorithms are not precise algorithms but approximated
# algorithms (in order to save memory), so you can tune it for speed or
# accuracy. For default Redis will check five keys and pick the one that was
# used less recently, you can change the sample size using the following
# configuration directive.
#
# The default of 5 produces good enough results. 10 Approximates very closely
# true LRU but costs a bit more CPU. 3 is very fast but not very accurate.
# 默认值为5产生足够好的结果。 10近似很是接近真实的LRU,但成本更多的CPU。 3是很是快,但不是很准确。
# maxmemory-samples 5  #因此通常都是默认
############################## APPEND ONLY 模式 ###############################
# By default Redis asynchronously dumps the dataset on disk. This mode is
# good enough in many applications, but an issue with the Redis process or
# a power outage may result into a few minutes of writes lost (depending on
# the configured save points).
#默认状况下,Redis异步地转储磁盘上的数据集。 这种模式在许多应用程序中是足够好的,
 可是Redis进程或断电的问题可能致使几分钟的写入丢失(取决于配置的保存点)。
# The Append Only File is an alternative persistence mode that provides
# much better durability. For instance using the default data fsync policy
# (see later in the config file) Redis can lose just one second of writes in a
# dramatic event like a server power outage, or a single write if something
# wrong with the Redis process itself happens, but the operating system is
# still running correctly.
# 仅附加文件是可提供更好耐久性的替代持久性模式。 例如,使用默认数据fsync策略(见后面的配置文件)
# Redis在一个戏剧性的事件,如服务器断电,丢失只有一秒的写入,或一个单一的写若是Redis进程自己发生错误,
# 可是操做系统仍然正常运行。
# AOF and RDB persistence can be enabled at the same time without problems.
# If the AOF is enabled on startup Redis will load the AOF, that is the file
# with the better durability guarantees.
# AOF和RDB持久性能够同时启用而没有问题。 若是在启动时启用AOF,Redis将加载AOF,这是具备更好的持久性保证的文件。
# Please check http://redis.io/topics/persistence for more information. 
appendonly no    ##aof日志的类型默认是no,若是要保持更好的数据持久性,设置为yes
补充:appendonly yes/no  appendonly配置,指出是否在每次更新操做后进行日志记录,若是不开启,可能会在断电时致使一段时间内的数据丢失。
# The name of the append only file (default: "appendonly.aof")
仅追加文件的名称(默认值:“appendonly.aof”)
appendfilename "appendonly.aof"
# The fsync() call tells the Operating System to actually write data on disk
# instead to wait for more data in the output buffer. Some OS will really flush 
# data on disk, some other OS will just try to do it ASAP.(as soon as possible)
#
# Redis supports three different modes:
#
# no: don't fsync, just let the OS flush the data when it wants. Faster.
# always: fsync after every write to the append only log . Slow, Safest.
# everysec: fsync only one time every second. Compromise.
#
# The default is "everysec", as that's usually the right compromise between
# speed and data safety. It's up to you to understand if you can relax this to
# "no" that will let the operating system flush the output buffer when
# it wants, for better performances (but if you can live with the idea of
# some data loss consider the default persistence mode that's snapshotting),
# or on the contrary, use "always" that's very slow but a bit safer than
# everysec.
#
# More details please check the following article:
# http://antirez.com/post/redis-persistence-demystified.html
#
# If unsure, use "everysec".
# appendfsync always
appendfsync everysec    #默认everysec便可
# appendfsync no
注:appendfsync no/always/everysec ,appendfsync是Redis将OS数据缓冲区中数据刷新到磁盘的策略
no表示操做系统进行数据缓存同步到磁盘,always表示每次更新操做后手动调用fsync()将数据写到磁盘,everysec表示每秒同步一次,默认是everysec。
# When the AOF fsync policy is set to always or everysec, and a background
# saving process (a background save or AOF log background rewriting) is
# performing a lot of I/O against the disk, in some Linux configurations
# Redis may block too long on the fsync() call. Note that there is no fix for
# this currently, as even performing fsync in a different thread will block
# our synchronous write(2) call.
# In order to mitigate this problem it's possible to use the following option
# that will prevent fsync() from being called in the main process while a
# BGSAVE or BGREWRITEAOF is in progress.
# This means that while another child is saving, the durability of Redis is
# the same as "appendfsync none". In practical terms, this means that it is
# possible to lose up to 30 seconds of log in the worst scenario (with the
# default Linux settings).
# 
# If you have latency problems turn this to "yes". Otherwise leave it as
# "no" that is the safest pick from the point of view of durability.
若是有延迟问题,请将其设置为“yes”。 不然,从耐久性的角度看,它是“no”,这是最安全的选择。
no-appendfsync-on-rewrite no
# Automatic rewrite of the append only file.
# Redis is able to automatically rewrite the log file implicitly calling
# BGREWRITEAOF when the AOF log size grows by the specified percentage.
# 
# This is how it works: Redis remembers the size of the AOF file after the
# latest rewrite (if no rewrite has happened since the restart, the size of
# the AOF at startup is used).
#
# This base size is compared to the current size. If the current size is
# bigger than the specified percentage, the rewrite is triggered. Also
# you need to specify a minimal size for the AOF file to be rewritten, this
# is useful to avoid rewriting the AOF file even if the percentage increase
# is reached but it is still pretty small.
#
# Specify a percentage of zero in order to disable the automatic AOF
# rewrite feature.
  指定一个百分比为零,以禁用自动AOF重写功能。
auto-aof-rewrite-percentage 100    
auto-aof-rewrite-min-size 64mb

# An AOF file may be found to be truncated at the end during the Redis
# startup process, when the AOF data gets loaded back into memory.
# This may happen when the system where Redis is running
# crashes, especially when an ext4 filesystem is mounted without the
# data=ordered option (however this can't happen when Redis itself
# crashes or aborts but the operating system still works correctly).
#
# Redis can either exit with an error when this happens, or load as much
# data as possible (the default now) and start if the AOF file is found
# to be truncated at the end. The following option controls this behavior.
#
# If aof-load-truncated is set to yes, a truncated AOF file is loaded and
# the Redis server starts emitting a log to inform the user of the event.
# Otherwise if the option is set to no, the server aborts with an error
# and refuses to start. When the option is set to no, the user requires
# to fix the AOF file using the "redis-check-aof" utility before to restart
# the server.
#
# Note that if the AOF file will be found to be corrupted in the middle
# the server will still exit with an error. This option only applies when
# Redis will try to read more data from the AOF file but not enough bytes
# will be found.
请注意,若是AOF文件将被发如今中间被损坏,服务器仍将退出并出现错误。此选项仅在Redis尝试从AOF文件读取更多数据但找不到足够的字节时适用。
aof-load-truncated yes
################################ LUA SCRIPTING  ###############################
# Max execution time of a Lua script in milliseconds.
#
# If the maximum execution time is reached Redis will log that a script is
# still in execution after the maximum allowed time and will start to
# reply to queries with an error.
#
# When a long running script exceed the maximum execution time only the
# SCRIPT KILL and SHUTDOWN NOSAVE commands are available. The first can be
# used to stop a script that did not yet called write commands. The second
# is the only way to shut down the server in the case a write commands was
# already issue by the script but the user don't want to wait for the natural
# termination of the script.
#
# Set it to 0 or a negative value for unlimited execution without warnings.
lua-time-limit 5000 
################################ REDIS 集群  ###############################
#
# 启用或停用集群
# cluster-enabled yes
# Every cluster node has a cluster configuration file. This file is not
# intended to be edited by hand. It is created and updated by Redis nodes.
# Every Redis Cluster node requires a different cluster configuration file.
# Make sure that instances running in the same system does not have
# overlapping cluster configuration file names.
# 集群配置文件
# cluster-config-file nodes-6379.conf
# Cluster node timeout is the amount of milliseconds a node must be unreachable 
# for it to be considered in failure state.
# Most other internal time limits are multiple of the node timeout.
# 集群节点超时时间
# cluster-node-timeout 15000
# A slave of a failing master will avoid to start a failover if its data
# looks too old.
# There is no simple way for a slave to actually have a exact measure of
# its "data age", so the following two checks are performed:
#
# 1) If there are multiple slaves able to failover, they exchange messages
#    in order to try to give an advantage to the slave with the best
#    replication offset (more data from the master processed).
#    Slaves will try to get their rank by offset, and apply to the start
#    of the failover a delay proportional to their rank.
#
# 2) Every single slave computes the time of the last interaction with
#    its master. This can be the last ping or command received (if the master
#    is still in the "connected" state), or the time that elapsed since the
#    disconnection with the master (if the replication link is currently down).
#    If the last interaction is too old, the slave will not try to failover
#    at all.
#
# The point "2" can be tuned by user. Specifically a slave will not perform
# the failover if, since the last interaction with the master, the time
# elapsed is greater than:
#
#   (node-timeout * slave-validity-factor) + repl-ping-slave-period
#
# So for example if node-timeout is 30 seconds, and the slave-validity-factor
# is 10, and assuming a default repl-ping-slave-period of 10 seconds, the
# slave will not try to failover if it was not able to talk with the master
# for longer than 310 seconds.
#
# A large slave-validity-factor may allow slaves with too old data to failover
# a master, while a too small value may prevent the cluster from being able to
# elect a slave at all.
#
# For maximum availability, it is possible to set the slave-validity-factor
# to a value of 0, which means, that slaves will always try to failover the
# master regardless of the last time they interacted with the master.
# (However they'll always try to apply a delay proportional to their
# offset rank).
#
# Zero is the only value able to guarantee that when all the partitions heal
# the cluster will always be able to continue.
#
# cluster-slave-validity-factor 10
# Cluster slaves are able to migrate to orphaned masters, that are masters
# that are left without working slaves. This improves the cluster ability
# to resist to failures as otherwise an orphaned master can't be failed over
# in case of failure if it has no working slaves.
#
# Slaves migrate to orphaned masters only if there are still at least a
# given number of other working slaves for their old master. This number
# is the "migration barrier". A migration barrier of 1 means that a slave
# will migrate only if there is at least 1 other working slave for its master
# and so forth. It usually reflects the number of slaves you want for every
# master in your cluster.
#
# Default is 1 (slaves migrate only if their masters remain with at least
# one slave). To disable migration just set it to a very large value.
# A value of 0 can be set but is useful only for debugging and dangerous
# in production.
#
# cluster-migration-barrier 1
# In order to setup your cluster make sure to read the documentation
# available at http://redis.io web site.
################################## SLOW LOG ###################################
# The Redis Slow Log is a system to log queries that exceeded a specified
# execution time. The execution time does not include the I/O operations
# like talking with the client, sending the reply and so forth,
# but just the time needed to actually execute the command (this is the only
# stage of command execution where the thread is blocked and can not serve
# other requests in the meantime).
# You can configure the slow log with two parameters: one tells Redis
# what is the execution time, in microseconds, to exceed in order for the
# command to get logged, and the other parameter is the length of the
# slow log. When a new command is logged the oldest one is removed from the
# queue of logged commands.
# The following time is expressed in microseconds, so 1000000 is equivalent
# to one second. Note that a negative number disables the slow log, while
# a value of zero forces the logging of every command.
slowlog-log-slower-than 10000   
# There is no limit to this length. Just be aware that it will consume memory.
# You can reclaim memory used by the slow log with SLOWLOG RESET.
slowlog-max-len 128
############################# Event notification ##############################
# Redis can notify Pub/Sub clients about events happening in the key space.
# This feature is documented at http://redis.io/topics/keyspace-events
# 
# For instance if keyspace events notification is enabled, and a client
# performs a DEL operation on key "foo" stored in the Database 0, two
# messages will be published via Pub/Sub:
#
# PUBLISH __keyspace@0__:foo del
# PUBLISH __keyevent@0__:del foo
#
# It is possible to select the events that Redis will notify among a set
# of classes. Every class is identified by a single character:
#
#  K     Keyspace events, published with __keyspace@<db>__ prefix.
#  E     Keyevent events, published with __keyevent@<db>__ prefix.
#  g     Generic commands (non-type specific) like DEL, EXPIRE, RENAME, ...
#  $     String commands
#  l     List commands
#  s     Set commands
#  h     Hash commands
#  z     Sorted set commands
#  x     Expired events (events generated every time a key expires)
#  e     Evicted events (events generated when a key is evicted for maxmemory)
#  A     Alias for g$lshzxe, so that the "AKE" string means all the events.
#
#  The "notify-keyspace-events" takes as argument a string that is composed
#  by zero or multiple characters. The empty string means that notifications
#  are disabled at all.
#
#  Example: to enable list and generic events, from the point of view of the
#           event name, use:
#
#  notify-keyspace-events Elg
#
#  Example 2: to get the stream of the expired keys subscribing to channel
#             name __keyevent@0__:expired use:
#
#  notify-keyspace-events Ex
#
#  By default all notifications are disabled because most users don't need
#  this feature and the feature has some overhead. Note that if you don't
#  specify at least one of K or E, no events will be delivered.
notify-keyspace-events ""
############################### ADVANCED CONFIG ###############################
# Hashes are encoded using a memory efficient data structure when they have a
# small number of entries, and the biggest entry does not exceed a given
# threshold. These thresholds can be configured using the following directives.
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
# Similarly to hashes, small lists are also encoded in a special way in order
# to save a lot of space. The special representation is only used when
# you are under the following limits:
list-max-ziplist-entries 512
list-max-ziplist-value 64
# Sets have a special encoding in just one case: when a set is composed
# of just strings that happens to be integers in radix 10 in the range
# of 64 bit signed integers.
# The following configuration setting sets the limit in the size of the
# set in order to use this special memory saving encoding.
set-max-intset-entries 512
# Similarly to hashes and lists, sorted sets are also specially encoded in
# order to save a lot of space. This encoding is only used when the length and
# elements of a sorted set are below the following limits:
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
# HyperLogLog sparse representation bytes limit. The limit includes the
# 16 bytes header. When an HyperLogLog using the sparse representation crosses
# this limit, it is converted into the dense representation.
#
# A value greater than 16000 is totally useless, since at that point the
# dense representation is more memory efficient.
# 
# The suggested value is ~ 3000 in order to have the benefits of
# the space efficient encoding without slowing down too much PFADD,
# which is O(N) with the sparse encoding. The value can be raised to
# ~ 10000 when CPU is not a concern, but space is, and the data set is
# composed of many HyperLogLogs with cardinality in the 0 - 15000 range.
hll-sparse-max-bytes 3000
# Active rehashing uses 1 millisecond every 100 milliseconds of CPU time in
# order to help rehashing the main Redis hash table (the one mapping top-level
# keys to values). The hash table implementation Redis uses (see dict.c)
# performs a lazy rehashing: the more operation you run into a hash table
# that is rehashing, the more rehashing "steps" are performed, so if the
# server is idle the rehashing is never complete and some more memory is used
# by the hash table.
# 
# The default is to use this millisecond 10 times every second in order to
# active rehashing the main dictionaries, freeing memory when possible.
#
# If unsure:
# use "activerehashing no" if you have hard latency requirements and it is
# not a good thing in your environment that Redis can reply form time to time
# to queries with 2 milliseconds delay.
#
# use "activerehashing yes" if you don't have such hard requirements but
# want to free memory asap when possible.
activerehashing yes
# The client output buffer limits can be used to force disconnection of clients
# that are not reading data from the server fast enough for some reason (a
# common reason is that a Pub/Sub client can't consume messages as fast as the
# publisher can produce them).
# The limit can be set differently for the three different classes of clients:
# normal -> normal clients
# slave  -> slave clients and MONITOR clients
# pubsub -> clients subscribed to at least one pubsub channel or pattern
#
# The syntax of every client-output-buffer-limit directive is the following:
#
# client-output-buffer-limit <class> <hard limit> <soft limit> <soft seconds>
#
# A client is immediately disconnected once the hard limit is reached, or if
# the soft limit is reached and remains reached for the specified number of
# seconds (continuously).
# So for instance if the hard limit is 32 megabytes and the soft limit is
# 16 megabytes / 10 seconds, the client will get disconnected immediately
# if the size of the output buffers reach 32 megabytes, but will also get
# disconnected if the client reaches 16 megabytes and continuously overcomes
# the limit for 10 seconds.
#
# By default normal clients are not limited because they don't receive data
# without asking (in a push way), but just after a request, so only
# asynchronous clients may create a scenario where data is requested faster
# than it can read.
#
# Instead there is a default limit for pubsub and slave clients, since
# subscribers and slaves receive data in a push fashion.
#
# Both the hard or the soft limit can be disabled by setting them to zero.
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60 
# Redis calls an internal function to perform many background tasks, like
# closing connections of clients in timeout, purging expired keys that are
# never requested, and so forth.
#
# Not all tasks are performed with the same frequency, but Redis checks for
# tasks to perform accordingly to the specified "hz" value.
#
# By default "hz" is set to 10. Raising the value will use more CPU when
# Redis is idle, but at the same time will make Redis more responsive when
# there are many keys expiring at the same time, and timeouts may be
# handled with more precision.
#
# The range is between 1 and 500, however a value over 100 is usually not
# a good idea. Most users should use the default of 10 and raise this up to
# 100 only in environments where very low latency is required.
hz 10
# When a child rewrites the AOF file, if the following option is enabled
# the file will be fsync-ed every 32 MB of data generated. This is useful
# in order to commit the file to the disk more incrementally and avoid
# big latency spikes.
aof-rewrite-incremental-fsync yes

二、redis经常使用命令

进入redis进行操做:

#redis-cli -h 10.0.18.145 -p 6379
10.0.18.145:6379> 
也能够这样,其实和上面是同样的
#redis-cli  -p 6379
127.0.0.1:6379> 
127.0.0.1:6379> exit  #退出
127.0.0.1:6379> info  #能够查看全部redis相关的信息
127.0.0.1:6379> help  #查看帮助信息
redis-cli 3.0.7
Type: "help @<group>" to get a list of commands in <group>
      "help <command>" for help on <command>
      "help <tab>" to get a list of possible help topics
      "quit" to exit
10.0.18.145:6379> set name curry   #设置一个key的值为curry
OK
10.0.18.145:6379> get name         #查看一个key的值
"curry"
10.0.18.145:6379> exists name      #key存在返回非0
(integer) 1
10.0.18.145:6379> del name         #删除key
(integer) 1
10.0.18.145:6379> exists name      #key不存在返回0
(integer) 0
10.0.18.145:6379> mset name curry num 30 team golden  #同时设置多个key
OK
10.0.18.145:6379> get num        
"30"
10.0.18.145:6379> keys *           #查看全部的key,*表示全部,匹配全部字符
1) "name"
2) "num"
3) "team"
10.0.18.145:6379>keys n*           #列出以n开头的全部key
1) "name"
2) "num"
10.0.18.145:6379> keys ??m?        #?是匹配一个或者多个单个字符
1) "name"
10.0.18.145:6379> keys t[e]*       #[]符号,匹配一个或者多个字符
1) "team"
10.0.18.145:6379> keys t[ebc]*
1) "team" 
10.0.18.145:6379> flushdb          #慎用此命令,会清除全部的key
OK
10.0.18.145:6379> keys *
(empty list or set)
10.0.18.145:6379> randomkey        #从当前数据库中随机返回(不删除)一个key。
"name"
10.0.18.145:6379> randomkey
"name"
10.0.18.145:6379> randomkey
"num"
10.0.18.145:6379> randomkey
"team"
返回给定key的剩余生存时间(time to live)(以秒为单位),以下:
10.0.18.145:6379> set name james   #设置key-name的值为james
OK
10.0.18.145:6379> expire name 30   #过时值30秒
(integer) 1
10.0.18.145:6379> get name         #查看key的值
"james"
10.0.18.145:6379> ttl name         #查看ttl值,这里还剩下22秒过时
(integer) 22
10.0.18.145:6379> get name         #过了30秒,过时了,值消失了
(nil)
不带TTL的key
10.0.18.145:6379> set site www.redis.cn
OK
10.0.18.145:6379> ttl site
(integer) -1
10.0.18.145:6379> ttl www.redis.cn
(integer) -2
将当前数据库(默认为0)的key移动到给定的数据库db当中。
10.0.18.145:6379> select 0   #redis默认使用数据库0,为了清洗起见,这里在显示指定一次
OK
10.0.18.145:6379> set song "secret base - Zone"  #设置一个key
OK
10.0.18.145:6379> move song 1        #将song 这个key移动到数据库1
(integer) 1
10.0.18.145:6379> exists song        #查看song,返回0,表示已经被移走
(integer) 0
10.0.18.145:6379> select 1           #使用数据库1
OK
10.0.18.145:6379[1]> exists song     #证明song被移到了数据库1(注意命令提示符变成了"ip:6379[1]",代表正在使用数据库1)
(integer) 1
10.0.18.145:6379[1]> get song        #查看key的值
"secret base - Zone"
重命名key
10.0.18.145:6379[1]> rename song songtest   #用法
OK
10.0.18.145:6379[1]> get song
(nil)
10.0.18.145:6379[1]> get songtest
"secret base - Zone"
注:若是是批量删除key能够这样
redis-cli -p 7000 -h 192.168.10.20 -c del soa:position:*

4、redis数据持久化相关介绍

redis与memcached等缓存系统有一个最大的区别就是能够按期将内存中的数据保存到硬盘中,以实现数据持久化。

redis实现数据持久化有两种方式:第一种是RDB快照方式;另一种是AOF方式!

一、RDB持久化

RDB持久化是根据必定的保存规则按期对redis在内存中的数据作一个快照,把快照数据同步到硬盘上,每次的快照文件就是一个保存着redis数据的二进制文件。它也是redis默认的持久化方法。
RDB持久化保存快照文件的过程:
a、redis快照启动后,建立一个当前进程(父进程)的副本(子进程)。
b、父进程继续接受来自客户端的命令,子进程将内存中的数据开始同步到硬盘中,保存到一个临时文件中。
c、子进程完成快照后,快照的临时文件替换旧的快照文件,并断开与父进程的链接。
RDB持久化配置,在redis.conf中,默认以下:
save 900 1
save 300 10
save 60 10000
这是一个保存规则,前面的redis配置文件详解中有介绍!
注意:若是不想启用RDB持久化,将save参数删除掉或者注释掉便可!
RDB持久化其余配置
dir ./               #持久化文件保存目录,能够自定义
dbfilename dump.rdb  #RDB持久化保存的文件名,也能够自定义
stop-writes-on-bgsave-error yes #当保存时遇到错误(磁盘或者其余硬件错误)是否中止写入,默认为yes
rdbcompression yes   #保存文件时是否启动压缩
rdbchecksum yes      #是否启用RDB数据检验

二、AOF持久化

AOF持久化是将用户操做的命令(主要对修改数据的命令,就是除了查询之外的命令,包括增删改的命令)保存到一个文本文件中。
AOF持久化配置,在redis.conf中:
appendonly yes   #默认是no,启动aof,就修改成yes
appendfilename "appendonly.aof"  #保存aof持久化的文件名
appendfsync everysec  #aof文件同步频率
注:启用AOF持久化,并非立刻将内存数据写到文件中,它是将它写到硬盘缓存中,而后根据必定频率同步到文件中
共有三个值可选择:always表示只要有更新就同步;everysec是每秒种同步一次;no表示不一样步!
no-appendfsync-on-rewrite no #在不启用硬盘缓存同步到硬盘文件这个选项时,是否重写文件,默认为no,
若是发生redis服务阻塞,将此参数改成yes!
#重写文件的频率#
auto-aof-rewrite-percentage 100  #第一种表示当文件超过旧文件100%时进行重写
auto-aof-rewrite-min-size 64mb   #第二种表示当文件超过64mb时进行重写
aof-load-truncated yes           #当redis遇到退出时,从新载入时是否加载异常出现时的残余数据,默认yes

三、RDB和AOF的比较以及优缺点

RDB优势:
a、RDB是一个很是紧凑(compact)的文件,保存了redis在某个时间点上的数据集,这种文件很是适合用于进行备份
能够根据本身的需求自定义备份,好比每小时备份一次RDB文件,或者每月的每一天,也备份一个RDB文件。
b、RDB很是适用于灾难恢复(disaster recovery),它只有一个文件,而且内容都很是紧凑,能够(在加密后)将它传送到别的服务器。
c、RDB能够最大化Redis的性能:父进程在保存RDB文件时惟一要作的就是fork出一个子进程,而后这个子进程就会处理接下来的全部保存工做,父进程无须执行任何磁盘 I/O 操做
d、RDB 在恢复大数据集时的速度比 AOF 的恢复速度要快。
RDB缺点:
a、若是你没法接受偶尔的数据丢失,rdb方式不适合你,虽然这种方式能够自定义不一样的save point来控制保存rdb文件的频率
可是,由于RDB文件须要保存整个数据集的状态,因此它并非一个轻松的操做。所以你可能会至少5分钟才保存一次RDB文件。 
在这种状况下,一旦发生故障停机,你就可能会丢失好几分钟的数据。
b、每次保存RDB的时候,Redis都要fork()出一个子进程,并由子进程来进行实际的持久化工做。在数据集比较庞大时,fork()会很是耗时和耗费内存资源
并且形成服务器在某某毫秒内中止处理客户端;若是数据集很是巨大,而且CPU时间很是紧张的话,那么这种中止时间甚至可能会更长!
#####
AOF优势
a、使用AOF持久化会让Redis变得很是耐久(much more durable),AOF的默认策略为每秒钟fsync一次,在这种配置下Redis仍然能够保持良好的性能,而且就算发生故障停机,也最多只会丢失一秒钟的数据!
b、AOF文件是一个只进行追加操做的日志文件(append only log)所以对AOF文件的写入不须要进行seek,即便日志由于某些缘由而包含了未写入完整的命令(好比写入时磁盘已满,写入中途停机,等等),redis-check-aof工具也能够轻易地修复这种问题.
c、Redis能够在AOF文件体积变得过大时,自动地在后台对AOF进行重写:重写后的新AOF文件包含了恢复当前数据集所需的最小命令集合
d、AOF重写也须要进行fork(),但不管AOF重写的执行间隔有多长,数据的耐久性都不会有任何损失。
AOF缺点
a、对于相同的数据集来讲,AOF文件的体积一般要大于RDB文件的体积
b、在通常状况下,每秒fsync的性能依然很是高,而关闭fsync可让AOF的速度和RDB同样快,即便在高负荷之下也是如此。
不过在处理巨大的写入载入时,RDB能够提供更有保证的最大延迟时间(latency)

四、在生产环境中如何选择redis的持久化方式

通常来讲,若是想达到足以媲美PostgreSQL的数据安全性,你应该同时使用两种持久化功能。若是你很是关心你的数据,但仍然能够承受数分钟之内的数据丢失,那么你能够只使用RDB持久化。有不少用户都只使用AOF持久化,但并不推荐这种方式:由于定时生成RDB快照(snapshot)很是便于进行数据库备份,而且RDB恢复数据集的速度也要比AOF恢复的速度要快!

五、redis备份相关

注:我在生产环境就是取消配置文件中的save,只保留appendonly!

RDB+AOF的备份方式,以下:

在redis.conf中取消rdb的自动save配置,启动aof的配置:
#save 900 1    #注释或者删除
#save 300 10 
#save 60 10000
appendonly yes #启用aof持久化
appendfilename "appendonly.aof"  
而后在服务器上定时对RDB数据进行备份(也能够不备份RDB)
#redis-cli save 或者    #能够每周或者天天执行一次
#redis-cli -h 10.0.9.25 -p 6379 save(指定端口来备份)
若是设置了密码,能够添加-a 参数,以下:
./bin/redis-cli -h 10.0.9.25 -a abcd123 -p 6379 save   
#天天执行一次下面的bgrewriteaof###
/usr/local/redis/bin/redis-cli -h host -p 6379 BGREWRITEAOF > /root/backup/log/`date`.log #这个必须执行

补充:

BGREWRITEAOF:执行一个AOF文件重写操做。重写会建立一个当前AOF文件的体积优化版本。即便BGREWRITEAOF执行失败,也不会有任何数据丢失,由于旧的AOF文件在BGREWRITEAOF成功以前不会被修改。重写操做只会在没有其余持久化工做在后台执行时被触发,也就是说:若是Redis的子进程正在执行快照的保存工做,那么AOF重写的操做会被预约(scheduled),等到保存工做完成以后再执行AOF重写。在这种状况下,BGREWRITEAOF的返回值仍然是OK ,但还会加上一条额外的信息,说明BGREWRITEAOF要等到保存操做完成以后才能执行。在Redis 2.6或以上的版本可使用INFO命令查看BGREWRITEAOF是否被预约。若是已经有别的AOF文件重写在执行,那么BGREWRITEAOF返回一个错误,而且这个新的BGREWRITEAOF 请求也不会被预约到下次执行。

5、redis简单优化

一、若是都是内网的服务器,尽可能不要给redis外网ip地址,这样不安全,容易被扫到

二、修改redis默认6379的端口,这一个端口也是***扫描的对象

三、redis配置文件参数优化:

maxmemory 3GB
maxmemory-policy volatile-lru   #内存策略
##配置rename-command避免误操做##
rename-command FLUSHALL "XSFLUSHALL"
rename-command FLUSHDB "XSFLUSHDB"
rename-command SHUTDOWN "XSSHUTDOWN"
rename-command DEBUG "XSDEBUG"
rename-command CONFIG "XSCONFIG"
rename-command SLAVEOF "XSSLAVEOF"

redis进行中文转换

192.168.1.43:7000>keys agentServer:houseInfo:agentId_*   --列出匹配到的key
192.168.1.43:7000>SMEMBERS "agentServer:houseInfo:agentId_3071"   --转换keys
192.168.1.19:7000> "\xe8\x8b\x8f\xe5\xa0\xa4\xe6\x98\xa5\xe6\x99\x93\xe5\x90\x8d\xe8\x8b\x91 21\xe5\x8f\xb7 1503\xe5\xae\xa4"   --查看中文内容
(error) ERR unknown command '苏堤春晓名苑 21号 1503室'
乱码问题:
在redis中查看到的key的值是乱码,形如:
192.168.10.62:7000>hget bdm:cache:3:cityName 3020
"\xe6\x98\x8c\xe5\x90\x89\xe8\x87\xaa\xe6\xb2\xbb\xe5\xb7\x9e"
想把这些乱码转换成中文,须要添加--raw 参数进入redis,以下
./redis-cli --raw -h 192.168.10.62 -p 7000
再次查看:就是中文了
192.168.10.62:7000> hget bdm:cache:3:cityName 3020
昌吉自治州

redis修改key的值:

192.168.10.62:7000> hget bdm:cache:3:longitude 3020    --查看
87.28944100
192.168.10.62:7000> hset bdm:cache:3:longitude 3020 89.69238300   --修改成89.69238300
0
192.168.10.62:7000> hget bdm:cache:3:longitude 3020    --再次查看
89.69238300

四、redis模块

有时候使用LNAMP环境,须要redis模块,加入已经配置好了LANMP环境,可是没有redis模块,能够动态添加,以下:

php的redis扩展模块安装配置
php-redis客户端下载地址:https://codeload.github.com/nicolasff/phpredis/zip/master
1.安装配置
#unzip phpredis-master.zip
#cd phpredis-master
#/usr/local/php/bin/phpize
#./configure --with-php-config=/usr/local/php/bin/php-config
#make
#make install
2.执行完make install后会生成 
#Installing shared extensions:     /usr/local/php//lib/php/extensions/no-debug-non-zts-20060613/  
3.修改php.ini 
vi /usr/local/php/etc/php.ini
查找extension_dir,修改成 
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/" 
#在php配置文件最后一行添加redis.so
extension = redis.so  
4.重启apache 
#service httpd restart
5.检查redis模块是否加载。
#php -m |grep redis

6、redis命令行界面修改参数

redis除了能够在redis.conf中修改参数以外,还能够在命令行需改

192.168.1.112:7000> config get *
  1) "dbfilename"
  2) "dump.rdb"
  3) "requirepass"
  4) ""
  5) "masterauth"
  6) ""
  7) "unixsocket"
  8) ""
  9) "logfile"
 10) "/usr/local/redis/7000/redis.log"
 11) "pidfile"
 12) "/usr/local/redis/7000/redis.pid"
 13) "maxmemory"
 14) "943718400"
 15) "maxmemory-samples"
 16) "5"
 17) "timeout"
 18) "0"
 19) "tcp-keepalive"
 20) "0"
 21) "auto-aof-rewrite-percentage"
 22) "100"
 23) "auto-aof-rewrite-min-size"
 24) "67108864"
 25) "hash-max-ziplist-entries"
 26) "512"
 27) "hash-max-ziplist-value"
 28) "64"
 29) "list-max-ziplist-entries"
 30) "512"
 31) "list-max-ziplist-value"
 32) "64"
 33) "set-max-intset-entries"
 34) "512"
 35) "zset-max-ziplist-entries"
 36) "128"
 37) "zset-max-ziplist-value"
 38) "64"
 39) "hll-sparse-max-bytes"
 40) "3000"
 41) "lua-time-limit"
 42) "5000"
 43) "slowlog-log-slower-than"
 44) "10000"
 45) "latency-monitor-threshold"
 46) "0"
 47) "slowlog-max-len"
 48) "128"
 49) "port"
 50) "7000"
 51) "tcp-backlog"
 52) "511"
 53) "databases"
 54) "1"
 55) "repl-ping-slave-period"
 56) "10"
 57) "repl-timeout"
 58) "60"
 59) "repl-backlog-size"
 60) "1048576"
 61) "repl-backlog-ttl"
 62) "3600"
 63) "maxclients"
 64) "10000"
 65) "watchdog-period"
 66) "0"
 67) "slave-priority"
 68) "100"
 69) "min-slaves-to-write"
 70) "0"
 71) "min-slaves-max-lag"
 72) "10"
 73) "hz"
 74) "10"
 75) "cluster-node-timeout"
 76) "10000"
 77) "cluster-migration-barrier"
 78) "1"
 79) "cluster-slave-validity-factor"
 80) "10"
 81) "repl-diskless-sync-delay"
 82) "5"
 83) "cluster-require-full-coverage"
 84) "yes"
 85) "no-appendfsync-on-rewrite"
 86) "no"
 87) "slave-serve-stale-data"
 88) "yes"
 89) "slave-read-only"
 90) "yes"
 91) "stop-writes-on-bgsave-error"
 92) "no"
 93) "daemonize"
 94) "yes"
 95) "rdbcompression"
 96) "yes"
 97) "rdbchecksum"
 98) "yes"
 99) "activerehashing"
100) "yes"
101) "repl-disable-tcp-nodelay"
102) "no"
103) "repl-diskless-sync"
104) "no"
105) "aof-rewrite-incremental-fsync"
106) "yes"
107) "aof-load-truncated"
108) "yes"
109) "appendonly"
110) "yes"
111) "dir"
112) "/usr/local/redis/7000"
113) "maxmemory-policy"
114) "volatile-lru"
115) "appendfsync"
116) "everysec"
117) "save"
118) ""
119) "loglevel"
120) "notice"
121) "client-output-buffer-limit"
122) "normal 0 0 0 slave 268435456 67108864 60 pubsub 33554432 8388608 60"
123) "unixsocketperm"
124) "0"
125) "slaveof"
126) ""
127) "notify-keyspace-events"
128) ""
129) "bind"
130) "0.0.0.0"
修改日志级别:
192.168.1.112:7000> config set loglevel "notice"
OK    #显示ok表示set 成功
192.168.1.112:7000> config set maxmemory "943718400"
OK    #显示ok表示set 成功

不足之处,请多多指出!

相关文章
相关标签/搜索