下载
tar -xzvf redis-3.2.10.tar.gz
cd redis-3.2.10
make
make install
执行完之后会在/usr/local/bin下生成redis相关的可执行文件
➜ ~ cd /usr/local/bin
➜ bin git:(master) ll -h redis*
-rwxr-xr-x 1 mahaiyuan admin 98K 8 1 22:17 redis-benchmark
-rwxr-xr-x 1 mahaiyuan admin 14K 8 1 22:17 redis-check-aof
-rwxr-xr-x 1 mahaiyuan admin 1.0M 8 1 22:17 redis-check-rdb
-rwxr-xr-x 1 mahaiyuan admin 159K 8 1 22:17 redis-cli
lrwxr-xr-x 1 mahaiyuan admin 12B 8 1 22:17 redis-sentinel -> redis-server
-rwxr-xr-x 1 mahaiyuan admin 1.0M 8 1 22:17 redis-server
- redis-server redis服务器
- redis-cli Redis命令行客户端
- redis-benchmark Redis性能测试工具
- redis-check-aof AOF文件修复工具
启动
后台启动
redis-server &
[1] 23870
23870:C 01 Aug 22:59:39.866 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
23870:M 01 Aug 22:59:39.868 * Increased maximum number of open files to 10032 (it was originally set to 7168).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.2.10 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 23870
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
23870:M 01 Aug 22:59:39.871 # Server started, Redis version 3.2.10
23870:M 01 Aug 22:59:39.872 * DB loaded from disk: 0.001 seconds
23870:M 01 Aug 22:59:39.872 * The server is now ready to accept connections on port 6379
停止redis
➜ ~ redis-cli shutdown
23870:M 01 Aug 23:00:38.226 # User requested shutdown...
23870:M 01 Aug 23:00:38.226 * Saving the final RDB snapshot before exiting.
23870:M 01 Aug 23:00:38.227 * DB saved on disk
23870:M 01 Aug 23:00:38.227 # Redis is now ready to exit, bye bye...
[1] + 23870 done redis-server
客户端使用
➜ ~ redis-cli
127.0.0.1:6379>
or
➜ ~ redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379>
自定义启动和停止脚本
➜ redis ll -h
total 104
-rw-r--r--@ 1 mahaiyuan staff 46K 8 1 23:07 redis.conf
-rwxr-xr-x 1 mahaiyuan staff 1.6K 8 1 23:24 redis.sh
redis.sh内容如下
#!/bin/bash
path=$(cd "$(dirname "$0")"; pwd)
cd $path
CONFIG_FILE=./redis.conf
# check pid exists
psid=0
checkpid() {
redisps=`ps -ef | grep redis-server | grep 6379`
if [ -n "$redisps" ]; then
psid=`echo $redisps | awk '{print $2}'`
else
psid=0
fi
}
## 启动redis操作
start() {
checkpid
if [ $psid -ne 0 ]; then
echo "================================"
echo "warn: redis already started! (pid=$psid)"
echo "================================"
else
echo -n "Starting redis ..."
/usr/local/bin/redis-server $CONFIG_FILE &
checkpid
if [ $psid -ne 0 ]; then
echo "(pid=$psid) [Success]"
else
echo "[Failed]"
fi
fi
}
## 停止redis操作
stop() {
checkpid
if [ $psid -ne 0 ]; then
echo -n "Stopping redis ...(pid=$psid) "
## shutdown gracefully first
/usr/local/bin/redis-cli SHUTDOWN
count=0
while [ $count -lt 20 ]
do
echo "Sleep 3s to wait server stopping..."
sleep 3
checkpid
if [ $psid -eq 0 ]; then
echo "Redis stopped."
break
fi
count=`expr $count + 1`
done
checkpid
if [ $psid -ne 0 ]; then
echo "Shutdown gracefully failed, kill -9."
kill -9 $psid
if [ $? -eq 0 ]; then
echo "[Success]"
else
echo "[Failed]"
fi
fi
else
echo "================================"
echo "warn: Redis is not running"
echo "================================"
fi
}
case "$1" in
'start')
start
;;
'stop')
stop
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit 0