Redis 的安裝與使用(單節點)git
IP:192.168.4.111
環境:CentOS 6.6
Redis 版本:redis-3.0 (考慮到 Redis3.0 在集羣和性能提高方面的特性,rc 版爲正式版的候選版,並且 很快就出正式版)
安裝目錄:/usr/local/redis
用戶:root github
編譯和安裝所需的包:
# yum install gcc tcl redis
下載 3.0 版 Redis(當前最新版 redis-3.0.0-rc5.tar.gz,請學員們在安裝時自行選用最新版) # cd /usr/local/src
# wget https://github.com/antirez/redis/archive/3.0.0-rc5.tar.gz 安全
建立安裝目錄:
# mkdir /usr/local/redis tcp
解壓:
# tar -zxvf 3.0.0-rc5.tar.gz # mv redis-3.0.0-rc5 redis3.0 # cd redis3.0 性能
安裝(使用 PREFIX 指定安裝目錄):
# make PREFIX=/usr/local/redis install
安裝完成後,能夠看到/usr/local/redis 目錄下有一個 bin 目錄,bin 目錄裏就是 redis 的命令腳本: redis-benchmark redis-check-aof redis-check-dump redis-cli redis-server ui
將 Redis 配置成服務:
按上面的操做步驟,Redis 的啓動腳本爲:/usr/local/src/redis3.0/utils/redis_init_script 將啓動腳本複製到/etc/rc.d/init.d/目錄下,並命名爲 redis:
# cp /usr/local/src/redis3.0/utils/redis_init_script /etc/rc.d/init.d/redis 編輯/etc/rc.d/init.d/redis,修改相應配置,使之能註冊成爲服務:
# vi /etc/rc.d/init.d/redis
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem. rest
REDISPORT=6379 EXEC=/usr/local/bin/redis-server CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pidserver
CONF="/etc/redis/${REDISPORT}.conf"
case "$1" in start)
if [ -f $PIDFILE ] then
echo "$PIDFILE exists, process is already running or crashed"
fiip
;; *)
else $EXEC $CONF
fi
;; stop)
if [ ! -f $PIDFILE ] then
else
echo "$PIDFILE does not exist, process is not running"
PID=$(cat $PIDFILE) echo "Stopping ..." $CLIEXEC -p $REDISPORT shutdown while [ -x /proc/${PID} ] do
echo "Waiting for Redis to shutdown ..."
sleep 1 done
echo "Redis stopped"
echo "Starting Redis server..."
echo "Please use start or stop as first argument"
;; esac
查看以上 redis 服務腳本,關注標爲橙色的幾個屬性,作以下幾個修改的準備:
(1) 在腳本的第一行後面添加一行內容以下:
#chkconfig: 2345 80 90
(若是不添加上面的內容,在註冊服務時會提示:service redis does not support chkconfig)
(2) REDISPORT 端口保持 6379 不變;(注意,端口名將與下面的配置文件名有關)
(3) EXEC=/usr/local/bin/redis-server 改成 EXEC=/usr/local/redis/bin/redis-server
(4) CLIEXEC=/usr/local/bin/redis-cli 改成 CLIEXEC=/usr/local/redis/bin/redis-cli
(5) 配置文件設置:
建立 redis 配置文件目錄
# mkdir /usr/local/redis/conf
複製 redis 配置文件/usr/local/src/redis3.0/redis.conf 到/usr/local/redis/conf 目錄並按端口 號重命名爲 6379.conf
# cp /usr/local/src/redis3.0/redis.conf /usr/local/redis/conf/6379.conf 作了以上準備後,再對 CONF 屬性做以下調整:
CONF="/etc/redis/${REDISPORT}.conf" 改成 CONF="/usr/local/redis/conf/${REDISPORT}.conf" (6) 更改 redis 開啓的命令,之後臺運行的方式執行:
$EXEC $CONF & #「&」做用是將服務轉到後面運行
修改後的/etc/rc.d/init.d/redis 服務腳本內容爲:
#!/bin/sh
#chkconfig: 2345 80 90
#
# Simple Redis init.d script conceived to work on Linux systems # as it does use of the /proc filesystem.
REDISPORT=6379 EXEC=/usr/local/redis/bin/redis-server CLIEXEC=/usr/local/redis/bin/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pid CONF="/usr/local/redis/conf/${REDISPORT}.conf"
case "$1" in start)
if [ -f $PIDFILE ] then
else
fi
;; stop)
echo "$PIDFILE exists, process is already running or crashed" echo "Starting Redis server..."
$EXEC $CONF &
if [ ! -f $PIDFILE ] then
else
echo "$PIDFILE does not exist, process is not running"
PID=$(cat $PIDFILE) echo "Stopping ..." $CLIEXEC -p $REDISPORT shutdown while [ -x /proc/${PID} ] do
echo "Waiting for Redis to shutdown ..."
sleep 1
done echo "Redis stopped"
fi
;; *)
echo "Please use start or stop as first argument"
;; esac
以上配置操做完成後,即可將 Redis 註冊成爲服務: # chkconfig --add redis
防火牆中打開對應的端口
# vi /etc/sysconfig/iptables
添加:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT 重啓防火牆:
# service iptables restart
修改 redis 配置文件設置:
# vi /usr/local/redis/conf/6379.conf
修改以下配置
daemonize no 改成> daemonize yes
pidfile /var/run/redis.pid 改成> pidfile /var/run/redis_6379.pid
啓動 Redis 服務
# service redis start
將 Redis 添加到環境變量中: # vi /etc/profile 在最後添加如下內容:
## Redis env
export PATH=$PATH:/usr/local/redis/bin
使配置生效:
# source /etc/profile
如今就能夠直接使用 redis-cli 等 redis 命令了:
關閉 Redis 服務
# service redis stop
默認狀況下,Redis 開啓安全認證,能夠經過/usr/local/redis/conf/6379.conf 的 requirepass 指定一個
驗證密碼。