Centos7安装Redis5详细教程并配置自启动

安装Redis

解压文件

tar xvf redis-5.0.4.tar.gz 

rm -rf redis-5.0.4.tar.gz  # 删除文件

进入redis目录安装

cd redis-5.0.4/

make && make install

修改配置文件redis.conf

vi redis.conf 
# 注释掉这句
	# bind 127.0.0.1
# 把这句改成no,开启远程连接
	protected-mode no

返回键 再输入 :wq 保存并退出

前台启动(建议改为后台)

 redis-server redis.conf 

后台启动

vi redis.conf # 修改配置文件

# 修改为yes,开启线程守护
	daemonize yes

测试

redis-server redis.conf # 启动
redis-cli # 连接

设置开机自启

vi /etc/init.d/redis
#!/bin/sh
# chkconfig:   2345 90 10
# description:  Redis is a persistent key-value database
PATH=/usr/local/bin:/sbin:/usr/bin:/bin

REDISPORT=6379
EXEC=/usr/local/bin/redis-server
REDIS_CLI=/usr/local/bin/redis-cli

PIDFILE=/var/run/redis.pid

CONF="/home/leyou/redis-5.0.4/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 does not exist, 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

授予文件权限

chmod 755 /etc/init.d/redis

chkconfig --add /etc/init.d/redis 

chkconfig redis on

启动

redis-server redis.conf

redis-cli

开发端口

firewall-cmd --zone=public --add-port=6379/tcp --permanent

firewall-cmd --reload

你可能感兴趣的:(Centos7安装Redis5详细教程并配置自启动)