meishaoming / blog

MIT License
1 stars 2 forks source link

redis #103

Open meishaoming opened 4 years ago

meishaoming commented 4 years ago

安装

wget http://download.redis.io/redis-stable.tar.gz
tar xf redis-stable.tar.gz
cd redis-stable
make

出现报错:

    CC server.o
In file included from server.c:30:0:
server.h:1045:5: 错误:expected specifier-qualifier-list before ‘_Atomic’
     _Atomic unsigned int lruclock; /* Clock for LRU eviction */

原因是定主数据结构里用了 _Atomic 这个关键字,而系统里的 gcc 版本还不支持这个关键字。

sam redis-stable gcc --version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39)
Copyright © 2015 Free Software Foundation, Inc.
本程序是自由软件;请参看源代码的版权声明。本软件没有任何担保;
包括没有适销性和某一专用目的下的适用性担保。

安装 clang 用 clang 编译:

sudo yum install -y clang
CC=clang make -j8

在 src/ 下生成了几个可执行文件:

  1. redis-server
  2. redis-cli
  3. redis-sentinel
  4. redis-check-rdb
  5. redis-check-aof
  6. redis-benchmark

启动

./redis-server 启动 redis

启动信息:

sam src ./redis-server 
26796:C 22 May 2020 16:33:41.620 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
26796:C 22 May 2020 16:33:41.620 # Redis version=6.0.3, bits=64, commit=00000000, modified=0, pid=26796, just started
26796:C 22 May 2020 16:33:41.620 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 6.0.3 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 26796
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

26796:M 22 May 2020 16:33:41.621 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
26796:M 22 May 2020 16:33:41.621 # Server initialized
26796:M 22 May 2020 16:33:41.621 # 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.
26796:M 22 May 2020 16:33:41.621 # 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.
26796:M 22 May 2020 16:33:41.622 * Ready to accept connections

从其中可以看到一些信息:

  1. 当前版本、进程id
  2. 用的默认配置,如果要指定配置文件使用 ./redis-server /path/to/redis.conf
  3. TCP backlog 就是监听队列大小 511 的设置未生效,因为 Centos 系统的默认限制没改 /proc/sys/net/core/somaxconn 默认是 128
  4. 操作系统内存分配策略 overcommit_memory=0,建议设置为 1
  5. 操作系统默认启用了 Transparent Huge Pages,建议关闭

可见 redis 非常依赖操作系统的一些配置优化。

netstat 查看一下端口使用情况:

sam src$ sudo netstat -lnapt
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      26796/./redis-serve 

试试