官网
下载并解压安装
#下载安装包
wget https://github.com/redis/redis/archive/7.2.4.tar.gz
#创建redis7文件夹
mkdir redis7
#进入安装目录
cd redis7/
#解压压缩包
tar -zxvf ../7.2.4.tar.gz
#检查是否安装gcc
gcc -v
-bash: gcc: command not found (未安装)
#安装Redis依赖环境gcc
yum -y install gcc-c++
#进入Redis安装目录
cd redis-7.2.4/
#在安装目录执行make命令
make && make install
#看到以下文件代表安装成功
[root@lavm-kixcj1bq2r bin]# ls
redis-check-aof redis-cli redis-server
redis-benchmark redis-check-rdb redis-sentinel
#回到Redis安装目录
/opt/redis7/redis-7.2.4
#创建myredis文件夹
mkdir myredis
#将Redis安装目录下的conf文件复制到myredis
[root@lavm-kixcjxxxx redis-7.2.4]# cp -v redis.conf myredis/redis7.conf
#编辑Redis7.conf,按i进入插入模式
vim redis7.conf
#Redis 将以守护进程方式运行,即在后台默默运行,不再占用终端。
#这通常用于生产环境中,以确保 Redis 在后台稳定运行,而不受终端关闭的影响。
daemonize no -> daemonize yes
#当 `protected-mode` 设置为 `no` 时,Redis 禁用保护模式。在禁用保护模式的情况下,
#Redis 可以接受来自任何网络地址的连接。这通常用于允许外部系统或应用程序连接到 Redis 服务器。
protected-mode yes -> protected-mode no
#将bind 127.0.0.1直接注释掉,意味着 Redis 不再绑定到特定的 IP 地址,
#而是允许来自任何网络地址的连接。这将使 Redis 监听所有可用的网络接口,而不仅仅是本地回环接口。
bind 127.0.0.1 -::1 -> #bind 127.0.0.1 -::1
#设置Redis连接密码
#requirepass foobared -> requirepass youpassword
#配置文件修改完成
esc :wq!
启动Redis服务
[root@lavm-xxxxxx myredis]# redis-server /opt/redis7/redis-7.2.4/myredis/redis7.conf
15918:C 12 Jan 2024 00:35:00.552 # 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.
修复完成后
[root@lavm-kixcxxxxx myredis]# redis-cli -a xxxx
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> keys *
(empty array)
127.0.0.1:6379>
一切OK
修复启动报错
#进入sysctl.conf,按i进入插入模式
#写入vm.overcommit_memory = 1
vim /etc/sysctl.conf
如:
# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
vm.overcommit_memory = 1
esc :wq! 退出
#重启服务器
reboot
设置Redis开启自启动
#创建 Redis 用户组
sudo groupadd redis
#创建 Redis 用户并将其添加到 Redis 组:
sudo useradd -g redis --no-create-home --system redis
#设置 Redis 配置文件的权限
sudo chown redis:redis /opt/redis7/redis-7.2.4/myredis/redis7.conf
#创建并编辑 Redis systemd 服务文件:
sudo vim /etc/systemd/system/redis7.service
[Unit]
Description=Redis Server
After=network.target
[Service]
ExecStart=/usr/local/bin/redis-server /opt/redis7/redis-7.2.4/myredis/redis7.conf #路径要对
ExecStop=/usr/local/bin/redis-cli shutdown
Restart=always
User=redis
Group=redis
RuntimeDirectory=redis
[Install]
WantedBy=multi-user.target
#启用和启动 Redis 服务:
##systemctl enable:该命令用于启用服务,使得服务在系统启动时自动启动。通过执行以下命令:
sudo systemctl enable redis7
##systemctl start:该命令用于手动启动一个服务,即立即启动服务而不等待系统重启。
sudo systemctl start redis7
#检查 Redis 服务状态:
sudo systemctl status redis7