下面我按常见场景给你说明 CentOS 上 Redis 的设置方法,从 安装 → 基础配置 → 常用设置 → 安全配置 → 开机自启,你可以按需查看。
yum install -y gcc make
cd /usr/local/src
wget https://download.redis.io/redis-stable.tar.gz
tar -zxvf redis-stable.tar.gz
cd redis-stable
make
make install
安装完成后,Redis 可执行文件在:
/usr/local/bin/redis-server
/usr/local/bin/redis-cli
mkdir -p /etc/redis
cp /usr/local/src/redis-stable/redis.conf /etc/redis/6379.conf
编辑配置:
vim /etc/redis/6379.conf
bind 0.0.0.0
⚠️ 如果只本机访问:
bind 127.0.0.1
port 6379
requirepass YourPassword
登录方式:
redis-cli -a YourPassword
daemonize yes
logfile "/var/log/redis.log"
dir /var/lib/redis
创建目录:
mkdir -p /var/lib/redis
maxmemory 512mb
maxmemory-policy allkeys-lru
redis-server /etc/redis/6379.conf
ps -ef | grep redis
netstat -tunlp | grep 6379
vim /etc/systemd/system/redis.service
内容如下:
[Unit]
Description=Redis
After=network.target
[Service]
ExecStart=/usr/local/bin/redis-server /etc/redis/6379.conf
ExecStop=/usr/local/bin/redis-cli shutdown
Restart=always
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl start redis
systemctl enable redis
systemctl status redis
firewall-cmd --add-port=6379/tcp --permanent
firewall-cmd --reload
redis-cli
127.0.0.1:6379> auth YourPassword
127.0.0.1:6379> ping
PONG
示例:
rename-command FLUSHALL ""
rename-command CONFIG ""
tail -f /var/log/redis.log
netstat -tunlp | grep 6379
如果你愿意,我可以帮你:
直接告诉我你的 **CentOS 版本 **即可。