确保系统已更新:
sudo yum update -y
安装必要的开发工具和依赖:
sudo yum groupinstall "Development Tools" -y
sudo yum install wget tcl -y
下载 Redis 6.0.16 源代码:
wget https://download.redis.io/releases/redis-6.0.16.tar.gz
解压源代码:
tar xzf redis-6.0.16.tar.gz
cd redis-6.0.16
编译安装 Redis:
make
sudo make install
创建 Redis 配置目录和数据目录:
sudo mkdir -p /etc/redis /var/lib/redis
复制默认配置文件:
sudo cp redis.conf /etc/redis/
编辑配置文件:
sudo vi /etc/redis/redis.conf
修改以下关键配置项:
daemonize yes
pidfile /var/run/redis_6379.pid
logfile "/var/log/redis_6379.log"
dir /var/lib/redis
# 如果需要远程访问,注释掉下面这行
# bind 127.0.0.1
protected-mode no # 如果允许远程访问,需要设置为no
创建 Redis 服务文件:
sudo vi /etc/systemd/system/redis.service
添加以下内容:
[Unit]
Description=Redis In-Memory Data Store
After=network.target
[Service]
User=redis
Group=redis
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecStop=/usr/local/bin/redis-cli shutdown
Restart=always
[Install]
WantedBy=multi-user.target
创建 Redis 用户并设置权限:
sudo adduser --system --group --no-create-home redis
sudo chown -R redis:redis /var/lib/redis
sudo chmod 770 /var/lib/redis
重新加载 systemd 配置:
sudo systemctl daemon-reload
启动 Redis 服务:
sudo systemctl start redis
设置开机启动:
sudo systemctl enable redis
检查服务状态:
sudo systemctl status redis
使用 Redis 客户端连接测试:
redis-cli ping
应该返回 PONG
测试基本操作:
redis-cli
127.0.0.1:6379> set test "Hello Redis"
OK
127.0.0.1:6379> get test
"Hello Redis"
防火墙设置(如果需要远程访问):
sudo firewall-cmd --permanent --add-port=6379/tcp
sudo firewall-cmd --reload
设置密码(在 redis.conf 中):
requirepass yourpassword
启用持久化(在 redis.conf 中):
appendonly yes
appendfilename "appendonly.aof"
编译错误:如果遇到编译错误,可能是 GCC 版本过低。Redis 6.x 需要 GCC 5.3 或更高版本。
bash
sudo yum install centos-release-scl
sudo yum install devtoolset-9
scl enable devtoolset-9 bash
内存分配错误:如果启动时报错关于 overcommit memory
bash
sudo sysctl vm.overcommit_memory=1
THP 警告:Redis 建议禁用透明大页
bash
echo never > /sys/kernel/mm/transparent_hugepage/enabled
并添加到 /etc/rc.local
使其永久生效按照以上步骤操作后,Redis 6.0.16 应该已经在您的 CentOS 7 系统上成功安装并运行。