当前位置:网站首页>CentOS系统下Redis安装和自启动配置的步骤
CentOS系统下Redis安装和自启动配置的步骤
2022-07-07 09:06:00 【全栈程序员站长】
相信大家都知道Redis是一个C实现的基于内存、可持久化的键值对数据库,在分布式服务中常作为缓存服务。所以这篇文章将详细介绍在CentOS系统下如何从零开始安装到配置启动服务。有需要的可以参考借鉴。
一. 安装Redis
Redis的安装其实相当简单,推荐的方式是下载redis的源码,并在本机编译后安装。
首次进入主文件夹的下载目录下,执行wget下载源码
[[email protected] ~]$
cd
下载
[[email protected] 下载]$ wget http:
//download
.redis.io
/redis-stable
.
tar
.gz
接下来解压之后,移动到/usr/redis目录下
[[email protected] 下载]$
tar
-zxvf redis-stable.
tar
.gz
[[email protected] 下载]$
su
mv
redis-stable
/usr/redis
然后进入redis目录,执行make命令,编译redis源码
[[email protected] 下载]
# cd /usr/redis/
[[email protected] redis]
# make
编译完成之后,在src目录下有2个重要程序生成,一个是redis-server
,另一个是redis-cli
;接着进入src目录,执行make install
,这时会把这些可执行程序拷贝到/usr/local/bin
目录下,由于/usr/local/bin
是在系统的环境变量$PATH下定义的,因此终端在任意位置就可以执行redis-server
和redis-cli
了。
[[email protected] redis]
# cd src/
[[email protected] src]
# make install
至此安装redis的工作就完成了。
我们来看看编译出来的几个程序分别是干什么的:
redis-server
:顾名思义,redis服务
redis-cli
:redis client,提供一个redis客户端,以供连接到redis服务,进行增删改查等操作
redis-sentinel
:redis实例的监控管理、通知和实例失效备援服务
redis-benchmark
:redis的性能测试工具
redis-check-aof
:若以AOF方式产生日志,当意外发生时用来快速修复
redis-check-rdb
:若以RDB方式产生日志,当意外发生时用来快速修复
安装完成之后,启动redis-server
,并运行redis-cli
进行测试
[[email protected] ~]$ redis-server
[[email protected] ~]$ redis-cli
127.0.0.1:6379> PING
PONG
127.0.0.1:6379>
如此说明redis服务已经正常工作,如果redis服务未启动,则运行redis-cli
时会报Could not connect to Redis at 127.0.0.1:6379: Connection refused
的错误。
二. 配置自启动
为了让redis-server能在系统启动时自动运行,需要将redis服务作为守护进程(daemon)来运行,我们回到/usr/redis/
目录中找到一个redis.conf
的文件,这个文件是redis服务运行时加载的配置,我们先观察一下其中的内容
[[email protected] redis]$
vi
redis.conf
此文件内容非常长,但是大部分是注释,我们重点关注其中的几个设置daemonize
和pidfile
:
其中daemonize
默认值是false,pidfile
默认值是pidfile /var/run/redis_6379.pid
第一个表示是否daemon化,显然我们要把它改成daemonize yes
;
第二个表示当服务以守护进程方式运行时,redis默认会把pid写入/var/run/redis_6379.pid
文件,服务运行中该文件就存在,服务一旦停止该文件就自动删除,因而可以用来判断redis是否正在运行。
保存后退出。
有了基本配置,redis还需要有一个管理启动、关闭、重启的一个脚本。redis源码里其实已经提供了一个初始化脚本,位置在/usr/redis/utils/redis_init_script
。
我们来看看这个脚本做了些什么:
#!/bin/sh#
REDISPORT=6379
EXEC=
/usr/local/bin/redis-server
CLIEXEC=
/usr/local/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
脚本中指定了端口、server路径、cli路径、pidfile路径以及conf路径,上述标黄的地方都需要正确配置,多说一句,如果在安装时执行了make install
,那么这里的脚本不需要做多大改动,因为make install
把server和cli都拷到/usr/local/bin
下面了。
另外看到这里conf的路径,我们需要把redis目录下的redis.conf文件拷贝到/etc/redis/6379.conf
[[email protected] utils]
# cd /etc
[[email protected] etc]
# mkdir redis
[[email protected] etc]
# cp /usr/redis/redis.conf /etc/redis/6379.conf
接着将redis_init_script
脚本拷贝到/etc/init.d/redisd
[[email protected] etc]
# cp /usr/redis/utils/redis_init_script /etc/init.d/redisd
在/etc/init.d下的脚本都是可以在系统启动是自动启动的服务,而现在还缺一个系统启动时的配置:
[[email protected] zhxilin]
# chkconfig redisd on
然后就会发现报了一个错误:服务 redisd 不支持 chkconfig ?
这是因为我们需要在redis_init_script
的开头加一个小改动:
#!/bin/sh
# chkconfig: 2345 90 10
# description: Redis is a persistent key-value database
保存完重新拷贝到/etc/init.d/redisd
后,再运行chkconfig
就完成了。
一切就绪之后,可以执行以下命令检验service是否设置成功:
[[email protected] zhxilin]
# service redisd start
[[email protected] zhxilin]
# service redisd stop
等价于
[[email protected] zhxilin]
# /etc/init.d/redisd start
[[email protected] zhxilin]
# /etc/init.d/redisd stop
总结
最后重启一下系统吧,进入系统之后直接运行redis-cli检验redis服务是否已经自动运行了。以上就是这篇文章的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/113810.html原文链接:https://javaforall.cn
边栏推荐
- How to successfully pass the senior system architecture designer in the second half of the year?
- CSAPP bomb lab parsing
- What are the test preparation materials and methods for soft exam information processing technicians?
- Bookmarking - common website navigation for programmers
- Ffmpeg record a video command from RTSP
- Avoid mutating a prop directly since the value will be overwritten whenever the parent component
- Project ERROR: Unknown module(s) in QT: core gui
- Using tansformer to segment three-dimensional abdominal multiple organs -- actual battle of unetr
- 【STM32】实战3.1—用STM32与TB6600驱动器驱动42步进电机(一)
- 2021-04-08
猜你喜欢
Mpx 插件
Operation method of Orange Pie orangepi 4 lts development board connecting SATA hard disk through mini PCIe
从色情直播到直播电商
What does intermediate software evaluator test
[untitled]
[untitled]
Avoid mutating a prop directly since the value will be overwritten whenever the parent component
shardingsphere分库分表示例(逻辑表,真实表,绑定表,广播表,单表)
Unity script generates configurable files and loads
July 10, 2022 "five heart public welfare" activity notice + registration entry (two-dimensional code)
随机推荐
Is the gold content of intermediate e-commerce division in the soft exam high?
基于STC8G1K08的0.96寸IIC液晶屏驱动程序
Online hard core tools
[untitled]
The use of list and Its Simulation Implementation
Typescript interface inheritance
【推薦系統 01】Rechub
Différences entre les contraintes monotones et anti - monotones
Deep understanding of Apache Hudi asynchronous indexing mechanism
QT document
[machine learning 03] Lagrange multiplier method
【STM32】实战3.1—用STM32与TB6600驱动器驱动42步进电机(一)
Some online academic report websites and machine learning videos
seata 1.3.0 四種模式解决分布式事務(AT、TCC、SAGA、XA)
uniCloud
Find the greatest common divisor and the least common multiple (C language)
[untitled]
Une fois que l'uniapp a sauté de la page dans onlaunch, cliquez sur Event Failure resolution
When initializing 'float', what is the difference between converting to 'float' and adding 'f' as a suffix?
What are the test preparation materials and methods for soft exam information processing technicians?