Skip to content


linux centos5.8 安装redis

Redis 是一个高性能的key-value数据库。 redis的出现,很大程度补偿了memcached这类key/value存储的不足,在部 分场合可以对关系数据库起到很好的补充作用。它提供了Python,Ruby,Erlang,PHP客户端,使用很方便。
一.安装tcl
否则在redis make test时出报错

  1. You need tcl 8.5 or newer in order to run the Redis test
  2. make[1]: *** [test] Error 1
  1. wget http://downloads.sourceforge.net/tcl/tcl8.6.0-src.tar.gz
  2. tar zxvf tcl8.6.0-src.tar.gz
  3. cd tcl8.6.0-src
  4. cd unix &&
  5. ./configure --prefix=/usr \
  6.             --mandir=/usr/share/man \
  7.             $([ $(uname -m) = x86_64 ] && echo --enable-64bit)
  8. make &&
  9. sed -e "s@^\(TCL_SRC_DIR='\).*@\1/usr/include'@" \
  10.     -e "/TCL_B/s@='\(-L\)\?.*unix@='\1/usr/lib@" \
  11.     -i tclConfig.sh
  12.  
  13. make test
  1. Tests ended at Tue Apr 16 12:02:27 CST 2013
  2. all.tcl:        Total   116     Passed  116     Skipped 0       Failed  0
  1. make install &&
  2. make install-private-headers &&
  3. ln -v -sf tclsh8.6 /usr/bin/tclsh &&
  4. chmod -v 755 /usr/lib/libtcl8.6.so

二.redis
1.安装redis

  1. wget http://redis.googlecode.com/files/redis-2.6.12.tar.gz
  2. tar xzf redis-2.6.12.tar.gz
  3. cd redis-2.6.12
  4. make
  5. make test

