暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Redis 6.x/7.x Install on Centos7.9(单机多实例)

原创 OnTheRoad 2022-09-26
340

1. Redis 6.x/7.x Install on Centos7.9 单机多实例

1.1. 主机设置

1.1.1. 创建 Redis 用户

## 创建用户、用户组 groupadd redis && useradd -g redis redis ## 设置用户密码 echo "redis123" |passwd --stdin redis
## 配置用户环境变量 su - redis echo "export PATH=\$PATH:/usr/local/redis/bin" >> ~/.bash_profile

1.1.2. 安装依赖

yum install -y gcc-c++ autoconf automaker

1.1.3. 主机优化

  1. 设置内存 OOM 策略
cat >> /etc/sysctl.conf << EOF net.core.somaxconn = 32768 vm.overcommit_memory = 1 EOF sysctl -p
  1. 设置 Redis 可利用的 open files

该数值可限制 Redis 最大客户端连接数。maxclients = open files - 32

cat >> /etc/security/limits.conf << EOF
# For Redis
redis   soft    nproc   2047
redis   hard    nproc   16384
redis   soft    nofile  65535
redis   hard    nofile  65535
EOF
  1. 禁用 THP
## 查看 THP cat /sys/kernel/mm/transparent_hugepage/enabled [always] madvise never ## 禁用 THP echo "echo never > /sys/kernel/mm/transparent_hugepage/enabled" >> /etc/rc.local

重启系统,使设置生效。针对 RedHat 需将 /sys/kernel/mm/transparent_hugepage/enabled 修改为 /sys/kernel/mm/redhat_transparent_hugepage/enable

cat /sys/kernel/mm/transparent_hugepage/enabled always madvise [never]

1.2. 多实例安装 Redis

1.2.1. 编译安装

官方链接: https://download.redis.io/redis-stable.tar.gz

华为云镜像:https://repo.huaweicloud.com/redis/

## 1. 下载源码包 export VERSION=6.2.5 export VERSION=7.0.4 wget https://repo.huaweicloud.com/redis/redis-${VERSION}.tar.gz ## 2. 解压 tar -zxvf redis-${VERSION}.tar.gz ## 3. 编译 cd redis-${VERSION}/ ## 4. 编译 redis make ## 5. 安装 ## 将 Redis 安装到指定目录(默认安装位置 /usr/local/bin/redis-server) make PREFIX=/usr/local/redis-${VERSION} install

1.2.2. 创建软连接

创建软连接,便于后续版本升级。

export VERSION=7.0.4 ln -s /usr/local/redis-${VERSION} /usr/local/redis chown -R redis:redis /usr/local/redis* chmod -R 755 /usr/local/redis* [redis@localhost ~]# tree -L 2 /usr/local/redis-$VERSION /usr/local/redis-7.0.4 └── bin ├── redis-benchmark ├── redis-check-aof -> redis-server ├── redis-check-rdb -> redis-server ├── redis-cli ├── redis-sentinel -> redis-server └── redis-server 1 directory, 6 files [root@localhost ~]# ll /usr/local/|grep redis lrwxrwxrwx. 1 redis redis 22 Sep 26 15:16 redis -> /usr/local/redis-7.0.4 drwxr-xr-x. 3 redis redis 4096 Sep 26 11:52 redis-7.0.4

1.2.3. 多实例配置

1.2.3.1. 目录设置

export VERSION=7.0.4 export RDATA=/redis-data for REDIS_PORT in 6381 6382 6383 do echo ">>> ${REDIS_PORT}" mkdir -p $RDATA/${REDIS_PORT} cp ~/redis-${VERSION}/redis.conf $RDATA/${REDIS_PORT}/redis_${REDIS_PORT}.conf chown -R redis:redis $RDATA/${REDIS_PORT} chmod -R 755 $RDATA/${REDIS_PORT} chmod -R 644 $RDATA/${REDIS_PORT}/redis_${REDIS_PORT}.conf done
[root@localhost ~]# tree -L 2 $RDATA /redis-data/ ├── 6381 │ └── redis_6381.conf ├── 6382 │ └── redis_6382.conf └── 6383 └── redis_6383.conf

1.2.3.2. 准备配置文件

