您好,登錄后才能下訂單哦!
獨立緩存服務器: 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共享管理
歡迎大家一起學習研究相關技術,源碼獲取請求求 貳零四貳八四九貳叁柒
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。