cd /usr/local wget https://download.redis.io/releases/redis-3.0.5.tar.gz tar -zxvf redis-3.0.5.tar.gz cd redis-3.0.5/src make install
啟動與驗證:
redis-server & # 背景啟動 redis-cli # 客戶端連線測試
停止服務時在客戶端按 Ctrl+C 即可跳出。
Redis 自帶的啟動腳本 redis_init_script 位於 utils/ 目錄:
cp /usr/local/redis-3.0.5/utils/redis_init_script /etc/init.d/redis vi /etc/init.d/redis
腳本重點是補上 chkconfig 設定與正確的路徑:
#!/bin/sh
# chkconfig: 2345 80 90
REDISPORT=6379
EXEC=/usr/local/redis-3.0.5/src/redis-server
CLIEXEC=/usr/local/redis-3.0.5/src/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.conf"
與原始檔相比主要有三處差異:
# chkconfig: 2345 80 90 這一行,讓 chkconfig 能管理服務。EXEC 與 CLIEXEC 的路徑要改成本機實際安裝位置。&:$EXEC $CONF &。若不加,Redis 會佔住前台,主畫面被占用,其他命令都下不了。下面是一份改寫後、支援 start/stop/restart 的版本:
#!/bin/bash
# chkconfig: 2345 80 90
# description: The redis daemon is a network memory cache service.
# processname: redis
# pidfile: /var/run/redis.pid
REDISPORT=6379
EXEC=/usr/local/redis-3.0.5/src/redis-server
CLIEXEC=/usr/local/redis-3.0.5/src/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.conf"
RETVAL=0
prog="redis"
. /etc/init.d/functions
. /etc/sysconfig/network
[ ${NETWORKING} = "no" ] && exit 0
[ -x $EXEC ] || exit 0
start() {
if [ -f $PIDFILE ]; then
echo "$PIDFILE exists, process is already running or crashed"
exit 1
fi
echo $"Starting $prog: "
$EXEC $CONF &
RETVAL=$?
return $RETVAL
}
stop() {
if [ ! -f $PIDFILE ]; then
echo "$PIDFILE does not exist, process is not running"
exit 1
fi
echo -n $"Stopping $prog: "
PID=$(cat $PIDFILE)
$CLIEXEC -p $REDISPORT shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "redis stopped"
RETVAL=$?
}
case "$1" in
start) start ;;
stop) stop ;;
restart|reload) stop; start ;;
*) echo $"Usage: $prog {start|stop|restart|reload}"
exit 1 ;;
esac
exit $RETVAL
mkdir /etc/redis cp /usr/local/redis-3.0.5/redis.conf /etc/redis/6379.conf chmod +x /etc/init.d/redis chkconfig --add redis chkconfig --level 35 redis on chkconfig --list | grep redis
同時要修改設定檔的兩項:daemonize no 改為 yes 讓 Redis 以守護行程啟動;pidfile 改為 /var/run/redis_6379.pid。
服務操作:
service redis start service redis stop
vi /etc/profile export PATH=$PATH:$JAVA_HOME/bin:/usr/local/redis-3.0.5/src source /etc/profile
如此一來就能直接呼叫 redis-cli,離開客戶端輸入 quit。
ps -ef | grep redis # 背景行程是否存在 netstat -tunpl | grep 6379 # 埠是否監聽
編輯設定檔加上 requirepass test123 後重啟:
service redis restart redis-cli redis 127.0.0.1:6379> set h helloworld (error) ERR operation not permitted redis 127.0.0.1:6379> auth test123 OK redis 127.0.0.1:6379> set h helloworld OK
也可以在連線時直接用參數帶入:
redis-cli -a test123
更靈活的方式是用 config 動態改密碼,不必重啟:
redis-cli -a test123 redis 127.0.0.1:6379> config get requirepass redis 127.0.0.1:6379> config set requirepass passabc OK redis 127.0.0.1:6379> auth passabc OK
若 master 有密碼,slave 端要加上 masterauth password。
/etc/redis/6379.conf,找到 # slaveof <masterip> <masterport> 並取消註解,填入 master 的 IP 與埠,例如 slaveof 192.168.1.101 6379。可到官網 redis.io 或 GitHub 下載後解壓。
其中 redis.windows.conf 就是 Windows 版的設定檔。解壓後 Redis 不會出現在服務清單裡,每次都得開一個黑窗口,因此建議安裝成 Windows 服務:
redis-server --service-install redis.windows.conf
執行後即可在服務清單看到它:
最後再裝一個圖形介面客戶端來連線,就大功告成了。