Linux学习之Redis安装与开机自启
一、yum 方式安装redis
- 通过yum install redis 默认安装的是最新的redis
- 输入y 继续执行
二、检测redis是否成功启动
- redis-server 命令启动
- 配置redis.conf 绑定ip地址
- 保护模式 由yes改为no
- 设置redis密码 requirepass
- Redis守护进程
定义:守护进程(daemon)
是一类在后台运行的特殊进程,用于执行特定的系统任务。很多守护进程在系统引导的时候启动,并且一直运行直到系统关闭。另一些只在需要的时候才启动,完成任务后就自动结束。
Redis
默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes
启用守护进程,设置为no
- 启动方式 是修改的配置文件生效
redis-server /etc/redis.conf
三、设置redis开机自启
- 脚本材料一
- 脚本材料二
- 创建
redis
脚本文件
在/etc/init.d/
目录下创建脚本文件redis
- 编写文件脚本
#!/bin/sh
# chkconfig: 2345 10 90
# description: Start and Stop redis
REDISPORT=6379
EXEC=/usr/bin/redis-server
CLIEXEC=/usr/bin/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF=/etc/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
- 启动脚本
- 授权脚本文件
chmod 777 redis
四、检测redis是否开机启动
- 将脚本加入到系统服务中
chkconfig --add redis
- chkconfig 命令用于检查,设置系统的各种服务。
此处直接配置开启自启动chkconfig redis on
将报错误:service redis does not support chkconfig
在启动脚本开头添加如下两行注释以修改其运行级别:
#!/bin/sh
# chkconfig: 2345 90 10
# description: Redis is a persistent key-value database
#再设置即可成功。
3. 设置为开机自启动服务
chkconfig redis on
4. 打开服务命令
service redis start
5. 关闭服务命令
service redis stop
五、redis添加环境变量
- 修改
.bash_profile
添加环境变量
[root@localhost src]# vi ~/.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
export REDIS_HOME=/opt/redis-5.0.3/
export PATH=$PATH:$REDIS_HOME/src
#PATH=$PATH:$HOME/bin
#export PATH
- 使环境变量生效
#source ~/.bash_profile
六、相关链接
https://www.cnblogs.com/likwo/p/5903396.html
https://www.cnblogs.com/skyessay/p/6433349.html