网站首页 > 技术教程 正文
如何在Linux中配置Redis服务并设置为开机自启
废话不多说,咱们直接开始。
1、修改redis.conf配置文件
配置redis.conf中daemonize为yes,确保守护进程开启。
大概在148行,当然你也可以使用:/daemonize搜索。
2、查看原生的redis启动脚本
[root@kingdom ~]# find / -name redis_init_script
/general-redis/redis-4.0/utils/redis_init_script
提示:
redis启动脚本一般在redis根目录的utils
(2)查看脚本
vim /general-redis/redis-4.0/utils/redis_init_script
提示:
注意以下标红的这几点。
因为我们下面的操作都是为它服务的。
其实就是把这些变量的值修改为我们自己redis的实际路径和配置文件。
补充一点:
大家可以多looklook人家的脚本,可以学到不少东西。
3、复制脚本redis_init_script
将redis_init_script复制到/etc/init.d/redis
这里是为了将其配置为服务,方便管理。
[root@kingdom ~]# cp /general-redis/redis-4.0/utils/redis_init_script /etc/init.d/redis
[root@kingdom ~]# ll /etc/init.d/redis
-rwxr-xr-x 1 root root 1098 Sep 20 10:11 /etc/init.d/redis
4、 修改脚本redis
注意:
这里修改的是/etc/init.d/redis脚本,它将在修改完后完全为我们服务。
vim /etc/init.d/redis
(1) 、添加如下图两行内容
# chkconfig: 2345 66 77
# description: redis service shell
关于为什么这样做,请参考在下之前的文章:
(2) 、修改server和cli路径
查看自己redis的server和cli绝对路径
我的是这个
/general-redis/redis4.0/bin
修改脚本如下:
(3) 、创建配置文件
原来的配置文件CONF="/etc/redis/${REDISPORT}.conf",这里的REDISPORT=6379。
即:CONF的位置在/etc/redis/6379.conf
这里我们可以和它保持一致,当然也可以自定位置。
我选择前者。
补充:要是选择后者需要指定CONF=你的配置文件实际位置
在/etc创建redis目录and将我们的配置文件cp到/etc/redis/下。
[root@kingdom bin]# mkdir /etc/redis
[root@kingdom bin]# cp /general-redis/redis4.0/etc/redis.conf /etc/redis/6379.conf
[root@kingdom bin]# ll /etc/redis/6379.conf
-rw-r--r-- 1 root root 58882 Sep 20 10:34 /etc/redis/6379.conf
别忘把redis脚本:wq保存退出。
最后补充一点:
如果你的redis设置了密码,stop时需要cli客户端指定密码登录来进行执行shutdown命令。
如下图:
$CLIEXEC -p $REDISPORT -a 123456 shutdown
5、 测试脚本
ps -ef | grep redis*
service redis start
service redis stop
6、 设置为开机启动
[root@kingdom ~]# chkconfig redis on
[root@kingdom ~]# chkconfig --list
测试完成,感谢大家支持。
7、补充
Redis脚本内容如下所示,或者大家可以留言。
#!/bin/sh
# chkconfig: 2345 66 77
# description: redis service shell
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
REDISPORT=6379
EXEC=/general-redis/redis4.0/bin/redis-server
CLIEXEC=/general-redis/redis4.0/bin/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.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
;;
*)
echo "Please use start or stop as first argument"
;;
esac
猜你喜欢
- 2025-05-23 CentOS6设置开机自启动
- 2025-05-23 快速搭建 SpringCloud 微服务开发环境的脚手架
- 2025-05-23 「Linux基础」开启SSH服务教程
- 2025-05-23 只有高手才知道的15个 Linux 趣味命令,你玩过几个?
- 2025-05-23 如何优雅的在 Linux 下开机自动重启脚本
- 2025-05-23 Linux 命令速查手册:这 30 个高频指令,拯救 90% 的运维小白!
- 2025-05-23 Linux系统工作管理Job Control详解
- 2025-05-23 Elasticsearch服务自动启动配置指南
- 2025-05-23 Mysql启动选项和配置文件
- 2025-05-23 Linux操作系统之常用命令
你 发表评论:
欢迎- 最近发表
-
- Win11学院:如何在Windows 11上使用WSL安装Ubuntu
- linux移植(Linux移植freemodbus)
- 独家解读:Win10预览版9879为何无法识别硬盘
- 基于Linux系统的本地Yum源搭建与配置(ISO方式、RPM方式)
- Docker镜像瘦身(docker 减小镜像大小)
- 在linux上安装ollama(linux安装locale)
- 渗透测试系统Kali推出Docker镜像(kali linux渗透测试技术详解pdf)
- Linux环境中部署Harbor私有镜像仓库
- linux之间传文件命令之Rsync傻瓜式教程
- 解决ollama在linux中安装或升级时,通过国内镜像缩短安装时长
- 标签列表
-
- 下划线是什么 (87)
- 精美网站 (58)
- qq登录界面 (90)
- nginx 命令 (82)
- nginx .http (73)
- nginx lua (70)
- nginx 重定向 (68)
- Nginx超时 (65)
- nginx 监控 (57)
- odbc (59)
- rar密码破解工具 (62)
- annotation (71)
- 红黑树 (57)
- 智力题 (62)
- php空间申请 (61)
- 按键精灵 注册码 (69)
- 软件测试报告 (59)
- ntcreatefile (64)
- 闪动文字 (56)
- guid (66)
- abap (63)
- mpeg 2 (65)
- column (63)
- dreamweaver教程 (57)
- excel行列转换 (56)
本文暂时没有评论,来添加一个吧(●'◡'●)