redis安装(linux Centos7)

配置文件

# Redis configuration file example.                                                                                                                                                                                                                                         
#                                                                                                                                                                                                                                                                           
# Note that in order to read the configuration file, Redis must be                                                                                                                                                                                                          
# started with the file path as first argument:                                                                                                                                                                                                                             
#                                                                                                                                                                                                                                                                           
# ./redis-server /path/to/redis.conf                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                            
# Note on units: when memory size is needed, it is possible to specify                                                                                                                                                                                                      
# it in the usual form of 1k 5GB 4M and so forth:                                                                                                                                                                                                                           
#                                                                                                                                                                                                                                                                           
# 1k => 1000 bytes                                                                                                                                                                                                                                                          
# 1kb => 1024 bytes                                                                                                                                                                                                                                                         
# 1m => 1000000 bytes                                                                                                                                                                                                                                                       
# 1mb => 1024*1024 bytes                                                                                                                                                                                                                                                    
# 1g => 1000000000 bytes                                                                                                                                                                                                                                                    
# 1gb => 1024*1024*1024 bytes                                                                                                                                                                                                                                               
#                                                                                                                                                                                                                                                                           
# units are case insensitive so 1GB 1Gb 1gB are all the same.                                                                                                                                                                                                               
                                                                                                                                                                                                                                                                            
#在不同环境下要修改的配置旁都有MODIFY标记                                                                                                                                                                                                                                   
                                                                                                                                                                                                                                                                            
#可访问redis的地址,暂不指定                                                                                                                                                                                                                                                
#bind 127.0.0.1                                                                                                                                                                                                                                                             
#保护模式,暂时不保护,因为redis没有绑定可访问地址                                                                                                                                                                                                                          
protected-mode no                                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                                            
#监听端口                                                                                                                                                                                                                                                                   
port 6379                                                                                                                                                                                                                                                                   
                                                                                                                                                                                                                                                                            
#请求囤积数                                                                                                                                                                                                                                                                 
tcp-backlog 511                                                                                                                                                                                                                                                             
                                                                                                                                                                                                                                                                            
# 客户端空闲timeout时间后关闭连接,0表示禁用                                                                                                                                                                                                                                
timeout 0                                                                                                                                                                                                                                                                   
                                                                                                                                                                                                                                                                            
#保持连接,默认300s                                                                                                                                                                                                                                                         
tcp-keepalive 300                                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                                            
################################# GENERAL #####################################                                                                                                                                                                                             
#以守护进程方式启动,即后台启动                                                                                                                                                                                                                                             
daemonize yes                                                                                                                                                                                                                                                               
                                                                                                                                                                                                                                                                            
#因为使用的是systemd的方式启动redis,所以开启下面配置                                                                                                                                                                                                                       
supervised auto                                                                                                                                                                                                                                                             
                                                                                                                                                                                                                                                                            
#指定pid文件,daemonized为yes时指定,默认值/var/run/redis_6379.pid,建议/run/redis_6379.pid                                                                                                                                                                                 
pidfile /run/redis_6379.pid                                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                                                            
#日志级别                                                                                                                                                                                                                                                                   
#debug 测试开发                                                                                                                                                                                                                                                             
#verbose 适中                                                                                                                                                                                                                                                               
#notice 生产环境                                                                                                                                                                                                                                                            
#warning                                                                                                                                                                                                                                                                    
#当前作为测试  MODIFY                                                                                                                                                                                                                                                       
loglevel debug                                                                                                                                                                                                                                                              
                                                                                                                                                                                                                                                                            
# Specify the log file name. Also the empty string can be used to force                                                                                                                                                                                                     
# Redis to log on the standard output. Note that if you use standard                                                                                                                                                                                                        
# output for logging but daemonize, logs will be sent to /dev/null                                                                                                                                                                                                          
#日志输出文件名,在daemonized为yes时默认输出到/dev/null(这是一个c类型的文件,代表空设备),为no时默认输出到标准输出  MODIFY                                                                                                                                                
logfile "/opt/redis/log/redis.log"                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                            
#数据库数量,保持默认值                                                                                                                                                                                                                                                     
databases 16                                                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                                            
#显示logo的时机,保持默认                                                                                                                                                                                                                                                   
always-show-logo no                                                                                                                                                                                                                                                         
                                                                                                                                                                                                                                                                            
