初识redis:安装与配置

来源:互联网 发布:免费淘宝店铺推广软件 编辑:程序博客网 时间:2024/06/04 17:42

  redis是一个开源的key-value存储引擎。它支持string、hash、list、set和sorted set等多种数值类型。提供了Python,Ruby,Erlang,PHP等客户端。它跟memcached类似,不过数据可以持久化,支持在服务器端计算集合的并,交和补集(difference)等,还支持多种排序功能。所以Redis也可以被看成是一个数据结构服务 器。   

    Redis的所有数据都是保存在内存中,然后不定期的通过异步方式保存到磁盘上(这称为“半持久化模式”);也可以把每一次数据变化都写入到一个append only file(aof)里面(这称为“全持久化模式”)。

    接下来就来尝试安装,并让其run起来。


  • 安装

1. 源码下载

wget http://redis.googlecode.com/files/redis-2.4.2.tar.gz

或者,curl http://redis.googlecode.com/files/redis-2.4.2.tar.gz > redis-2.4.2.tar.gz

2. 解压

tar -xvf redis-2.4.2.tar.gz

3. 编译,安装

在redis-2.4.2目录下执行make;make install。


OK,这样在src/目录下就会生成redis-server,redis-cli,redis-check-dump,redis-benchmark等可执行文件。

其中,

redis-server:Redis服务器的daemon启动程序

redis-cli:Redis命令行操作工具。当然,你也可以用telnet根据其纯文本协议来操作

redis-benchmark:Redis性能测试工具,测试Redis在你的系统及你的配置下的读写性能


  • 配置

redis.conf:

[plain] view plaincopyprint?
  1. # By default Redis does not run as a daemon. Use 'yes' if you need it.  
  2. # Note that Redis will write a pid file in /var/run/redis.pid when daemonized.  
  3. # 是否以后台daemon形式运行  
  4. daemonize no  
  5.   
  6. # When running daemonized, Redis writes a pid file in /var/run/redis.pid by  
  7. # default. You can specify a custom pid file location here.  
  8. # pid文件的位置  
  9.  pidfile /var/run/redis.pid  
  10.   
  11. # Accept connections on the specified port, default is 6379.  
  12. # If port 0 is specified Redis will not listen on a TCP socket.  
  13. # 端口号  
  14.  port 6379  
  15.   
  16. # Close the connection after a client is idle for N seconds (0 to disable)  
  17. # 请求超时时间  
  18.  timeout 300  
  19.   
  20. # Set server verbosity to 'debug'  
  21. # it can be one of:  
  22. # debug (a lot of information, useful for development/testing)  
  23. # verbose (many rarely useful info, but not a mess like the debug level)  
  24. # notice (moderately verbose, what you want in production probably)  
  25. # warning (only very important / critical messages are logged)  
  26. # log信息的级别  
  27.  loglevel verbose  
  28.   
  29. # Specify the log file name. Also 'stdout' can be used to force  
  30. # Redis to log on the standard output. Note that if you use standard  
  31. # output for logging but daemonize, logs will be sent to /dev/null  
  32. # 打印log的输出流  
  33. logfile stdout  
  34.   
  35. # Set the number of databases. The default database is DB 0, you can select  
  36. # a different one on a per-connection basis using SELECT <dbid> where  
  37. # dbid is a number between 0 and 'databases'-1  
  38. # 开启数据库的数量  
  39. databases 16  
  40.   
  41. #   save <seconds> <changes>  
  42. # <span class="top11">保存快照的频率,表示多长时间执行多少次写操作。在一定时间内执行一定数量的写操作时,自动保存快照。可设置多个条件。</span>  
  43. save 900 1  
  44. save 300 10  
  45. save 60 10000  
  46.   
  47. # 是否使用压缩  
  48. rdbcompression yes  
  49.   
  50. # The filename where to dump the DB  
  51. # 数据快照的文件名,不包括目录,只是文件名  
  52.  dbfilename dump.rdb  
  53.   
  54. # The working directory.  
  55. # 数据快照的目录  
  56. dir ./  
  57.   
  58. # When a slave lost the connection with the master, or when the replication  
  59. # is still in progress, the slave can act in two different ways:  
  60. #  
  61. # 1) if slave-serve-stale-data is set to 'yes' (the default) the slave will  
  62. #    still reply to client requests, possibly with out of data data, or the  
  63. #    data set may just be empty if this is the first synchronization.  
  64. #  
  65. # 2) if slave-serve-stale data is set to 'no' the slave will reply with  
  66. #    an error "SYNC with master in progress" to all the kind of commands  
  67. #    but to INFO and SLAVEOF.  
  68. #  
  69. slave-serve-stale-data yes  
  70.   
  71. # <span class="top11">是否开启appendonlylog,开启的话每次写操作会记一条log,这会提高数据抗风险能力,但影响效率</span>  
  72. appendonly no  
  73. <span class="top11"># appendonlylog如何同步到磁盘</span>,默认为“everysec”  
  74. appendfsync everysec  
  75.   
  76. no-appendfsync-on-rewrite no  
  77.   
  78. auto-aof-rewrite-percentage 100  
  79. auto-aof-rewrite-min-size 64mb  
  80.   
  81. slowlog-log-slower-than 10000  
  82.   
  83. vm-enabled no  
  84.   
  85. vm-swap-file /tmp/redis.swap  
  86.   
  87. vm-max-memory 0  
  88.   
  89. vm-page-size 32  
  90.   
  91. vm-pages 134217728  
  92.   
  93. vm-max-threads 4  
  94.   
  95. ############################### ADVANCED CONFIG ###############################  
  96.   
  97. hash-max-zipmap-entries 512  
  98. hash-max-zipmap-value 64  
  99.   
  100. list-max-ziplist-entries 512  
  101. list-max-ziplist-value 64  
  102.   
  103. set-max-intset-entries 512  
  104.   
  105. zset-max-ziplist-entries 128  
  106. zset-max-ziplist-value 64  
  107.   
  108. activerehashing yes  

可以根据需要修改该配置,如:

[plain] view plaincopyprint?
  1. daemonize yes  
  2. pidfile ./redis.pid  
  3. port 6379  
  4. timeout 300  
  5. loglevel debug  
  6. save 900 1  
  7. save 300 10  
  8. save 60 10000  

  • run it

1. 启动服务

redis-server ../redis.conf 
查看redis是否启动:ps -aux | grep "redis"

或者

./redis-cli ping   

PONG

2. 命令行客户端

redis-cli

set key-value

./redis-cli set key1 value1

OK

./redis-cli set key2 value2

OK

get key

./redis-cli get key1

"value1"

./redis-cli get key3
(nil)

del key

./redis-cli del key1
(integer) 1

更多redis-cli命令:http://redis.io/commands

http://slj.me/2011/04/redis-cli-commands/

3. 停止服务

./redis-cli shutdown  

或者,关闭指定端口的redis-server

 ./redis-cli -p 6379 shutdown