export RDATA=/redis-data for REDIS_PORT in 6381 6382 6383 do echo ">>> start for ${REDIS_PORT}" sed -i 's|^bind|#bind|g' $RDATA/${REDIS_PORT}/redis_${REDIS_PORT}.conf sed -i 's|^daemonize.*$|daemonize yes|g' $RDATA/${REDIS_PORT}/redis_${REDIS_PORT}.conf sed -i 's|^#.requirepass.*$|requirepass redis123|g' $RDATA/${REDIS_PORT}/redis_${REDIS_PORT}.conf sed -i "s|^dir.*$|dir $RDATA/${REDIS_PORT}/|g" $RDATA/${REDIS_PORT}/redis_${REDIS_PORT}.conf sed -i "s|^port.*$|port ${REDIS_PORT}|g" $RDATA/${REDIS_PORT}/redis_${REDIS_PORT}.conf sed -i "s|^pidfile.*$|pidfile /var/run/redis_${REDIS_PORT}.pid|g" $RDATA/${REDIS_PORT}/redis_${REDIS_PORT}.conf sed -i "s|^logfile.*$|logfile $RDATA/${REDIS_PORT}/redis_${REDIS_PORT}.log|g" $RDATA/${REDIS_PORT}/redis_${REDIS_PORT}.conf echo ">> config for ${REDIS_PORT}" cat $RDATA/${REDIS_PORT}/redis_${REDIS_PORT}.conf |grep -E '^#bind|^daemonize.*$|^requirepass.*$|^dir.*$|^logfile.*$|^port.*$' echo ">>> end for ${REDIS_PORT}" sleep 10 done

预期输出内容如下

>>> start for 6381 >> config for 6381 #bind 127.0.0.1 -::1 port 6381 daemonize yes pidfile /var/run/redis_6381.pid logfile /redis-data/6381/redis_6381.log dir /redis-data/6381/ requirepass redis123 >>> end for 6381

配置文件内容如下(可根据实际需要,进一步修改配置内容):

export RDATA=/redis-data [root@localhost ~]# grep -Ev '^\s*#|^$' $RDATA/6381/redis_6381.conf protected-mode yes port 6381 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize yes pidfile /var/run/redis_6381.pid loglevel notice logfile /redis-data/6381/redis_6381.log databases 16 always-show-logo no set-proc-title yes proc-title-template "{title} {listen-addr} {server-mode}" stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb rdb-del-sync-files no dir /redis-data/6381/ replica-serve-stale-data yes replica-read-only yes repl-diskless-sync yes repl-diskless-sync-delay 5 repl-diskless-sync-max-replicas 0 repl-diskless-load disabled repl-disable-tcp-nodelay no replica-priority 100 acllog-max-len 128 requirepass redis123 lazyfree-lazy-eviction no lazyfree-lazy-expire no lazyfree-lazy-server-del no replica-lazy-flush no lazyfree-lazy-user-del no lazyfree-lazy-user-flush no oom-score-adj no oom-score-adj-values 0 200 800 disable-thp yes appendonly no appendfilename "appendonly.aof" appenddirname "appendonlydir" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes aof-use-rdb-preamble yes aof-timestamp-enabled no slowlog-log-slower-than 10000 slowlog-max-len 128 latency-monitor-threshold 0 notify-keyspace-events "" hash-max-listpack-entries 512 hash-max-listpack-value 64 list-max-listpack-size -2 list-compress-depth 0 set-max-intset-entries 512 zset-max-listpack-entries 128 zset-max-listpack-value 64 hll-sparse-max-bytes 3000 stream-node-max-bytes 4096 stream-node-max-entries 100 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit replica 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 dynamic-hz yes aof-rewrite-incremental-fsync yes rdb-save-incremental-fsync yes jemalloc-bg-thread yes

1.2.3.3. 配置开机启停

  1. 创建 service 配置文件
