獨立快取伺服器: LinuxCentOS
Redis版本: 3.0
下面我們針對於Redis安裝做下詳細的記錄:
編譯和安裝所需的包:
#yum install gcc tcl建立安裝目錄:
#mkdir /usr/local/redis
解壓:
#tar -zxvf 3.xx.tar.gz
#mv redis-3.xx 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-dumpredis-cli redis-server
將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.
REDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.conf"
case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
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服務指令碼,關注標為橙色的幾個屬性,做如下幾個修改的準備:
(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
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF&
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
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 --dport6379 -j ACCEPT
重啟防火牆:
#service iptables restart
修改redis配置檔案設定:
#vi /usr/local/redis/conf/6379.conf
修改如下配置
daemonizeno改為daemonizeyes
備註:如果不改為yes,pid檔案是不會生成,start、stop命令是不會生效的(依賴pid檔案)
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-cli
關閉Redis服務
#service redis stop提醒:預設情況下,Redis開啟安全認證,可以通過/usr/local/redis/conf/6379.conf的requirepass指定一個驗證密碼
在jeesz中主要使用redis做分散式快取,儲存登入使用者資訊,做session共享管理
歡迎大家一起學習研究相關技術,原始碼獲取請求求: 2042849237