一、redis介绍:
1、redis定义:
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客户端,使用很方便。
2、redis存储介绍
redis使用了两种文件格式:全量数据和增量请求。全量数据格式是把内存中的数据写入磁盘,便于下次读取文件进行加载;增量请求文件则是把内存中的数据序列化为操作请求,用于读取文件进行replay得到数据,序列化的操作包括SET、RPUSH、SADD、ZADD。
3、redis性能:
下面是官方的bench-mark数据:
测试完成了50个并发执行100000个请求。
设置和获取的值是一个256字节字符串。
Linux box是运行Linux 2.6,这是X3320 Xeon 2.5 ghz。
文本执行使用loopback接口(127.0.0.1)。
结果:写的速度是110000次/s,读的速度是81000次/s 。
二、redis单台服务的安装配置
环境:centos 6.6 x86_64
注:有2种安装方式,第一种是直接yum安装;第二种是编译安装
1、使用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
2、使用编译方式安装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
三、redis配置文件参数和常用命令介绍
1、配置文件参数,如下:
#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
2、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:*
四、redis数据持久化相关介绍
redis与memcached等缓存系统有一个最大的区别就是可以定期将内存中的数据保存到硬盘中,以实现数据持久化。
redis实现数据持久化有两种方式:第一种是RDB快照方式;另外一种是AOF方式!
1、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数据检验
2、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
3、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)
4、在生产环境中如何选择redis的持久化方式
一般来说,如果想达到足以媲美PostgreSQL的数据安全性,你应该同时使用两种持久化功能。如果你非常关心你的数据,但仍然可以承受数分钟以内的数据丢失,那么你可以只使用RDB持久化。有很多用户都只使用AOF持久化,但并不推荐这种方式:因为定时生成RDB快照(snapshot)非常便于进行数据库备份,并且RDB恢复数据集的速度也要比AOF恢复的速度要快!
5、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 请求也不会被预定到下次执行。
五、redis简单优化
1、如果都是内网的服务器,尽量不要给redis外网ip地址,这样不安全,容易被扫到
2、修改redis默认6379的端口,这一个端口也是***扫描的对象
3、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
4、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
六、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 成功
不足之处,请多多指出!
转载于:https://blog.51cto.com/linuxg/1862040