for REDIS_PORT in 6381 6382 6383 do echo ">>> start for ${REDIS_PORT}" echo "[Unit]" >> /etc/systemd/system/redis-${REDIS_PORT}.service echo "Description=The redis-server Process Manager for ${REDIS_PORT}" >> /etc/systemd/system/redis-${REDIS_PORT}.service echo "After=syslog.target network.target" >> /etc/systemd/system/redis-${REDIS_PORT}.service echo "[Service]" >> /etc/systemd/system/redis-${REDIS_PORT}.service echo "Type=forking" >> /etc/systemd/system/redis-${REDIS_PORT}.service echo "User=redis" >> /etc/systemd/system/redis-${REDIS_PORT}.service echo "LimitNOFILE=65536" >> /etc/systemd/system/redis-${REDIS_PORT}.service echo "WorkingDirectory=/redis-data/${REDIS_PORT}" >> /etc/systemd/system/redis-${REDIS_PORT}.service echo "ExecStart=/usr/local/redis/bin/redis-server /redis-data/${REDIS_PORT}/redis_${REDIS_PORT}.conf" >> /etc/systemd/system/redis-${REDIS_PORT}.service echo "ExecStop=/usr/local/redis/bin/redis-cli -p ${REDIS_PORT} shutdown" >> /etc/systemd/system/redis-${REDIS_PORT}.service echo "[Install]" >> /etc/systemd/system/redis-${REDIS_PORT}.service echo "WantedBy=multi-user.target" >> /etc/systemd/system/redis-${REDIS_PORT}.service cat /etc/systemd/system/redis-${REDIS_PORT}.service sleep 10 done

预期的输出

>>> start for 6381 [Unit] Description=The redis-server Process Manager for 6381 After=syslog.target network.target [Service] Type=forking User=redis LimitNOFILE=65536 WorkingDirectory=/redis-data/6381 ExecStart=/usr/local/redis/bin/redis-server /redis-data/6381/redis_6381.conf ExecStop=/usr/local/redis/bin/redis-cli -p 6381 shutdown [Install] WantedBy=multi-user.target
  1. 加载 service 文件
systemctl daemon-reload
  1. 开机自启
## 批量启用开机自启 for REDIS_PORT in 6381 6382 6383 do echo ">>> enable redis-${REDIS_PORT}" systemctl enable redis-${REDIS_PORT} done
## 批量启动 redis 服务进程 for REDIS_PORT in 6381 6382 6383 do echo ">>> start redis-${REDIS_PORT}" systemctl start redis-${REDIS_PORT} done
## 批量停止 redis 服务进程 for REDIS_PORT in 6381 6382 6383 do echo ">>> stop redis-${REDIS_PORT}" systemctl stop redis-${REDIS_PORT} done
## 批量重启 redis 服务进程 for REDIS_PORT in 6381 6382 6383 do echo ">>> restart redis-${REDIS_PORT}" systemctl restart redis-${REDIS_PORT} done
## 批量查看 redis 进程状态 for REDIS_PORT in 6381 6382 6383 do echo ">>> status redis-${REDIS_PORT}" systemctl status redis-${REDIS_PORT} done
## 批量禁用 redis 开机自启 for REDIS_PORT in 6381 6382 6383 do echo ">>> disable redis-${REDIS_PORT}" systemctl disable redis-${REDIS_PORT} done
  1. 验证启动结果
[root@localhost ~]# ps -ef|grep redis redis 16650 1 0 15:34 ? 00:00:00 /usr/local/redis/bin/redis-server *:6381 redis 16662 1 0 15:34 ? 00:00:00 /usr/local/redis/bin/redis-server *:6382 redis 16674 1 0 15:34 ? 00:00:00 /usr/local/redis/bin/redis-server *:6383 root 16698 13584 0 15:34 pts/1 00:00:00 grep --color=auto redis

1.2.3.4. 手动启停 Redis

## 1. # 手动启动 su - redis export REDIS_PORT=6381 cd /usr/local/redis/bin ./redis-server /redis-data/${REDIS_PORT}/redis_${REDIS_PORT}.conf ## 2. 查看 redis 进程 [redis@localhost bin]$ ps -ef|grep redis root 17014 13584 0 15:38 pts/1 00:00:00 su - redis redis 17015 17014 0 15:38 pts/1 00:00:00 -bash redis 17077 1 0 15:39 ? 00:00:00 ./redis-server *:6381 redis 17090 17015 0 15:39 pts/1 00:00:00 ps -ef redis 17091 17015 0 15:39 pts/1 00:00:00 grep --color=auto redis [redis@localhost bin]$ ## 3. 连接 redis,密码为 redis123 /usr/local/redis/bin/redis-cli -c -h 127.0.0.1 -p ${REDIS_PORT} -a "redis123" ## 4. 结束 redis 进程 /usr/local/redis/bin/redis-cli -c -h 127.0.0.1 -p ${REDIS_PORT} -a "redis123" shutdown
最后修改时间:2022-09-26 19:44:27
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论