redis配置文件概括说明
[root@db03 ~]# grep -i '^###' /etc/redis.conf
################################## INCLUDES ################################### #相当于nginx配置文件的include,包含额外的文件
################################## NETWORK ##################################### #redis的网络配置
################################# GENERAL ##################################### #redis的通用配置
################################ SNAPSHOTTING ################################ #redis的快照配置
################################# REPLICATION ################################# #redis的复制配置
################################## SECURITY ################################### #redis的安全配置
################################### LIMITS #################################### #redis的资源限制配置
############################## APPEND ONLY MODE ############################### #redis的aof配置
################################ LUA SCRIPTING ############################### #redis LUA脚本配置
################################ REDIS CLUSTER ############################### #redis集群配置
################################## SLOW LOG ################################### #redis 慢日志配置
################################ LATENCY MONITOR ##############################
############################# EVENT NOTIFICATION ############################## #redis事件通知配置
############################### ADVANCED CONFIG ############################### #redis高级配置
redis 常用配置
# NETWORK 配置
bind 0.0.0.0 指定redis绑定的网卡地址
protected-mode yes 开启保护模式
port 6379 监听的端口号
timeout 0 设置客户端超时时间,0默认没有限制
# GENERAL 通用配置
daemonize no 是否是后台运行,默认前台。因为他要把控制权限交给systemd来控制,所以要前台
logfile /var/log/redis/redis.log
databases 16 有多少个数据库,-1 表示无数个
# SNAPSHOTTING 快照
save 900 1 900秒内有1次更改,及保存
save 300 10 300秒内,有10次修改,及保存
save 60 10000 60秒内,有10000次修改,及保存
redis 快照保存是逆序的,先执行最后一个
stop-writes-on-bgsave-error yes 如果快照保存失败,是否停止写入
rdbcompression yes 快照压缩
rdbchecksum yes 快照数据一致性检查
dbfilename dump.rdb 快照文件的名字
dir /var/lib/redis 快照文件的路径
# SECURITY 安全模式
requirepass foobared 设置redis的密码
# LIMITS 资源限制
maxclients 10000 客户端的最大连接数
maxmemory 1024000000 最大可以使用的内存,默认是字节
maxmemory-policy noeviction 内存淘汰机制
# volatile-lru -> 按照lru算法,删除有过期时间的key
# allkeys-lru -> 按照lru算法,删除全部的key
# volatile-random -> 随机删除,有过期时间的key
# allkeys-random -> 随机删除任何key
# volatile-ttl -> 按照ttl,key的过期时间多少,来删除过期时间的key
# noeviction -> 内存满了,默认什么都不做
maxmemory-samples 5 删除key时,取样的个数。每5个,按照内存淘汰机制进行淘汰。因为如果全部比较的话,内存中的key很多。会造成大量的cpu资源消耗
# APPEND ONLY MODE aof的配置
appendonly no 是否开启AOF
appendfilename "appendonly.aof" AOF的文件名,这是相对路径,相对于RDB的dir,也可以设置成绝对路径
# appendfsync always redis变化,从内存写入到aof文件中。每变化一次,写入一次。如果大量变化,会造成磁盘IO压力巨大
appendfsync everysec redis变化,从内存写入到aof文件中。每秒写入一次,最大会丢失1秒的数据
# appendfsync no redis变化,从内存写入到aof文件中。根据内核自己的机制进行写入,无法控制
auto-aof-rewrite-percentage 100 如果aof文件中,一个key 反复执行了20万次。需要对这个key进行合并操作,因为20万次的操作在aof文件中,会造成回放慢。且文件过大。100是当变化的次数和原次数相比,变化达到100%才会写入。例原先写入了1次,变化了1次。redis就会对key的操作进行合并操作
auto-aof-rewrite-min-size 64mb 当aof文件达到64M 的时候,变化次数和原次数达到100%时,才会进行合并操作。两个条件必须同时满足
# SLOW LOG 慢日志相关的配置
slowlog-log-slower-than 10000 单位微妙,也就是超过10毫秒的查询就记录
slowlog-max-len 128 最大记录128条记录
# ADVANCED CONFIG 高级配置
client-output-buffer-limit normal 0 0 0 普通redis客户端的配置
client-output-buffer-limit slave 256mb 64mb 60 在redis中,从网卡往slave端传输数据。由于网卡是串行的,逐个包进行传输。需要一个reids 缓存。在网卡后面进行等待,256m为缓存的大小不能超过256m,为硬限制,64mb,为缓存的大小不应超过64mb为软限制。在60秒内,可以允许超过64m,小于256m,但超过60秒,则进行清空
client-output-buffer-limit pubsub 32mb 8mb 60 redis 当消息队列时的配置
[hard-limit]
[soft-limit]
[seconds]
redis 主从复制
配置文件修改
#REPLICATION
slaveof 10.0.0.11 6379
masterauth czazm
slave-serve-stale-data yes 主库宕机了,从库是否返回客户端数据
slave-read-only yes 从库只读
repl-diskless-sync no
# 1) Disk-backed: 在做主从复制时,如果rdb文件过大。而网络传输带宽不足,把rdb文件放在磁盘中。从磁盘中,传输文件给从库
# 2) Diskless: redis主库,直接传输文件给从库。通过网络带宽直接传输,而不放在磁盘中
repl-diskless-sync-delay 5 使用diskless传输时,延时5秒,启动程序进行传输
repl-disable-tcp-nodelay no 是否启用tcp 延时 nodelay 是不延时。no表示不启动,yes表示启动。no的话,及主库做了操作,立马同步到从库。yes,表示主库做了操作。然后打包操作,然后进行传输
slave-priority 100 从库优先级操作,越小。优先级越高
min-slaves-to-write 3 如果从库多的话,可以指定最少有3个从库在线时,才进行写操作
min-slaves-max-lag 10 从库的最大延时多少
-----------------------------------------------------------------------------------------------------------------------------------
# 查看状态
[root@mysql ~]# redis-cli -a czazm
127.0.0.1:6379> INFO replication
# Replication
role:master
connected_slaves:2
slave0:ip=10.0.0.12,port=6379,state=online,offset=29,lag=1
slave1:ip=10.0.0.13,port=6379,state=online,offset=29,lag=1
master_repl_offset:29
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:28
手动修改
[root@db03 etc]# redis-cli -a czazm
127.0.0.1:6379> SLAVEOF 10.0.0.11 6379
OK
127.0.0.1:6379> CONFIG SET masterauth czazm
OK
127.0.0.1:6379> CONFIG REWRITE
OK
redis 哨兵部署
配置文件修改
bind 0.0.0.0
# sentinel monitor <master-name> <ip> <redis-port> <quorum>
sentinel monitor mymaster 10.0.0.11 6379 2 #master-name 为自定义, quorum为
选举机制的个数,默认3个节点。超过半数以上,的sentinel认为主节点宕机了,就进行选主
sentinel auth-pass mymaster czazm 主节点的密码
sentinel down-after-milliseconds mymaster 30000 sentinel多久联系不上主节点,才认为其宕机。默认30分钟
sentinel parallel-syncs mymaster 1 当进行故障切换时,新主一次同步多少个从库
sentinel failover-timeout mymaster 180000 故障切换规定在多长时间内完成。如果没有完成,就停止切换
logfile /var/log/redis/sentinel.log
----------------------------------------------------------------------------------------------------------------------------
scp /etc/redis-sentinel.conf 10.0.0.13:/etc/
scp /etc/redis-sentinel.conf 10.0.0.12:/etc/
[root@mysql ~]# tail -f /etc/redis-sentinel.conf
# sentinel client-reconfig-script mymaster /var/redis/reconfig.sh
logfile "/var/log/redis/sentinel.log"
# Generated by CONFIG REWRITE
supervised systemd
sentinel known-slave mymaster 10.0.0.13 6379
sentinel known-slave mymaster 10.0.0.12 6379
sentinel known-sentinel mymaster 10.0.0.12 26379 91666ef6ecc7d7a89a88acc021008b720ef5e023
sentinel known-sentinel mymaster 10.0.0.13 26379 f1a16bbcc05bdaa19b1cbb52a2ec20a212143d5f
sentinel current-epoch 0
redis-cli -p 26379
127.0.0.1:26379> INFO sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=10.0.0.11:6379,slaves=2,sentinels=3
#### 测试
10.0.0.11 systemctl stop redis
[root@mysql ~]# tail -f /etc/redis-sentinel.conf
# sentinel client-reconfig-script mymaster /var/redis/reconfig.sh
logfile "/var/log/redis/sentinel.log"
# Generated by CONFIG REWRITE
supervised systemd
sentinel known-slave mymaster 10.0.0.11 6379 #从库变成了11
sentinel known-slave mymaster 10.0.0.13 6379
sentinel known-sentinel mymaster 10.0.0.12 26379 91666ef6ecc7d7a89a88acc021008b720ef5e023
sentinel known-sentinel mymaster 10.0.0.13 26379 f1a16bbcc05bdaa19b1cbb52a2ec20a212143d5f
sentinel current-epoch 1 #进行了第一次切换
[root@ceshi ~]# redis-cli -p 26379
127.0.0.1:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=10.0.0.12:6379,slaves=2,sentinels=3
修复好10.0.0.11 即可加入
systemctl start redis
redis-cluster配置
# 先停止sentinel和解除哨兵
vim /etc/redis.conf
# REDIS CLUSTER
cluster-enabled yes 开启集群模式
cluster-config-file cluster.conf 集群的配置文件,相对路径于dir路径。也可以写为绝对路径
cluster-node-timeout 15000 集群各节点的超时时间
cluster-slave-validity-factor 10 在进行故障转移时。所有slave都会想成为master。该参数判断,slave和master的失联是否过长。能否成为主
scp /etc/redis.conf root@10.0.0.12:/etc/
scp /etc/redis.conf root@10.0.0.13:/etc/
# redis 默认对 16384进行取模,及一共有0-16383个分片。需要手动去分,这些分片
10.0.0.11 0-5500
10.0.0.12 5501-11000
10.0.0.13 11001-16383
127.0.0.1:6379> help @cluster
CLUSTER ADDSLOTS slot [slot ...]
summary: Assign new hash slots to receiving node
since: 3.0.0
CLUSTER COUNT-FAILURE-REPORTS node-id
summary: Return the number of failure reports active for a given node
since: 3.0.0
CLUSTER COUNTKEYSINSLOT slot
summary: Return the number of local keys in the specified hash slot
since: 3.0.0
CLUSTER DELSLOTS slot [slot ...]
summary: Set hash slots as unbound in receiving node
since: 3.0.0
CLUSTER FAILOVER [FORCE|TAKEOVER]
summary: Forces a slave to perform a manual failover of its master.
since: 3.0.0
CLUSTER FORGET node-id
summary: Remove a node from the nodes table
since: 3.0.0
CLUSTER GETKEYSINSLOT slot count
summary: Return local key names in the specified hash slot
since: 3.0.0
CLUSTER INFO -
summary: Provides info about Redis Cluster node state
since: 3.0.0
CLUSTER KEYSLOT key
summary: Returns the hash slot of the specified key
since: 3.0.0
CLUSTER MEET ip port
summary: Force a node cluster to handshake with another node
since: 3.0.0
CLUSTER NODES -
summary: Get Cluster config for the node
since: 3.0.0
CLUSTER REPLICATE node-id
summary: Reconfigure a node as a slave of the specified master node
since: 3.0.0
CLUSTER RESET [HARD|SOFT]
summary: Reset a Redis Cluster node
since: 3.0.0
CLUSTER SAVECONFIG -
summary: Forces the node to save cluster state on disk
since: 3.0.0
CLUSTER SET-CONFIG-EPOCH config-epoch
summary: Set the configuration epoch in a new node
since: 3.0.0
CLUSTER SETSLOT slot IMPORTING|MIGRATING|STABLE|NODE [node-id]
summary: Bind a hash slot to a specific node
since: 3.0.0
CLUSTER SLAVES node-id
summary: List slave nodes of the specified master node
since: 3.0.0
CLUSTER SLOTS -
summary: Get array of Cluster slot to node mappings
since: 3.0.0
READONLY -
summary: Enables read queries for a connection to a cluster slave node
since: 3.0.0
READWRITE -
summary: Disables read queries for a connection to a cluster slave node
since: 3.0.0
----------------------------------------------------------------------------------------------
# 手动分片
redis-cli -h 10.0.0.11 -a czazm -c cluster addslots {0..5500}
redis-cli -h 10.0.0.12 -a czazm -c cluster addslots {5501..11000}
redis-cli -h 10.0.0.13 -a czazm -c cluster addslots {11001..16383}
-c 表示已集群模式运行
[root@mysql ~]# redis-cli -h 10.0.0.11 -a czazm
10.0.0.11:6379> INFO cluster
# Cluster
cluster_enabled:1
10.0.0.11:6379> CLUSTER info
cluster_state:fail
cluster_slots_assigned:5501
cluster_slots_ok:5501
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:1
cluster_size:1
cluster_current_epoch:0
cluster_my_epoch:0
cluster_stats_messages_sent:0
cluster_stats_messages_received:0
[root@mysql ~]# redis-cli -h 10.0.0.12 -a czazm
10.0.0.12:6379> CLUSTER info
cluster_state:fail
cluster_slots_assigned:5500
cluster_slots_ok:5500
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:1
cluster_size:1
cluster_current_epoch:0
cluster_my_epoch:0
cluster_stats_messages_sent:0
cluster_stats_messages_received:0
[root@mysql ~]# redis-cli -h 10.0.0.13 -a czazm
10.0.0.13:6379> cluster info
cluster_state:fail
cluster_slots_assigned:5383
cluster_slots_ok:5383
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:1
cluster_size:1
cluster_current_epoch:0
cluster_my_epoch:0
cluster_stats_messages_sent:0
cluster_stats_messages_received:0
各节点进行会面,已配置成真正的分布式
[root@mysql ~]# redis-cli -h 10.0.0.11 -a czazm
10.0.0.11:6379> help cluster meet
CLUSTER MEET ip port
summary: Force a node cluster to handshake with another node
since: 3.0.0
group: cluster
10.0.0.11:6379> cluster meet 10.0.0.12 6379
OK
10.0.0.11:6379> cluster meet 10.0.0.13 6379
OK
10.0.0.11:6379> CLUSTER info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:3
cluster_size:3
cluster_current_epoch:2
cluster_my_epoch:2
cluster_stats_messages_sent:31
cluster_stats_messages_received:31
---------------------------------------------------------------------------------------------------------
进行测试
[root@mysql ~]# redis-cli -h 10.0.0.11 -a czazm
10.0.0.11:6379> set a a
(error) MOVED 15495 10.0.0.13:6379
10.0.0.11:6379>
[root@mysql ~]# redis-cli -h 10.0.0.13 -a czazm
10.0.0.13:6379> set a a
OK
网友评论