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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章