預先準備一個目錄 /opt
1、進入opt 下載並解壓
wget http://download.redis.io/releases/redis-5.0.5.tar.gz
tar -zxvf redis-5.0.5.tar.gz
2、編譯
make
3、安裝: PREFIX 指定了安裝到哪個目錄
make PREFIX=/opt/redis install
4、可以進入/opt/redis/bin目錄查看安裝好的redis
[root@hello opt]# cd /opt/redis/bin
[root@hello bin]# ll
total 32736
-rwxr-xr-x 1 root root 4366624 Aug 29 11:27 redis-benchmark
-rwxr-xr-x 1 root root 8111832 Aug 29 11:27 redis-check-aof
-rwxr-xr-x 1 root root 8111832 Aug 29 11:27 redis-check-rdb
-rwxr-xr-x 1 root root 4806848 Aug 29 11:27 redis-cli
lrwxrwxrwx 1 root root 12 Aug 29 11:27 redis-sentinel -> redis-server
-rwxr-xr-x 1 root root 8111832 Aug 29 11:27 redis-server
[root@hello bin]#
redis-benchmark:是官方自帶的Redis性能測試工具;
當AOF文 件或者RDB文件出現語法錯誤時,可以使用redis-check-aof或者redis- check-rdb修復;
redis-cli是客戶端命令行工具;
可以通過命令redis-cli- h {host} -p {port}連接到指定Redis服務器;
redis-sentinel是Redis哨兵啓動程序;
redis-server是Redis服務端啓動程序。
5、redis的解壓目錄redis.conf文件,是redis服務啓動的模板配置文件。
下面列舉了該文章涉及到的幾個配置
配置項 | 描述 | 默認值 |
---|---|---|
bind | 綁定的網口ip 可配置多個 | 127.0.0.1 |
port | Redis服務的啓動端口 | 6379 |
daemoniz | Redis服務是否後臺運行 【yes爲開啓】 | no |
pidfile | Redis服務PID寄存文件 | /var/run/redis_6379.pid |
logfile | Redis服務輸出日誌文件 | "" |
dir | Redis持久化文件的輸出目錄 | ./,也就是當前目錄 |
requirepass | Redis服務數訪問認證密碼 |
對於bind要綁定的ip可以通過ifconfig查看,當配置了10.105.108.39 可通過其他機器訪問。
創建/opt/redis/conf目錄,將redis.conf 拷貝到該目錄下,並重命名爲6381.conf【6381爲redis啓動監聽的端口號】。
[root@hello redis-5.0.5]# mkdir -p /opt/redis/conf
[root@hello redis-5.0.5]# cp /opt/redis-5.0.5/redis.conf /opt/redis/conf/6381.conf
創建下面幾個目錄
mkdir -p /opt/redis/db/6381 #存放redis持久化文件
mkdir -p /opt/redis/log #存放redis的日誌文件
修改6381.conf配置文件的下面幾個選項
bind 127.0.0.1 10.105.108.39
port 6381
daemonize yes
logfile "/opt/redis/log/6381.log"
dir /opt/redis/db/6381/
requirepass 123
6、啓動redis
[root@hello redis-5.0.5]# /opt/redis/bin/redis-server /opt/redis/conf/6381.conf
[root@hello redis-5.0.5]# ps -ef|grep redis
root 25689 1 0 12:28 ? 00:00:00 /opt/redis/bin/redis-server 127.0.0.1:6381
root 25699 4644 0 12:28 pts/0 00:00:00 grep --color=auto redis
如果啓動失敗,可以查看 /opt/redis/log/6381.log文件,分析失敗的原因
7、通過客戶端連接redis
[root@hello redis-5.0.5]# /opt/redis/bin/redis-cli -h 127.0.0.1 -p 6381
127.0.0.1:6381> auth 123 #授權
OK
127.0.0.1:6381> set 101 zs #設置 key爲101 value爲zs
OK
127.0.0.1:6381> get 101 #獲取key爲101 的字符串值
"zs"
127.0.0.1:6381> exit #退出客戶端
[root@hello redis-5.0.5]
8、停止服務
[root@hello redis-5.0.5]# /opt/redis/bin/redis-cli -h 127.0.0.1 -p 6381
127.0.0.1:6381> auth 123
OK
127.0.0.1:6381> shutdown
not connected> exit
通過編寫shell腳本停止服務可以使用下面這種方式。
/opt/redis/bin/redis-cli -h 127.0.0.1 -p 6381 -a 123 shutdown
9、腳本
在/opt/redis-5.0.5/utils 目錄下 有個redis_init_script的腳本,可以方便的啓動redis和停止redis。
創建/opt/redis/script 目錄 ,將redis_init_script複製到該目錄,並重命名爲6381
[root@hello opt]# mkdir -p /opt/redis/script
[root@hello opt]# cp /opt/redis-5.0.5/utils/redis_init_script /opt/redis/script/6381
將腳本文件端口號變量改爲6381
REDISPORT=6381
停止服務命令加上 -a 123 密碼驗證
完整腳本如下:
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
### BEGIN INIT INFO
# Provides: redis_6379
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Redis data structure server
# Description: Redis data structure server. See https://redis.io
### END INIT INFO
#redis服務器監聽的端口
REDISPORT=6381
#服務端所處位置
EXEC=/usr/local/bin/redis-server
#客戶端位置
CLIEXEC=/usr/local/bin/redis-cli
#redis的PID文件位置
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 ..."
# -a 123 指定密碼爲123
$CLIEXEC -p $REDISPORT -a 123 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
啓動
[root@hello opt]# /opt/redis/script/6381 start
Starting Redis server...
[root@hello opt]# ps -ef|grep redis
root 30340 1 0 13:03 ? 00:00:00 /opt/redis/bin/redis-server 127.0.0.1:6381
root 30359 4644 0 13:04 pts/0 00:00:00 grep --color=auto redis
[root@hello opt]#
停止
[root@hello opt]# /opt/redis/script/6381 stop
Stopping ...
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Redis stopped
[root@hello opt]# ps -ef|grep redis
root 30544 4644 0 13:05 pts/0 00:00:00 grep --color=auto redis
[root@hello opt]#