redis启动警告及info查看redis

redis启动3个警告解决方法

11980:M 08 Dec 11:30:51.347 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

11980:M 08 Dec 11:30:51.347 # 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.

11980:M 08 Dec 11:30:51.347 # 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.第一个提示somaxconn这个值为128太小了,这个值是系统的网络连接队列大小,而redis的TCP backlog设置的值为511,因此受限,所以修改下系统的值

$ vim /etc/sysctl.conf
$ net.core.somaxconn = 20480  #最大队列长度,应付突发的大并发连接请求,默认为128
$ net.ipv4.tcp_max_syn_backlog = 20480  #半连接队列长度,此值受限于内存大小,默认为1024
$ sysctl -p  #使参数生效

2.overcommit_memory设置为0,在低内存条件下可能会保存失败,修复方法是在sysctl.conf下添加vm.overcommit_memory = 1

vm.overcommit_memory不同的值说明:
0 表示检查是否有足够的内存可用,如果是,允许分配;如果内存不够,拒绝该请求,并返回一个错误给应用程序。
1 允许分配超出物理内存加上交换内存的请求
2 内核总是返回true

redis的数据回写机制分为两种

  • 同步回写即SAVE命令。redis主进程直接写数据到磁盘。当数据量大时,这个命令将阻塞,响应时间长
  • 异步回写即BGSAVE命令。redis 主进程fork一个子进程,复制主进程的内存并通过子进程回写数据到磁盘。

由于RDB文件写的时候fork一个子进程。相当于复制了一个内存镜像。当时系统的内存是4G,而redis占用了近3G的内存,因此肯定会报内存无法分配。如果 「vm.overcommit_memory」设置为0,在可用内存不足的情况下,就无法分配新的内存。如果 「vm.overcommit_memory」设置为1。 那么redis将使用交换内存。

修改方案:

$ vim /etc/sysctl.conf
$ vm.overcommit_memory = 1  
$ sysctl -p  #使参数生效

3.关闭THP透明内存
Transparent Huge Pages (THP)告警,这是一个关于透明内存巨页的话题。简单来说内存可管理的最小单位是page,一个page通常是4kb,那1M内存就会有256个page,CPU通过内置的内存管理单元管理page表记录。Huge Pages就是表示page的大小已超过4kb了,一般是2M到1G,它的出现主要是为了管理超大内存。个人理解上TB的内存。而THP就是管理Huge Pages的一个抽象层次,根据一些资料显示THP会导致内存锁影响性能,所以一般建议关闭此功能。

/sys/kernel/mm/transparent_hugepage/enabled有三个值,如下:

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

# always 尽量使用透明内存,扫描内存,有512个 4k页面可以整合,就整合成一个2M的页面
# never 关闭,不使用透明内存
# madvise 避免改变内存占用

修改方法:

$ vim /etc/rc.local
$ echo never > /sys/kernel/mm/transparent_hugepage/enabled  #在开机脚本里追加此命令

再启动,则不报错了

以上参考:https://www.jianshu.com/p/da69edda2a43
https://blog.csdn.net/a491857321/article/details/52006376


设置Redis最大占用内存

Redis需要设置最大占用内存吗?如果Redis内存使用超出了设置的最大值会怎样?

Redis设置最大占用内存,打开redis配置文件,找到如下段落,设置maxmemory参数,maxmemory是bytes字节类型,注意转换。修改如下所示:

vim redis.conf

# In short... if you have slaves attached it is suggested that you set a lower
# limit for maxmemory so that there is some free RAM on the system for slave
# output buffers (but this is not needed if the policy is 'noeviction').
#
# maxmemory <bytes>
maxmemory 268435456

本机服务器redis配置文件路径:/etc/redis/6379.conf,由于本机自带内存只有1G,一般推荐Redis设置内存为最大物理内存的四分之三,所以设置0.75G,换成byte是751619276.

可以在CentOS下输入命令:find / -name redis查找redis目录:

[root@iZ94r80gdghZ ~]# find / -name redis
/usr/share/nginx/html/blog.tanteng.me/wp-content/cache/supercache/blog.tanteng.me/tag/redis
/etc/redis
/var/redis

Redis配置文件一般在etc下的redis安装目录下。

Redis使用超过设置的最大值

如果Redis的使用超过了设置的最大值会怎样?我们来改一改上面的配置,故意把最大值设为1个byte试试。

# output buffers (but this is not needed if the policy is 'noeviction').
#
# maxmemory <bytes>
maxmemory 1

打开debug模式下的页面,提示错误:OOM command not allowed when used memory > ‘maxmemory’.

设置了maxmemory的选项,redis内存使用达到上限。可以通过设置LRU算法来删除部分key,释放空间。默认是按照过期时间的,如果set时候没有加上过期时间就会导致数据写满maxmemory。

如果不设置maxmemory或者设置为0,64位系统不限制内存,32位系统最多使用3GB内存。

LRU是Least Recently Used 近期最少使用算法。

