Centos7 安装redis 5.0.8 开机自启动
redis安装
将安装包移动到linux上
执行解压
tar -xvf redis-5.0.8.tar.gz
修改redis文件夹名称
mv redis-5.0.8 redis
修改redis权限
chmod 755 redis
编译redis
因为redis是c语言写的所以需要编译安装
进入redis目录
cd redis
进行编译安装
make $$ make install
修改安装目录下的配置文件,可以外网访问
vi redis.conf
修改如下配置
#bind 127.0.0.1 # 将这行代码注释,监听所有的ip地址,外网可以访问 protected-mode no # 把yes改成no,允许外网访问 daemonize yes # 把no改成yes,后台运行
设置开机自启动
输入命令创建新文件
vi /etc/init.d/redis
写入shell脚本
1 #!/bin/sh 2 # chkconfig: 2345 90 10 3 # description: Redis is a persistent key-value database 4 PATH=/usr/local/bin:/sbin:/usr/bin:/bin 5 6 REDISPORT=6379 7 EXEC=/usr/local/bin/redis-server 8 REDIS_CLI=/usr/local/bin/redis-cli 9 10 PIDFILE=/var/run/redis.pid 11 12 CONF="/usr/local/leyou/redis/redis.conf" 13 14 case "$1" in 15 start) 16 if [ -f $PIDFILE ] 17 then 18 echo "$PIDFILE exists, process is already running or crashed" 19 else 20 echo "Starting Redis server..." 21 $EXEC $CONF 22 fi 23 if [ "$?"="0" ] 24 then 25 echo "Redis is running..." 26 fi 27 ;; 28 stop) 29 if [ ! -f $PIDFILE ] 30 then 31 echo "$PIDFILE does not exist, process is not running" 32 else 33 PID=$(cat $PIDFILE) 34 echo "Stopping ..." 35 $REDIS_CLI -p $REDISPORT SHUTDOWN 36 while [ -x ${PIDFILE} ] 37 do 38 echo "Waiting for Redis to shutdown ..." 39 sleep 1 40 done 41 echo "Redis stopped" 42 fi 43 ;; 44 restart|force-reload) 45 ${0} stop 46 ${0} start 47 ;; 48 *) 49 echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2 50 exit 1 51 esac
保存并退出
注意:以下信息需要根据安装目录进行调整:
EXEC=/usr/local/bin/redis-server # 执行脚本的地址
REDIS_CLI=/usr/local/bin/redis-cli # 客户端执行脚本的地址
PIDFILE=/var/run/redis.pid # 进程id文件地址
设置权限
chmod 755 /etc/init.d/redis
启动测试
/etc/init.d/redis start
启动成功会得到如下信息
[root@localhost redis]# /etc/init.d/redis start
Starting Redis server...
53790:C 15 Apr 2020 20:29:27.953 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
53790:C 15 Apr 2020 20:29:27.953 # Redis version=5.0.8, bits=64, commit=00000000, modified=0, pid=53790, just started
53790:C 15 Apr 2020 20:29:27.953 # Configuration loaded
Redis is running...
设置开机自启动
chkconfig --add /etc/init.d/redis #添加shell脚本到服务列表
chkconfig redis on #设置开机自启动
本文来自博客园,作者:BaldHead,转载请注明原文链接:https://www.cnblogs.com/strict/p/12708224.html