#允许修改进程title以获取运行时信息(通过top和ps命令可查看到),保持默认                                                                                                                                                                                                       
set-proc-title yes                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                            
#修改的京城title格式,保持默认                                                                                                                                                                                                                                              
# Template variables are specified in curly brackets. The following variables are                                                                                                                                                                                           
# supported:                                                                                                                                                                                                                                                                
#                                                                                                                                                                                                                                                                           
# {title}           Name of process as executed if parent, or type of child process.                                                                                                                                                                                        
# {listen-addr}     Bind address or '*' followed by TCP or TLS port listening on, or                                                                                                                                                                                        
#                   Unix socket if only that's available.                                                                                                                                                                                                                   
# {server-mode}     Special mode, i.e. "[sentinel]" or "[cluster]".                                                                                                                                                                                                         
# {port}            TCP port listening on, or 0.                                                                                                                                                                                                                            
# {tls-port}        TLS port listening on, or 0.                                                                                                                                                                                                                            
# {unixsocket}      Unix domain socket listening on, or "".                                                                                                                                                                                                                 
# {config-file}     Name of configuration file used.                                                                                                                                                                                                                        
#                                                                                                                                                                                                                                                                           
proc-title-template "{title} {listen-addr} {server-mode}"                                                                                                                                                                                                                   
                                                                                                                                                                                                                                                                            
################################ SNAPSHOTTING  ################################                                                                                                                                                                                             
#rdb配置,同时开启rdb和aof后,恢复时只加载aof文件,除非开启了混合持久化模式,即同时使用rdb和aof,开启配置为aof-use-rdb-preamble yes                                                                                                                                         
#数据备份规则,在后有次修改就备份                                                                                                                                                                                                                         
# save   [  ...]                                                                                                                                                                                                                        
# 除特殊指定,默认值如下:                                                                                                                                                                                                                                                  
#   * After 3600 seconds (an hour) if at least 1 change was performed                                                                                                                                                                                                       
#   * After 300 seconds (5 minutes) if at least 100 changes were performed                                                                                                                                                                                                  
#   * After 60 seconds if at least 10000 changes were performed                                                                                                                                                                                                             
#                                                                                                                                                                                                                                                                           
#显示指定默认值                                                                                                                                                                                                                                                             
save 3600 1 300 100 60 10000                                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                                            
#后台备份错误停止写入                                                                                                                                                                                                                                                       
stop-writes-on-bgsave-error yes                                                                                                                                                                                                                                             
                                                                                                                                                                                                                                                                            
#rdb文件压缩                                                                                                                                                                                                                                                                
rdbcompression yes                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                            
#rdb文件校验,使格式更耐损坏,关闭有10%性能提升                                                                                                                                                                                                                             
rdbchecksum yes                                                                                                                                                                                                                                                             
                                                                                                                                                                                                                                                                            
#rdb文件名 MODIFY                                                                                                                                                                                                                                                           
dbfilename dump.rdb                                                                                                                                                                                                                                                         
                                                                                                                                                                                                                                                                            
# 未启用持久化时,删除磁盘中已有的持久化文件,必须同时禁用rdb和aof才生效                                                                                                                                                                                                    
rdb-del-sync-files no                                                                                                                                                                                                                                                       
                                                                                                                                                                                                                                                                            
#rdb文件保存补录,文件名为dbfilename指定 MODIFY                                                                                                                                                                                                                             
dir /opt/redis/data                                                                                                                                                                                                                                                         
                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                            
############################## APPEND ONLY MODE ###############################                                                                                                                                                                                             
#开启aof持久化                                                                                                                                                                                                                                                              
appendonly yes                                                                                                                                                                                                                                                              
#aof文件前缀,会生成3个文件 MODIFY                                                                                                                                                                                                                                          
appendfilename "appendonly.aof"                                                                                                                                                                                                                                             
#aof文件保存目录,是相对于dir配置的,如dir为/opt/redis/data,则aof保存目录为/opt/redis/data/aof  MODIFY                                                                                                                                                                     
appenddirname "aof"                                                                                                                                                                                                                                                         
#aof保存时机                                                                                                                                                                                                                                                                
# appendfsync always                                                                                                                                                                                                                                                        
appendfsync everysec                                                                                                                                                                                                                                                        
# appendfsync no                                                                                                                                                                                                                                                            
#保持默认                                                                                                                                                                                                                                                                   
no-appendfsync-on-rewrite no                                                                                                                                                                                                                                                
auto-aof-rewrite-percentage 100                                                                                                                                                                                                                                             
auto-aof-rewrite-min-size 64mb                                                                                                                                                                                                                                              
aof-load-truncated yes                                                                                                                                                                                                                                                      
#开启混合持久化                                                                                                                                                                                                                                                             
aof-use-rdb-preamble yes                                                                                                                                                                                                                                                    
aof-timestamp-enabled no                                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                            
#纯缓存模式                                                                                                                                                                                                                                                                 
#appendonly no                                                                                                                                                                                                                                                              
#save ""                                                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                            
################################# REPLICATION #################################                                                                                                                                                                                             
#主从复制配置,保持默认                                                                                                                                                                                                                                                     
replica-serve-stale-data yes                                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                                            
replica-read-only yes                                                                                                                                                                                                                                                       
                                                                                                                                                                                                                                                                            
