Redis Ubuntu配置常见问题及解决方法
sudo ss -ltnp 'sport = :6379'或sudo lsof -i :6379 -sTCP:LISTEN命令定位占用进程,记录PID后用sudo kill -9 <PID>强制终止;若需更换端口,修改/etc/redis/redis.conf中的port参数(如port 6380)并重启服务。sudo grep -E "^(port|bind|dir|daemonize)" /etc/redis/redis.conf检查关键配置项是否有重复(如多个port指令),或用redis-server /etc/redis/redis.conf --test-config命令测试配置文件合法性。redis)运行,尝试绑定1024以下端口(如6379)会因权限限制失败。解决方案有两种:一是赋予redis-server绑定低位端口的权限(sudo setcap 'cap_net_bind_service=+ep' /usr/bin/redis-server),二是修改配置文件将端口改为高位(如6380)。free -h查看内存使用情况,top查看CPU负载,若资源紧张,调整maxmemory参数(如maxmemory 512mb)限制Redis内存使用,或升级服务器配置。bind 127.0.0.1)。需修改/etc/redis/redis.conf中的bind参数为0.0.0.0(允许所有IP访问)或指定允许的IP段(如bind 192.168.1.100 10.0.0.1),修改后重启Redis服务。若Redis部署在公网环境,还需关闭保护模式(protected-mode no)或设置强密码(requirepass your_password)。maxmemory参数,Redis会持续占用内存直至系统耗尽,引发服务崩溃。需在配置文件中设置合理的最大内存(如maxmemory 2gb),并根据业务需求选择内存淘汰策略(如maxmemory-policy allkeys-lru,淘汰最近最少使用的键)。/etc/redis/redis.conf中设置save指令(如save 900 1表示900秒内至少1个键变化则保存),并指定dbfilename(如dump.rdb)和dir(数据存储路径,如/var/lib/redis);appendonly yes,设置appendfilename "appendonly.aof",并选择同步策略(如appendfsync everysec,每秒同步一次)。chmod 700 /etc/redis/redis.conf),会导致启动失败。需将配置文件权限改为644(sudo chmod 644 /etc/redis/redis.conf),并确保所有者为redis用户(sudo chown redis:redis /etc/redis/redis.conf)。/etc/redis/redis.conf文件,可从Redis GitHub仓库下载最新版本的配置文件(git clone https://github.com/redis/redis.git,cp redis/redis.conf /etc/redis/redis.conf),或通过sudo find / -name "redis.conf"命令查找配置文件位置。