安装1
# sudo apt install redis-server
查看redis运行状态1
2
3
4
5
6#ps -aux|grep redis
#netstat -nlt|grep 6379
#sudo /etc/init.d/redis-server status
--指定配置文件启动
redis-server /etc/redis/redis.conf --loglevel warning
redis>CONFIG SET loglevel warning
命令行参数1
2
3
4
5
6
7#redis-cli SHUTDOWN
#redis-cli -h 127.0.0.1 -p 6379
#redis-cli PING
--选择数据与库 默认可以有16个
redis> SELECT 1
redis> info Keyspace --查看存储信息
redis> keys * --查看所有key
命令1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152--建立一个键
127.0.0.1:6379> set bar 1
127.0.0.1:6379> get bar
--查看所有键
127.0.0.1:6379> keys *
--判断键是否存在
127.0.0.1:6379> exists bar
--删除键
127.0.0.1:6379> del bar
--查看键数据类型(string/hash/list/set/zset)
127.0.0.1:6379> type bar
##字符串类型
--向指定列表类型键中增加元素
127.0.0.1:6379> lpush bar 1
--递增/递减当前键值
127.0.0.1:6379> incr bar
127.0.0.1:6379> decr bar
--指定增加数
127.0.0.1:6379> incrby bar 2
--增加浮点数
127.0.0.1:6379> incrbyfloat bar 2.7
--尾部追加值
127.0.0.1:6379> append bar “hello”
--获取字符串长度
127.0.0.1:6379> strlen bar
--同时获取/设置多个键值
127.0.0.1:6379> mget key1 key2
127.0.0.1:6379> mset key1 v1 key2 v2
##散列数据类型
--散列类型的赋值与取值
127.0.0.1:6379> hset car name bmw
127.0.0.1:6379> hget car name
--获取多个字段值
127.0.0.1:6379> hmget car name price
127.0.0.1:6379> hgeall car
--判断字段是否存在
127.0.0.1:6379> hexists car name
127.0.0.1:6379> hsetnx key field value
--删除字段
127.0.0.1:6379> hdel car name
--仅取字段/值/数量
127.0.0.1:6379> hkeys car
127.0.0.1:6379> hvals car
127.0.0.1:6379> hlen car
##列表类型
--向前后添加元素
127.0.0.1:6379> lpush number 1 2
127.0.0.1:6379> rpush number 1 2
--向前后弹出元素
127.0.0.1:6379> lpop number
127.0.0.1:6379> rpop number
--获取列表元素个数
127.0.0.1:6379> llen number
--列表切片
127.0.0.1:6379> lrange number 0 2
--删除列表指定值
127.0.0.1:6379> lrem number 2
--获取设置指定索引元素
127.0.0.1:6379> lindex key value
127.0.0.1:6379> lset key index value
--只保留指定片段
127.0.0.1:6379> ltrim key start end
--向列表指定位置插入元素
127.0.0.1:6379> linsert key before|after pivot value
127.0.0.1:6379> linsert number after 2 3
--列表转移(作为队列)
127.0.0.1:6379> rpoplpush source dest
##集合类型
--增加/删除元素
127.0.0.1:6379> sadd key a b
127.0.0.1:6379> srem key a
--获得所有元素
127.0.0.1:6379> smenbers key
--判断元素是否在集合中
127.0.0.1:6379> sismember key a
--集合运算
127.0.0.1:6379> sdiff set1 set2 #差集
127.0.0.1:6379> sinter set1 set2 #交集
127.0.0.1:6379> sunion set1 set2 #并集
--获得集合元素个数
127.0.0.1:6379> scard set1
--存储集合运算结果
127.0.0.1:6379> sdiffstore dest key1 key2
--从集合中弹出元素(随机)
127.0.0.1:6379> spop key
##有序集合
--增加/修改元素
127.0.0.1:6379> zadd bord 1 a 2 b
--获得元素分数
127.0.0.1:6379> zscore bord a
--获得排名范围列表(带分数)
127.0.0.1:6379> zrange bord 1 -1
127.0.0.1:6379> zrange bord 1 -1 withscore
127.0.0.1:6379> zrevrange bord 1 -1 withscore #从大到小
--指定分数范围元素
127.0.0.1:6379> zrangebyscore bord 5 (10
--增加元素分数
127.0.0.1:6379> zincby bord 4 a
--获得集合总元素数量
127.0.0.1:6379> zcard bord
--获得指定分数范围内的元素个数
127.0.0.1:6379> zcount bord 5 10
--删除一个/多个元素
127.0.0.1:6379> zrem bord a b
--按排名删除元素
127.0.0.1:6379> zremrangebyrank bord 5 10
--按照分数删除元素
127.0.0.1:6379> zremrangebyscore bord 5 10
--获得元素排名
127.0.0.1:6379> zrank bord a
127.0.0.1:6379> zrevrank bord a #从大到小
##事务
--
127.0.0.1:6379> multi
127.0.0.1:6379> set key1 1
127.0.0.1:6379> set key2 2
127.0.0.1:6379> exec
--设置键过期时间
127.0.0.1:6379> expire key seconds
127.0.0.1:6379> pexpire key millisecond
127.0.0.1:6379> ttl key #查看剩余时间
127.0.0.1:6379> presist key #取消设置,set命令也可清除过期时间
###通过修改maxmemory参数设置最大可用内存,根据maxmenory-policy参数指定策略删除键(可以使用LRU算法 allkeys-lru)
--排序
127.0.0.1:6379> sort tag:mylist:posts
127.0.0.1:6379> sort tag:mylist:posts desc #从大到小
127.0.0.1:6379> sort tag:mylist:posts desc limit 1 2
127.0.0.1:6379> sort mylist alpha #非数字元素
127.0.0.1:6379> sort tag:mylist:posts by post:*->time desc
127.0.0.1:6379> sort tag:mylist:posts by post:*->time desc get post:*->title get post:*->time get # store sort.result
--订阅发布
127.0.0.1:6379> publish channel message
127.0.0.1:6379> subscribe channel
127.0.0.1:6379> unsubscribe channel
--执行脚本
127.0.0.1:6379> eval "while true do end" 0
127.0.0.1:6379> eval "return redis.call('set',KEYS[1],ARGV[1])" 1 foo bar
127.0.0.1:6379> eval script key arg
redis持久化
RDB方式:根据某种规则”定时”将内存中的数据存储在硬盘上
配置文件:/etc/redis/redis.conf
快照规则:save [时间窗口] [改动次数]
存储路径:dir
rdb文件名:dbfilename dump.rdb
存储压缩:rdbcompression
AOF方式:每次执行命令后将命令本身记录下来(重要数据)
开启:appendonly yes
存储路径:dir
存储文件名:appendfilename appendonly.aof
重写条件:auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
同步时机:appendfsync everysec
和主数据库的连接建立完成后,哨兵会定期执行如下操作:
- [1]每10秒哨兵会向主数据库和从数据库发送INFO命令
获取当前数据库相关信息,获取从数据库列表- [2]每2秒哨兵会向主数据库的 sentinel: hello频道发送自己的信息
与其他哨兵分享自己信息- [3]每1秒哨兵会向主数据库、从数据库和其他哨兵节点发送ping命令
若无响应,哨兵自己认为主数据库(主观下线)同时询问其他哨兵,如果一定数量哨兵都认为主观下线则认为主数据库(客观下线)通过raft算法选举令头哨兵进行故障恢复
##redis-trib.rb配置集群
http://blog.csdn.net/tengxing007/article/details/77102068
使用cluster meet使节点认识集群中的其他节点
1下载源码(使用/src/redis-trib.rb)
1
2
3
4 wget http://download.redis.io/releases/redis-4.0.7.tar.gz
tar xzf redis-4.0.7.tar.gz
cd redis-4.0.7
make
2安装ruby
1
2
3 sudo apt install ruby
sudo gem install redis
sudo apt install ruby-dev
3设置配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 cd ~/work/redis/
cp /etc/redis/redis.conf master-6380.conf
修改如下参数
port 6380
bind 127.0.0.1
daemonize yes
pidfile /var/run/redis/redis-server-master-6380.pid
cluster-enabled yes
cluster-config-file master-6380.conf
cluster-node-timeout 15000
appendonly yes
#当负责一个插槽的主库下线且没有相应的从库进行故障恢复时,集群不可用(默认为yes)允许可用(no)
cluster-require-full-coverage no
分别新建 master-6380.conf master-6381.conf master-6382.conf slave-6383.conf slave-6384.conf slave-6385.conf
4启动多实例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 sudo redis-server master-6380.conf
sudo redis-server master-6381.conf
sudo redis-server master-6382.conf
sudo redis-server slave-6383.conf
sudo redis-server slave-6384.conf
sudo redis-server slave-6385.conf
ps -ef|grep redis
redis 1225 1 0 1月27 ? 00:00:55 /usr/bin/redis-server 127.0.0.1:6379
root 18068 1 0 01:10 ? 00:00:02 redis-server 127.0.0.1:6381 [cluster]
root 18086 1 0 01:10 ? 00:00:02 redis-server 127.0.0.1:6382 [cluster]
root 18109 1 0 01:11 ? 00:00:02 redis-server 127.0.0.1:6380 [cluster]
root 18144 1 0 01:11 ? 00:00:01 redis-server 127.0.0.1:6384 [cluster]
root 18157 1 0 01:11 ? 00:00:01 redis-server 127.0.0.1:6385 [cluster]
root 18181 1 0 01:11 ? 00:00:01 redis-server 127.0.0.1:6383 [cluster]
5建立集群
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 sudo src/redis-trib.rb create --replicas 1 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 127.0.0.1:6385
#create参数表示初始化集群 --replicas 1 表示每个主数据库拥有的从数据库个数为1
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:6380
127.0.0.1:6381
127.0.0.1:6382
Adding replica 127.0.0.1:6384 to 127.0.0.1:6380
Adding replica 127.0.0.1:6385 to 127.0.0.1:6381
Adding replica 127.0.0.1:6383 to 127.0.0.1:6382
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 84116c6ec23b747f06d96d6d88e06feedc505ed7 127.0.0.1:6380
slots:0-5460 (5461 slots) master
M: 4cbf023aabf707ab6e80d452ca1f2091f639659f 127.0.0.1:6381
slots:5461-10922 (5462 slots) master
M: 90caad16c0227970941b5efd70e783a86b8e8404 127.0.0.1:6382
slots:10923-16383 (5461 slots) master
S: 996f8a927c6def84a2bd195d4719dfb9167b936c 127.0.0.1:6383
replicates 84116c6ec23b747f06d96d6d88e06feedc505ed7
S: da391e12dfce497c1e0a2d98330deedebacbd251 127.0.0.1:6384
replicates 4cbf023aabf707ab6e80d452ca1f2091f639659f
S: fde8da3d9ae8b40d9d5c652b9c9de8ce2e9ae87b 127.0.0.1:6385
replicates 90caad16c0227970941b5efd70e783a86b8e8404
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join....
>>> Performing Cluster Check (using node 127.0.0.1:6380)
M: 84116c6ec23b747f06d96d6d88e06feedc505ed7 127.0.0.1:6380
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 996f8a927c6def84a2bd195d4719dfb9167b936c 127.0.0.1:6383
slots: (0 slots) slave
replicates 84116c6ec23b747f06d96d6d88e06feedc505ed7
M: 90caad16c0227970941b5efd70e783a86b8e8404 127.0.0.1:6382
slots:10923-16383 (5461 slots) master
1 additional replica(s)
M: 4cbf023aabf707ab6e80d452ca1f2091f639659f 127.0.0.1:6381
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: fde8da3d9ae8b40d9d5c652b9c9de8ce2e9ae87b 127.0.0.1:6385
slots: (0 slots) slave
replicates 90caad16c0227970941b5efd70e783a86b8e8404
S: da391e12dfce497c1e0a2d98330deedebacbd251 127.0.0.1:6384
slots: (0 slots) slave
replicates 4cbf023aabf707ab6e80d452ca1f2091f639659f
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
6>创建完成1
2
3
4
5
6
7
8
9#记得加上-c参数支持自动重定向 Enable cluster mode (follow -ASK and -MOVED redirections).
$ redis-cli -c -p 6381
redis 127.0.0.1:8000>cluster nodes
996f8a927c6def84a2bd195d4719dfb9167b936c 127.0.0.1:6383 slave 84116c6ec23b747f06d96d6d88e06feedc505ed7 0 1517073381082 4 connected
fde8da3d9ae8b40d9d5c652b9c9de8ce2e9ae87b 127.0.0.1:6385 slave 90caad16c0227970941b5efd70e783a86b8e8404 0 1517073383088 6 connected
4cbf023aabf707ab6e80d452ca1f2091f639659f 127.0.0.1:6381 myself,master - 0 0 2 connected 5461-10922
da391e12dfce497c1e0a2d98330deedebacbd251 127.0.0.1:6384 slave 4cbf023aabf707ab6e80d452ca1f2091f639659f 0 1517073382085 5 connected
84116c6ec23b747f06d96d6d88e06feedc505ed7 127.0.0.1:6380 master - 0 1517073384089 1 connected 0-5460
90caad16c0227970941b5efd70e783a86b8e8404 127.0.0.1:6382 master - 0 1517073385092 3 connected 10923-16383
##只允许本机连接
bind 127.0.0.1
##数据库密码
requirepass 123123
(从数据库)masterauth 123123
redis>auth 123123
##重命名命令可使只有自己可以使用
##查看耗时命令日志
redis>slowlog get
##查询分析工具 基于monitor命令的redis-faina工具
https://github.com/Instragram/redis-faina
##rdbtools快照解析器
https://github.com/sripathikrishnan/redis-rdb-tools