使用的服务器为centos7.4
redis版本为6.2.6
下载redis源码包
wget https://download.redis.io/releases/redis-6.2.6.tar.gz
解压安装包
tar xf redis-6.2.6.tar.gz
编译 需要下载好C 的编译环境
yum install gcc -y
安装到/opt 目录下
make install PREFIX=/opt/redis6
编辑profile 文件在最后一行加入
vim /etc/profile
export REDIS_HOME=/opt/redis6
export PATH=$PATH:$REDIS_HOME/bin
编辑完记得source 一下
source /etc/profile
在源码目录下的utils里有一个install_server.sh 文件
Welcome to the redis service installer
This script will help you easily set up a running redis server
This systems seems to use systemd.
Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!
在centos7下执行会出现这个问题 在查找相关资料后 需要注释这个文件里面的
#bail if this system is managed by systemd
#_pid_1_exe="$(readlink -f /proc/1/exe)"
#if [ "${_pid_1_exe##*/}" = systemd ]
#then
# echo "This systems seems to use systemd."
# echo "Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!"
# exit 1
#fi
这段代码
后面默认配置完成就能自动启动
但是这个有个问题 不可以用systemctl 来管理服务
用service来管理服务的时候,是在/etc/init.d/目录中创建一个脚本文件,来管理服务的启动和停止
systemctl也有一个默认的目录在管理服务的启动和停止
在/lib/systemd/system 目录下创建一个 redis.service
里面内容如下
Description=Redis
After=network.target
[Service]
ExecStart=/opt/redis6/bin/redis-server /etc/redis/6379.conf --daemonize no
ExecStop=/opt/redis6/bin/redis-cli -h 127.0.0.1 -p 6379 shutdown
[Install]
WantedBy=multi-user.target
[Unit] 表示这是基础信息
Description 是描述
After 是在那个服务后面启动,一般是网络服务启动后启动
[Service] 表示这里是服务信息
ExecStart 是启动服务的命令
ExecStop 是停止服务的指令
[Install] 表示这是是安装相关信息
WantedBy 是以哪种方式启动:multi-user.target表明当系统以多用户方式(默认的运行级别)启动时,这个服务需要被自动运行。
创建软连接 为了系统重启时自启动服务准备
ln -s /lib/systemd/system/redis.service /etc/systemd/system/multi-user.target.wants/redis.service
刷新配置
systemctl daemon-reload
使用systemctl 启动之前需要先把redis的进程杀死
ps aux|grep redis
kill -9 [pid]
启动redis
systemctl start redis
停止redis
systemctl stop redis
重启redis
systemctl restart redis
开机自启动redis
systemctl enable redis
禁止开机启动
systemctl disable redis
查看状态
systemctl status redis