Centos下安裝Redis並配置開機啟動

2022-06-23 21:54:11 字數 2253 閱讀 6459

wget

tar -zxf redis-2.2.13.tar.gz

cd redis-2.2.13

make

sudo make install

cp redis.conf /etc

install的時候,redis的命令會被拷貝到/usr/local/bin下面

2,建立使用者與日誌目錄

第一次啟動redis前,建議為redis單獨建立一個使用者,並新建data和日誌資料夾

sudo useradd redis

sudo mkdir -p /var/lib/redis

sudo mkdir -p /var/log/redis

sudo chown redis.redis /var/lib/redis #db檔案放在這裡,要修改redis.conf

sudo chown redis.redis /var/log/redis

3,配置init指令碼

其實git上上很多老外寫好的啟動指令碼,不過大部分都是ubuntu的,對於centos,也有一份

經過我的修改,如下:

###########################

path=/usr/local/bin:/sbin:/usr/bin:/bin

redisport=6379

exec=/usr/local/bin/redis-server

redis_cli=/usr/local/bin/redis-cli

pidfile=/var/run/redis.pid

conf="/etc/redis.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

fiif [ "$?"="0" ]

then

echo "redis is running..."

fi ;;

stop)

if [ ! -f $pidfile ]

then

echo "$pidfile does not exist, process is not running"

else

pid=$(cat $pidfile)

echo "stopping ..."

$redis_cli -p $redisport shutdown

while [ -x $ ]

doecho "waiting for redis to shutdown ..."

sleep 1

done

echo "redis stopped"

fi;;

restart|force-reload)

$ stop

$ start

;;*)

echo "usage: /etc/init.d/redis " >&2

exit 1

esac

##############################

把上述**存為redis,放到/etc/init.d/下面

chown +x /etc/init.d/redis

其實做成服務啟動,也是呼叫redis-server,如果想讓它在後臺作為daemon執行,那麼

需要修改redis.conf,將 daemonize yes 改為 daemonize no

4,設定開機啟動服務

sudo chkconfig redis on

5,啟動,停止redis

啟動:service redis start   或者 /etc/init.d/redis start

停止:service redis stop   或者 /etc/init.d/redis stop

6,測試redis

redis-cli

redis 127.0.0.1:6379> set foo 123

okredis 127.0.0.1:6379> get foo

"123"

redis 127.0.0.1:6379> exit