以下错误可以忽略
https://github.com/antirez/redis/issues/1034

  1. [exception]: Executing test client: assertion:Server started even if RDB was unreadable!.
  2. assertion:Server started even if RDB was unreadable!
  3.     while executing
  4. "error "assertion:$msg""
  5.     (procedure "fail" line 2)
  6.     invoked from within
  7. "fail "Server started even if RDB was unreadable!""
  8.     ("uplevel" body line 2)
  9.     invoked from within
  10. "uplevel 1 $elsescript"
  11.     (procedure "wait_for_condition" line 7)
  12.     invoked from within
  13. "wait_for_condition 50 100 {
  14.         [string match {*Fatal error loading*}  [exec tail -n1 < [dict get $srv stdout]]]
  15.     } else {
  16.         fail "Server..."
  17.     ("uplevel" body line 2)
  18.     invoked from within
  19. "uplevel 1 $code"
  20.     (procedure "start_server_and_kill_it" line 5)
  21.     invoked from within
  22. "start_server_and_kill_it [list "dir" $server_path] {
  23.     wait_for_condition 50 100 {
  24.         [string match {*Fatal error loading*} \
  25.             [exec..."
  26.     (file "tests/integration/rdb.tcl" line 57)
  27.     invoked from within
  28. "source $path"
  29.     (procedure "execute_tests" line 4)
  30.     invoked from within
  31. "execute_tests $data"
  32.     (procedure "test_client_main" line 9)
  33.     invoked from within
  34. "test_client_main $::test_server_port "
  35. make[1]: *** [test] Error 1

make命令执行完成后,会在当前目录下生成4个可执行文件,分别是redis-server、redis-cli、redis-benchmark、redis-stat,它们的作用如下:
redis-server:Redis服务器的daemon启动程序
redis-cli:Redis命令行操作工具。当然,你也可以用telnet根据其纯文本协议来操作
redis-benchmark:Redis性能测试工具,测试Redis在你的系统及你的配置下的读写性能
redis-stat:Redis状态检测工具,可以检测Redis当前状态参数及延迟状况

2.建立Redis目录

  1. mkdir -p /opt/redis/bin
  2. mkdir -p /opt/redis/etc
  3. mkdir -p /opt/redis/var
  4.  
  5. cp redis.conf /opt/redis/etc/
  6. cd src
  7. cp redis-server redis-cli redis-benchmark redis-check-aof redis-check-dump /opt/redis/bin/
  8.  
  9. useradd redis
  10. chown -R redis.redis /opt/redis

建立Redis目录,只是为了将Redis相关的资源更好的统一管理。你也可以使用
make install
安装在系统默认目录

3.复制启动文件

  1. cp ../utils/redis_init_script /etc/init.d/redis

4.启动redis

  1. cd /opt/redis/bin
  2. ./redis-server /opt/redis/etc/redis.conf
  1. _._                                                 
  2.            _.-``__ ''-._                                             
  3.       _.-``    `.  `_.  ''-._           Redis 2.6.12 (00000000/0) 64 bit
  4.   .-`` .-```.  ```\/    _.,_ ''-._                                   
  5.  (    '      ,       .-`  | `,    )     Running in stand alone mode
  6.  |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
  7.  |    `-._   `._    /     _.-'    |     PID: 24454
  8.   `-._    `-._  `-./  _.-'    _.-'                                   
  9.  |`-._`-._    `-.__.-'    _.-'_.-'|                                 
  10.  |    `-._`-._        _.-'_.-'    |           http://redis.io       
  11.   `-._    `-._`-.__.-'_.-'    _.-'                                   
  12.  |`-._`-._    `-.__.-'    _.-'_.-'|                                 
  13.  |    `-._`-._        _.-'_.-'    |                                 
  14.   `-._    `-._`-.__.-'_.-'    _.-'                                   
  15.       `-._    `-.__.-'    _.-'                                       
  16.           `-._        _.-'                                           
  17.               `-.__.-'                                               
  18.  
  19. [24454] 12 Apr 10:34:19.519 # Server started, Redis version 2.6.12
  20. [24454] 12 Apr 10:34:19.519 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add '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.
  21. [24454] 12 Apr 10:34:19.519 * The server is now ready to accept connections on port 6379

成功安装Redis后,直接执行redis-server即可运行Redis,此时它是按照默认配置来运行的(默认配置不是后台运行)。如果我们希望Redis按我们的要求运行,则需要修改配置文件,

5.设置itpables,充许192.168.0内网网段访问

  1. iptables -A INPUT -s 192.168.0.0/24 -p tcp -m tcp --dport 6379 -j ACCEPT
  2. /etc/init.d/iptables save

6.配置redis
vi /opt/redis/etc/redis.conf

  1. daemonize yes
  2. #是否作为守护进程运行 默认0
  3. pidfile /var/run/redis.pid
  4. # 指定一个pid,默认为/var/run/redis.pid
  5. port 6379
  6. #Redis默认监听端口
  7. bind 192.168.0.41
  8. #绑定主机IP,默认值为127.0.0.1
  9. timeout 300
  10. #客户端闲置多少秒后,断开连接,默认为300(秒)
  11. tcp-keepalive 0
  12. # tcp保持连接
  13. loglevel notice
  14. #日志记录等级,有4个可选值,debug,verbose(默认值),notice,warning
  15. logfile /opt/redis/var/redis.log
  16. #指定日志输出的文件名,默认值为stdout,也可设为/dev/null屏蔽日志
  17. databases 16
  18. #可用数据库数,默认值为16
  19. save 900 1
  20. #保存数据到disk的策略
  21. #当有一条Keys数据被改变是,900秒刷新到disk一次
  22. save 300 10
  23. #当有10条Keys数据被改变时,300秒刷新到disk一次
  24. save 60 10000
  25. #当有1w条keys数据被改变时,60秒刷新到disk一次
  26. #当dump .rdb数据库的时候是否压缩数据对象
  27. rdbcompression yes
  28. #本地数据库文件名,默认值为dump.rdb
  29. dbfilename dump.rdb
  30. #本地数据库存放路径,默认值为 ./
  31. dir /opt/redis/var/
  32. #内存限制
  33. maxmemory 2G
  34. #刷新策略
  35. maxmemory-policy allkeys-lru

7. 调整系统内核参数
如果内存情况比较紧张的话,需要设定内核参数:

  1. echo 1 > /proc/sys/vm/overcommit_memory

这里说一下这个配置的含义:/proc/sys/vm/overcommit_memory
该文件指定了内核针对内存分配的策略,其值可以是0、1、2。
0,表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。
1,表示内核允许分配所有的物理内存,而不管当前的内存状态如何。
2,表示内核允许分配超过所有物理内存和交换空间总和的内存
Redis在dump数据的时候,会fork出一个子进程,理论上child进程所占用的内存和parent是一样的,比如parent占用的内存为8G,这个时候也要同样分配8G的内存给child, 如果内存无法负担,往往会造成redis服务器的down机或者IO负载过高,效率下降。所以这里比较优化的内存分配策略应该设置为 1(表示内核允许分配所有的物理内存,而不管当前的内存状态如何)

vi /etc/sysctl.conf

  1. vm.overcommit_memory = 1

然后应用生效:

  1. sysctl –p

8.修改redis服务脚本
vi /etc/init.d/redis

  1. REDISPORT=6379
  2. EXEC=/opt/redis/bin/redis-server
  3. CLIEXEC=/opt/redis/bin/redis-cli
  4.  
  5. PIDFILE=/var/run/redis.pid
  6. CONF="/opt/redis/etc/redis.conf"

如果监听非本机IP地址时还需修改下脚本,不然关不掉

  1. #增加监听地址变量
  2. REDIHOST=192.168.0.41
  3.  
  4. #增加-h $REDIHOST
  5. echo "Stopping ..."
  6.                 $CLIEXEC -h $REDIHOST -p $REDISPORT shutdown

9.测试

  1. /etc/init.d/redis start

Starting Redis server…

  1. /opt/redis/bin/redis-cli -h 192.168.0.41

redis 127.0.0.1:6379> ping
PONG
redis 127.0.0.1:6379> set mykey c1gstduio
OK
redis 127.0.0.1:6379> get mykey
“c1gstduio”
redis 127.0.0.1:6379> exit

benchmark
/opt/redis/bin/redis-benchmark -h 192.168.0.41

  1. ====== PING_INLINE ======
  2.   10000 requests completed in 0.17 seconds
  3.   50 parallel clients
  4.   3 bytes payload
  5.   keep alive: 1
  6.  
  7. 99.51% <= 1 milliseconds
  8. 100.00% <= 1 milliseconds
  9. 60240.96 requests per second
  10.  
  11. ====== PING_BULK ======
  12.   10000 requests completed in 0.17 seconds
  13.   50 parallel clients
  14.   3 bytes payload
  15.   keep alive: 1
  16.  
  17. 100.00% <= 0 milliseconds
  18. 60240.96 requests per second
  19.  
  20. ====== SET ======
  21.   10000 requests completed in 0.16 seconds
  22.   50 parallel clients
  23.   3 bytes payload
  24.   keep alive: 1
  25.  
  26. 100.00% <= 0 milliseconds
  27. 61728.39 requests per second
  28.  
  29. ====== GET ======
  30.   10000 requests completed in 0.17 seconds
  31.   50 parallel clients
  32.   3 bytes payload
  33.   keep alive: 1
  34.  
  35. 99.49% <= 1 milliseconds
  36. 100.00% <= 1 milliseconds
  37. 60606.06 requests per second
  38.  
  39. ====== INCR ======
  40.   10000 requests completed in 0.16 seconds
  41.   50 parallel clients
  42.   3 bytes payload
  43.   keep alive: 1
  44.  
  45. 96.26% <= 1 milliseconds
  46. 100.00% <= 1 milliseconds
  47. 64516.13 requests per second
  48.  
  49. ====== LPUSH ======
  50.   10000 requests completed in 0.15 seconds
  51.   50 parallel clients
  52.   3 bytes payload
  53.   keep alive: 1
  54.  
  55. 93.60% <= 1 milliseconds
  56. 100.00% <= 1 milliseconds
  57. 65789.48 requests per second
  58.  
  59. ====== LPOP ======
  60.   10000 requests completed in 0.15 seconds
  61.   50 parallel clients
  62.   3 bytes payload
  63.   keep alive: 1
  64.  
  65. 93.50% <= 1 milliseconds
  66. 100.00% <= 1 milliseconds
  67. 66666.66 requests per second
  68.  
  69. ====== SADD ======
  70.   10000 requests completed in 0.16 seconds
  71.   50 parallel clients
  72.   3 bytes payload
  73.   keep alive: 1
  74.  
  75. 96.52% <= 1 milliseconds
  76. 100.00% <= 1 milliseconds
  77. 61728.39 requests per second
  78.  
  79. ====== SPOP ======
  80.   10000 requests completed in 0.17 seconds
  81.   50 parallel clients
  82.   3 bytes payload
  83.   keep alive: 1
  84.  
  85. 100.00% <= 0 milliseconds
  86. 57471.27 requests per second
  87.  
  88. ====== LPUSH (needed to benchmark LRANGE) ======
  89.   10000 requests completed in 0.17 seconds
  90.   50 parallel clients
  91.   3 bytes payload
  92.   keep alive: 1
  93.  
  94. 100.00% <= 0 milliseconds
  95. 58139.53 requests per second
  96.  
  97. ====== LRANGE_100 (first 100 elements) ======
  98.   10000 requests completed in 0.25 seconds
  99.   50 parallel clients
  100.   3 bytes payload
  101.   keep alive: 1
  102.  
  103. 99.67% <= 1 milliseconds
  104. 100.00% <= 1 milliseconds
  105. 40322.58 requests per second
  106.  
  107. ====== LRANGE_300 (first 300 elements) ======
  108.   10000 requests completed in 0.56 seconds
  109.   50 parallel clients
  110.   3 bytes payload
  111.   keep alive: 1
  112.  
  113. 0.22% <= 1 milliseconds
  114. 98.62% <= 2 milliseconds
  115. 99.01% <= 3 milliseconds
  116. 99.49% <= 4 milliseconds
  117. 100.00% <= 4 milliseconds
  118. 17921.15 requests per second
  119.  
  120. ====== LRANGE_500 (first 450 elements) ======
  121.   10000 requests completed in 0.96 seconds
  122.   50 parallel clients
  123.   3 bytes payload
  124.   keep alive: 1
  125.  
  126. 0.07% <= 1 milliseconds
  127. 40.25% <= 2 milliseconds
  128. 73.76% <= 3 milliseconds
  129. 99.51% <= 4 milliseconds
  130. 100.00% <= 5 milliseconds
  131. 10362.69 requests per second
  132.  
  133. ====== LRANGE_600 (first 600 elements) ======
  134.   10000 requests completed in 1.00 seconds
  135.   50 parallel clients
  136.   3 bytes payload
  137.   keep alive: 1
  138.  
  139. 0.07% <= 1 milliseconds
  140. 0.35% <= 2 milliseconds
  141. 98.18% <= 3 milliseconds
  142. 99.12% <= 4 milliseconds
  143. 99.48% <= 5 milliseconds
  144. 99.60% <= 6 milliseconds
  145. 99.85% <= 7 milliseconds
  146. 100.00% <= 8 milliseconds
  147. 10010.01 requests per second
  148.  
  149. ====== MSET (10 keys) ======
  150.   10000 requests completed in 0.26 seconds
  151.   50 parallel clients
  152.   3 bytes payload
  153.   keep alive: 1
  154.  
  155. 0.34% <= 1 milliseconds
  156. 100.00% <= 2 milliseconds
  157. 38167.94 requests per second

10. 关闭服务
/opt/redis/bin/redis-cli shutdown
如果端口变化可以指定端口:
/opt/redis/bin/redis-cli -h 192.168.0.41 -p 6379 shutdown

11. 保存/备份
数据备份可以通过定期备份该文件实现。
因为redis是异步写入磁盘的,如果要让内存中的数据马上写入硬盘可以执行如下命令:
redis-cli save 或者 redis-cli -p 6379 save(指定端口)
注意,以上部署操作需要具备一定的权限,比如复制和设定内核参数等。
执行redis-benchmark命令时也会将内存数据写入硬盘。

/opt/redis/bin/redis-cli save
OK

查看结果
ll -h var/

  1. total 48K
  2. -rw-r--r-- 1 root root  40K Apr 12 11:49 dump.rdb
  3. -rw-r--r-- 1 root root 5.3K Apr 12 11:49 redis.log

12.开机运行
vi /etc/rc.local

  1. /etc/init.d/redis start

三.php扩展

redis扩展非常多,光php就有好几个,这里选用phpredis,Predis需要PHP >= 5.3
Predis ? ★ Repository JoL1hAHN Mature and supported
phpredis ? ★ Repository yowgi This is a client written in C as a PHP module.
Rediska ? Repository Homepage shumkov
RedisServer Repository OZ Standalone and full-featured class for Redis in PHP
Redisent ? Repository justinpoliey
Credis Repository colinmollenhour Lightweight, standalone, unit-tested fork of Redisent which wraps phpredis for best performance if available.

1.安装php扩展phpredis

  1. wget https://nodeload.github.com/nicolasff/phpredis/zip/master --no-check-certificate
  2. cd phpredis-master
  3. unzip master
  4. cd phpredis-master/
  5. /opt/php/bin/phpize
  6. ./configure --with-php-config=/opt/php/bin/php-config
  7. make
  8. make install

接下来在php.ini中添加extension=redis.so
vi /opt/php/etc/php.ini

  1. extension_dir = "/opt/php/lib/php/extensions/no-debug-non-zts-20060613/"
  2. extension=redis.so

vi test.php

  1. <?
  2. phpinfo();
  3.  
  4. $redis = new Redis();
  5. $redis->connect("127.0.0.1",6379);
  6. $redis->set("test","Hello World");
  7. echo $redis->get("test");
  8.  
  9. ?>

redis
Redis Support enabled
Redis Version 2.2.2

四.基于php的web管理工具

phpRedisAdmin
https://github.com/ErikDubbelboer/phpRedisAdmin
演示:http://dubbelboer.com/phpRedisAdmin/?overview
这个用的人很多,但是我装完无法显示

readmin
http://readmin.org/
演示:http://demo.readmin.org/
需要装在根目录

phpredmin
https://github.com/sasanrose/phpredmin#readme
可以显示,有漂良的图表
使用伪rewrite,/index.php/welcome/stats

wget https://nodeload.github.com/sasanrose/phpredmin/zip/master
unzip phpredmin-master.zip

ln -s phpredmin/public predmin

编辑nginx,支持rewrite
nginx.conf

  1. location ~* ^/predmin/index.php/
  2.              {
  3.   rewrite ^/predmin/index.php/(.*) /predmin/index.php?$1 break;
  4.   fastcgi_pass  127.0.0.1:9000;
  5.                   fastcgi_index index.php;
  6.                   include fcgi.conf;
  7.              }

crontab -e

  1. * * * * * root cd /var/www/phpredmin/public && php index.php cron/index

配置redis
vi phpreadmin/config

  1. 'redis' => Array(
  2.             'host'     => '192.168.0.30',
  3.             'port'     => '6379',
  4.             'password' => Null,
  5.             'database' => 0

访问admin.xxx.com/predmin/就可以显示界面

五.实际使用感觉
redis装完后做discuzx2.5的cache
带宽消耗惊人,大概是memcached的十倍左右
内存占用也多了四分之一左右
discuz连接redis偶尔会超时和出错
最后还是切回了memcached

参考:
http://redis.io/topics/quickstart
http://hi.baidu.com/mucunzhishu/item/ead872ba3cec36db84dd798c
http://www.linuxfromscratch.org/blfs/view/svn/general/tcl.html

Posted in Memcached/redis.

Tagged with .


4 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. ttx says

    为什么首页可以打开,但是单击info或home或configurations 就会出现 404

  2. C1G says

    路径不对,那可能是nginx或程序里路径没配对.

  3. ttx says

    我在/home/www/predmin 软连接到 /var/phpredmin-master
    在ngnix的default 配置中
    location ~* ^/predmin/index.php/ {
    rewrite ^/predmin/index.php/(.*) /home/www/predmin/index.php/$1 break;
    include /etc/nginx/fastcgi_params;
    # fastcgi_split_path_info ^(.+\.php)(/.+)$;
    # # NOTE: You should have “cgi.fix_pathinfo = 0;” in php.ini
    #
    # # With php5-cgi alone:
    # fastcgi_pass 127.0.0.1:9000;
    # # With php5-fpm:
    # fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /home/www/predmin$fastcgi_script_name;
    # include fastcgi_params;
    }

  4. C1G says

    rewrite ^/predmin/index.php/(.*) /home/www/predmin/index.php/$1 break;
    改成相对于网站根目录的路径试试
    rewrite ^/predmin/index.php/(.*) /predmin/index.php/$1 break;



Some HTML is OK

or, reply to this post via trackback.