Linux 下Redis安装与集群配置 Redis配置文件详解

来源:互联网 发布:php文件上传下载 编辑:程序博客网 时间:2024/04/28 00:29

Redis(2.8.19目前最新稳定版本)的安装:

 主从Redis的安装和配置只是配置文件不同,其他的均一样,一般主从配好就可以测试主从操作了,主server上set,从server马上就可以get到了。

      1.下载:

          http://pan.baidu.com/s/1jGvJxBo

       2.安装

        解压下载文件:

        [root@jhq0229 src]# tar xzvf redis-stable.tar.gz

        编译安装:

        [root@jhq0229 src]# cd redis-stable

        [root@jhq0229 redis-stable]# make 

        [root@jhq0229 redis-stable]# make  PREFIX=/usr/local install

        配置:

        [root@jhq0229 redis-stable]# mkdir /etc/redis

        [root@jhq0229 redis-stable]# cp redis.conf /etc/redis/

         创建Redis数据及日志存放目录:

         [root@jhq0229 redis-stable]# mkdir /data/redis


         Redis配置文件详解:

[plain] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. daemonize:如果需要在后台运行,把该项心为yes  
  2. pidfile:配置多个pid的地址,默认在/var/run/redis.pid  
  3. bind:绑定ip,设置后只接受来自该ip的请求  
  4. port:监听端口,默认为6379  
  5. timeout:设置客户端连接时的超时时间,单位为秒  
  6. loglevel:分为4级,debug、verbose、notice、warning  
  7. logfile:配置log文件地址  
  8. databases:设置数据库的个数,默认使用的数据库为0  
  9. save:设置redis进行数据库镜像的频率  
  10. rdbcompression:在进行镜像备份时,是否进行压缩  
  11. Dbfilename:镜像备份文件的文件名  
  12. Dir:数据库镜像备份的文件放置路径  
  13. Slaveof:设置数据库为其他数据库的从数据库  
  14. Masterauth:主数据库连接需要的密码验证  
  15. Requirepass:设置登录登录时需要使用的密码  
  16. Maxclients:限制同时连接的客户数量  
  17. Maxmemory:设置redis能够使用的最大内存  
  18. Appendonly:开启append only模式  
  19. Appendfsync:设置对appendonly.aof文件同步的频率  
  20. vm-enabled:是否开启虚拟内存支持  
  21. vm-swap-file:设置虚拟内存的交换文件路径  
  22. vm-max-memory:设置redis使用的最大物理内存大小  
  23. vm-page-size:设置虚拟内存的页大小  
  24. vm-pages:设置交换文件的总的page数量  
  25. vm-max-threads:设置VMIO同时使用的线程数量  
  26. Glueoutputbuf:把小的输出缓存存放在一起  
  27. hash-max-zipmap-entries:设置hash的临界值  
  28. Activerehashing:重新hash  


        配置自己的Redis集群(若不配置集群可以只配置Master):

       [root@jhq0229 redis-stable]# vim /etc/redis/redis.conf

        我的Redis主从配置:

        主:192.168.1.18              认证密码:01130229

        从:192.168.1.16


        主Redis(Master)配置:

       

[plain] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. # Redis configuration file example  
  2. #是否以后台进程方式运行  
  3. daemonize yes  
  4. #以后台进行运行,需指定pid  
  5. pidfile /var/run/redis.pid  
  6. port 6379  
  7. tcp-backlog 511  

  8. bind 192.168.1.18  
  9. #客户端闲置N秒后关闭连接  
  10. timeout 30  
  11.   
  12. tcp-keepalive 0  
  13.  
  14. #日志级别  
  15. loglevel notice  
  16. #记录日志的文件名称  
  17. logfile "redlog"  

  18. #可用数据库数量  
  19. databases 16  
  20.   
  21. #当有一条数据更新,900秒后同步数据到磁盘数据库  
  22. save 900 1  
  23. #当有10条数据更新,300秒后同步数据到磁盘数据库  
  24. save 300 10  
  25. save 60 10000  
  26.   
  27. stop-writes-on-bgsave-error yes  
  28.  
  29. #当dump .rdb的时候是否压缩数据对象,默认值为yes  
  30. rdbcompression yes  
  31.   
  32. rdbchecksum yes  
  33.   
  34. # The filename where to dump the DB  
  35. # 磁盘数据库文件名称  
  36. dbfilename myredis.rdb  
  37.   
  38. #本地数据库存放路径  
  39. dir /data/redis/  
  40.   
  41. ################################# REPLICATION #################################  
  42. #Redis主从复制配置  
  43.   
  44. #若当前服务为slave,在此处设置主服务的ip及端口  
  45. # slaveof <masterip> <masterport>  
  46.   
  47. #若当前服务为slave,设置主服务的认证密码  
  48. # masterauth <master-password>  
  49.   
  50. slave-serve-stale-data yes  
  51.   
  52. #若当前为slave服务,设置slave是否为只读服务  
  53. #slave-read-only yes  
  54. repl-diskless-sync no  
  55. repl-diskless-sync-delay 5  
  56. repl-disable-tcp-nodelay no  
  57. slave-priority 100  
  58.   
  59. #认证密码  
  60. requirepass 01130229  
  61.   
  62.   

  63. appendonly no  
  64. appendfsync everysec  

  65. no-appendfsync-on-rewrite no  
  66.   
  67. auto-aof-rewrite-percentage 100  
  68. auto-aof-rewrite-min-size 64mb  
  69.   
  70. aof-load-truncated yes  
  71.   
  72. lua-time-limit 5000  
  73. slowlog-log-slower-than 10000  
  74.   
  75. slowlog-max-len 128  
  76.  
  77. latency-monitor-threshold 0  
  78.   
  79. notify-keyspace-events ""  
  80.   

  81. hash-max-ziplist-entries 512  
  82. hash-max-ziplist-value 64  
  83.   
  84. list-max-ziplist-entries 512  
  85. list-max-ziplist-value 64  
  86.   
  87. set-max-intset-entries 512  
  88.   
  89. zset-max-ziplist-entries 128  
  90. zset-max-ziplist-value 64  
  91.   
  92. hll-sparse-max-bytes 3000  
  93. activerehashing yes  
  94. client-output-buffer-limit normal 0 0 0  
  95. client-output-buffer-limit slave 256mb 64mb 60  
  96. client-output-buffer-limit pubsub 32mb 8mb 60  
  97. hz 10  
  98.   
  99. aof-rewrite-incremental-fsync yes  

      

       从Redis(Slave)配置,配置与Master类似,不一致内容如下:


