172173.com

Redis官网: https://redis.io/
参考博文《Redis源码编译》https://www.920430.com/archives/649a48c5.html

一、安装Redis-4.0.11

1.下载安装

1
2
3
4
5
cd /usr/local/src/
wget http://download.redis.io/releases/redis-4.0.14.tar.gz
tar xvf redis-4.0.14.tar.gz && cd redis-4.0.14
make PREFIX=/usr/local/redis-4.0.14 install
cd /usr/local/ && ln -s redis-4.0.14 redis

2.创建6379实例目录

1
2
3
4
mkdir -p /usr/local/redis/{var,conf}
mkdir -p /var/log/redis
mkdir -p /data/redis_data/6379
mkdir -p /data/redis_log

3.拷贝配置文件

1
2
# 默认配置文件
cp /usr/local/src/redis-4.0.14/redis.conf /usr/local/redis-4.0.14/conf/redis-6379.conf

4.配置启动脚本

启动脚本仅有start,stop功能

1
2
3
4
5
6
7
cp /usr/local/src/redis-4.0.14/utils/redis_init_script /etc/init.d/redis_6379
sed -i "/^EXEC/s/bin/redis\/bin/g" /etc/init.d/redis_6379
sed -i "/^CLIEXEC/s/bin/redis\/bin/g" /etc/init.d/redis_6379
sed -i "/^CONF/s/etc\/redis\//usr\/local\/redis\/conf\/redis-/g" /etc/init.d/redis_6379
chmod +x /etc/init.d/redis_6379
chkconfig --add redis_6379
chkconfig --level 2345 redis_6379 on

5.Redis-6379.conf 配置

  • 同时打开RDB和AOF持久化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
daemonize yes
pidfile /var/run/redis_6379.pid
port 6379
bind 0.0.0.0
timeout 0
loglevel notice
logfile /data/redis_log/redis-6379.log
databases 32
#save 900 1
save 300 100
#save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump-6379.rdb
dir /data/redis_data/6379
maxclients 10000
maxmemory 10GB
maxmemory-policy volatile-lru
maxmemory-samples 3

appendonly yes
appendfsync everysec
appendfilename appendonly-6379.aof
no-appendfsync-on-rewrite yes
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb

lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 1024

## Special encoding of small aggregate data types
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64

activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60

slave-serve-stale-data yes
slave-read-only no
slave-priority 100

6.启动服务

启动Redis

1
2
3
4
# 指定配置文件启动redis服务
/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis-6379.conf &

/etc/init.d/redis_6379 start

关闭Redis

1
2
3
4
# 关闭redis服务,关闭指定端口的redis
/usr/local/redis/bin/redis-cli -h 192.168.xx.xx -p 6379 shutdown

/etc/init.d/redis_6379 stop

二、redis-6379.log 可能遇到的警告

1.net.core.somaxconn

警告

1
11268:M 12 Aug 16:11:13.026 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

原因分析

  • net.core.somaxconn 是linux中的一个kernel参数,表示socket监听(listen)的backlog上限。backlog是socket的监听队列,当一个请求(request)尚未被处理或建立时,他会进入backlog。而socket server可以一次性处理backlog中的所有请求,处理后的请求不再位于监听队列中。当server处理请求较慢,以至于监听队列被填满后,新来的请求会被拒绝。
  • 所以说net.core.somaxconn限制了接收新 TCP 连接侦听队列的大小。
    对于一个经常处理新连接的高负载 web服务环境来说,默认的 128 太小了。大多数环境这个值建议增加到 1024 或者更多。

解决措施

1
2
3
4
vim /etc/sysctl.conf        # 增加内核参数
net.core.somaxconn = 1024

sysctl -p # 立即生效

2.overcommit_memory

警告

1
11268:M 12 Aug 16:11:13.026 # 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.

原因分析

设置内存分配策略(可选,根据服务器的实际情况进行设置)/proc/sys/vm/overcommit_memory 可选值:0、1、2:

  • 0, 表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。
  • 1, 表示内核允许分配所有的物理内存,而不管当前的内存状态如何。
  • 2, 表示内核允许分配超过所有物理内存和交换空间总和的内存
    注意:redis在dump数据的时候,会fork出一个子进程,理论上child进程所占用的内存和parent是一样的,比如parent占用的内存为8G,这个时候也要同样分配8G的内存给child,如果内存无法负担,往往会造成redis服务器的down机或者IO负载过高,效率下降。所以这里比较优化的内存分配策略应该设置为 1(表示内核允许分配所有的物理内存,而不管当前的内存状态如何)。

解决措施

1
2
3
4
vim /etc/sysctl.conf        # 增加内核参数
vm.overcommit_memory= 1

sysctl -p # 立即生效

3.transparent_hugepage

警告

1
11392:M 12 Aug 16:17:16.429 # 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.

解决措施

1
2
# 如下配置,同时追加到/etc/rc.local
echo never > /sys/kernel/mm/transparent_hugepage/enabled

三、Redis 客户端连接

1.linux redis-cli 连接

1
2
3
4
5
6
# Redis主备版连接
/usr/local/redis/redis-cli -h 192.168.xx.xx -p 6379
/usr/local/redis/redis-cli -h 192.168.xx.xx -p 6379 -a password # 带密码登陆

# Redis集群版连接
/usr/local/redis/redis-cli -c -h 192.168.xx.xx -p 6379

2.telnet 连接

1
2
3
4
5
6
7
# 不带密码
telnet 10.19.xx.xx 6379
info

# 带密码
telnet 10.19.xx.xx 6379
auth Xxxxxxxxxxxxxxxxxxx

 评论