标签:daemonize nes 增加 nta fail foreign cal 0.0.0.0 快照
目录
九、redis介绍
十、redis安装
十一、redis持久化
十二、redis数据类型
十三、redis常用操作
十四、redis操作键值
十五、redis安全设置
十六、redis慢查询日志
十七、php安装redis扩展
十八、redis存储session
十九、redis主从配置
二十、redis集群介绍
二十一、redis集群搭建配置
二十二、redis集群操作
二十三、扩展
Redis和Memcached类似,也属于k-v数据存储。
Redis官网redis.io, 截止于(2018-08-23)当前最新稳定版4.0.11。
支持更多value类型,除了和string外,还支持hash、lists(链表)、sets(集合)和sorted sets(有序集合)
redis使用了两种文件格式:全量数据(RDB)和增量请求(aof)。全量数据格式是把内存中的数据写入磁盘,便于下次读取文件进行加载。增量请求文件则是把内存中的数据序列化为操作请求,用于读取文件进行replay得到数据,这种类似于mysql binlog。
redis的存储分为内存存储、磁盘存储和log文件三部分
1.下载redis稳定安装包,当前最新4.0.11
[root@lnmp-server src]# cd /usr/local/src/
[root@lnmp-server src]# wget http://download.redis.io/releases/redis-4.0.11.tar.gz
--2018-08-23 21:23:11-- http://download.redis.io/releases/redis-4.0.11.tar.gz
Resolving download.redis.io (download.redis.io)... 109.74.203.151
Connecting to download.redis.io (download.redis.io)|109.74.203.151|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1739656 (1.7M) [application/x-gzip]
Saving to: ‘redis-4.0.11.tar.gz’
100%[==========================================>] 1,739,656 838KB/s in 2.0s
2018-08-23 21:23:14 (838 KB/s) - ‘redis-4.0.11.tar.gz’ saved [1739656/1739656]
2.解压软件包
[root@lnmp-server src]# tar -zxvf redis-4.0.11.tar.gz
3.编译安装
[root@lnmp-server src]# cd redis-4.0.11/
[root@lnmp-server redis-4.0.11]# make
cd src && make all
make[1]: Entering directory `/usr/local/src/redis-4.0.11/src‘
CC Makefile.dep
make[1]: Leaving directory `/usr/local/src/redis-4.0.11/src‘
make[1]: Entering directory `/usr/local/src/redis-4.0.11/src‘
rm -rf redis-server redis-sentinel redis-cli redis-benchmark redis-check-rdb redis-check-aof *.o *.gcda *.gcno *.gcov redis.info lcov-html Makefile.dep dict-benchmark
...中间略...
Hint: It‘s a good idea to run ‘make test‘ ;)
make[1]: Leaving directory `/usr/local/src/redis-4.0.11/src‘
[root@lnmp-server redis-4.0.11]# make install
cd src && make install
make[1]: Entering directory `/usr/local/src/redis-4.0.11/src‘
CC Makefile.dep
make[1]: Leaving directory `/usr/local/src/redis-4.0.11/src‘
make[1]: Entering directory `/usr/local/src/redis-4.0.11/src‘
Hint: It‘s a good idea to run ‘make test‘ ;)
INSTALL install
INSTALL install
INSTALL install
INSTALL install
INSTALL install
make[1]: Leaving directory `/usr/local/src/redis-4.0.11/src‘
4.新建配置文件
[root@lnmp-server redis-4.0.11]# cp redis.conf /etc/redis.conf
[root@lnmp-server redis-4.0.11]# ls !$
ls /etc/redis.conf
/etc/redis.conf
5.编辑配置文件
[root@lnmp-server redis-4.0.11]# vim /etc/redis.conf
#修改如下配置
daemonize yes
logfile "/var/log/redis.log"
dir /data/redis_data/
appendonly yes
[root@lnmp-server redis-4.0.11]# mkdir /data/redis_data
#内核优化参数,添加到/etc/rc.local
[root@lnmp-server redis-4.0.11]# vim /etc/rc.local
#redis optimize parameter
sysctl vm.overcommit_memory=1
echo never > /sys/kernel/mm/transparent_hugepage/enabled
#需要给/etc/rc.d/rc.local赋予执行权限。否则,/etc/rc.local中的任务无法执行
[root@lnmp-server redis-4.0.11]# ls -l /etc/rc.local
lrwxrwxrwx. 1 root root 13 Apr 20 23:18 /etc/rc.local -> rc.d/rc.local
[root@lnmp-server redis-4.0.11]# chmod a+x /etc/rc.d/rc.local
[root@lnmp-server redis-4.0.11]# ls -l !$
ls -l /etc/rc.d/rc.local
-rwxr-xr-x. 1 root root 587 Aug 23 21:53 /etc/rc.d/rc.local
6.启动redis并验证
[root@lnmp-server redis-4.0.11]# redis-server /etc/redis.conf
[root@lnmp-server redis-4.0.11]# ps aux | grep redis
root 4914 0.1 0.2 145264 2180 ? Ssl 21:57 0:00 redis-server 127.0.0.1:6379
root 4919 0.0 0.0 112660 972 pts/0 S+ 21:58 0:00 grep --color=auto redis
Redis提供了两种持久化的方式,分别是RDB(Redis DataBase)和AOF(Append Only File)
RDB,简而言之,就是在不同的时间点,将redis存储的数据生成快照并存储到磁盘等介质上。
RDB主要受配置文件中的以下几项影响
# save ""
# save 900 1 表示每15分钟且至少有1个key改变,就触发一次持久化
save 900 1
# save 300 10 表示每5分钟且至少有10个key改变,就触发一次持久化
save 300 10
# save 60 10000 表示每60秒至少有10000个key改变,就触发一次持久
save 60 10000
# 注释其他三项,取消注释 # save "",这样可以禁用rdb持久化
AOF,则是换了一个角度来实现持久化,那就是将redis执行过的所有写指令记录下来,在下次redis重新启动时,只要把这些写指令从前到后再重复执行一遍,就可以实现数据恢复了。
AOF主要受配置文件中的以下几项影响
#如果是yes,则开启aof持久化
appendonly yes
# 指定aof文件名字
appendfilename "appendonly.aof"
#指定fsync()调用模式,有三种no(不调用fsync),always(每次写都会调用fsync),everysec(每秒钟调用一次fsync)。第一种最快,第二种数据最安全,但性能会差一些,第三种为这种方案,默认为第三种。
# appendfsync always
appendfsync everysec
# appendfsync no
其实RDB和AOF两种方式也可以同时使用,在这种情况下,如果redis重启的话,则会优先采用AOF方式来进行数据恢复,这是因为AOF方式的数据恢复完整度更高。
如果你没有数据持久化的需求,也完全可以关闭RDB和AOF方式,这样的话,redis将变成一个纯内存数据库,就像memcache一样。
1.Redis数据类型-string
string为最简单的类型,与Memcached一样的类型,一个key对应一个value,其支持的操作与Memcached的操作类似,它的功能更丰富。设置可以存二进制的对象。
简单演示
[root@lnmp-server redis-4.0.11]# redis-cli
127.0.0.1:6379> set mykey "kennminn.net"
OK
127.0.0.1:6379> get mykey
"kennminn.net"
# 批量设置key
127.0.0.1:6379> mset k1 a k2 b k3 c
OK
127.0.0.1:6379> mget k1 k2 k3
1) "a"
2) "b"
3) "c"
127.0.0.1:6379>
2.Redis数据类型-list
list是一个链表结构,主要功能是push、pop、获取一个范围的所有值等等。操作中key理解为链表的名字。
使用 list 结构,我们可以轻松地实现最新消息排行等功能(比如新浪微博的 TimeLine )。list 的另一个应用就是消息队列,可以利用list的push操作,将任务存在list中,然后工作线程再用pop操作将任务取出进行执行。
简单演示
127.0.0.1:6379> LPUSH list1 "kennminn"
(integer) 1
127.0.0.1:6379> LPUSH list1 "1 2 3"
(integer) 2
127.0.0.1:6379> LPUSH list1 "huannan huaihua"
(integer) 3
127.0.0.1:6379> LRANGE list1 0 10
1) "huannan huaihua"
2) "1 2 3"
3) "kennminn"
127.0.0.1:6379> LPOP list1
"huannan huaihua"
127.0.0.1:6379> LPOP list1
"1 2 3"
127.0.0.1:6379> LPOP list1
"kennminn"
127.0.0.1:6379> LPOP list1
(nil)
127.0.0.1:6379>
3.Redis数据类型-set
set是集合,和我们数学中的集合概念相似,对集合的操作有添加删除元素,有对多个集合求交并差等操作。操作中key理解为集合的名字。比如在微博应用中,可以将一个用户所有的关注人存在一个集合中,将其所有粉丝存在一个集合。因为 Redis 非常人性化的为集合提供了求交集、并集、差集等操作,那么就可以非常方便的实现如共同关注、共同喜好、二度好友等功能,对上面的所有集合操作,你还可以使用不同的命令选择将结果返回给客户端还是存集到一个新的集合中。
简单演示
# set1添加元素
127.0.0.1:6379> SADD set1 aa
(integer) 1
127.0.0.1:6379> SADD set1 bb
(integer) 1
127.0.0.1:6379> SADD set1 cc
(integer) 1
127.0.0.1:6379> SADD set1 dd
(integer) 1
# set1的元素
127.0.0.1:6379> SMEMBERS set1
1) "bb"
2) "dd"
3) "cc"
4) "aa"
# 从set1删除aa元素
127.0.0.1:6379> SREM set1 aa
(integer) 1
127.0.0.1:6379> SMEMBERS set1
1) "bb"
2) "dd"
3) "cc"
# 添加set2
127.0.0.1:6379> SADD set2 a1 2 bb
(integer) 3
127.0.0.1:6379> SMEMBERS set2
1) "2"
2) "bb"
3) "a1"
# 取set1和set2的交集
127.0.0.1:6379> SINTER set1 set2
1) "bb"
# 取set1和set2的并集
127.0.0.1:6379> SUNION set1 set2
1) "bb"
2) "cc"
3) "dd"
4) "2"
5) "a1"
# 取set1和set2的差集
127.0.0.1:6379> SDIFF set1 set2
1) "cc"
2) "dd"
127.0.0.1:6379>
4.Redis数据类型-sort set
sorted set是有序集合,它比set多了一个权重参数score,使得集合中的元素能够按 score 进行有序排列,比如一个存储全班同学成绩的 Sorted Sets,其集合 value 可以是同学的学号,而 score 就可以是其考试得分,这样在数据插入集合的时候,就已经进行了天然的排序。
简单演示
127.0.0.1:6379> ZADD set3 12 abc
(integer) 1
127.0.0.1:6379> ZADD set3 2 "guanzhou"
(integer) 1
127.0.0.1:6379> ZADD set3 10 "shenzhen"
(integer) 1
127.0.0.1:6379> ZADD set3 11 "huizhou"
(integer) 1
127.0.0.1:6379> ZRANGE set3 0 -1
1) "guanzhou"
2) "shenzhen"
3) "huizhou"
4) "abc"
# 反序
127.0.0.1:6379> Zrevrange set3 0 -1
1) "abc"
2) "huizhou"
3) "shenzhen"
4) "guanzhou"
127.0.0.1:6379>
5.Redis数据类型-hash
在 Memcached 中,我们经常将一些结构化的信息打包成 hashmap,在客户端序列化后存储为一个字符串的值(一般是 JSON 格式),比如用户的昵称、年龄、性别、积分等。
127.0.0.1:6379> hset h1 name nico
(integer) 1
127.0.0.1:6379> hget h1 name
"nico"
127.0.0.1:6379> hset h1 age 6
(integer) 1
127.0.0.1:6379> hget h1 age
"6"
127.0.0.1:6379> hgetall h1
1) "name"
2) "nico"
3) "age"
4) "6"
127.0.0.1:6379>
1.Redis常用操作 (string, list)
127.0.0.1:6379> set k1 nico
OK
127.0.0.1:6379> get k1
"nico"
# 再次赋值会覆盖原来的值
127.0.0.1:6379> set k1 aliva
OK
127.0.0.1:6379> get k1
"aliva"
# 如果key存在,返回0
127.0.0.1:6379> setnx k1 kennminn
(integer) 0
127.0.0.1:6379> setnx k3 kennminn
(integer) 0
# 如果不存在,则返回1并创建相应元素
127.0.0.1:6379> setnx k4 kennminn
(integer) 1
# 给元素设置过期时间
127.0.0.1:6379> setex k5 5 aaaa
OK
# 5s以后再次查k5的值
127.0.0.1:6379> get k5
(nil)
# mset一次设置多个元素
127.0.0.1:6379> mset key1 111 key2 222 key3 333
OK
127.0.0.1:6379> mget kye1 key2 key3
1) (nil)
2) "222"
3) "333"
127.0.0.1:6379> LPUSH list_a 1
(integer) 1
127.0.0.1:6379> LPUSH list_a 2
(integer) 2
127.0.0.1:6379> LRANGE list_a 0 -1
1) "2"
2) "1"
127.0.0.1:6379> LPOP list_a
"2"
127.0.0.1:6379> RPUSH list_a 3
(integer) 2
127.0.0.1:6379> LRANGE list_a 0 -1
1) "1"
2) "3"
127.0.0.1:6379> rpop list_a
"3"
127.0.0.1:6379>
2.Redis常用操作(list, set)
127.0.0.1:6379> LRANGE list_a 0 -1
1) "1"
# 元素值为1的前面增加元素0
127.0.0.1:6379> LINSERT list_a before 1 0
(integer) 2
127.0.0.1:6379> LRANGE list_a 0 -1
1) "0"
2) "1"
# 第二行元素的值赋值为aaa
127.0.0.1:6379> lset list_a 1 aaa
OK
127.0.0.1:6379> LRANGE list_a 0 -1
1) "0"
2) "aaa"
127.0.0.1:6379> LINDEX list_a 0
"0"
127.0.0.1:6379> LLEN list_a
(integer) 2
127.0.0.1:6379> SADD set_a aaa
(integer) 1
127.0.0.1:6379> SMEMBERS set_a
1) "aaa"
127.0.0.1:6379> SREM set_a aaa
(integer) 1
127.0.0.1:6379> SMEMBERS set_a
(empty list or set)
127.0.0.1:6379> SADD set_a 111
(integer) 1
127.0.0.1:6379> SADD set_a 222
(integer) 1
127.0.0.1:6379> SADD set_a 333
(integer) 1
127.0.0.1:6379> SADD set_a 444
(integer) 1
127.0.0.1:6379> SADD set_a 555
(integer) 1
127.0.0.1:6379> SMEMBERS set_a
1) "111"
2) "222"
3) "333"
4) "444"
5) "555"
127.0.0.1:6379> SPOP set_a
"111"
127.0.0.1:6379> SMEMBERS set_a
1) "222"
2) "333"
3) "444"
4) "555"
127.0.0.1:6379> SADD set_b 333
(integer) 1
127.0.0.1:6379> SADD set_b 444
(integer) 1
127.0.0.1:6379> SADD set_b aaa
(integer) 1
127.0.0.1:6379> SADD set_b bbb
(integer) 1
127.0.0.1:6379> SDIFF set_a set_b
1) "222"
2) "555"
127.0.0.1:6379> SDIFFSTORE set_c set_a set_b
(integer) 2
127.0.0.1:6379> SMEMBERS set_c
1) "222"
2) "555"
127.0.0.1:6379> SINTER set_a set_b
1) "333"
2) "444"
127.0.0.1:6379> SINTERSTORE set_d set_a set_b
(integer) 2
127.0.0.1:6379> SMEMBERS set_d
1) "333"
2) "444"
127.0.0.1:6379> SUNION set_a set_b
1) "222"
2) "333"
3) "444"
4) "aaa"
5) "bbb"
6) "555"
127.0.0.1:6379> SUNIONSTORE set_e set_a set_b
(integer) 6
127.0.0.1:6379> SMEMBERS set_e
1) "222"
2) "333"
3) "444"
4) "aaa"
5) "bbb"
6) "555"
127.0.0.1:6379>
3.Redis常用操作(set, zset)
127.0.0.1:6379> SMEMBERS set_e
1) "222"
2) "333"
3) "444"
4) "aaa"
5) "bbb"
6) "555"
127.0.0.1:6379>
127.0.0.1:6379> SMEMBERS set_a
1) "222"
2) "333"
3) "444"
4) "555"
# 判断一个元素是否属于集合set_a
127.0.0.1:6379> SISMEMBER set_a 222
(integer) 1
127.0.0.1:6379> SISMEMBER set_a 111
(integer) 0
# 随机取出一个元素,但不删除
127.0.0.1:6379> SRANDMEMBER set_a
"222"
127.0.0.1:6379> ZADD zset_a 11 aaaa
(integer) 1
# 按顺序显示所有元素
127.0.0.1:6379> ZRANGE zset_a 0 -1
1) "aaaa"
# 带分值按顺序显示所有元素
127.0.0.1:6379> ZRANGE zset_a 0 -1 withscores
1) "aaaa"
2) "11"
# 删除指定元素
127.0.0.1:6379> ZREM zset_a 11
(integer) 0
# 返回元素的索引值,索引从0开始,按分值正序
127.0.0.1:6379> ZRANK zset_a aaaa
(integer) 0
127.0.0.1:6379> ZADD zset_a 13 2345
(integer) 1
127.0.0.1:6379> ZADD zset_a 9 bb2345
(integer) 1
127.0.0.1:6379> ZRANGE zset_a 0 -1
1) "bb2345"
2) "aaaa"
3) "2345"
127.0.0.1:6379> ZRANk zset_a aaaa
(integer) 1
127.0.0.1:6379> ZREVRANGE zset_a 0 -1
1) "2345"
2) "aaaa"
3) "bb2345"
# 返回集合中元素的个数
127.0.0.1:6379> ZCARD zset_a
(integer) 3
# 返回分值范围1-10之间的元素
127.0.0.1:6379> ZCOUNT zset_a 1 10
(integer) 1
127.0.0.1:6379> ZRANGEBYSCORE zset_a 0 2
(empty list or set)
127.0.0.1:6379> ZADD zset_a 12 aaa
(integer) 1
127.0.0.1:6379> ZADD zset_a 13 bbb
(integer) 1
127.0.0.1:6379> ZREMRANGEBYSCORE zset_a 1 10
(integer) 1
127.0.0.1:6379>
4.Redis常用操作(hash)
# 新建hash
127.0.0.1:6379> hset user1 name aming
(integer) 0
127.0.0.1:6379> hset user1 age 30
(integer) 0
127.0.0.1:6379> hset user1 job it
(integer) 0
127.0.0.1:6379> hgetall user1
1) "name"
2) "aming"
3) "age"
4) "30"
5) "job"
6) "it"
# 批量添加键值对
127.0.0.1:6379> hmset user2 name aming age 30 job it
OK
127.0.0.1:6379> hmget user2 name age job
1) "aming"
2) "30"
3) (nil)
# 删除指定键
127.0.0.1:6379> hdel user2 age
(integer) 1
# 打印打有键
127.0.0.1:6379> HKEYS user2
1) "name"
2) "job"
# 打印所有值
127.0.0.1:6379> HVALS user2
1) "aming"
2) "it"
# 查看hash有几个field
127.0.0.1:6379> HLEN user2
(integer) 2
127.0.0.1:6379>
# 取出所有key
127.0.0.1:6379> keys *
1) "set_c"
2) "k3"
3) "set_b"
4) "k2"
5) "k1"
6) "key2"
7) "set_a"
8) "user2"
9) "set1"
10) "k4"
11) "set_e"
12) "set_d"
13) "key3"
14) "h1"
15) "list1"
16) "user1"
17) "list_a"
18) "zset_a"
19) "key1"
20) "mykey"
21) "set3"
22) "set2"
# 模糊匹配
127.0.0.1:6379> keys k*
1) "k3"
2) "k2"
3) "k1"
4) "key2"
5) "k4"
6) "key3"
7) "key1"
# 有name为set1的集合,返回1,没有返回0
127.0.0.1:6379> EXISTS set1
(integer) 1
127.0.0.1:6379> EXISTS aaaa
(integer) 0
# 删除一个key,成功为1,不成功返回0
127.0.0.1:6379> del set1
(integer) 1
# 设置key的超时时间
127.0.0.1:6379> EXPIRE k2 5
(integer) 1
# 查看键 还有多长时间过期,单位是s,当 key 不存在时,返回 -2 。 当 key 存在但没有设置剩余生存时间时,返回 -1 。 否则,返回 key 的剩余生存时间。
127.0.0.1:6379> ttl k2
(integer) -2
127.0.0.1:6379> expire k3 100
(integer) 1
127.0.0.1:6379> ttl k3
(integer) 94
127.0.0.1:6379>
# 代表选择当前数据库,默认进入0 数据库
127.0.0.1:6379> select 0
OK
# 把age 移动到1 数据库, 当前因数据库1不存在, 移动不成功
127.0.0.1:6379> MOVE age 1
(integer) 0
# 取消key3的过期时间
127.0.0.1:6379> expire key3 100
(integer) 1
127.0.0.1:6379> persist key3
(integer) 1
# 随机地返回key
127.0.0.1:6379> randomkey
"key3"
# 重命名key
127.0.0.1:6379> rename key3 key4
OK
# 查看key的类型
127.0.0.1:6379> type key4
string
# 返回当前数据库中key的数目
127.0.0.1:6379> dbsize
(integer) 19
# 返回数据库的状态信息
127.0.0.1:6379> info
# Server
redis_version:4.0.11
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:379f2baee845925
redis_mode:standalone
os:Linux 3.10.0-693.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:4.8.5
process_id:4914
run_id:98e0aafbc301473e5cd79d449aff2b8b656b33e9
tcp_port:6379
uptime_in_seconds:40854
uptime_in_days:0
hz:10
lru_clock:8346862
executable:/usr/local/src/redis-4.0.11/redis-server
config_file:/etc/redis.conf
# Clients
connected_clients:1
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0
# Memory
used_memory:851128
used_memory_human:831.18K
used_memory_rss:2510848
used_memory_rss_human:2.39M
used_memory_peak:851128
used_memory_peak_human:831.18K
used_memory_peak_perc:100.07%
used_memory_overhead:837262
used_memory_startup:786584
used_memory_dataset:13866
used_memory_dataset_perc:21.48%
total_system_memory:1023713280
total_system_memory_human:976.29M
used_memory_lua:37888
used_memory_lua_human:37.00K
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
mem_fragmentation_ratio:2.95
mem_allocator:jemalloc-4.0.3
active_defrag_running:0
lazyfree_pending_objects:0
# Persistence
loading:0
rdb_changes_since_last_save:5
rdb_bgsave_in_progress:0
rdb_last_save_time:1535072813
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:0
rdb_current_bgsave_time_sec:-1
rdb_last_cow_size:348160
aof_enabled:1
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
aof_current_size:3081
aof_base_size:0
aof_pending_rewrite:0
aof_buffer_length:0
aof_rewrite_buffer_length:0
aof_pending_bio_fsync:0
aof_delayed_fsync:0
# Stats
total_connections_received:3
total_commands_processed:166
instantaneous_ops_per_sec:0
total_net_input_bytes:6218
total_net_output_bytes:33240
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:3
expired_stale_perc:0.00
expired_time_cap_reached_count:0
evicted_keys:0
keyspace_hits:75
keyspace_misses:13
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:302
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:a6c48034ad413a880216a547ea633dd19acf1042
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:34.93
used_cpu_user:26.09
used_cpu_sys_children:0.02
used_cpu_user_children:0.01
# Cluster
cluster_enabled:0
# Keyspace
db0:keys=19,expires=0,avg_ttl=0
127.0.0.1:6379>
# 清空当前数据库的所有key
127.0.0.1:6379> flushdb
# 清空所有数据库的所有key
127.0.0.1:6379> flushall
# 保存数据到 rdb文件中,在后台运行
127.0.0.1:6379> BGSAVE
Background saving started
# 保存数据到rdb文件中,在前台运行
127.0.0.1:6379> save
# 获取所有配置参数
127.0.0.1:6379> config get *
# 获取配置参数
127.0.0.1:6379> config get dir
1) "dir"
2) "/data/redis_data"
# 更改配置参数
config set dir 值
# 数据恢复: 首先定义或者确定dir目录和dbfilename,然后把备份的rdb文件放到dir目录下面,重启redis服务即可恢复数据
1.设置监听ip
[root@lnmp-server redis-4.0.11]# vim /etc/redis.conf
# 修改为信任的ip,可以是多个ip,用空格分隔
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1 ::1
# 可将默认端口修改为其他不知名端口高端口
# port 6379
port 16000
3.设置密码
# 可取消此句注释,并设置密码为123456
# requirepass foobared
requirepass 123456
4.将config命令改名
# security of read only slaves using ‘rename-command‘ to shadow all the
# administrative / dangerous commands.
# 添加此行
# 如果想禁用config命令,可以添加此行,但是将重命名的命令设置为空
# rename-command CONFIG ""
rename-command config aming
slave-read-only yes
5.验证
# 关闭redis
[root@lnmp-server redis-4.0.11]# redis-cli shutdown
5750:M 24 Aug 09:52:45.308 # User requested shutdown...
5750:M 24 Aug 09:52:45.308 * Saving the final RDB snapshot before exiting.
5750:M 24 Aug 09:52:45.310 * DB saved on disk
5750:M 24 Aug 09:52:45.310 * Removing the pid file.
5750:M 24 Aug 09:52:45.310 # Redis is now ready to exit, bye bye...
# 启动redis
[root@lnmp-server redis-4.0.11]# redis-server /etc/redis.conf
[root@lnmp-server redis-4.0.11]# netstat -nltup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1152/nginx: master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1096/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1230/master
tcp 0 0 127.0.0.1:16000 0.0.0.0:* LISTEN 5765/redis-server 1
tcp6 0 0 :::22 :::* LISTEN 1096/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1230/master
tcp6 0 0 :::3306 :::* LISTEN 1367/mysqld
udp 0 0 127.0.0.1:323 0.0.0.0:* 696/chronyd
udp6 0 0 ::1:323 :::* 696/chronyd
[root@lnmp-server redis-4.0.11]# redis-cli -a 123456 -p 16000
Warning: Using a password with ‘-a‘ option on the command line interface may not be safe.
127.0.0.1:16000> keys *
1) "user1"
2) "set_e"
3) "key1"
4) "k1"
5) "list_a"
6) "set_a"
7) "set_c"
8) "key2"
9) "list1"
10) "set3"
11) "set_b"
12) "set_d"
13) "key4"
14) "user2"
15) "k4"
16) "h1"
17) "set2"
18) "zset_a"
19) "mykey"
127.0.0.1:16000> CONFIG GET dir
(error) ERR unknown command `CONFIG`, with args beginning with: `GET`, `dir`,
127.0.0.1:16000> aming get dir
1) "dir"
2) "/data/redis_data"
编辑/etc/redis.conf
[root@lnmp-server redis-4.0.11]# vim /etc/redis.conf
# 针对慢查询日志,可以设置两个参数,一个是执行时长,单位是微秒,另一个是慢查询日志的长度。当一个新的命令被写入日志时,最老的一条会从命令日志队列中被移除。
# 默认慢查询日志已经开启
# a value of zero forces the logging of every command.
# 单位ms,表示慢于10000ms则记录日志
slowlog-log-slower-than 10000
# There is no limit to this length. Just be aware that it will consume memory.
# You can reclaim memory used by the slow log with SLOWLOG RESET.
# 定义日志长度,表示最多存128条
slowlog-max-len 128
slowlog相关的命令
# 列出所有的慢查询日志
slowlog get
# 只列出2条
slowlog get 2
# 查看慢查询日志条数
slowlog len
1.下载redis的php扩展
[root@lnmp-server redis-4.0.11]# cd /usr/local/src/
[root@lnmp-server redis-4.0.11]# cd /usr/local/src/
[root@lnmp-server src]# wget https://codeload.github.com/phpredis/phpredis/zip/develop
--2018-08-24 10:19:53-- https://codeload.github.com/phpredis/phpredis/zip/develop
Resolving codeload.github.com (codeload.github.com)... 54.251.140.56, 13.229.189.0, 13.250.162.133
Connecting to codeload.github.com (codeload.github.com)|54.251.140.56|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [application/zip]
Saving to: ‘develop’
[ <=> ] 240,041 402KB/s in 0.6s
2018-08-24 10:19:55 (402 KB/s) - ‘develop’ saved [240041]
[root@lnmp-server src]# mv develop phpredis-develop.zip
2.解压软件包
[root@lnmp-server src]# unzip phpredis-develop.zip
Archive: phpredis-develop.zip
1d997873750e5fdef1fe368b5296f4427dab3ea0
creating: phpredis-develop/
...中间略...
inflating: phpredis-develop/tests/regenerateSessionId.php
inflating: phpredis-develop/tests/startSession.php
3.配置
[root@lnmp-server src]# cd phpredis-develop/
[root@lnmp-server phpredis-develop]# /usr/local/php-fpm/bin/phpize
Configuring for:
PHP Api Version: 20131106
Zend Module Api No: 20131226
Zend Extension Api No: 220131226
4.配置、编译安装
[root@lnmp-server phpredis-develop]# ./configure --with-php-config=/usr/local/php-fpm/bin/php-config
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking for a sed that does not truncate output... /usr/bin/sed
...中间略...
creating libtool
appending configuration tag "CXX" to libtool
configure: creating ./config.status
config.status: creating config.h
[root@lnmp-server phpredis-develop]# make
/bin/sh /usr/local/src/phpredis-develop/libtool --mode=compile cc -I. -I/usr/local/src/phpredis-develop -DPHP_ATOM_INC -I/usr/local/src/phpredis-develop/include -I/usr/local/src/phpredis-develop/main -I/usr/local/src/phpredis-develop -I/usr/local/php-fpm/include/php -I/usr/local/php-fpm/include/php/main -I/usr/local/php-fpm/include/php/TSRM -I/usr/local/php-fpm/include/php/Zend -I/usr/local/php-fpm/include/php/ext -I/usr/local/php-fpm/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /usr/local/src/phpredis-develop/redis.c -o redis.lo
...中间略...
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
Build complete.
Don‘t forget to run ‘make test‘.
[root@lnmp-server phpredis-develop]# make install
Installing shared extensions: /usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20131226/
5.配置/usr/local/php-fpm/etc/php.ini
/usr/local/php-fpm/etc/php.ini
# 添加下行
extension=redis.so
[root@lnmp-server phpredis-develop]# /usr/local/php-fpm/sbin/php-fpm -m | grep redis
redis
# 重启php-fpm
[root@lnmp-server phpredis-develop]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
1.修改php-fpm主配置文件
vim /usr/local/php-fpm/etc/php.ini
# 更改或增加
session.save_handler = "redis"
session.save_path = "tcp://127.0.0.1:6379"
2.apache虚拟主机配置文件中也可以这样配置:
php_value session.save_handler " redis" php_value session.save_path " tcp://127.0.0.1:6379"
3.php-fpm配置文件对应的pool中增加:
php_value[session.save_handler] = redis
php_value[session.save_path] = " tcp://127.0.0.1:6379 "
这里以第1种方法,修改php-fpm主配置文件为例
1.修改配置文件后重启php-tpm
2.新建测试文件
[root@lnmp-server src]# vim /usr/local/nginx/html/bbb.com/session.php
# session.php内容可参考https://coding.net/u/aminglinux/p/yuanke_centos7/git/blob/master/21NOSQL/session.php
#内容如下
<?php
session_start();
if (!isset($_SESSION[‘TEST‘])) {
$_SESSION[‘TEST‘] = time();
}
$_SESSION[‘TEST3‘] = time();
print $_SESSION[‘TEST‘];
print "<br><br>";
print $_SESSION[‘TEST3‘];
print "<br><br>";
print session_id();
?>
3.测试
[root@lnmp-server src]# curl localhost/session.php
1535079050<br><br>1535079050<br><br>qra2l77cbvqca62ljtoibpjcl6
[root@lnmp-server src]# redis-cli
127.0.0.1:6379> keys *
1) "set_d"
2) "user1"
3) "set2"
# 说明已经保存到redis中了
4) "PHPREDIS_SESSION:qra2l77cbvqca62ljtoibpjcl6"
5) "set3"
6) "list_a"
7) "PHPREDIS_SESSION:uo3tmvekflcp0q051u0rjp5i25"
8) "set_e"
9) "set_a"
10) "list1"
11) "mykey"
12) "key4"
13) "set_b"
14) "k4"
15) "key1"
16) "set_c"
17) "k1"
18) "user2"
19) "zset_a"
20) "key2"
21) "h1"
127.0.0.1:6379>
为了节省资源,我们可以在一台机器上启动两个redis服务
1.新建从配置文件
[root@lnmp-server src]# cp /etc/redis.conf /etc/redis1.conf
[root@lnmp-server src]# vim /etc/redis1.conf
# port,dir,pidfile,logfile
port 6380
dir /data/redis1_data/
pidfile /var/run/redis_6380.pid
logfile "/var/log/redis1.log"
# 增加
slaveof 127.0.0.1 6379
# 如果主上设置了密码,还需要增加
# masterauth aminglinux>com 设置主的密码
# 启动之前不要忘记创建新的dir目录
[root@lnmp-server src]# mkdir /data/redis1_data/
2.启动从redis
[root@lnmp-server src]# redis-server /etc/redis1.conf
[root@lnmp-server src]# netstat -nltup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 1482/redis-server 1
tcp 0 0 127.0.0.1:6380 0.0.0.0:* LISTEN 1690/redis-server 1
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1133/nginx: master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1077/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1348/master
tcp6 0 0 :::22 :::* LISTEN 1077/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1348/master
tcp6 0 0 :::3306 :::* LISTEN 1308/mysqld
udp 0 0 127.0.0.1:323 0.0.0.0:* 689/chronyd
udp6 0 0 ::1:323 :::* 689/chronyd
[root@lnmp-server src]#
3.测试
# 登录从redis,查看数据,主上的数据自动同步过来了
[root@lnmp-server src]# redis-cli -p 6380
127.0.0.1:6380> keys *
1) "k4"
2) "mykey"
3) "key4"
4) "user2"
5) "list_a"
6) "set_d"
7) "set2"
8) "set_a"
9) "k1"
10) "user1"
11) "set_e"
12) "key1"
13) "list1"
14) "set_c"
15) "h1"
16) "zset_a"
17) "set_b"
18) "key2"
19) "set3"
多个redis节点网络互联,数据共享
所有的节点都是一主一从(可以是多个从),其中从不提供服务,仅作为备用
不支持同时处理多个键(如mset/mget),因为redis需要把键均匀分布在各个节点上,并发量很高的情况下同时创建键值会降低性能并导致不可预测的行为。
支持在线增加、删除节点
客户端可以连任何一个主节点进行读写
场景设置:
两台机器,分别开启三个Redis服务(端口)
A机器上三个端口7000,7002,7004,全部为主
B机器上三个端口7001,7003,7005,全部为从
两台机器上都要编译安装redis,然后编辑并复制3个不同的redis.conf,分别设置不同的端口号、dir等参数,还需要增加cluster相关参数,然后分别启动6个redis服务
1.分别在两台机器上编译安装redis, 各建3个配置文件,配置文件可参考https://coding.net/u/aminglinux/p/yuanke_centos7/git/tree/master/21NOSQL。分别启动各reids
[root@redis-master src]# ps aux | grep redis
root 18786 0.0 0.9 147312 9624 ? Ssl 14:31 0:02 redis-server 127.0.0.1:7000 [cluster]
root 18792 0.0 0.9 147312 9624 ? Ssl 14:31 0:02 redis-server 127.0.0.1:7002 [cluster]
root 18798 0.0 0.9 147312 9624 ? Ssl 14:31 0:02 redis-server 127.0.0.1:7004 [cluster]
root 18861 0.0 0.0 112660 972 pts/0 R+ 15:19 0:00 grep --color=auto redis
[root@redis-slave src]# ps aux | grep redis
root 4830 0.0 0.9 147312 9624 ? Ssl 14:32 0:02 redis-server 127.0.0.1:7001 [cluster]
root 4835 0.0 0.9 147312 9624 ? Ssl 14:32 0:02 redis-server 127.0.0.1:7003 [cluster]
root 4840 0.0 0.9 147312 9624 ? Ssl 14:32 0:02 redis-server 127.0.0.1:7005 [cluster]
root 18868 0.0 0.0 112660 972 pts/0 R+ 15:19 0:00 grep --color=auto redis
2.安装ruby2.2
# 如果未安装开发工具,需要安装一下开发工具
# yum groupinstall "Development Tools" -y
# 安装依赖包
[root@redis-slave src]# yum -y install gdbm-devel libdb4-devel libffi-devel libyaml libyaml-devel ncurses-devel openssl-devel readline-devel tcl-deve
[root@redis-master src]# cd /root/
[root@redis-master ~]# mkdir -p rpmbuild/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS}
[root@redis-master ~]# wget http://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.3.tar.gz -P rpmbuild/SOURCES
--2018-08-24 15:32:43-- http://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.3.tar.gz
Resolving cache.ruby-lang.org (cache.ruby-lang.org)... 151.101.229.178, 2a04:4e42:36::434
Connecting to cache.ruby-lang.org (cache.ruby-lang.org)|151.101.229.178|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 16626772 (16M) [application/x-tar]
Saving to: ‘rpmbuild/SOURCES/ruby-2.2.3.tar.gz’
100%[====================================================================================>] 16,626,772 6.94MB/s in 2.3s
2018-08-24 15:32:53 (6.94 MB/s) - ‘rpmbuild/SOURCES/ruby-2.2.3.tar.gz’ saved [16626772/16626772]
[root@redis-master ~]# wget https://raw.githubusercontent.com/tjinjin/automate-ruby-rpm/master/ruby22x.spec -P rpmbuild/SPECS
--2018-08-24 15:33:18-- https://raw.githubusercontent.com/tjinjin/automate-ruby-rpm/master/ruby22x.spec
Resolving raw.githubusercontent.com (raw.githubusercontent.com)...
151.101.228.133
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.228.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 4484 (4.4K) [text/plain]
Saving to: ‘rpmbuild/SPECS/ruby22x.spec’
100%[====================================================================================>] 4,484 --.-K/s in 0s
2018-08-24 15:33:29 (41.0 MB/s) - ‘rpmbuild/SPECS/ruby22x.spec’ saved [4484/4484]
[root@redis-master ~]# rpmbuild -bb rpmbuild/SPECS/ruby22x.spec
+ exit 0
[root@redis-master ~]# ls rpmbuild/RPMS/x86_64/
ruby-2.2.3-1.el7.centos.x86_64.rpm ruby-debuginfo-2.2.3-1.el7.centos.x86_64.rpm
ruby-2.2.3-1.el7.centos.x86_64.rpm ruby-debuginfo-2.2.3-1.el7.centos.x86_64.rpm
[root@redis-master ~]# yum -y localinstall rpmbuild/RPMS/x86_64/ruby-2.2.3-1.el7.centos.x86_64.rpm
[root@redis-master ~]# rpm -q ruby
ruby-2.2.3-1.el7.centos.x86_64
[root@redis-master ~]# gem install redis
Fetching: redis-4.0.2.gem (100%)
Successfully installed redis-4.0.2
Parsing documentation for redis-4.0.2
Installing ri documentation for redis-4.0.2
Done installing documentation for redis after 0 seconds
1 gem installed
3.Redis集群配置
[root@redis-master ~]# cp /usr/local/src/redis-4.0.11/src/redis-trib.rb /usr/bin/
# 配置集群
[root@redis-master ~]# redis-trib.rb create --replicas 1 192.168.1.45:7000 192.168.1.45:7002 192.168.1.45:7004 192.168.1.46:7001 192.168.1.46:7003 192.168.1.46:7005
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.1.45:7000
192.168.1.46:7001
192.168.1.45:7002
Adding replica 192.168.1.46:7005 to 192.168.1.45:7000
Adding replica 192.168.1.45:7004 to 192.168.1.46:7001
Adding replica 192.168.1.46:7003 to 192.168.1.45:7002
M: 65fff908dc0736d0cabea55d6dd5908f5655b38a 192.168.1.45:7000
slots:0-5460 (5461 slots) master
M: 45876690d891e369cc1ffd0ae61b2f6b4c59e70b 192.168.1.45:7002
slots:10923-16383 (5461 slots) master
S: 4837f52c7731531c7c6ea903f0c81b28b6956d21 192.168.1.45:7004
replicates 31abbf90e210d813bbfa6e6cecbd1d616d579461
M: 31abbf90e210d813bbfa6e6cecbd1d616d579461 192.168.1.46:7001
slots:5461-10922 (5462 slots) master
S: 215d5b451cbe5e3e68ba131f64acf6e1b6fe62e7 192.168.1.46:7003
replicates 45876690d891e369cc1ffd0ae61b2f6b4c59e70b
S: 24cbab3b253d8a3e89978a402873bfb3a72a8d21 192.168.1.46:7005
replicates 65fff908dc0736d0cabea55d6dd5908f5655b38a
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 192.168.1.45:7000)
M: 65fff908dc0736d0cabea55d6dd5908f5655b38a 192.168.1.45:7000
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 45876690d891e369cc1ffd0ae61b2f6b4c59e70b 192.168.1.45:7002
slots:10923-16383 (5461 slots) master
1 additional replica(s)
M: 31abbf90e210d813bbfa6e6cecbd1d616d579461 192.168.1.46:7001
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 4837f52c7731531c7c6ea903f0c81b28b6956d21 192.168.1.45:7004
slots: (0 slots) slave
replicates 31abbf90e210d813bbfa6e6cecbd1d616d579461
S: 24cbab3b253d8a3e89978a402873bfb3a72a8d21 192.168.1.46:7005
slots: (0 slots) slave
replicates 65fff908dc0736d0cabea55d6dd5908f5655b38a
S: 215d5b451cbe5e3e68ba131f64acf6e1b6fe62e7 192.168.1.46:7003
slots: (0 slots) slave
replicates 45876690d891e369cc1ffd0ae61b2f6b4c59e70b
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
1.连接redis集群
# -c以集群的方式登录,-h 主机地址,-p 端口号
[root@redis-master ~]# redis-cli -c -h 192.168.1.45 -p 7000
192.168.1.45:7000>
[root@redis-master ~]# redis-cli -c -h 192.168.1.45 -p 7000
192.168.1.45:7000> set k1 "nico"
-> Redirected to slot [12706] located at 192.168.1.45:7002
OK
192.168.1.45:7002> get k1
"nico"
[root@redis-master ~]# redis-cli -c -h 192.168.1.45 -p 7002
192.168.1.45:7002> get k1
"nico"
2.常用操作
# 检测集群状态
[root@redis-master ~]# redis-trib.rb check 192.168.1.45:7000
>>> Performing Cluster Check (using node 192.168.1.45:7000)
M: 65fff908dc0736d0cabea55d6dd5908f5655b38a 192.168.1.45:7000
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 45876690d891e369cc1ffd0ae61b2f6b4c59e70b 192.168.1.45:7002
slots:10923-16383 (5461 slots) master
1 additional replica(s)
M: 31abbf90e210d813bbfa6e6cecbd1d616d579461 192.168.1.46:7001
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 4837f52c7731531c7c6ea903f0c81b28b6956d21 192.168.1.45:7004
slots: (0 slots) slave
replicates 31abbf90e210d813bbfa6e6cecbd1d616d579461
S: 24cbab3b253d8a3e89978a402873bfb3a72a8d21 192.168.1.46:7005
slots: (0 slots) slave
replicates 65fff908dc0736d0cabea55d6dd5908f5655b38a
S: 215d5b451cbe5e3e68ba131f64acf6e1b6fe62e7 192.168.1.46:7003
slots: (0 slots) slave
replicates 45876690d891e369cc1ffd0ae61b2f6b4c59e70b
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
# 列出节点
[root@redis-master releasetools]# redis-cli -c -h 192.168.1.45 -p 7000
192.168.1.45:7000> cluster nodes
45876690d891e369cc1ffd0ae61b2f6b4c59e70b 192.168.1.45:7002@17002 master - 0 1535103063059 2 connected 10923-16383
31abbf90e210d813bbfa6e6cecbd1d616d579461 192.168.1.46:7001@17001 master - 0 1535103061000 4 connected 5461-10922
65fff908dc0736d0cabea55d6dd5908f5655b38a 192.168.1.45:7000@17000 myself,master - 0 1535103060000 1 connected 0-5460
4837f52c7731531c7c6ea903f0c81b28b6956d21 192.168.1.45:7004@17004 slave 31abbf90e210d813bbfa6e6cecbd1d616d579461 0 1535103062000 4 connected
24cbab3b253d8a3e89978a402873bfb3a72a8d21 192.168.1.46:7005@17005 slave 65fff908dc0736d0cabea55d6dd5908f5655b38a 0 1535103062356 6 connected
215d5b451cbe5e3e68ba131f64acf6e1b6fe62e7 192.168.1.46:7003@17003 slave 45876690d891e369cc1ffd0ae61b2f6b4c59e70b 0 1535103060341 5 connected
# 查看集群信息
192.168.1.45:7000> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:4232
cluster_stats_messages_pong_sent:4159
cluster_stats_messages_sent:8391
cluster_stats_messages_ping_received:4154
cluster_stats_messages_pong_received:4232
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:8391
# cluster meet ip port 添加节点
# cluster forget node_id 移除节点
# cluster replicate node_id 将当前节点设置为指定节点的从节点
# 新增加2个配置文件,并启动
[root@redis-master releasetools]# redis-server /etc/redis7006.conf
[root@redis-master releasetools]# redis-server /etc/redis7007.conf
[root@redis-master releasetools]# ps aux | grep redis
root 34213 0.2 1.1 151408 11928 ? Ssl 16:18 0:13 redis-server 192.168.1.45:7000 [cluster]
root 34264 0.2 1.1 151408 11936 ? Ssl 16:20 0:13 redis-server 192.168.1.45:7002 [cluster]
root 34269 0.2 1.1 151408 11928 ? Ssl 16:20 0:14 redis-server 192.168.1.45:7004 [cluster]
root 34504 0.0 0.9 147312 9624 ? Ssl 17:52 0:00 redis-server 192.168.1.45:7006 [cluster]
root 34509 0.2 0.9 147312 9704 ? Ssl 17:52 0:00 redis-server 192.168.1.45:7007 [cluster]
root 34515 0.0 0.0 112660 968 pts/0 R+ 17:52 0:00 grep --color=auto redis
[root@redis-master releasetools]# redis-cli -c -h 192.168.1.45 -p 7000
192.168.1.45:7000> cluster nodes
45876690d891e369cc1ffd0ae61b2f6b4c59e70b 192.168.1.45:7002@17002 master - 0 1535104403139 2 connected 10923-16383
31abbf90e210d813bbfa6e6cecbd1d616d579461 192.168.1.46:7001@17001 master - 0 1535104402000 4 connected 5461-10922
65fff908dc0736d0cabea55d6dd5908f5655b38a 192.168.1.45:7000@17000 myself,master - 0 1535104402000 1 connected 0-5460
4837f52c7731531c7c6ea903f0c81b28b6956d21 192.168.1.45:7004@17004 slave 31abbf90e210d813bbfa6e6cecbd1d616d579461 0 1535104402129 4 connected
24cbab3b253d8a3e89978a402873bfb3a72a8d21 192.168.1.46:7005@17005 slave 65fff908dc0736d0cabea55d6dd5908f5655b38a 0 1535104400117 6 connected
215d5b451cbe5e3e68ba131f64acf6e1b6fe62e7 192.168.1.46:7003@17003 slave 45876690d891e369cc1ffd0ae61b2f6b4c59e70b 0 1535104403138 5 connected
# 添加节点,
193.192.168.1.45:7000> CLUSTER MEET 192.168.1.45 7006
OK
192.168.1.45:7000> CLUSTER MEET 192.168.1.45 7007
OK
192.168.1.45:7000> cluster nodes
6157feade2af15a81a982ab467fc9746514552e3 192.168.1.45:7007@17007 slave 65fff908dc0736d0cabea55d6dd5908f5655b38a 0 1535104467000 1 connected
45876690d891e369cc1ffd0ae61b2f6b4c59e70b 192.168.1.45:7002@17002 master - 0 1535104465000 2 connected 10923-16383
31abbf90e210d813bbfa6e6cecbd1d616d579461 192.168.1.46:7001@17001 master - 0 1535104467106 4 connected 5461-10922
65fff908dc0736d0cabea55d6dd5908f5655b38a 192.168.1.45:7000@17000 myself,master - 0 1535104464000 1 connected 0-5460
4837f52c7731531c7c6ea903f0c81b28b6956d21 192.168.1.45:7004@17004 slave 31abbf90e210d813bbfa6e6cecbd1d616d579461 0 1535104466000 4 connected
24cbab3b253d8a3e89978a402873bfb3a72a8d21 192.168.1.46:7005@17005 slave 65fff908dc0736d0cabea55d6dd5908f5655b38a 0 1535104467813 6 connected
215d5b451cbe5e3e68ba131f64acf6e1b6fe62e7 192.168.1.46:7003@17003 slave 45876690d891e369cc1ffd0ae61b2f6b4c59e70b 0 1535104466000 5 connected
81885a03578dbd587136c233b48aee633703286e 192.168.1.45:7006@17006 master - 0 1535104466000 0 connected
# 删除节点
192.168.1.45:7000> CLUSTER FORGET 6157feade2af15a81a982ab467fc9746514552e3
OK
192.168.1.45:7000> cluster nodes
45876690d891e369cc1ffd0ae61b2f6b4c59e70b 192.168.1.45:7002@17002 master - 0 1535104692665 2 connected 10923-16383
31abbf90e210d813bbfa6e6cecbd1d616d579461 192.168.1.46:7001@17001 master - 0 1535104689000 4 connected 5461-10922
65fff908dc0736d0cabea55d6dd5908f5655b38a 192.168.1.45:7000@17000 myself,master - 0 1535104688000 1 connected 0-5460
4837f52c7731531c7c6ea903f0c81b28b6956d21 192.168.1.45:7004@17004 slave 31abbf90e210d813bbfa6e6cecbd1d616d579461 0 1535104691000 4 connected
24cbab3b253d8a3e89978a402873bfb3a72a8d21 192.168.1.46:7005@17005 slave 65fff908dc0736d0cabea55d6dd5908f5655b38a 0 1535104690652 6 connected
215d5b451cbe5e3e68ba131f64acf6e1b6fe62e7 192.168.1.46:7003@17003 slave 45876690d891e369cc1ffd0ae61b2f6b4c59e70b 0 1535104691000 5 connected
81885a03578dbd587136c233b48aee633703286e 192.168.1.45:7006@17006 master - 0 1535104691658 0 connected
# cluster saveconfig 保存配置文件
192.168.1.45:7000> cluster saveconfig
OK
redis哨兵
https://segmentfault.com/a/1190000002680804
http://www.cnblogs.com/jaycekon/p/6237562.html
标签:daemonize nes 增加 nta fail foreign cal 0.0.0.0 快照
原文地址:https://www.cnblogs.com/minn/p/9531272.html