Centos 源码安装redis4.0

Redis的服务器4.0版本源码编译安装

1.1 环境准备

  • 系统:Centos7
  • 版本:redis4.0
  • redis下载地址:https://redis.io/download

1.2 redis源码编译

yum -y install wget gcc gcc-c++ make tar openssl-devel cmake
tar xf redis-4.0.10.tar.gz -C /usr/local/src
cd /usr/local/src/redis-4.0.11/
make;make MALLOC=jemalloc;make PREFIX=/data/redis install
mkdir -p /data/redis/conf
cp /usr/local/src/redis-4.0.11/redis.conf /data/redis/conf/
cp /usr/local/src/redis-4.0.11/sentinel.conf /data/redis/conf/
ln -s /data/redis/bin/* /usr/local/bin/

1.3 redis常用命令介绍

redis-server    #Redis服务器的daemon启动程序
redis-cli   #Redis命令操作工具。当然,你也可以用telnet根据其纯文本协议来操作
redis-benchmark   #Redis性能测试工具,测试Redis在你的系统及你的配置下的读写性能。
redis-check-aof   #对更新日志appendonly.aof检查,是否可用,类似检查mysql binlog的工具
redis-check-dump #用于本地数据库rdb文件的检查

1.4 Redis的服务器启动和系统参数调整

简化redis配置文件
cp /data/redis/conf/redis.conf{,.bak}               #备份配置文件                    
egrep -v "^$|^#" /data/redis/conf/redis.conf.bak > /data/redis/conf/redis.conf#简化配置文件
mkdir -p /data/redis/db/        #创建redis数据目录
修改成以下设置cat -n conf/redis.conf | sed -n ‘1p;3p;4p;7p;9p;11p;21p’
 1  bind 0.0.0.0            #监听地址
 3  port 6379               #监听端口
 4  tcp-backlog 1024        #tcp连接数
 7  daemonize yes           #是否后台启动
 9  pidfile /data/redis/redis.pid   #pid存放目录
11  logfile "/data/redis/redis.log" #日志存放目录
21  dir /data/redis/db/                #工作目录
启动redis服务器

redis-server /data/redis/conf/redis.conf
netstat -antup | grep redis

关闭redis服务器

redis-cli shutdown
netstat -antup | grep redis#连接redis服务器

系统参数优化调整

#启动redis以后,我们查看系统日志cat /data/redis/redis.log cat /data/redis/redis.log 7702:C 20 Aug 04:09:02.029 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo7702:C 20 Aug 04:09:02.029 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=7702, just started7702:C 20 Aug 04:09:02.029 # Configuration loaded7703:M 20 Aug 04:09:02.033 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                 #警告提示1:系统文件描述符设置的太小了,才1024,我们最好设置到10032
.
.-__ ''-._ _.- . . ‘’-._ Redis 4.0.10 (00000000/0) 64 bit
.- .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.-.|'_.-'| Port: 6379 | -. ._ / _.-' | PID: 7703 -._ -._ -./ .-’ .-’
|-._-.
-.__.-' _.-'_.-'| | -.
-._ _.-'_.-' | http://redis.io -._ -._-..-'.-’ .-’
|-._-.
-.__.-' _.-'_.-'| | -.
-._ _.-'_.-' | -._ -._-.
.-‘_.-’ _.-’
-._ -..-’ _.-’
-._ _.-' -.
.-’
7703:M 20 Aug 04:09:02.035 # WARNING: The TCP backlog setting of 1024 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
                #由于/proc/sys/net/core/somaxconn设置为较低的128,因此无法强制设置1024的TCP backlog。
7703:M 20 Aug 04:09:02.035 # Server initialized7703:M 20 Aug 04:09:02.035 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add ‘vm.overcommit_memory = 1’ to /etc/sysctl.conf and then reboot or run the command ‘sysctl vm.overcommit_memory=1’ for this to take effect.
                #警告提示3:overcommit_memory=0为不允许超额抢占内存,但是,rdb保存可能会失败。建议将vm.overcommit_memory = 1进行修改
7703:M 20 Aug 04:09:02.035 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command ‘echo never > /sys/kernel/mm/transparent_hugepage/enabled’ as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
                #警告提示4:你的内核中启用了巨大内存页的支持,这将与redis的延迟内存使用冲突。
7703:M 20 Aug 04:09:02.035 * Ready to accept connections

(1)调整系统文件描述符

echo ulimit -n 65535 >>/etc/profile
Source /etc/profile #刷新下即生效
ulimit -n
65535

(2)调整系统的TCP连接数

sysctl -a | grep soma
sysctl: reading key “net.ipv6.conf.all.stable_secret”
sysctl: reading key “net.ipv6.conf.default.stable_secret”
sysctl: reading key “net.ipv6.conf.eth0.stable_secret”
sysctl: reading key “net.ipv6.conf.lo.stable_secret”
net.core.somaxconn = 128
echo “net.core.somaxconn = 10240” >> /etc/sysctl.conf

sysctl -p
net.core.somaxconn = 10240

(3)调整系统内存分配策略

echo “vm.overcommit_memory = 1” >> /etc/sysctl.conf
sysctl -p
net.core.somaxconn = 10240
vm.overcommit_memory = 1
vm.overcommit_memory参数说明:
根据内核文档,该参数有三个值,分别是:
0:当用户空间请求更多的内存时,内核尝试估算出剩余可用的内存。
1:当设这个参数值为1时,内核允许超量使用内存直到用完为止,主要用于科学计算
2:当设这个参数值为2时,内核会使用一个绝不过量使用内存的算法,即系统整个内存地址空间不能超过swap+50%的RAM值,50%参数的设定是在overcommit_ratio中设定。

(4)关闭系统内核的巨大内存页支持

echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag #添加到/etc/rc.local
echo ‘echo never > /sys/kernel/mm/transparent_hugepage/enabled’ >> /etc/rc.local
echo ‘echo never > /sys/kernel/mm/transparent_hugepage/defrag’ >> /etc/rc.local

(5)重启redis的服务器验证修改

redis-cli shutdown > /data/redis/redis.log #关闭redis #清空redis日志
redis-server /data/redis/conf/redis.conf        #启动redis

cat /data/redis/redis.log           #查看redis日志8064:C 20 Aug 04:22:42.049 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo8064:C 20 Aug 04:22:42.050 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=8064, just started8064:C 20 Aug 04:22:42.050 # Configuration loaded
.
.-__ ''-._ _.- . . ‘’-._ Redis 4.0.10 (00000000/0) 64 bit
.- .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.-.|'_.-'| Port: 6379 | -. ._ / _.-' | PID: 8065 -._ -._ -./ .-’ .-’
|-._-.
-.__.-' _.-'_.-'| | -.
-._ _.-'_.-' | http://redis.io -._ -._-..-'.-’ .-’
|-._-.
-.__.-' _.-'_.-'| | -.
-._ _.-'_.-' | -._ -._-.
.-‘_.-’ _.-’
-._ -..-’ _.-’
-._ _.-' -.
.-’
8065:M 20 Aug 04:22:42.055 # Server initialized8065:M 20 Aug 04:22:42.055 * DB loaded from disk: 0.000 seconds8065:M 20 Aug 04:22:42.055 * Ready to accept connections

1.5 Redis配置到系统服务[systemctl的方式]

配置过程:
进入/usr/lib/systemd/system/目录中,创建redis.service文件:
#表示基础信息
[Unit]
#描述
Description=Redis
#在哪个服务之后启动
After=syslog.target network.target remote-fs.target nss-lookup.target

#表示服务信息
[Service]
Type=forking
#注意:需要和redis.conf配置文件中的信息一致
PIDFile=/data/redis/redis.pid
#启动服务的命令
#redis-server安装的路径 和 redis.conf配置文件的路径
ExecStart=/usr/local/bin/redis-server /data/redis/conf/redis.conf
#重新加载命令
ExecReload=/bin/kill -s HUP $MAINPID
#停止服务的命令
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

#安装相关信息
[Install]
#以哪种方式启动
WantedBy=multi-user.target
#multi-user.target表明当系统以多用户方式(默认的运行级别)启动时,这个服务需要被自动运行。

文件创建好保存之后,要执行 systemctl daemon-reload 命令,使配置生效。

重新启动: systemctl start redis
开机启动: systemctl enable redis

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

山有木兮—

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值