Centos 7 Redis 安装并开机启动
一、下载并编译
cd /usr/local/bin
目录可以自己选,我将redis安装到/usr/local/bin目录下。
wget http://download.redis.io/releases/redis-5.0.5.tar.gz
tar xzf redis-5.0.5.tar.gz
cd redis-5.0.5
make
使用make命令之前,需要先安装gcc,如果在安装gcc之前执行了make命令,等安装gcc之后再执行make命令,会报“jemalloc/jemalloc.h: No such file or directory”错,此时需要执行“make distclean && make”命令,这个命令会先清理上次编译的残留文件再执行编译。
完成第一步之后,redis-cli和redis-server这两个客户端和服务文件就放在redis-5.0.5/src下面,redis.conf文件在redis-5.0.5下面
二、配置开机启动脚本
vim /etc/init.d/redis
#!/bin/sh # chkconfig: 2345 80 90 # description: Start and Stop redis # PATH=/usr/local/bin:/sbin:/usr/bin:/bin REDISPORT=6379 #端口 EXEC=/usr/local/bin/redis-5.0.5/src/redis-server #redis-server路径 REDIS_CLI=/usr/local/bin/redis-5.0.5/src/redis-cli #redis_cli路径 PIDFILE=/var/run/redis_$REDISPORT.pid CONF="/usr/local/bin/redis-5.0.5/redis.conf" #redis.conf路径 AUTH="mypassword123" #密码信息 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 ..." if [ -z $AUTH ] then $REDIS_CLI -p $REDISPORT SHUTDOWN else $REDIS_CLI -a $AUTH -p $REDISPORT SHUTDOWN fi 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
使用以下命令添加开机启动服务
systemctl enable redis
三、配置
#开启后台运行
daemonize yes
#配置密码
requirepass 123456
#设置绑定ip
bind 127.0.0.1 192.168.137.100