Linux 下安装Redis
下载地址:http://redis.io/download,下载最新稳定版本。
一、本教程使用的最新文档版本为 2.8.17,下载安装包:
新建目录:
mkdir /usr/local/redis
cd /usr/local/redis/
下载地址: wget http://download.redis.io/releases/redis-2.8.17.tar.gz
如下:
[root@instance-38r7isl1 /]# mkdir /usr/local/redis
[root@instance-38r7isl1 /]# cd /usr/local/redis/
[root@instance-38r7isl1 redis]# wget http://download.redis.io/releases/redis-2.8.17.tar.gz
–2020-02-09 16:06:29-- http://download.redis.io/releases/redis-2.8.17.tar.gz
Resolving download.redis.io… 109.74.203.151
Connecting to download.redis.io|109.74.203.151|:80… connected.
HTTP request sent, awaiting response… 200 OK
Length: 1234543 (1.2M) [application/x-gzip]
Saving to: “redis-2.8.17.tar.gz”
100%[===========================================================================================>] 1,234,543 5.99K/s in 3m 47s a
2020-02-09 16:10:18 (5.32 KB/s) - “redis-2.8.17.tar.gz” saved [1234543/1234543]
[root@instance-38r7isl1 redis]#
二、解压安装包:
[root@instance-38r7isl1 redis]# tar -vxzf redis-2.8.17.tar.gz
三、make命令编译
注:如果出现下面错误:
[root@instance-38r7isl1 redis-2.8.17]# make
cd src && make all
make[1]: Entering directory/usr/local/redis/redis-2.8.17/src' rm -rf redis-server redis-sentinel redis-cli redis-benchmark redis-check-dump redis-check-aof *.o *.gcda *.gcno *.gcov redis.info lcov-html (cd ../deps && make distclean) make[2]: Entering directory
/usr/local/redis/redis-2.8.17/deps’
(cd hiredis && make clean) > /dev/null || true
(cd linenoise && make clean) > /dev/null || true
(cd lua && make clean) > /dev/null || true
(cd jemalloc && [ -f Makefile ] && make distclean) > /dev/null || true
(rm -f .make-)
make[2]: Leaving directory/usr/local/redis/redis-2.8.17/deps' (rm -f .make-*) echo STD=-std=c99 -pedantic >> .make-settings echo WARN=-Wall -W >> .make-settings echo OPT=-O2 >> .make-settings echo MALLOC=jemalloc >> .make-settings echo CFLAGS= >> .make-settings echo LDFLAGS= >> .make-settings echo REDIS_CFLAGS= >> .make-settings echo REDIS_LDFLAGS= >> .make-settings echo PREV_FINAL_CFLAGS=-std=c99 -pedantic -Wall -W -O2 -g -ggdb -I../deps/hiredis -I../deps/linenoise -I../deps/lua/src -DUSE_JEMALLOC -I../deps/jemalloc/include >> .make-settings echo PREV_FINAL_LDFLAGS= -g -ggdb -rdynamic >> .make-settings (cd ../deps && make hiredis linenoise lua jemalloc) make[2]: Entering directory
/usr/local/redis/redis-2.8.17/deps’
(cd hiredis && make clean) > /dev/null || true
(cd linenoise && make clean) > /dev/null || true
(cd lua && make clean) > /dev/null || true
(cd jemalloc && [ -f Makefile ] && make distclean) > /dev/null || true
(rm -f .make-)
(echo “” > .make-ldflags)
(echo “” > .make-cflags)
MAKE hiredis
cd hiredis && make static
make[3]: Entering directory/usr/local/redis/redis-2.8.17/deps/hiredis' gcc -std=c99 -pedantic -c -O3 -fPIC -Wall -W -Wstrict-prototypes -Wwrite-strings -g -ggdb net.c make[3]: gcc: Command not found make[3]: *** [net.o] Error 127 make[3]: Leaving directory
/usr/local/redis/redis-2.8.17/deps/hiredis’
make[2]: *** [hiredis] Error 2
make[2]: Leaving directory/usr/local/redis/redis-2.8.17/deps' make[1]: [persist-settings] Error 2 (ignored) CC adlist.o /bin/sh: cc: command not found make[1]: *** [adlist.o] Error 127 make[1]: Leaving directory
/usr/local/redis/redis-2.8.17/src’
make: *** [all] Error 2
[root@instance-38r7isl1 redis-2.8.17]#
安装c++的编译器; 执行 yum -y install gcc-c++
yum -y install gcc-c++
执行make MALLOC=libc命令
make MALLOC=libc
显示成功:
…
CC redis-cli.o
LINK redis-cli
CC redis-benchmark.o
LINK redis-benchmark
CC redis-check-dump.o
LINK redis-check-dump
CC redis-check-aof.o
LINK redis-check-aof
Hint: It’s a good idea to run ‘make test’ ??
make[1]: Leaving directory `/usr/local/redis/redis-2.8.17/src’
[root@instance-38r7isl1 redis-2.8.17]#
执行命令启动成功
[root@instance-38r7isl1 redis-2.8.17]# cd src
[root@instance-38r7isl1 src]# ./redis-server &
[1] 1386
[root@instance-38r7isl1 src]# [1386] 09 Feb 16:25:33.681 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
…
[1386] 09 Feb 16:25:33.683 # Server started, Redis version 2.8.17
[1386] 09 Feb 16:25:33.683 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add ‘vm.overcommit_memory = 1’ to /etc/sysctl.conf and then reboot or run the command ‘sysctl vm.overcommit_memory=1’ for this to take effect.
[1386] 09 Feb 16:25:33.683 * The server is now ready to accept connections on port 6379
[root@instance-38r7isl1 src]#
修改redis.conf配置
daemonize yes
requirepass 123456
设置Redis自启动,新建配置文件redis
[root@instance-38r7isl1 redis-2.8.17]# vi /etc/init.d/redis
脚本如下(摘自:https://www.cnblogs.com/laoyang-123/p/11796837.html):
#!/bin/bash
#chkconfig: 2345 10 90
#description: Start and Stop redis
PATH=/usr/local/redis/bin:/sbin:/usr/redis/bin:/bin
REDISPORT=6379
EXEC=/usr/local/redis/redis-2.8.17/src/redis-server
REDIS_CLI=/usr/local/redis/redis-2.8.17/src/redis-cli
PIDFILE=/var/run/redis.pid
CONF="/usr/local/redis/redis-2.8.17/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
fi
if [ "$?"="0" ]
then
echo "Redis is running..."
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE exists, process is not running."
else
PID=$(cat $PIDFILE)
echo "Stopping..."
$REDIS_CLI -p $REDISPORT SHUTDOWN
while [ -x $PIDFILE ]
do
echo "Waiting for Redis to shutdown..."
sleep 1
done
echo "Redis stopped"
fi
;;
restart|force-reload)
${0} stop
${0} start
;;
*)
echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
exit 1
esac
设置文件权限和启动Redis服务并设置开机启动
[root@instance-38r7isl1 init.d]# chmod +x /etc/init.d/redis
[root@instance-38r7isl1 init.d]# /etc/init.d/redis start
Starting Redis server…
Redis is running…
[root@instance-38r7isl1 init.d]# ps -ef | grep redis
root 1386 30835 0 16:25 pts/1 00:00:00 ./redis-server *:6379
root 4257 30835 0 16:41 pts/1 00:00:00 grep redis
[root@instance-38r7isl1 init.d]# chkconfig redis on