目录
4、将可执行文件复制到自己的安装目录:/home/redis/
以4.0.6为例,附上地址:redis-4.0.6.tar.gz http://download.redis.io/releases/redis-4.0.6.tar.gz
1、 下载Redis4.0.6安装包
wget -C http://download.redis.io/releases/redis-3.2.8.tar.gz
2、解压、编译、安装redis-4.0.6
tar -zxvf redis-4.0.6.tar.gz -C /usr/src/
cd /usr/src/redis-4.0.6/
make && make install
3、创建redis相关目录:
mkdir -p /home/redis/bin
mkdir -p /home/redis/log
mkdir -p /home/redis/pid
mkdir -p /home/redis/db
4、将可执行文件复制到自己的安装目录:/home/redis/
*注意这个需要绝对路径 若采用相对路径,在启动redis时会报错,报错后的解决方案:删除软连接,重新执行下面的命令即可。
ln -s /usr/local/bin/redis-* /home/redis/bin/
5、复制配置文件到自己的安装目录:/home/redis/
cp /usr/src/redis-4.0.6/redis.conf /home/redis/
6、进入自己的安装目录,编辑redis.conf配置文件
cd /home/redis/
vim /home/redis/redis.conf
redis默认只允许本机连接,所以注释掉这行配置就可以远程访问:# bind 127.0.0.1
redis3.0版本增加了保护模式,需要我们设置密码,如果不想设置密码,就关闭保护模式:protected-mode no
设置redis以守护线程方式启动:daemonize yes
添加配置pid,log,db文件的保存地址:
pidfile /home/redis/pid/redis.pid
logfile /home/redis/log/redis.log
dir /home/redis/db
其他配置就默认即可,有需要再自行修改.
7、创建redis服务启动脚本,并赋予权限
vim /etc/init.d/redis
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
PATH=/home/redis/bin:/sbin:/usr/bin:/bin
REDISPORT=6379
EXEC=/home/redis/bin/redis-server
CLIEXEC=/home/redis/bin/redis-cli
PIDFILE=/home/redis/pid/redis.pid
CONF="/home/redis/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
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$CLIEXEC -p $REDISPORT 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
8、设置权限,添加redis服务开机启动:
chmod a+x /etc/init.d/redis
9、启动redis服务:
service redis start
ps -ef | grep redis
netstat -anptu | grep 6379
10、测试OK
redis-cli
set key1 hello
get key1
quit
(防火墙启用6379端口: iptables -A INPUT -p tcp --dport 6379 -j ACCEPT)