在 CentOS 7 下编译安装 Redis 5

关键字

CentOS, Redis, CentOS 7, Redis 5

背景概述

Redis 是一个使用标准 C 编写的开源、支持网络、基于内存、可选持久性的键值对存储数据库。在 PHP 中普通场景中,通常搭配 Memcached 加上缓存,但如果需要持久化,或拆表,用 Memcached 实现都比较麻烦,尤其当需要更复杂的数据结构时,Memcached 需要自己处理,此时,我们可以使用地球上最快的 K-V 数据库 Redis。比如在 OpenStack 中广泛使用。

Redis 支持:

  • 数据结构:String、List、Set、Hash、Sorted Set、pub/sub、Transactions数据结构的存储
  • 数据持久化:可将内存中的数据异步保存到磁盘中,重启时可再次加载,而 Memcached 不支持数据持久存储。
  • 分布式存储:Redis 支持 master-slave 复制模式,Memcached 用一致性 hash 做分布式
  • 进程模型:Redis 是单进程单线程模型,只使用一个 CPU,但可开启多进程。
  • 存储优势:大文件 Memcached 更有优势,100 K 以下 Redis 完胜。

本文旨在帮助读者在 CentOS 7 下顺利安装 Redis 最新版,本安装过程应已经过笔者反复测试,以确保成功。

如果按此文安装时遇到任何问题,请留言或通过【桃花岛】官方联系我们。

软件依赖

  • CentOS 7
  • Redis 5.0.5 源码
  • libjemalloc

具体步骤

yum 安装

CentOS 7 仓库自带 Redis 服务,截止本文发布,版本是 Reids 3.2,如果可以满足,可使用 yum install 安装

[root@carbon ~]# yum list redis    
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyuncs.com
 * epel: mirror01.idc.hinet.net
 * extras: mirrors.aliyuncs.com
 * updates: mirrors.aliyuncs.com
Available Packages
redis.x86_64                       3.2.12-2.el7                        epel

安装 Redis 最新版

如果选择 jemalloc 为默认内存分配库,则先安装 jemalloc 依赖,如果选择使用 libc 则可以跳过这个步骤,后续有说明。

yum -y install jemalloc

接着下载官方源码编译

cd /usr/local/src/
wget http://download.redis.io/releases/redis-5.0.5.tar.gz
tar xzf redis-5.0.5.tar.gz
cd redis-5.0.5
make MALLOC=jemalloc
内存分配

C 语言对于如何分配内存有不同实现,有默认的 libc 中的,还有 ptmalloc、tcmalloc、jemalloc。Redis 编译时可用环境变量 MALLOC 指定内存分配,可使用 libc 的 malloc 或 jemalloc,这里我们使用的是更优秀的 jemalloc,源自 FreeBSD。

在其 README.md 中有关于 Allocator 的说明:

Allocator
---------

Selecting a non-default memory allocator when building Redis is done by setting
the `MALLOC` environment variable. Redis is compiled and linked against libc
malloc by default, with the exception of jemalloc being the default on Linux
systems. This default was picked because jemalloc has proven to have fewer
fragmentation problems than libc malloc.

To force compiling against libc malloc, use:

    % make MALLOC=libc

To compile against jemalloc on Mac OS X systems, use:

    % make MALLOC=jemalloc

可在 redis-cli 中使用 info 命令查看

[root@carbon ~]# /usr/local/src/redis-5.0.5/src/redis-cli
127.0.0.1:6379> info
# Server
redis_version:5.0.5
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:d2a25c4b2e8a6eb4
redis_mode:standalone
os:Linux 3.10.0-957.21.3.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:4.8.5
process_id:10517
run_id:c12083f239cddc8d716d24355fa02a7c07a50036
tcp_port:6379
uptime_in_seconds:19
uptime_in_days:0
hz:10
configured_hz:10
lru_clock:4796831
executable:/usr/local/src/redis-5.0.5/src/./redis-server
config_file:

# Clients
connected_clients:1
client_recent_max_input_buffer:2
client_recent_max_output_buffer:0
blocked_clients:0

# Memory
used_memory:913392
used_memory_human:891.98K
used_memory_rss:3284992
used_memory_rss_human:3.13M
used_memory_peak:913392
used_memory_peak_human:891.98K
used_memory_peak_perc:100.11%
used_memory_overhead:911878
used_memory_startup:862184
used_memory_dataset:1514
used_memory_dataset_perc:2.96%
allocator_allocated:879600
allocator_active:3247104
allocator_resident:3247104
total_system_memory:1927315456
total_system_memory_human:1.79G
used_memory_lua:37888
used_memory_lua_human:37.00K
used_memory_scripts:0
used_memory_scripts_human:0B
number_of_cached_scripts:0
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
allocator_frag_ratio:3.69
allocator_frag_bytes:2367504
allocator_rss_ratio:1.00
allocator_rss_bytes:0
rss_overhead_ratio:1.01
rss_overhead_bytes:37888
mem_fragmentation_ratio:3.73
mem_fragmentation_bytes:2405392
mem_not_counted_for_evict:0
mem_replication_backlog:0
mem_clients_slaves:0
mem_clients_normal:49694
mem_aof_buffer:0
mem_allocator:libc
active_defrag_running:0
lazyfree_pending_objects:0

# Persistence
loading:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1565077900
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:-1
rdb_current_bgsave_time_sec:-1
rdb_last_cow_size:0
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok
aof_last_cow_size:0