volatile-lru -> 根据LRU算法生成的过期时间来删除。
allkeys-lru -> 根据LRU算法删除任何key。
volatile-random -> 根据过期设置来随机删除key。
allkeys->random -> 无差别随机删。
volatile-ttl -> 根据最近过期时间来删除(辅以TTL)
noeviction -> 谁也不删,直接在写操作时返回错误。

如果设置了maxmemory,一般都要设置过期策略。打开Redis的配置文件有如下描述,Redis有六种过期策略:

Vim

# volatile-lru -> remove the key with an expire set using an LRU algorithm
# allkeys-lru -> remove any key accordingly to the LRU algorithm
# volatile-random -> remove a random key with an expire set
# allkeys-random -> remove a random key, any key
# volatile-ttl -> remove the key with the nearest expire time (minor TTL)
# noeviction -> don't expire at all, just returnan error on write operations

那么打开配置文件,添加如下一行,使用volatile-lru的过期策略:

maxmemory-policy volatile-lru

保存文件退出,重启redis服务。

以上参考:https://blog.csdn.net/weixin_41541272/article/details/81872938


info命令查看Redis内存使用情况

[root@iZ94r80gdghZ src]# ./redis-cli
127.0.0.1:6379> info
  • Memory

used_memory:13490096 //数据占用了多少内存(字节)
used_memory_human:12.87M //数据占用了多少内存(带单位的,可读性好)
used_memory_rss:13490096 //redis占用了多少内存
used_memory_peak:15301192 //占用内存的峰值(字节)
used_memory_peak_human:14.59M //占用内存的峰值(带单位的,可读性好)
used_memory_lua:31744 //lua引擎所占用的内存大小(字节)
mem_fragmentation_ratio:1.00 //内存碎片率
mem_allocator:libc //redis内存分配器版本,在编译时指定的。有libc、jemalloc、tcmalloc这3种。

以上参考:https://blog.csdn.net/ahaotata/article/details/84938359
https://www.ywnds.com/?p=7113
https://baijiahao.baidu.com/s?id=1610139396248906453&wfr=spider&for=pc


查看系统内存大小及使用情况

输入free -h命令,在结果中找到total即可查看到系统总的内存情况

[root@crawl-01 bin]# free -h
              total        used        free      shared  buff/cache   available
Mem:           7.6G        7.2G        211M        612K        254M        218M
Swap:            0B          0B          0B

面是一些命令的集合,供参考:

uname -a # 查看内核/操作系统/CPU信息的linux系统信息  
head -n l /etc/issue # 查看操作系统版本  
cat /proc/cpuinfo # 查看CPU信息  
hostname # 查看计算机名的linux系统信息命令  
lspci -tv # 列出所有PCI设备   
lsusb -tv # 列出所有USB设备的linux系统信息命令  
lsmod # 列出加载的内核模块   
env # 查看环境变量资源  
free -m # 查看内存使用量和交换区使用量   
df -h # 查看各分区使用情况  
du -sh # 查看指定目录的大小   
grep MemTotal /proc/meminfo # 查看内存总量  
grep MemFree /proc/meminfo # 查看空闲内存量   
uptime # 查看系统运行时间、用户数、负载  
cat /proc/loadavg # 查看系统负载磁盘和分区   
mount | column -t # 查看挂接的分区状态  
fdisk -l # 查看所有分区   
swapon -s # 查看所有交换分区  
hdparm -i /dev/hda # 查看磁盘参数(仅适用于IDE设备)   
dmesg | grep IDE # 查看启动时IDE设备检测状况网络  
ifconfig # 查看所有网络接口的属性   
iptables -L # 查看防火墙设置  
route -n # 查看路由表   
netstat -lntp # 查看所有监听端口  
netstat -antp # 查看所有已经建立的连接   
netstat -s # 查看网络统计信息进程  
ps -ef # 查看所有进程   
top # 实时显示进程状态用户  
w # 查看活动用户   
id # 查看指定用户信息  
last # 查看用户登录日志   
cut -d: -f1 /etc/passwd # 查看系统所有用户  
cut -d: -f1 /etc/group # 查看系统所有组   
crontab -l # 查看当前用户的计划任务服务  
chkconfig –list # 列出所有系统服务   
chkconfig –list | grep on # 列出所有启动的系统服务程序  
rpm -qa # 查看所有安装的软件包   
cat /proc/cpuinfo :查看CPU相关参数的linux系统命令  
cat /proc/partitions :查看linux硬盘和分区信息的系统信息命令   
cat /proc/meminfo :查看linux系统内存信息的linux系统命令  
cat /proc/version :查看版本,类似uname -r   
cat /proc/ioports :查看设备io端口  
cat /proc/interrupts :查看中断   
cat /proc/pci :查看pci设备的信息  
cat /proc/swaps :查看所有swap分区的信息  

查看硬盘大小

# fdisk -l | grep Disk
Disk /dev/cciss/c0d0: 146.7 GB, 146778685440 bytes

总结:硬盘大小146.7G,即厂商标称的160G

以上参考:https://jingyan.baidu.com/article/6181c3e0d7d8aa152ef153a8.html
https://blog.csdn.net/zhangliao613/article/details/79021606

猜你喜欢

转载自blog.csdn.net/xc_zhou/article/details/93137815
今日推荐