# 安装必要的依赖和库

sudo dnf groupinstall "Development Tools"

# 创建 redis 用户

用 root 用户启动 redis 是非常危险的,所以先创建一个 redis 用户

h
useradd redis

# 下载 redis

Redis-download

这里选择最新的 Stable 版本 7.2.4

# 编译安装

解压之后直接编译安装

h
make -j && make PREFIX=/usr/local/redis install

# 配置 redis

将下载的 redis 软件包中的 redis.conf 文件复制到 /etc/local/redis/ 目录下

编辑 redis.conf 文件,主要修改以下几项

  • requirepass 这是访问密码,建议设置一个复杂一点的
  • daemonize 是否以守护进程方式启动 设置为 yes
  • dir redis 数据存储目录 设置为 /home/redis/
  • pidfile redis 进程 pid 文件路径 设置为 /home/redis/redis.pid
  • appendonly 是否开启 AOF 持久化 设置为 yes

其他的都可以保持默认设置,如果要开放 redis 的远程访问,可以修改 bind 和 port

# 使用 systemd 来管理 redis-server

创建 redis.service 文件

# /etc/systemd/system/redis.service
[Unit]
Description=Redis Server
After=network.target
[Service]
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/redis.conf
ExecStop=/usr/local/redis/bin/redis-cli -a {此处填写密码} shutdown
Type=forking
User=redis
Group=redis
Restart=always
[Install]
WantedBy=multi-user.target

然后启动 redis

systemctl start redis

加入开机自启动

systemctl enable redis

# 启动中遇到的问题

# 问题一:

如果是使用 systemctl start redis 命令启动 redis, 启动不了

但是如果直接使用命令 /usr/local/redis/bin/redis-server /usr/local/redis/redis.conf 就没问题

可能是 ulimit -n 设置的太小了

我们设大一点 ulimit -n 65535

持久化设置:

vim /etc/security/limits.conf
#文件末尾加入以下配置
*       hard    nofile  65535
*       soft    nofile  65535

# 问题二:

可能会报以下错误,但是依旧启动成功

WARNING Memory overcommit must be enabled! Without it, 
a background save or replication may fail under low memory condition. 
Being disabled, it can also cause failures without low memory condition, 
see https://github.com/jemalloc/jemalloc/issues/1328. 
To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then 
reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.

这个警告是 Redis 提示你的系统当前的内存过度提交(Memory Overcommit)被禁用。
Redis 在进行一些后台保存(background save)或复制(replication)等操作时,需要系统启用内存过度提交。
这里把这个配置打开.

vim /etc/sysctl.conf
#在文件最后一行加
vm.overcommit_memory = 1

重载配置

sysctl -p

# 结束

至此,redis 也已经安装完了

请我喝杯[咖啡]~( ̄▽ ̄)~*

一个放羊娃 微信支付

微信支付

一个放羊娃 支付宝

支付宝