repl-diskless-sync yes                                                                                                                                                                                                                                                      
                                                                                                                                                                                                                                                                            
repl-diskless-sync-delay 5                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                                            
repl-diskless-sync-max-replicas 0                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                                            
repl-diskless-load disabled                                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                                                            
repl-disable-tcp-nodelay no                                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                                                            
replica-priority 100                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                            
############################### KEYS TRACKING #################################                                                                                                                                                                                             
#key跟踪                                                                                                                                                                                                                                                                    
# tracking-table-max-keys 1000000                                                                                                                                                                                                                                           
################################## SECURITY ###################################                                                                                                                                                                                             
#acl日志最大长度                                                                                                                                                                                                                                                            
acllog-max-len 128                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                            
#重命名command,禁用某些危险命令                                                                                                                                                                                                                                            
#统计操作使用scan迭代器命令替代                                                                                                                                                                                                                                             
rename-command keys ""                                                                                                                                                                                                                                                      
#禁用删库命令                                                                                                                                                                                                                                                               
rename-command flushdb ""                                                                                                                                                                                                                                                   
rename-command flushall ""                                                                                                                                                                                                                                                  
############################## MEMORY MANAGEMENT ################################                                                                                                                                                                                           
#内存管理                                                                                                                                                                                                                                                                   
#最大内存,1GB                                                                                                                                                                                                                                                              
maxmemory 1GB                                                                                                                                                                                                                                                               
#驱逐策略                                                                                                                                                                                                                                                                   
# maxmemory-policy noeviction                                                                                                                                                                                                                                               
# maxmemory-samples 5                                                                                                                                                                                                                                                       
# maxmemory-eviction-tenacity 10                                                                                                                                                                                                                                            
# replica-ignore-maxmemory yes                                                                                                                                                                                                                                              
# active-expire-effort 1                                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                            
############################# LAZY FREEING ####################################                                                                                                                                                                                             
                                                                                                                                                                                                                                                                            
#延迟删除配置                                                                                                                                                                                                                                                               
                                                                                                                                                                                                                                                                            
lazyfree-lazy-eviction no                                                                                                                                                                                                                                                   
lazyfree-lazy-expire no                                                                                                                                                                                                                                                     
lazyfree-lazy-server-del yes                                                                                                                                                                                                                                                
replica-lazy-flush yes                                                                                                                                                                                                                                                      
#将删除键操作改为惰性删除                                                                                                                                                                                                                                                   
lazyfree-lazy-user-del yes                                                                                                                                                                                                                                                  
lazyfree-lazy-user-flush no                                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                                                            
################################ THREADED I/O #################################                                                                                                                                                                                             
#多线程读写配置,MODIFY                                                                                                                                                                                                                                                     
#读写线程数,一般为cpu核数的3分之2左右                                                                                                                                                                                                                                      
# io-threads 4                                                                                                                                                                                                                                                              
#开启多线程                                                                                                                                                                                                                                                                 
# io-threads-do-reads no                                                                                                                                                                                                                                                    
############################ KERNEL OOM CONTROL ##############################                                                                                                                                                                                              
                                                                                                                                                                                                                                                                            
oom-score-adj no                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                            
oom-score-adj-values 0 200 800                                                                                                                                                                                                                                              
                                                                                                                                                                                                                                                                            
#################### KERNEL transparent hugepage CONTROL ######################                                                                                                                                                                                             
                                                                                                                                                                                                                                                                            
disable-thp yes                                                                                                                                                                                                                                                             
                                                                                                                                                                                                                                                                            
################################## SLOW LOG ###################################                                                                                                                                                                                             
                                                                                                                                                                                                                                                                            
