centos下安装redis并且注册为服务

      先去把官网晃一下:  http://redis.io/download


1、下载资源:

$ wget http://redis.googlecode.com/files/redis-2.6.14.tar.gz
$ tar xzf redis-2.6.14.tar.gz
$ cd redis-2.6.14
$ make
注意服务器需要安装make和gcc哟                                                       
好了,现在redis就安装成功了
Redis 由四个可执行文件:redis-benchmark、redis-cli、redis-server、redis-stat 这四个文件,加上一个redis.conf就构成了整个redis的最终可用包。它们的作用如下:


redis-server:Redis服务器的daemon启动程序
redis-cli:Redis命令行操作工具。当然,你也可以用telnet根据其纯文本协议来操作
redis-benchmark:Redis性能测试工具,测试Redis在你的系统及你的配置下的读写性能
redis-stat:Redis状态检测工具,可以检测Redis当前状态参数及延迟状况
现在就可以启动redis了,redis只有一个启动参数,就是他的配置文件路径。


redis-server /etc/redis.conf


注意,默认复制过去的redis.conf文件的daemonize参数为no,所以redis不会在后台运行,这时要测试,我们需要重新开一个终端。修改为yes则为后台运行redis。另外配置文件中规定了pid文件,log文件和数据文件的地址,如果有需要先修改,默认log信息定向到stdout.


下面是redis.conf的主要配置参数的意义:


daemonize:是否以后台daemon方式运行
pidfile:pid文件位置
port:监听的端口号
timeout:请求超时时间
loglevel:log信息级别
logfile:log文件位置
databases:开启数据库的数量
save * *:保存快照的频率,第一个*表示多长时间,第三个*表示执行多少次写操作。在一定时间内执行一定数量的写操作时,自动保存快照。可设置多个条件。
rdbcompression:是否使用压缩
dbfilename:数据快照文件名(只是文件名,不包括目录)
dir:数据快照的保存目录(这个是目录)
appendonly:是否开启appendonlylog,开启的话每次写操作会记一条log,这会提高数据抗风险能力,但影响效率。
appendfsync:appendonlylog如何同步到磁盘(三个选项,分别是每次写都强制调用fsync、每秒启用一次fsync、不调用fsync等待系统自己同步)
这时你可以打开一个终端进行测试了,配置文件中默认的监听端口是6379
2、建立和配置用户和日志目录

第一次启动时建议为Redis建立用户和日志目录

[root@localhost redis-stable]# useradd redis
[root@localhost redis-stable]# mkdir -p /var/lib/redis 

#db文件放在这里,需要修改redis.conf(vim下用命令模式下用/后面接要检索的字符串)
# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# Also the Append Only File will be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir /var/lib/redis

[root@localhost redis-stable]# mkdir -p /var/log/redis
# Specify the log file name. Also 'stdout' can be used to force
# Redis to log on the standard output. Note that if you use standard
# output for logging but daemonize, logs will be sent to /dev/null
logfile /var/log/redis/redislog
[root@localhost redis-stable]# chown redis.redis /var/lib/redis 
[root@localhost redis-stable]# chown redis.redis /var/log/redis
3、配置Init脚本


Redis管理脚本基于Ubuntu 的发行版上的,Ubuntu的可以看这篇文章ubuntu安装启动redis,在Centos linux 上并不能用,下面有个脚本可以用于CentOS 。


用这个脚本管理之前,需要先配置下面的内核参数,否则Redis脚本在重启或停止redis时,将会报错,并且不能自动在停止服务前同步数据到磁盘上:


# vi /etc/sysctl.conf

vm.overcommit_memory = 1


然后应用生效:
# sysctl –p


建立redis启动脚本:
#!/bin/bash 
# 
# Init file for redis 
# 
# chkconfig: - 80 12 
# description: redis daemon 
# 
# processname: redis 
# config: /etc/redis.conf 
# pidfile: /var/run/redis.pid 
source /etc/init.d/functions 
#BIN="/usr/local/bin" 
BIN="/usr/local/bin" 
CONFIG="/etc/redis.conf" 
PIDFILE="/var/run/redis.pid" 
### Read configuration 
[ -r "$SYSCONFIG" ] && source "$SYSCONFIG" 
RETVAL=0 
prog="redis-server" 
desc="Redis Server" 
start() { 
        if [ -e $PIDFILE ];then 
             echo "$desc already running...." 
             exit 1 
        fi 
        echo -n $"Starting $desc: " 
        daemon $BIN/$prog $CONFIG 
        RETVAL=$? 
        echo 
        [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog 
        return $RETVAL 
} 
stop() { 
        echo -n $"Stop $desc: " 
        killproc $prog 
        RETVAL=$? 
        echo 
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog $PIDFILE 
        return $RETVAL 
} 
restart() { 
        stop 
        start 
} 
case "$1" in 
  start) 
        start 
        ;; 
  stop) 
        stop 
        ;; 
  restart) 
        restart 
        ;; 
  condrestart) 
        [ -e /var/lock/subsys/$prog ] && restart 
        RETVAL=$? 
        ;; 
  status) 
        status $prog 
        RETVAL=$? 
        ;; 
   *) 
        echo $"Usage: $0 {start|stop|restart|condrestart|status}" 
        RETVAL=1 
esac 
exit $RETVAL

4、增加服务并开机自启动:


# chmod 755 /etc/init.d/redis 
# chkconfig --add redis 
# chkconfig --level 345 redis on 
# chkconfig --list redis
启动一下试试吧
[root@localhost redis-2.6.14]# service redis start
Starting Redis Server:                                     [  OK  ]
看下刚才的启动log吧
[root@localhost redis-2.6.14]# cat /var/log/redis/redis.log                     
[1587] 26 Jun 19:16:55.887 * Max number of open files set to 10032
[1587] 26 Jun 19:16:55.887 # Warning: 32 bit instance detected but no memory limit set. Setting 3 GB maxmemory limit with 'noeviction' policy now.
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 2.6.14 (00000000/0) 32 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in stand alone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 1587
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'




[1587] 26 Jun 19:16:55.888 # Server started, Redis version 2.6.14
[1587] 26 Jun 19:16:55.888 * DB loaded from disk: 0.000 seconds
[1587] 26 Jun 19:16:55.888 * The server is now ready to accept connections on port 6379
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值