linux部署redis7.0.7版本

优化主机

# 1.设置主机名
hostname redis
echo redis > /etc/hostname

# 2.关闭防火墙和linux
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i 's#SELINUX=enforcing#SELINUX=disabled#' /etc/selinux/config

# 3.修改系统时间时区
timedatectl set-timezone Asia/Shanghai
ntpdate ntp1.aliyun.com
# 有时间服务器可以再设置下时间同步

# 4.设置资源限制参数
cat >>  /etc/security/limits.conf << EOF
# * :代表对所有用户生效(也可指定具体用户名,如 root user1 等,精准限制特定用户 )。
# soft/hard :是限制类型,soft 为软限制,达到时系统通常会警告;hard 是硬限制,是严格上限,soft 不能超过 hard 。
# nofile :控制最大文件描述符数量
# nproc :控制最大进程数
* soft nofile 65535
* hard nofile 65535
* soft nproc  65535
* hard nproc  65535
EOF

# 5. 设置内核参数
cat >> /etc/sysctl.d/99-sysctl.conf << EOF
# 设置系统级别的 “最大文件句柄数” 上限 。
fs.file-max=2097152
# 设置单个进程可打开的 “最大文件描述符数量” 限制 
fs.nr_open=2097152 
# 控制 Linux 内核对 “内存过度申请(Overcommit)” 的处理策略,"1"代表允许过度申请。
vm.overcommit_memory=1
#设置 TCP 协议中 “半连接队列(SYN Backlog)” 的最大长度 。
net.ipv4.tcp_max_syn_backlog=2048 
EOF
sysctl -p

# 6. 关闭透明大页
echo never > /sys/kernel/mm/transparent_hugepage/enabled
cat >> /etc/rc.local << EOF
echo never > /sys/kernel/mm/transparent_hugepage/enabled
EOF
chmod +x /etc/rc.local

# 7. 配置OOM Killer特性优化
for redis in $(pgrep -f "redis-server")
do
    echo -17 > /prov/${redis}/oom_adj
done

安装redis

官网下载地址: https://download.redis.io/releases

# 1. 下载并编译redis
yum install -y gcc automake autoconf libtool make
wget https://download.redis.io/releases/redis-7.0.7.tar.gz
mkdir /data
tar -xf redis-7.0.7.tar.gz -C /data
cd /data/
mv redis-7.0.7 redis
cd redis/
make
cd src 
make PREFIX=/data/redis install 

# 2. 创建目录,用户,组
mkdir /data/redis/log
mkdir /data/redis/data
mkdir /data/redis/conf

useradd redis
chown -R redis:redis /data/redis
chmod -R 755 /data/redis

# 3. 拷贝默认配置文件到配置文件目录
cp /data/redis/redis.conf /data/redis/conf/

# 4. 创建环境变量
vim  /etc/profile
PATH=/data/redis/bin:$PATH
source /etc/profile

# 5. 修改redis默认配置文件
cat  /data/redis/conf/redis.conf  
# 基础配置
bind 0.0.0.0
port 6379
loglevel notice
logfile "/data/redis/log/redis.log"
pidfile /data/redis/redis.pid
daemonize yes
tcp-backlog 600 
timeout 0
tcp-keepalive 0

# 持久化配置
dir /data/redis/data
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite yes
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 1024mb
aof-load-truncated yes
aof-rewrite-incremental-fsync yes
save 900 1
save 300 10
save 60 10000
dbfilename dump.rdb
rdbcompression yes
stop-writes-on-bgsave-error yes
rdbchecksum yes 

# 安全配置
requirepass 1qaz@WSX

# 慢查询配置
slowlog-log-slower-than 1000000 
slowlog-max-len 50 

#参数优化
maxclients 10000
maxmemory 2G
maxmemory-policy allkeys-lru
maxmemory-samples 5 
lua-time-limit 5000 

# 6. 起停redis
su - redis
redis-server /data/redis/conf/redis.conf
netstat -lntup
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      26404/redis-server

redis-cli -a 1qaz@WSX shutdown
posted @ 2025-07-22 22:30  阿峰博客站  阅读(15)  评论(0)    收藏  举报