#慢查询日志                                                                                                                                                                                                                                                                 
slowlog-log-slower-than 10000                                                                                                                                                                                                                                               
slowlog-max-len 128                                                                                                                                                                                                                                                         
                                                                                                                                                                                                                                                                            
################################ LATENCY MONITOR ##############################                                                                                                                                                                                             
#保持默认                                                                                                                                                                                                                                                                   
latency-monitor-threshold 0                                                                                                                                                                                                                                                 
############################# EVENT NOTIFICATION ##############################                                                                                                                                                                                             
notify-keyspace-events ""                                                                                                                                                                                                                                                   
############################### ADVANCED CONFIG ###############################                                                                                                                                                                                             
#内部数据结构变更阈值配置,保持默认                                                                                                                                                                                                                                         
hash-max-listpack-entries 512                                                                                                                                                                                                                                               
hash-max-listpack-value 64                                                                                                                                                                                                                                                  
list-max-listpack-size -2                                                                                                                                                                                                                                                   
                                                                                                                                                                                                                                                                            
list-compress-depth 0                                                                                                                                                                                                                                                       
                                                                                                                                                                                                                                                                            
set-max-intset-entries 512                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                                            
zset-max-listpack-entries 128                                                                                                                                                                                                                                               
zset-max-listpack-value 64                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                                            
hll-sparse-max-bytes 3000                                                                                                                                                                                                                                                   
                                                                                                                                                                                                                                                                            
stream-node-max-bytes 4096                                                                                                                                                                                                                                                  
stream-node-max-entries 100                                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                                                            
activerehashing yes                                                                                                                                                                                                                                                         
                                                                                                                                                                                                                                                                            
client-output-buffer-limit normal 0 0 0                                                                                                                                                                                                                                     
client-output-buffer-limit replica 256mb 64mb 60                                                                                                                                                                                                                            
client-output-buffer-limit pubsub 32mb 8mb 60                                                                                                                                                                                                                               
                                                                                                                                                                                                                                                                            
hz 10                                                                                                                                                                                                                                                                       
                                                                                                                                                                                                                                                                            
dynamic-hz yes                                                                                                                                                                                                                                                              
                                                                                                                                                                                                                                                                            
aof-rewrite-incremental-fsync yes                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                                            
rdb-save-incremental-fsync yes                                                                                                                                                                                                                                              
                                                                                                                                                                                                                                                                            
########################### ACTIVE DEFRAGMENTATION #######################                                                                                                                                                                                                  
#碎片整理                                                                                                                                                                                                                                                                   
jemalloc-bg-thread yes

源码编译安装

源码获取方式:

  1. redis官网:https://redis.io/
  2. github仓库:https://github.com/redis/redis/releases

安装:

#获取源码包
#wget https://github.com/redis/redis/archive/refs/tags/7.0.11.tar.gz
wget https://download.redis.io/releases/redis-7.0.11.tar.gz
#安装gcc
yum install -y gcc

#解压
tar -xzvf redis-7.0.11.tar.gz
cd redis-7.0.11
#编译 过程较长
#make后会在src目录下生成一些可执行文件
make
#将可执行文件安装到指定路径,不指定路径 默认安装到/usr/local/bin
make install PREFIX=/opt/redis

#将上面的redis.conf文件上传到家目录
#创建配置文件目录并复制配置文件,将配置文件中的daemonize改为yes
mkdir /opt/redis/etc && cp ~/redis.conf /opt/redis/etc

#删除所有源码
cd ~ && rm -rf redis-7.0.11*

#创建Linux service
#默认使用redis-server可执行文件启动redis未使用任何配置文件
cat <<EOF > /usr/lib/systemd/system/redis.service
[Unit]
Description=Redis Service
Documentation=https://redis.io/docs/getting-started/
After=network.target

[Service]
Type=forking
ExecStart=/opt/redis/bin/redis-server /opt/redis/etc/redis.conf
Restart=on-failure
RestartSec=10
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
Alias=redis7.service
EOF

#额外系统配置
echo 1 > /proc/sys/vm/overcommit_memory
echo net.core.somaxconn = 1024 >> /etc/sysctl.conf
#加载service并设置开机启动且立即启动
systemctl daemon-reload
systemctl enable --now redis
#查看状态
systemctl status redis

yum方式安装

#安装yum源
yum install epel-release
#安装redis
yum install redis
#启动redis
systemctl enable --now redis
#默认配置文件位置为 /etc/redis.conf
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值