Linux(centos7)安装redis服务并配置开机自启动
1.下载安装包并解压到/usr/local,redis的下载地址https://redis.io/download
·
本次演示系统为Centos7,安装的版本为redis-6.2.7
1.下载安装包并解压到/usr/local,redis的下载地址
https://redis.io/download/
# wget https://download.redis.io/releases/redis-6.2.7.tar.gz
# tar -zxvf redis-6.2.7.tar.gz -C /usr/local/
2.安装redis编译所需插件
# yum -y install gcc automake autoconf libtool make
3.编译安装make编译 make install安装 PREFIX指定安装目录
# cd /usr/local/redis-6.2.7
# make && make PREFIX=/usr/local/redis-6.2.7 install
4.运行redis,看到redis运行动画以后代表安装成功
# cd bin && ./redis-server
基础配置
#bind 代表可访问id,设置为*则允许所有ip访问
bind 127.0.0.1 -::1
#保护模式 默认外网是访问不了的,如需外网访问请设置为no,建议使用ssh进行连接redis 比较安全
protected-mode yes
#端口号
port 6379
#客户端闲置多久以后关闭 为0则不关闭
timeout 0
#是否以守护进程运行,windows不支持守护进程
daemonize yes
#日志记录的等级
loglevel notice
#redis库数量,默认16
databases 16
#redis连接访问的密码,默认是关闭的,建议开启
requirepass foobared
配置开启自启动,这样服务器宕机重启以后会自动开启
编辑vim /etc/init.d/redis文件
把下方代码复制进去
#!/bin/sh
# description: Start and Stop redis
REDISPORT=6379
EXEC=/usr/local/redis-6.2.7/src/redis-server
CLIEXEC=/usr/local/redis-6.2.7/src/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/usr/local/redis-6.2.7/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
;;
restart)
"$0" stop
sleep 3
"$0" start
;;
*)
echo "Please use start or stop or restart as first argument"
;;
esac
将脚本添加到chkconfig,开机自启动
# chkconfig --add redis //开启自启动
# chkconfig redis off //关闭自启动
# chkconfig --del redis //删除自启动
更多推荐
已为社区贡献1条内容
所有评论(0)