学习redis

来源:互联网 发布:域名注册通 吾爱破解 编辑:程序博客网 时间:2024/06/08 19:47

手动迁移原创博客,原文发表于http://blog.itpub.net/20777547/viewspace-1355253/


一.安装

操作系统:rhel 6.2 64

 

网上下了一个当前的稳定版:http://download.redis.io/releases/redis-2.8.17.tar.gz

 

1.准备相关的包:

 

我是最小化安装的操作系统,所以要正常通过编译还需要安装以下的软件包:gcc,make,tcl

 

2.编译及安装


 tar zxvf redis-2.8.17.tar.gz cd redis-2.8.17/src/ makemake install 

/usr/local里面新建一个目录,把redis相关的都copy过去

 mkdir -p /usr/local/rediscp redis-2.8.17/src/* /usr/local/redis/cp redis-2.8.17/redis.conf /usr/local/redis/ 

二.日常操作

 

1.启动及关闭

 

a.修改配置文件redis.conf,让redis后台启动

 # By default Redis does not run as a daemon. Use 'yes' if you need it.# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.daemonize yes 

b.启动redis的时候需要制定配置文件

redis-server /usr/local/redis/redis.conf 

c.关闭redis

redis-cli shutdown 

2.使用redis

 

一般情况下直接使用redis-cli命令就可以操作redis了,不加任何参数默认就是连接本机的6379端口的redis

 

a.setget

 127.0.0.1:6379> set name paololiuOK127.0.0.1:6379> get name"paololiu" 

b.keys命令可以通过通配符查询数据库中匹配的key,和linux的通配符一样可以用*,也可以用?,还可以用正则表达式的方式

 keys *keys pao?okeys pa*okeys p[ab]olo 

c.type命令可以显示当前key的类型:string,list,set,zset,hash

 127.0.0.1:6379> type myhm1hash 

d.其他还有很多redis的命令,具体的可以查阅:http://redis.readthedocs.org/en/latest/index.html


三.持久化

 

1.RDB

redis是一个内存数据库,但也能将通过快照(RDB)的方式将数据持久化地放到磁盘上。

a.save设置

redis.conf配置文件中,默认是15分钟内有一个key放生变化,5分钟内10key发生变化,1分钟内10000key发生变化会自动save


 ########################## SNAPSHOTTING  ############################ Save the DB on disk:##   save  ##   Will save the DB if both the given number of seconds and the given#   number of write operations against the DB occurred.##   In the example below the behaviour will be to save:#   after 900 sec (15 min) if at least 1 key changed#   after 300 sec (5 min) if at least 10 keys changed#   after 60 sec if at least 10000 keys changed##   Note: you can disable saving at all commenting all the "save" lines.##   It is also possible to remove all the previously configured save#   points by adding a save directive with a single empty string argument#   like in the following example:##   save ""#“N秒内数据集至少有M个改动”这一条件被满足时,自动保存一次数据集 save 900 1save 300 10save 60 10000 

b.dump文件

dump文件指的就是之前面说的save的文件。

 # The filename where to dump the DB#dump文件名dbfilename dump.rdb # The working directory.## The DB will be written inside this directory, with the filename specified# above using the 'dbfilename' configuration directive.## The Append Only File will also be created inside this directory.## Note that you must specify a directory here, not a file name.#这里是相对路径,最好指定一个绝对路径dir ./  # Compress string objects using LZF when dump .rdb databases?# For default that's set to 'yes' as it's almost always a win.# If you want to save some CPU in the saving child set it to 'no' but# the dataset will likely be bigger if you have compressible values or keys.#是否压缩rdbcompression yes 

c.RDB缺陷

 

u RDB数据不是实时的,数据库一旦出现问题,那么上次生成RDB到数据库挂掉这段时间内生成的数据都会丢失掉

u 生成RDB是一个内存复制再写入文件的过程,如果一个redis数据库内存里有4G的数据,那么完成RDB则至少需要8G的内存

u RDB是无法实现增量的写入的


2.AOF

AOF是类似关系型数据库日志的一种文件,默认是关闭的,开启后它的优先权高于RDB

 ######################## APPEND ONLY MODE ########################## # 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).## 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.## 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.## Please check http://redis.io/topics/persistence for more information.#默认是关闭的 appendonly no # The name of the append only file (default: "appendonly.aof")#AOF文件命名格式 appendfilename "appendonly.aof" 

AOF rewrite功能

AOF的运作方式是不断地将命令追加到文件末尾,AOF文件的体积也越来越大。使用AOF rewrite功能后能对AOF文件进行重建,缩小文件的大小。


 # 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文件超过64M时,自动重建auto-aof-rewrite-min-size 64mb#重建的比例auto-aof-rewrite-percentage 100#另外也可以用过bgrewriteaof命令手动重建 

关于redis持久化rdbaof可以查看http://my.oschina.net/davehe/blog/174662


四.内存限制

redis是一款内存数据库,前面也说过当生成rdb(快照)文件时,至少要留出一半的内存空间,那么就需要设置最大内存限制

 # Don't use more memory than the specified amount of bytes.# When the memory limit is reached Redis will try to remove keys# according to the eviction policy selected (see maxmemory-policy).## If Redis can't remove keys according to the policy, or if the policy is# set to 'noeviction', Redis will start to reply with errors to commands# that would use more memory, like SET, LPUSH, and so on, and will continue# to reply to read-only commands like 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').##该选项告诉redis当使用了多少内存后就拒绝后续的写入,可以配合maxmemory-policy一起使用# maxmemory

五.主从复制

只需在redis.con中配置好masterIP和端口号即可

 ########################## REPLICATION ########################### # Master-Slave replication. Use slaveof to make a Redis instance a copy of# another Redis server. A few things to understand ASAP about Redis replication.## 1) Redis replication is asynchronous, but you can configure a master to#    stop accepting writes if it appears to be not connected with at least#    a given number of slaves.# 2) Redis slaves are able to perform a partial resynchronization with the#    master if the replication link is lost for a relatively small amount of#    time. You may want to configure the replication backlog size (see the next#    sections of this file) with a sensible value depending on your needs.# 3) Replication is automatic and does not need user intervention. After a#    network partition slaves automatically try to reconnect to masters#    and resynchronize with them.# slaveof 192.168.17.230 6379 




0 0
原创粉丝点击