# Stats
total_connections_received:1
total_commands_processed:1
instantaneous_ops_per_sec:0
total_net_input_bytes:31
total_net_output_bytes:11468
instantaneous_input_kbps:0.00
instantaneous_output_kbps:0.00
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
expired_stale_perc:0.00
expired_time_cap_reached_count:0
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:0
migrate_cached_sockets:0
slave_expires_tracked_keys:0
active_defrag_hits:0
active_defrag_misses:0
active_defrag_key_hits:0
active_defrag_key_misses:0

# Replication
role:master
connected_slaves:0
master_replid:d5ff70f565105490a36bdf73982d04383d13409e
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

# CPU
used_cpu_sys:0.013336
used_cpu_user:0.006750
used_cpu_sys_children:0.000000
used_cpu_user_children:0.000000

# Cluster
cluster_enabled:0

# Keyspace
127.0.0.1:6379> exit
[root@carbon redis-5.0.5]# /usr/local/src/redis-5.0.5/src/redis-cli
127.0.0.1:6379> info
# Server
redis_version:5.0.5
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:d2a25c4b2e8a6eb4
redis_mode:standalone
os:Linux 3.10.0-957.21.3.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:4.8.5
process_id:14948
run_id:79ebed349321e7b1444e67dd72c1a5f094e3340b
tcp_port:6379
uptime_in_seconds:10
uptime_in_days:0
hz:10
configured_hz:10
lru_clock:4797202
executable:/usr/local/src/redis-5.0.5/src/./redis-server
config_file:

# Clients
connected_clients:1
client_recent_max_input_buffer:2
client_recent_max_output_buffer:0
blocked_clients:0

# Memory
used_memory:854176
used_memory_human:834.16K
used_memory_rss:10952704
used_memory_rss_human:10.45M
used_memory_peak:854176
used_memory_peak_human:834.16K
used_memory_peak_perc:100.12%
used_memory_overhead:840974
used_memory_startup:791280
used_memory_dataset:13202
used_memory_dataset_perc:20.99%
allocator_allocated:1375792
allocator_active:1617920
allocator_resident:8474624
total_system_memory:1927315456
total_system_memory_human:1.79G
used_memory_lua:37888
used_memory_lua_human:37.00K
used_memory_scripts:0
used_memory_scripts_human:0B
number_of_cached_scripts:0
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
allocator_frag_ratio:1.18
allocator_frag_bytes:242128
allocator_rss_ratio:5.24
allocator_rss_bytes:6856704
rss_overhead_ratio:1.29
rss_overhead_bytes:2478080
mem_fragmentation_ratio:13.49
mem_fragmentation_bytes:10140528
mem_not_counted_for_evict:0
mem_replication_backlog:0
mem_clients_slaves:0
mem_clients_normal:49694
mem_aof_buffer:0
mem_allocator:jemalloc-5.1.0
active_defrag_running:0
lazyfree_pending_objects:0

# Persistence
loading:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1565078280
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:-1
rdb_current_bgsave_time_sec:-1
rdb_last_cow_size:0
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok
aof_last_cow_size:0

# Stats
total_connections_received:1
total_commands_processed:1
instantaneous_ops_per_sec:0
total_net_input_bytes:31
total_net_output_bytes:11468
instantaneous_input_kbps:0.00
instantaneous_output_kbps:0.00
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
expired_stale_perc:0.00
expired_time_cap_reached_count:0
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:0
migrate_cached_sockets:0
slave_expires_tracked_keys:0
active_defrag_hits:0
active_defrag_misses:0
active_defrag_key_hits:0
active_defrag_key_misses:0

# Replication
role:master
connected_slaves:0
master_replid:7085bb7cd47cb2653d786599c4d902e31b673e59
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

# CPU
used_cpu_sys:0.003501
used_cpu_user:0.010503
used_cpu_sys_children:0.000000
used_cpu_user_children:0.000000

# Cluster
cluster_enabled:0

# Keyspace
127.0.0.1:6379> 

安装验证

运行 Redis 服务端

[root@carbon src]# cd /usr/src/redis-5.0.5/src/
[root@carbon src]# ./redis-server
7060:C 06 Aug 2019 15:07:27.324 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
7060:C 06 Aug 2019 15:07:27.324 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=7060, just started
7060:C 06 Aug 2019 15:07:27.324 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
7060:M 06 Aug 2019 15:07:27.325 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 5.0.5 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 7060
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

7060:M 06 Aug 2019 15:07:27.326 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
7060:M 06 Aug 2019 15:07:27.326 # Server initialized
7060:M 06 Aug 2019 15:07:27.327 # 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.
7060:M 06 Aug 2019 15:07:27.327 # 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.
7060:M 06 Aug 2019 15:07:27.327 * DB loaded from disk: 0.000 seconds
7060:M 06 Aug 2019 15:07:27.327 * Ready to accept connections

Clone 一个 SSH Session,执行结果如下即表示 Redis 5.0.5 已安装成功

[root@carbon ~]# cd /usr/src/redis-5.0.5/src/
[root@carbon src]# ./redis-cli
127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379> get foo
"bar"

问题汇总

问题:执行编译配置 apr 时提示:

zmalloc.h:50:31: fatal error: jemalloc/jemalloc.h: No such file or directory

解决:

yum -y install jemalloc
make MALLOC=jemalloc

相关文章

暂无

发表评论

电子邮件地址不会被公开。 必填项已用*标注