编译安装Redis

编译安装Redis

ubuntu 22.04.3

1. 下载redis源码包

redis官网

下载最新的稳定版本。 (7.2)

2.编译安装

redis.tar.gz解压到 linxu下的 /usr/local/src 下,用make命令编译。需要一下依赖

  • pkg-config
  • gcc

如果报错缺失 ccpkg-config, 则需要安装对应的依赖

sudo apt-get install pkg-config
sudo apt-get install gcc

将redis安装到指定的目录下:

make PREFIX=/opt/redis install

此时会在 /opt/redis/bin 下生成相关的启动程序

  • redis-server 服务
  • redis-cli 客户端
  • redis-benchmark 性能测试工具
  • redis-check-aof
  • redis-check-rdb

复制一份 redis.conf文件到 /opt/redis/conf

mkdir /opt/redis/conf
cp /usr/local/src/redis.conf /opt/redis/conf

3.启动程序

前台启动

./redis-server ../conf/redis.conf 

后台启动

方式一:nohup

nohup redis-server &

方式二: 配置后台启动

修改 redis.conf配置文件 中的 daemonize属性值改为 yes即可

./redis-server ../conf/redis.conf

4. 注册service服务

redis注册为linxu服务,可开机启动

在我们的源码文件夹 /usr/local/src/redis/utils下有对应的工具包。 通过脚本 install_server.sh来注册服务。是交互式的,通过端口来区分实例,不过在新版linxu服务器上,建议使用 systemd 来管理服务,所以redis上会提示:

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!

可以通过修改安装的脚本来跳过校验, 注释下面的校验行就行了

#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
unset _pid_1_exe

不过不建议采用以上的方案, 应遵循新的linux管理方式, 采用 systemd

5. 注册为systemd服务

systemd-redis_server.service 文件拷贝到 /etc/systemd/system

sudo cp systemd-redis_server.service /etc/systemd/system/redis_server.service

根据自己需求定制化。 如下:

# 修改redis.conf
# 改为后台启动
daemoninze yes
# pid文件存放位置
pidfile /opt/redis/redis_6379.pid
# 日志存放位置
logfile /opt/redis/redis.log
# redis数据存放位置
dir /opt/redis


#  修改 redis_server.service 文件的启动文件
# 启动
ExecStart=/opt/redis/bin/redis-server /opt/redis/conf/redis.conf
# 停止
ExecStop=/opt/redis/bin/redis-cli shutdown
# 因为上面redis的配置 daemon 是 yes, 所以这里必须采用 forking
Type=forking
posted @   dongxp  阅读(213)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示