[plain] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. #绑定IP地址  
  2. bind 192.168.1.16  
  3.   
  4. #若当前服务为slave,在此处设置主服务的ip及端口  
  5. slaveof 192.168.1.18 6379  
  6.   
  7. #若当前服务为slave,设置主服务的认证密码  
  8. masterauth 01130229  

      配置Redis开机启动:

     [root@jhq0229 redis-stable]# vim /etc/init.d/redis

     内容如下:

[plain] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. #!/bin/bash  
  2. #  
  3. # redis - this script starts and stops the redis-server daemon  
  4. #  
  5. # chkconfig:   - 80 12  
  6. # description:  Redis is a persistent key-value database  
  7. # processname: redis-server  
  8. # config:      /etc/redis/redis.conf  
  9. # pidfile:     /var/run/redis.pid  
  10.   
  11. source /etc/init.d/functions  
  12.   
  13. BIN="/usr/local/bin"  
  14. CONFIG="/etc/redis/redis.conf"  
  15. PIDFILE="/var/run/redis.pid"  
  16.   
  17.   
  18. ### Read configuration  
  19. [ -r "$SYSCONFIG" ] && source "$SYSCONFIG"  
  20.   
  21. RETVAL=0  
  22. prog="redis-server"  
  23. desc="Redis Server"  
  24.   
  25. start() {  
  26.   
  27.         if [ -e $PIDFILE ];then  
  28.              echo "$desc already running...."  
  29.              exit 1  
  30.         fi  
  31.   
  32.         echo -n $"Starting $desc: "  
  33.         daemon $BIN/$prog $CONFIG  
  34.   
  35.         RETVAL=$?  
  36.         echo  
  37.         [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog  
  38.         return $RETVAL  
  39. }  
  40.   
  41. stop() {  
  42.         echo -n $"Stop $desc: "  
  43.         killproc $prog  
  44.         RETVAL=$?  
  45.         echo  
  46.         [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog $PIDFILE  
  47.         return $RETVAL  
  48. }  
  49.   
  50. restart() {  
  51.     stop  
  52.     start  
  53. }  
  54.   
  55. case "$1" in  
  56.   start)  
  57.         start  
  58.         ;;  
  59.   stop)  
  60.         stop  
  61.         ;;  
  62.   restart)  
  63.         restart  
  64.         ;;  
  65.   condrestart)  
  66.         [ -e /var/lock/subsys/$prog ] && restart  
  67.         RETVAL=$?  
  68.         ;;  
  69.   status)  
  70.         status $prog  
  71.         RETVAL=$?  
  72.         ;;  
  73.    *)  
  74.         echo $"Usage: $0 {start|stop|restart|condrestart|status}"  
  75.         RETVAL=1  
  76. esac  
  77.   
  78. exit $RETVAL  

 

     [root@jhq0229 redis-stable]# chmod +x /etc/init.d/redis


     [root@jhq0229 redis-stable]# chkconfig redis on

 

     补充配置,配置停止服务前同步数据到磁盘:

     [root@jhq0229 redis-stable]# vim /etc/sysctl.conf

     进行如下修改:

     vm.overcommit_memory=1

     应用修改:

      sysctl -p

    

      启动Redis:

      [root@jhq0229 redis-stable]# service redis start


     最后,奉上公司同事辛勤总结的Redis学习资料。希望可以帮到你。

     下载地址:    http://pan.baidu.com/s/1qWLV87a

     PPT内容版权归公司所有。

0 0