一、下载安装安装包
wget http://www.redis.cn/download.html/redis-5.0.3.tar.gz
(下载完查看大小1.86M,如果只有几十K,请直接上网页手动下载)
二、安装redis,修改允许后台运行
#!/bin/bash
yum install gcc-c++ -y
tar -zxf redis-5.0.3.tar.gz
cd redis-5.0.3
make MALLOC=libc
make
cd src
make install PREFIX=/usr/local/redis
cd ..
cp redis.conf /usr/local/redis/bin/
cd ..
rm -rf redis-5.0.3/
cd /usr/local/redis/bin
##让redis可以后台启动
AA=`cat -n redis.conf | grep daemonize | grep -v "#" | awk '{print $1}'`
sed -i ''"$AA"'c daemonize yes' redis.conf
三、单机多实例
1:创建多实列目录和启动脚本:
[root@localhost /]# mkdir /usr/local/redis/{conf,pid,log,data,}
[root@localhost /]# mkdir /usr/local/redis/data/{6380,6381,6382}
[root@localhost /]# cd /usr/local/redis/bin/
[root@localhost /]# cp redis-server redis-server6380;cp redis-server redis-server6381;cp redis-server redis-server6382
[root@localhost /]# mv redis.conf /usr/local/redis/conf/
[root@localhost /]# cd /usr/local/redis/conf/
[root@localhost /]# cp redis.conf redis6380.conf
[root@localhost /]# cp redis.conf redis6381.conf
[root@localhost /]# cp redis.conf redis6382.conf
2:分别修改配置文件:(这里只显示一个配置文件,其他配置文件以此类推的修改)
vim /usr/local/redis/conf/redis6380.conf
92 port 6380 <== 端口
#pidfile " "(更改为:)
158 pidfile /usr/local/redis/pid/redis_6380.pid <== 进程id存放文件
171 logfile /usr/local/redis/log/redis_6380.log <== 日志目录
#dir ./(更改为):
263 dir /usr/local/redis/data/6380/ <== db目录
四、启动和关闭脚本
cat /usr/bin/redis6380
#!/bin/bash
cd /usr/local/redis/bin
case $1 in
start)
./redis-server6380 /usr/local/redis/conf/redis6380.conf;ps -ef | grep redis | grep 6380 > /dev/null && echo "redis6380 Successful opening" || echo "redis6380 Open failure"
;;
stop)
killall ./redis-server6380 2> /dev/null;sleep 1;ps -ef | grep -v grep | grep redis-server6380 && echo "redis6380 Shutdown failure" || echo "redis6380 Shut down successfully"
;;
restart)
killall ./redis-server6380 2> /dev/null;sleep 1;ps -ef | grep -v grep | grep redis-server6380 && echo "redis6380 Shutdown failure" || echo "redis6380 Shut down successfully"
./redis-server6380 /usr/local/redis/conf/redis6380.conf;ps -ef | grep redis | grep 6380 > /dev/null && echo "redis6380 Successful opening" || echo "redis6380 Open failure"
;;
*)
exit
exit
;;
esac
cat /usr/bin/redis6381
#!/bin/bash
cd /usr/local/redis/bin
case $1 in
start)
./redis-server6381 /usr/local/redis/conf/redis6381.conf;ps -ef | grep redis | grep 6381 > /dev/null && echo "redis6381 Successful opening" || echo "redis6381 Open failure"
;;
stop)
killall ./redis-server6381 2> /dev/null;sleep 1;ps -ef | grep -v grep | grep redis-server6381 && echo "redis6381 Shutdown failure" || echo "redis6381 Shut down successfully"
;;
restart)
killall ./redis-server6381 2> /dev/null;sleep 1;ps -ef | grep -v grep | grep redis-server6381 && echo "redis6381 Shutdown failure" || echo "redis6381 Shut down successfully"
./redis-server6381 /usr/local/redis/conf/redis6381.conf;ps -ef | grep redis | grep 6381 > /dev/null && echo "redis6381 Successful opening" || echo "redis6381 Open failure"
;;
*)
exit
exit
;;
esac
cat /usr/bin/redis6382
#!/bin/bash
cd /usr/local/redis/bin
case $1 in
start)
./redis-server6382 /usr/local/redis/conf/redis6382.conf;ps -ef | grep redis | grep 6382 > /dev/null && echo "redis6382 Successful opening" || echo "redis6382 Open failure"
;;
stop)
killall ./redis-server6382 2> /dev/null;sleep 1;ps -ef | grep -v grep | grep redis-server6382 && echo "redis6382 Shutdown failure" || echo "redis6382 Shut down successfully"
;;
restart)
killall ./redis-server6382 2> /dev/null;sleep 1;ps -ef | grep -v grep | grep redis-server6382 && echo "redis6382 Shutdown failure" || echo "redis6382 Shut down successfully"
./redis-server6382 /usr/local/redis/conf/redis6382.conf;ps -ef | grep redis | grep 6382 > /dev/null && echo "redis6382 Successful opening" || echo "redis6382 Open failure"
;;
*)
exit
exit
;;
esac
#加上可执行权限:
chmod +x /usr/bin/redis*
五、启动和关闭脚本使用说明:
1:启动:
[root@beiyong bin]# redis6380 start
redis6380 Successful opening
[root@beiyong bin]# redis6381 start
redis6381 Successful opening
[root@beiyong bin]# redis6382 start
redis6382 Successful opening
[root@beiyong bin]# ps -ef | grep -v grep | grep redis
root 29713 1 0 15:28 ? 00:00:00 ./redis-server6380 127.0.0.1:6380
root 29723 1 0 15:28 ? 00:00:00 ./redis-server6381 127.0.0.1:6381
root 29732 1 0 15:28 ? 00:00:00 ./redis-server6382 127.0.0.1:6382
2:关闭:
[root@beiyong bin]# redis6380 stop
redis6380 Shut down successfully
[root@beiyong bin]# ps -ef | grep -v grep | grep redis
root 29723 1 0 15:28 ? 00:00:00 ./redis-server6381 127.0.0.1:6381
root 29732 1 0 15:28 ? 00:00:00 ./redis-server6382 127.0.0.1:6382
3:重启:
[root@beiyong bin]# redis6380 restart
redis6380 Shut down successfully
redis6380 Successful opening
[root@beiyong bin]# ps -ef | grep -v grep | grep redis
root 29723 1 0 15:28 ? 00:00:00 ./redis-server6381 127.0.0.1:6381
root 29732 1 0 15:28 ? 00:00:00 ./redis-server6382 127.0.0.1:6382
root 29759 1 0 15:30 ? 00:00:00 ./redis-server6380 127.0.0.1:6380
[root@beiyong bin]# redis6381 restart
redis6381 Shut down successfully
redis6381 Successful opening
[root@beiyong bin]# ps -ef | grep -v grep | grep redis
root 29732 1 0 15:28 ? 00:00:00 ./redis-server6382 127.0.0.1:6382
root 29759 1 0 15:30 ? 00:00:00 ./redis-server6380 127.0.0.1:6380
root 29776 1 0 15:30 ? 00:00:00 ./redis-server6381 127.0.0.1:6381
六、#开机自启:
echo "/usr/local/redis/bin/redis-server6380 /usr/local/redis/conf/redis6380.conf" >> /etc/rc.local
……
……
注意事项,多实列下还需要保持不一致的修改处(按自己的需求修改,配置文件默认是注释掉的):
dbfilename dump.rdb #多实例情况下需要修改,例如dump.6380.rdb
appendfilename "appendonly.aof" #多实例情况下需要修改,例如 appendonly_6380.aof
cluster-config-file /opt/redis/6380/nodes.conf #多实例情况下需要修改,例如/6380/