zl程序教程

您现在的位置是:首页 >  其他

当前栏目

Centos7安装Redis5.0.5并加入Systemd服务

2023-03-07 09:40:07 时间

Centos7编译安装Redis5.0.5

1. 安装gcc-c++, tcl

yum install gcc-c++ tcl

2. 解压缩, 编译, 测试

tar zxvf redis-5.0.5.tar.gz
make
make test

3. 安装至/opt

make PREFIX=/opt/redis/redis-5.0.5 install
# 创建软链
ln -s redis-5.0.5 latest

4. 配置文件, 在源文件目录下有例子 redis.conf, 最后的配置内容为(后半部分使用默认, 无改动)

[root@p01 ~]# cat /opt/redis/latest/conf/redis_16379.conf | grep -v '^$'|grep -v '^#'|grep -v '^;'
bind 192.168.123.32
protected-mode yes
port 16379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised auto
pidfile /var/run/redis_16379.pid
loglevel notice
logfile "/data/redis/logs/redis_16379.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /data/redis/db/
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
requirepass rzczurzlx4xzs|yjpkdjzhljlevY0bLh
....

5. 在配置daemonize为no的时候, 直接用命令行启动测试, 观察日志输出

./bin/redis-server ./conf/redis_16379.conf

6. 解决warning

对于Increased maximum number of open files to 10032的问题,增加限制需要修改/etc/security/limits.conf (或者 /etc/security/limits.d/20-nproc.conf), 增加或修改为以下内容

*          soft    nofile    10240
*          hard    nofile    10240
*          soft    nproc     10240
*          hard    nproc     10240
root       soft    nproc     unlimited

需要重启, 用 ulimit -n 检查

对于 /proc/sys/net/core/somaxconn 和 overcommit_memory is set to 0, 修改/etc/sysctl.conf , 增加

net.core.somaxconn = 1024
vm.overcommit_memory = 1

然后执行 sysctl -p

对于transparent_hugepage, 首先实时修改

echo madvise > /sys/kernel/mm/transparent_hugepage/enabled

检查

[root@middle ~]# cat /sys/kernel/mm/transparent_hugepage/enabled
[always] madvise never
[root@middle ~]# cat /sys/kernel/mm/transparent_hugepage/defrag
[always] madvise never
# 以上都需要变成madvise 

加入启动自动修改

# 在/etc/rc.local中增加如下内容
 
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
echo madvise > /sys/kernel/mm/transparent_hugepage/enabled
fi

然后chmod +x /etc/rc.d/rc.local (/etc/rc.local是这个文件的ln) ,这样启动时才会执行

7. 加入Systemd服务. 增加 /lib/systemd/system.redis.service, 内容如下

[Unit]
Description=Redis
After=network.target
 
[Service]
Type=forking
PIDFile=/var/run/redis_16379.pid
ExecStart=/opt/redis/latest/bin/redis-server /opt/redis/latest/conf/redis_16379.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/opt/redis/latest/bin/redis-cli -p 16379 shutdown
PrivateTmp=true
 
[Install]
WantedBy=multi-user.target

注意: 这边并没有使用 #ExecStop=/bin/kill -s QUIT $MAINPID 这样的命令来停止redis, 因为使用这个语句在运行 systemctl stop redis后, redis并未执行关闭动作, 而是直接退出. 这时候用 systemctl status redis 查看状态是failed. 只有用ExecStop=/opt/redis/latest/bin/redis-cli -p 16379 shutdown 才能正确停止redis, 即使conf中配置了口令, 这里也不需要指定口令.

加入服务并启动

systemctl enable redis.service
systemctl start redis
systemctl status redis

Centos7编译安装Redis6.0.10

需要先安装gcc新版才能编译Redis 6.x,centos7 默认的 gcc 版本为:4.8.5 < 5.3 无法编译

yum install centos-release-scl
yum install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils

# 临时有效,退出 shell 或重启会恢复原 gcc 版本
sudo scl enable devtoolset-9 bash

# 长期有效
sudo echo "source /opt/rh/devtoolset-9/enable" >>/etc/profile

# 解压缩
tar xf redis-6.0.5.tar.gz
#编译
cd redis-6.0.5
make
# 如果需要make test, 就需要再yum install tcl
# 安装
make install