Redis适用场景:php
- 取出最新N个数据的操做
- 排行榜应用,取出Top N操做
- 须要精确设定过时时间的应用
- 计数器应用
- uniq操做,获取某段时间全部数据排除重复值
- 实时系统,反垃圾系统
- Pub/Sub构建实时消息系统。Redis独有功能,发布/订阅
- 构建队列系统
- 缓存
[root@ying01 ~]# cd /usr/local/src/ [root@ying01 src]# wget http://download.redis.io/releases/redis-4.0.9.tar.gz [root@ying01 src]# tar zxf redis-4.0.9.tar.gz [root@ying01 src]# cd redis-4.0.9/ [root@ying01 redis-4.0.9]# [root@ying01 redis-4.0.9]# make && make install [root@ying01 redis-4.0.9]# echo $? 0
[root@ying01 redis-4.0.9]# vim /etc/redis.conf 如下为redis.conf配置内容的一些设置: # Examples: # # bind 192.168.1.100 10.0.0.1 //此处配置监听IP # bind 127.0.0.1 ::1 protected-mode yes //定义保护模式 # Accept connections on the specified port, default is 6379 (IANA #815344). # If port 0 is specified Redis will not listen on a TCP socket. port 6379 //监听端口 daemonize no //默认值为no,表明前台启动,yes表示后台启动 pidfile /var/run/redis_6379.pid //定义redis的PID logfile "" //此处定义redis的log存放路径,默认为空 dir ./ //定义rdb的存放位置(aof也是存在这个位置),默认为根下 slave-priority 100 //此为设置权重,相似于负责均衡 appendonly no //只增文件的名称,默认为 no ,不指定 # The name of the append only file (default: "appendonly.aof") appendfilename "appendonly.aof" //只增文件的名称为 appendonly.aof # appendfsync always // 每次更新数据都写入仅增日志文件。慢,可是最安全。 appendfsync everysec //每秒调用一次。折中。(默认状况) # appendfsync no ///不调用,等待操做系统来清空缓冲区当操做系统要输出数据时。很快。
大概了解以上设置含义,如今把配置文件中部份内容按下面设置;html
[root@ying01 ~]# vim /etc/redis.conf daemonize yes //默认为no,现设置为yes logfile "/var/log/redis.log" //默认为空 dir /data/redis //从新设置目录,原来为根下,等下须要建立定义的目录 appendonly yes //原为no
[root@ying01 redis-4.0.9]# mkdir /data/redis [root@ying01 redis-4.0.9]# redis-server /etc/redis.conf [root@ying01 redis-4.0.9]# netstat -lntp |grep redis tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 1285/redis-server 1
查看redis.log,发现有记下警告信息:node
[root@ying01 redis-4.0.9]# less /var/log/redis.log
第一条 警告信息mysql
WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. #警告:没法强制执行TCP的积压设置511,由于/ proc / sys / net / core / somaxconn设置为较低的值128。第二条 警告信息linux
WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect. #overcommit_memory设置为0!在低内存条件下,后台保存可能会失败。要解决此问题,请将“vm.overcommit_memory = 1”添加到/etc/sysctl.conf,而后从新启动或运行命令“sysctl vm.overcommit_memory = 1”以使其生效。第三条警告信息git
WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled. # 警告您在内核中启用了透明大页面(THP)支持。这将致使Redis的延迟和内存使用问题。要解决此问题,请以root身份运行命令“echo never> / sys / kernel / mm / transparent_hugepage / enabled”,并将其添加到/etc/rc.local中,以便在从新启动后保留设置。禁用THP后必须从新启动Redis。
解决方法:(安装日志说明来解决)github
在此文件中,先添加如下语句redis
[root@ying01 redis-4.0.9]# vim /etc/rc.local sysctl vm.overcommit_memory=1 echo never > /sys/kernel/mm/transparent_hugepage/enable
使用命令行,让其生效;算法
[root@ying01 redis-4.0.9]# sysctl vm.overcommit_memory=1 vm.overcommit_memory = 1 [root@ying01 redis-4.0.9]# echo never > /sys/kernel/mm/transparent_hugepage/enabled
重启redis服务sql
[root@ying01 redis-4.0.9]# redis-cli -h 127.0.0.1 -p 6379 shutdown //中止服务 [root@ying01 redis-4.0.9]# netstat -lntp |grep redis [root@ying01 redis-4.0.9]# redis-server /etc/redis.conf //开启服务 [root@ying01 redis-4.0.9]# netstat -lntp |grep redis tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 1285/redis-server 1
再次查看redis.log,发现2个警告已经解除
[root@ying01 redis-4.0.9]# less /var/log/redis.log _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 4.0.9 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 1394 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 1394:M 24 Aug 01:35:48.787 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is s et to the lower value of 128. 1394:M 24 Aug 01:35:48.787 # Server initialized 1394:M 24 Aug 01:35:48.787 * Ready to accept connections (END)
https://www.cnblogs.com/GoQC/p/5764201.html
https://www.cnblogs.com/pqchao/p/6549510.html
和Memcached服务同样,若是关闭RDB和aof数据就会存储在内存中,当重启服务或者重启机器 存储的数据就会丢失。若是数据很重要,咱们就须要作持久化。
Redis的两种持久化方式:RDB(Redis DataBase)和AOF(Append Only File)。
RDB:在不一样的时间点,将redis存储的数据生成快照并保存到磁盘等介质上。默认状况下,RDB是开启的。
AOF:将Redis执行过的全部指令记录下来,下次Redis从新启动时,只要把这些指令从前到后再重复执行一遍,就能够实现数据恢复了。默认状况,AOF是关闭的。
这两种方式能够同时使用。
若是你没有数据持久化的需求,就能够关闭RDB、AOF,这样就跟memcache同样,变成了一个纯内存数据库。
再次进入redis.conf配置文件
[root@ying01 redis-4.0.9]# vim /etc/redis.conf #注意下面的演示: # In the example below the behaviour will be to save: # after 900 sec (15 min) if at least 1 key changed # after 300 sec (5 min) if at least 10 keys changed # after 60 sec if at least 10000 keys changed # # Note: you can disable saving completely by commenting out all "save" lines. # # It is also possible to remove all the previously configured save # points by adding a save directive with a single empty string argument # like in the following example: # # save "" //开启持久化 save 900 1 //表示每(900s)15分钟且至少有1个key改变,就触发一次持久化 save 300 10 //表示每(300s)5分钟且至少有10个key改变,就触发一次持久化 save 60 10000 //表示每(60s)5分钟且至少有10个key改变,就触发一次持久化
关闭持久化:把#去掉则关闭持久化
save "" //关闭持久化 #save 900 1 //表示每(900s)15分钟且至少有1个key改变,就触发一次持久化 #save 300 10 //表示每(300s)5分钟且至少有10个key改变,就触发一次持久化 #save 60 10000
AOF格式中appendfsync三种形式解析:
# appendfsync always //一直写,每次有变动就写进去 appendfsync everysec //安全高效,每一秒记录一次,把数据从内存刷新到磁盘中去 # appendfsync no //每隔一段时间,根据系统的算法,Linux系统不按期把内存的数据同步到磁盘上去,根据这个频率走,容易丢数据
开启AOF:appendonly yes,默认是appendonly no。
string为最简单的类型,与Memcached同样的类型,一个key对应一个value,其支持的操做与Memcached的操做相似,它的功能更丰富。设置能够存二进制的对象。
[root@ying01 redis-4.0.9]# redis-cli 127.0.0.1:6379> set mykey "abc" //s设置单个键值 OK 127.0.0.1:6379> get mykey //获取键值 "abc" 127.0.0.1:6379> mset k1 a k2 b k3 c k4 d //设置多个键值 OK 127.0.0.1:6379> mget k2 mykey //获取多个键值 1) "b" 2) "abc" 127.0.0.1:6379>
list是一个链表结构,主要功能是push、pop、获取一个范围的全部值等等。操做中key理解为链表的名字。
应用场景:
使用 list 结构,咱们能够轻松地实现最新消息排行等功能(好比新浪微博的 TimeLine )。list 的另外一个应用就是消息队列,能够利用 list 的 push操做,将任务存在 list 中,而后工做线程再用pop操做将任务取出进行执行。
127.0.0.1:6379> LPUSH list1 "8" (integer) 1 127.0.0.1:6379> LPUSH list1 "18" (integer) 2 127.0.0.1:6379> LPUSH list1 "28" (integer) 3 127.0.0.1:6379> LRANGE list1 0 -1 1) "28" 2) "18" 3) "8" 127.0.0.1:6379> LPOP list1 "28" 127.0.0.1:6379> LRANGE list1 0 -1 1) "18" 2) "8" 127.0.0.1:6379>
set是集合,和咱们数学中的集合概念类似,对集合的操做有添加删除元素,有对多个集合求交并差等操做。操做中key理解为集合的名字。
应用场景:
好比在微博应用中,能够将一个用户全部的关注人存在一个集合中,将其全部粉丝存在一个集合。由于 Redis 很是人性化的为集合提供了求交集、并集、差集等操做,那么就能够很是方便的实现如共同关注、共同喜爱、二度好友等功能,对上面的全部集合操做,你还可使用不一样的命令选择将结果返回给客户端仍是存集到一个新的集合中。
创建set一、set2集合元素
127.0.0.1:6379> SADD set1 8 (integer) 0 127.0.0.1:6379> SADD set1 28 (integer) 0 127.0.0.1:6379> SADD set1 58 (integer) 1 127.0.0.1:6379> SADD set2 28 (integer) 1 127.0.0.1:6379> SADD set2 58 (integer) 1 127.0.0.1:6379> SADD set2 88 (integer) 1
查看set一、set2集合
127.0.0.1:6379> SMEMBERS set1 1) "8" 2) "28" 3) "58" 127.0.0.1:6379> SMEMBERS set2 1) "28" 2) "58" 3) "88"
集合并集
127.0.0.1:6379> SUNION set1 set2 1) "8" 2) "28" 3) "58" 4) "88"
集合交集
127.0.0.1:6379> SINTER set1 set2 1) "28" 2) "58"
集合差集
127.0.0.1:6379> SDIFF set1 set2 1) "8"
删除元素
127.0.0.1:6379> SREM set1 8 28 (integer) 2 127.0.0.1:6379> SMEMBERS set1 1) "58"
sorted set是有序集合,它比set多了一个权重参数score,使得集合中的元素可以按 score 进行有序排列。
应用场景:
好比一个存储全班同窗成绩的 Sorted Sets,其集合 value 能够是同窗的学号,而 score 就能够是其考试得分,这样在数据插入集合的时候,就已经进行了自然的排序。
127.0.0.1:6379> ZADD score 18 A (integer) 1 127.0.0.1:6379> ZADD score 8 B (integer) 1 127.0.0.1:6379> ZADD score 58 ZQ (integer) 1 127.0.0.1:6379> ZADD score 88 Q (integer) 1 127.0.0.1:6379> ZRANGE score 0 -1 //正序 1) "B" 2) "A" 3) "ZQ" 4) "Q" 127.0.0.1:6379> ZREVRANGE score 0 -1 //倒序 1) "Q" 2) "ZQ" 3) "A" 4) "B" 127.0.0.1:6379>
在 Memcached 中,咱们常常将一些结构化的信息打包成 hashmap,在客户端序列化后存储为一个字符串的值(通常是 JSON 格式),好比用户的昵称、年龄、性别、积分等
127.0.0.1:6379> HSET hash1 name zq //定义 (integer) 1 127.0.0.1:6379> HSET hash1 age 18 (integer) 1 127.0.0.1:6379> HSET hash1 job sing (integer) 1 127.0.0.1:6379> HSET hash1 home shanghai (integer) 1 127.0.0.1:6379> Hget hash1 job //查看 "sing" 127.0.0.1:6379> Hget hash1 name "zq" 127.0.0.1:6379> Hget hash1 home "shanghai" 127.0.0.1:6379> Hget hash1 age "18" 127.0.0.1:6379>
127.0.0.1:6379> set key1 18 OK 127.0.0.1:6379> set key2 58 OK 127.0.0.1:6379> set key1 88 OK 127.0.0.1:6379> get key1 "88" 127.0.0.1:6379>
127.0.0.1:6379> SETNX key1 zq (integer) 0 127.0.0.1:6379> get key1 "88" 127.0.0.1:6379> SETNX key3 zq (integer) 1 127.0.0.1:6379> get key3 "zq" 127.0.0.1:6379>
127.0.0.1:6379> set key3 zq01 ex 5 OK 127.0.0.1:6379> get key3 (nil) 127.0.0.1:6379> set key3 zq01 ex 50 OK 127.0.0.1:6379> get key3 "zq01" 127.0.0.1:6379> 127.0.0.1:6379> setex key3 10 zq02 //给key3设置过时时间为10s,值为zq02,若key已经存在,会覆盖新的值 OK 127.0.0.1:6379> get key3 "zq02" 127.0.0.1:6379> get key3 (nil)
lpush lista a //从左侧加入一个元素 lrange lista 0 -1 //取出从0开始到倒数第一个之间的元素 lpop lista //从左侧取出第一个元素 rpush lista 1 //从右侧加入一个元素 rpop lista //从右侧取出第一个元素
127.0.0.1:6379> LPUSH lista 111 (integer) 1 127.0.0.1:6379> LPUSH lista 222 (integer) 2 127.0.0.1:6379> LPUSH lista 333 (integer) 3 127.0.0.1:6379> LPUSH lista 555 (integer) 4 127.0.0.1:6379> LRANGE lista 0 -1 1) "555" 2) "333" 3) "222" 4) "111" 127.0.0.1:6379> LPOP lista "555" 127.0.0.1:6379> LPOP lista "333" 127.0.0.1:6379> LRANGE lista 0 -1 1) "222" 2) "111" 127.0.0.1:6379> RPOP lista "111" 127.0.0.1:6379> LRANGE lista 0 -1 1) "222" 127.0.0.1:6379>
元素的修改:先在xxx清单增长 A B C D E ,而后更换第二位的数值
127.0.0.1:6379> LPUSH xxx A (integer) 1 127.0.0.1:6379> LPUSH xxx B (integer) 2 127.0.0.1:6379> LPUSH xxx C (integer) 3 127.0.0.1:6379> LPUSH xxx D (integer) 4 127.0.0.1:6379> LPUSH xxx E (integer) 5 127.0.0.1:6379> LSET xxx 2 W OK 127.0.0.1:6379> LINDEX xxx 2 "W" 127.0.0.1:6379> LINDEX xxx 3 "B" 127.0.0.1:6379> LINDEX xxx 4 "A" 127.0.0.1:6379> LINDEX xxx 5 (nil) 127.0.0.1:6379> LINDEX xxx 1 "D" 127.0.0.1:6379> LINDEX xxx 0 "E"
查看清单中元素的数量
127.0.0.1:6379> LLEN xxx (integer) 5
在某个元素前面(后面)插入一个元素:
127.0.0.1:6379> LINSERT xxx before D S (integer) 6 127.0.0.1:6379> LRANGE xxx 0 -1 1) "E" 2) "S" 3) "D" 4) "W" 5) "B" 6) "A" 127.0.0.1:6379>
前面已经演示过,下面set操做整理起来;
命令格式 | 释义 |
---|---|
sadd 集合名 元素 | 向集合添加元素 |
smembers 集合名 | 查看集合中的所有元素 |
srem 集合名 元素 | 删除元素 |
spop 集合名 | 取出一个元素,删除 |
sdiff 集合1 集合2 | 以集合1为标准,求差集 |
sdiffstore 集合1 集合2 集合3 | 求差集并储存到集合1 |
sinter 集合1 集合2 | 求交集 |
sinterstore 集合1 集合2 集合3 | 求交集,并存储到集合1 |
sunion 集合1 集合2 | 求并集 |
sunion 集合1 集合2 集合3 | 求并集并存储到集合1 |
sismembaer 集合 元素 | 判断一个元素是否属于一个集合,是则返回1。 |
srandmember 集合 | 取出一个元素,但不删除 |
zadd 集合 | 建立有序集合 |
zrange 集合 0 -1 | 显示全部元素,按顺序显示 |
zrange 集合 0 -1 withscores | 带上分值 |
zrem 集合 元素 | 删除指定元素 |
zrank 集合 元素 | 返回元素的索引值 |
zrevrank 集合 元素 | 按score反向排列 |
zrevrange 集合 0 -1 | 反向显示,并带分值 |
zcard 集合 | 返回集合中的元素的个数 |
zrangebyscore 集合 1 10 | 返回1-10的元素个数 |
zremrangebyrank 集合 0 2 | 删除索引0-2的元素,按分值正向排序 |
zremrangebyscore 集合 0 2 | 删除分值0-2的元素 |
127.0.0.1:6379> HMSET hash2 a 1 b 2 c 3 d 4 e 5 OK 127.0.0.1:6379> Hgetall hash2 1) "a" 2) "1" 3) "b" 4) "2" 5) "c" 6) "3" 7) "d" 8) "4" 9) "e" 10) "5"
127.0.0.1:6379> hmget hash2 b c 1) "2" 2) "3"
HMGET 查询一个键值
127.0.0.1:6379> hdel hash2 b (integer) 1 127.0.0.1:6379> Hgetall hash2 1) "a" 2) "1" 3) "c" 4) "3" 5) "d" 6) "4" 7) "e" 8) "5"
127.0.0.1:6379> hkeys hash2 1) "a" 2) "c" 3) "d" 4) "e"
127.0.0.1:6379> hvals hash2 1) "1" 2) "3" 3) "4" 4) "5"
127.0.0.1:6379> hlen hash2 (integer) 4 127.0.0.1:6379>
经常使用如下键值
- keys * :取出全部键
- exists 键名:存在返回1
- del 键:删除键
- expire 键 100:设置键100秒后过时
- ttl 键:查看键的过时时间,单位是秒,当键不存在返回-2,键存在但没设置过时时间返回-1
- select 0 :表明选择当前数据库,默认进入0数据库
- move age 1 :把age移动到1数据库
- persist 键:取消键的过时时间
- randomkey:返回一个键
- rename oldname newname:重命名
- type 键:返回键的类型
查看全部键值:
127.0.0.1:6379> keys * 1) "score" 2) "key1" 3) "set2" 4) "key2" 5) "xxx" 6) "k4" 7) "lista" 8) "list1" 9) "hash1" 10) "set1" 11) "k2" 12) "k1" 13) "k3" 14) "mykey" 15) "hash2"
模糊匹配键:
127.0.0.1:6379> keys ke* 1) "key1" 2) "key2"
判断键是否存在:
127.0.0.1:6379> exists key1 (integer) 1 127.0.0.1:6379> exists key (integer) 0
删除键
127.0.0.1:6379> get key1 "88" 127.0.0.1:6379> del key1 (integer) 1 127.0.0.1:6379> get key1 (nil)
设置过时时间:
127.0.0.1:6379> expire key2 50 (integer) 1
查看键的过时时间:
127.0.0.1:6379> ttl key2 (integer) 42
选择当前数据库:
127.0.0.1:6379> select 0 OK 127.0.0.1:6379> select 1 OK 127.0.0.1:6379[1]> keys * (empty list or set) 127.0.0.1:6379[1]> select 0 //表明选择当前数据库,默认进入0 数据库 OK
将某个键移动到其余数据库:
127.0.0.1:6379> keys * 1) "score" 2) "set2" 3) "xxx" 4) "k4" 5) "lista" 6) "list1" 7) "hash1" 8) "set1" 9) "k2" 10) "k1" 11) "k3" 12) "mykey" 13) "hash2" 127.0.0.1:6379> move k1 3 (integer) 1
随机返回一个键:
127.0.0.1:6379> randomkey "k2" 127.0.0.1:6379> randomkey "set2" 127.0.0.1:6379> randomkey "set2"
键重命名:
127.0.0.1:6379> rename k1 kx //k1不存在 (error) ERR no such key 127.0.0.1:6379> keys * 1) "score" 2) "set2" 3) "xxx" 4) "k4" 5) "lista" 6) "list1" 7) "hash1" 8) "set1" 9) "k2" 10) "k3" 11) "mykey" 12) "hash2" 127.0.0.1:6379> rename k2 k8 OK
键的类型:
127.0.0.1:6379> type score zset 127.0.0.1:6379> type k8 string 127.0.0.1
dbsize:返回当前数据的key数量
:6379> dbsize (integer) 12
如何解决redis漏洞
root@ying01 redis-4.0.9]# vim /etc/redis.conf port 18000 //更改端口 requirepass www123 //设置密码 rename-command CONFIG ying //改成ying,自定义 # rename-command CONFIG "" //禁用
先不重启服务,查看k8的值
[root@ying01 redis-4.0.9]# redis-cli 127.0.0.1:6379> get k6 (nil) 127.0.0.1:6379> get k8 "b" 127.0.0.1:6379> quit
重启服务
[root@ying01 redis-4.0.9]# killall redis-server [root@ying01 redis-4.0.9]# redis-server /etc/redis.conf
尝试登陆redis
[root@ying01 redis-4.0.9]# redis-cli //直接登陆不上 Could not connect to Redis at 127.0.0.1:6379: Connection refused not connected> quit [root@ying01 redis-4.0.9]# netstat -lntp |grep redis //端口变为18000 tcp 0 0 127.0.0.1:18000 0.0.0.0:* LISTEN 1832/redis-server 1 [root@ying01 redis-4.0.9]# redis-cli -p 18000 //指定端口18000 仍是登陆不上 127.0.0.1:18000> get k8 (error) NOAUTH Authentication required. 127.0.0.1:18000> quit [root@ying01 redis-4.0.9]# redis-cli -p 18000 -a 'www123' //端口、密码都指定,才能登陆 127.0.0.1:18000> get k8 "b" 127.0.0.1:18000>
慢查询日志,两个参数:一个是执行时长,单位是微妙,另外一个是慢查询日志的长度。当一个新的命令被写入日志时,最老的一条会从命令日志队列中被移除。
在配置文件redis.conf中,查看慢日志的定义:
[root@ying01 ~]# vim /etc/redis.conf slowlog-log-slower-than 1000:表示慢于1000ms则记录日志 slowlog-max-len 128:定义长度,最多存128条
使用命令行,查询慢日志
- slowlog get:列出 所有慢查询日志
- slowlog len:查看慢查询日志条数
先在配置文件中,定义慢于10ms,则记录日志,(为了方便试验,由于任何操做都会大于10ms)
[root@ying01 ~]# vim /etc/redis.conf slowlog-log-slower-than 10
启动redis服务,开始作测试
[root@ying01 ~]# redis-server /etc/redis.conf [root@ying01 ~]# ps aux |grep redis root 1385 4.3 0.7 145304 7692 ? Ssl 23:36 0:00 redis-server 127.0.0.1:6379 root 1390 0.0 0.0 112720 984 pts/0 R+ 23:36 0:00 grep --color=auto redis [root@ying01 ~]# redis-cli -a 'www123' 127.0.0.1:6379> slowlog get //此时刚进入了,没有 1) 1) (integer) 0 2) (integer) 1535125047 3) (integer) 283 4) 1) "COMMAND" 5) "127.0.0.1:34834" 6) "" 127.0.0.1:6379> keys * //试着执行一个命令 1) "set2" 2) "score" 3) "k8" 4) "xxx" 5) "lista" 6) "k4" 7) "k3" 8) "hash2" 9) "hash1" 10) "list1" 11) "set1" 12) "mykey" 127.0.0.1:6379> get k3 //再执行第二个命令 "c" 127.0.0.1:6379> SLOWLOG get //此时列出所有查询日志 1) 1) (integer) 2 2) (integer) 1535125090 3) (integer) 30 4) 1) "keys" 2) "*" 5) "127.0.0.1:34834" 6) "" 2) 1) (integer) 1 2) (integer) 1535125066 3) (integer) 34 4) 1) "slowlog" 2) "get" 5) "127.0.0.1:34834" 6) "" 3) 1) (integer) 0 2) (integer) 1535125047 3) (integer) 283 4) 1) "COMMAND" 5) "127.0.0.1:34834" 6) "" 127.0.0.1:6379> SLOWLOG get 1 //得到当前的日志 1) 1) (integer) 3 2) (integer) 1535125141 3) (integer) 68 4) 1) "SLOWLOG" 2) "get" 5) "127.0.0.1:34834" 6) "" 127.0.0.1:6379> SLOWLOG get 2 //得到前二个日志 1) 1) (integer) 4 2) (integer) 1535125219 3) (integer) 15 4) 1) "SLOWLOG" 2) "get" 3) "1" 5) "127.0.0.1:34834" 6) "" 2) 1) (integer) 3 2) (integer) 1535125141 3) (integer) 68 4) 1) "SLOWLOG" 2) "get" 5) "127.0.0.1:34834" 6) "" 127.0.0.1:6379> SLOWLOG len //得到慢日志条数 (integer) 6 127.0.0.1:6379>
下载phpredis模块源码包,解压
[root@ying01]# cd /usr/local/src/ [root@ying01 src]# wget https://codeload.github.com/phpredis/phpredis/zip/develop [root@ying01 src]# mv develop phpredis-develop.zip [root@ying01 src]# unzip phpredis-develop.zip
编译,安装php的redis模块
[root@ying01 phpredis-develop]# cd phpredis-develop/ [root@ying01 phpredis-develop]# /usr/local/php-fpm/bin/phpize Configuring for: PHP Api Version: 20131106 Zend Module Api No: 20131226 Zend Extension Api No: 220131226 [root@ying01 phpredis-develop]# ./configure --with-php-config=/usr/local/php-fpm/bin/php-config [root@ying01 phpredis-develop]# make [root@ying01 phpredis-develop]# make install
在php.ini配置文件中添加 extension=redis.so
[root@ying01 phpredis-develop]# vim /usr/local/php-fpm/etc/php.ini extension=memcache.so //memcache模块以前添加的 extension=redis.so //添加
查看php-fpm模块中,是否有redis模块;
[root@ying01 phpredis-develop]# /usr/local/php-fpm/bin/php -m|grep redis redis [root@ying01 phpredis-develop]# /etc/init.d/php-fpm restart Gracefully shutting down php-fpm . done Starting php-fpm done
** redis存储session,有三种配置方法**:
一、/usr/local/php-fpm/etc/php.ini中添加:
session.save_handler = "redis"
session.save_path = "tcp://127.0.0.1:6379"
二、lamp中在Apache虚拟主机配置文件中添加:
php_value session.save_handler = "redis"
php_value session.save_path = "tcp://127.0.0.1:6379"
三、在php-fpm配置文件对应的pool:/usr/local/php-fpm/etc/php-fpm.d/ying.com.conf 中添加:
php_value[session.save_handler] = "redis"
php_value[session.save_path] = "tcp://127.0.0.1:6379"
把下面语句添加在 ying.com pool下
[root@ying01 ~]# vim /usr/local/php-fpm/etc/php-fpm.d/ying.com.conf 添加如下语句: php_value[session.save_handler] = redis php_value[session.save_path] = " tcp://127.0.0.1:6379 "
在redis配置文件中,取消密码,方便试验
[root@ying01 ~]# vim /etc/redis.conf #requirepass www123 //加上#,就表明此命令不执行
更改配置文件后,须要重启redis服务
[root@ying01 ~]# killall redis-server [root@ying01 ~]# redis-server /etc/redis.conf
测试中,能够看出来redis是能够存储ssion
[root@ying01 ~]# curl localhost/1.php 1535202678<br><br>1535202678<br><br>fb0tk0r5nnvgs1fhngten6kvf4 [root@ying01 ~]# curl localhost/1.php 1535202680<br><br>1535202680<br><br>d9u8oi1d0o1l2m6blm0gneb9p5 //注意 [root@ying01 ~]# curl localhost/1.php 1535202681<br><br>1535202681<br><br>ks2a20t9gikjbvhpevcrrts5r0 [root@ying01 ~]# curl localhost/1.php 1535202681<br><br>1535202681<br><br>26ff1ifkdi6s6vje10kl1ane36 [root@ying01 ~]# curl localhost/1.php 1535202682<br><br>1535202682<br><br>5setf9nc5ih8de5a8p6dda01r3 [root@ying01 ~]# curl localhost/1.php 1535202682<br><br>1535202682<br><br>phh916fl6s4fcl08agee13cqe6 [root@ying01 ~]# curl localhost/1.php 1535202683<br><br>1535202683<br><br>2467qf64g4j4rk4ac2o2ms4mm6 [root@ying01 ~]# curl localhost/1.php 1535202684<br><br>1535202684<br><br>cl3m6hpshf55unro62ccq4q5h6 [root@ying01 ~]# redis-cli 127.0.0.1:6379> keys * 1) "PHPREDIS_SESSION:d9u8oi1d0o1l2m6blm0gneb9p5" //注意 2) "score" 3) "PHPREDIS_SESSION:cl3m6hpshf55unro62ccq4q5h6" 4) "list1" 5) "k8" 6) "set1" 7) "PHPREDIS_SESSION:2467qf64g4j4rk4ac2o2ms4mm6" 8) "k3" 9) "PHPREDIS_SESSION:26ff1ifkdi6s6vje10kl1ane36" 10) "PHPREDIS_SESSION:phh916fl6s4fcl08agee13cqe6" 11) "mykey" 12) "PHPREDIS_SESSION:fb0tk0r5nnvgs1fhngten6kvf4" 13) "set2" 14) "hash1" 15) "lista" 16) "hash2" 17) "PHPREDIS_SESSION:ks2a20t9gikjbvhpevcrrts5r0" 18) "k4" 19) "PHPREDIS_SESSION:5setf9nc5ih8de5a8p6dda01r3" 20) "xxx" 127.0.0.1:6379> get PHPREDIS_SESSION:d9u8oi1d0o1l2m6blm0gneb9p5 //注意 "TEST|i:1535202680;TEST3|i:1535202680;" 127.0.0.1:6379> get PHPREDIS_SESSION:5setf9nc5ih8de5a8p6dda01r3 "TEST|i:1535202682;TEST3|i:1535202682;" 127.0.0.1:6379>
在master(ying01)上修改redis的配置文件,master上改动不大
[root@ying01 ~]# vim /etc/redis.conf bind 192.168.112.136 127.0.0.1 port 6379 pidfile /var/run/redis_6379.pid appendonly yes
在slave(ying02)上修改redis的配置文件
[root@ying02 ~]# vim /etc/redis.conf bind 127.0.0.1 192.168.112.138 port 6380 pidfile /var/run/redis_6380.pid slaveof 192.168.112.136 6379 appendonly yes
分别在master、slave机器上重启redis服务
[root@ying01 ~]# killall redis-server [root@ying01 ~]# redis-server /etc/redis.conf
[root@ying02 ~]# killall redis-server [root@ying02 ~]# redis-server /etc/redis.conf
分别在master、slave机器上登陆redis,使用命令"info Replication"
[root@ying01 ~]# redis-cli -p 6379 127.0.0.1:6379> info Replication # Replication role:master connected_slaves:1 slave0:ip=192.168.112.138,port=6380,state=online,offset=14,lag=1 master_replid:87fc3ee31a10a84b042f74e896e060d3acbd9de2 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:14 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:14 127.0.0.1:6379>
slave上的信息
[root@ying02 ~]# redis-cli -p 6380 127.0.0.1:6380> info Replication # Replication role:slave master_host:192.168.112.136 master_port:6379 master_link_status:up master_last_io_seconds_ago:5 master_sync_in_progress:0 slave_repl_offset:28 slave_priority:100 slave_read_only:1 connected_slaves:0 master_replid:87fc3ee31a10a84b042f74e896e060d3acbd9de2 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:28 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:28 127.0.0.1:6380>
上面的信息代表:主从启动成功
先看redis的集群架构图
在这个图中,每个蓝色的圈都表明着一个redis的服务器节点。它们任何两个节点之间都是相互连通的。客户端能够与任何一个节点相链接,而后就能够访问集群中的任何一个节点。对其进行存取和其余操做。
redis集群特色:
- 多个redis节点网络互联,数据共享
- 全部的节点都是一主一从(能够是多个从),其中从不提供服务,仅做为备用
- 不支持同时处理多个键(如mset/mget),由于redis须要把键均匀分布在各个节点上,并发量很高的状况下同时建立键值会下降性能并致使不可预测的行为。
- 支持在线增长、删除节点
- 客户端能够连任何一个主节点进行读写
参考资料:
ying01机器上
新建redis_7000.conf配置文件
[root@ying01 ~]# cd /etc/ [root@ying01 etc]# vim redis_7000.conf port 7000 bind 192.168.112.136 daemonize yes pidfile /var/run/redis_7000.pid dir /data/redis_data/7000 cluster-enabled yes cluster-config-file nodes_7000.conf cluster-node-timeout 10100 appendonly yes
新建redis_7002.conf配置文件
[root@ying01 etc]# vim redis_7002.conf port 7002 bind 192.168.112.136 daemonize yes pidfile /var/run/redis_7002.pid dir /data/redis_data/7002 cluster-enabled yes cluster-config-file nodes_7002.conf cluster-node-timeout 10100 appendonly yes
新建redis_7004.conf配置文件
[root@ying01 etc]# vim redis_7004.conf port 7004 bind 192.168.112.136 daemonize yes pidfile /var/run/redis_7004.pid dir /data/redis_data/7004 cluster-enabled yes cluster-config-file nodes_7004.conf cluster-node-timeout 10100 appendonly yes
建立目录
[root@ying01 etc]# mkdir /data/redis_data [root@ying01 etc]# mkdir /data/redis_data/{7000,7002,7004}
分别开启7000、700二、7004的redis服务
[root@ying01 etc]# redis-server /etc/redis_7000.conf 13835:C 27 Aug 00:20:59.978 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 13835:C 27 Aug 00:20:59.978 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=13835, just started 13835:C 27 Aug 00:20:59.978 # Configuration loaded [root@ying01 etc]# redis-server /etc/redis_7002.conf 13840:C 27 Aug 00:21:08.133 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 13840:C 27 Aug 00:21:08.133 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=13840, just started 13840:C 27 Aug 00:21:08.133 # Configuration loaded [root@ying01 etc]# redis-server /etc/redis_7004.conf 13845:C 27 Aug 00:21:13.131 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 13845:C 27 Aug 00:21:13.131 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=13845, just started 13845:C 27 Aug 00:21:13.131 # Configuration loaded [root@ying01 etc]# netstat -lntp |grep redis tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 1741/redis-server 1 tcp 0 0 192.168.112.136:6379 0.0.0.0:* LISTEN 1741/redis-server 1 tcp 0 0 192.168.112.136:17004 0.0.0.0:* LISTEN 13846/redis-server tcp 0 0 192.168.112.136:7000 0.0.0.0:* LISTEN 13836/redis-server tcp 0 0 192.168.112.136:7002 0.0.0.0:* LISTEN 13841/redis-server tcp 0 0 192.168.112.136:7004 0.0.0.0:* LISTEN 13846/redis-server tcp 0 0 192.168.112.136:17000 0.0.0.0:* LISTEN 13836/redis-server tcp 0 0 192.168.112.136:17002 0.0.0.0:* LISTEN 13841/redis-server [root@ying01 etc]# ps aux |grep redis root 1741 0.1 0.5 147352 9840 ? Ssl 8月26 0:05 redis-server 192.168.112.136:6379 root 13836 0.0 0.4 145308 7580 ? Ssl 00:20 0:00 redis-server 192.168.112.136:7000 [cluster] root 13841 0.1 0.4 145308 7576 ? Ssl 00:21 0:00 redis-server 192.168.112.136:7002 [cluster] root 13846 0.0 0.4 145308 7576 ? Ssl 00:21 0:00 redis-server 192.168.112.136:7004 [cluster] root 13853 0.0 0.0 112720 984 pts/0 S+ 00:21 0:00 grep --color=auto redis
ying02 机器上
新建redis_7001.conf配置文件
[root@ying02 etc]# vim redis_7001.conf port 7001 bind 192.168.112.138 daemonize yes pidfile /var/run/redis_7001.pid dir /data/redis_data/7001 cluster-enabled yes cluster-config-file nodes_7001.conf cluster-node-timeout 10100 appendonly yes
新建redis_7003.conf配置文件
[root@ying02 etc]# vim redis_7003.conf port 7003 bind 192.168.112.138 daemonize yes pidfile /var/run/redis_7003.pid dir /data/redis_data/7003 cluster-enabled yes cluster-config-file nodes_7003.conf cluster-node-timeout 10100 appendonly yes
新建redis_7005.conf配置文件
[root@ying02 etc]# vim redis_7005.conf port 7005 bind 192.168.112.138 daemonize yes pidfile /var/run/redis_7005.pid dir /data/redis_data/7005 cluster-enabled yes cluster-config-file nodes_7005.conf cluster-node-timeout 10100 appendonly yes
建立目录
[root@ying02 etc]# mkdir /data/redis_data [root@ying02 etc]# mkdir /data/redis_data/{7001,7003,7005}
分别开启700一、700三、7005的redis服务
[root@ying02 etc]# redis-server /etc/redis_7001.conf 4535:C 27 Aug 00:22:55.023 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 4535:C 27 Aug 00:22:55.023 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=4535, just started 4535:C 27 Aug 00:22:55.023 # Configuration loaded [root@ying02 etc]# redis-server /etc/redis_7003.conf 4544:C 27 Aug 00:22:59.103 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 4544:C 27 Aug 00:22:59.103 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=4544, just started 4544:C 27 Aug 00:22:59.103 # Configuration loaded [root@ying02 etc]# redis-server /etc/redis_7005.conf 4552:C 27 Aug 00:23:03.193 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 4552:C 27 Aug 00:23:03.193 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=4552, just started 4552:C 27 Aug 00:23:03.193 # Configuration loaded [root@ying02 etc]# netstat -lntp |grep redis tcp 0 0 192.168.112.138:17003 0.0.0.0:* LISTEN 4545/redis-server 1 tcp 0 0 192.168.112.138:6380 0.0.0.0:* LISTEN 2974/redis-server 1 tcp 0 0 127.0.0.1:6380 0.0.0.0:* LISTEN 2974/redis-server 1 tcp 0 0 192.168.112.138:17005 0.0.0.0:* LISTEN 4553/redis-server 1 tcp 0 0 192.168.112.138:7001 0.0.0.0:* LISTEN 4536/redis-server 1 tcp 0 0 192.168.112.138:7003 0.0.0.0:* LISTEN 4545/redis-server 1 tcp 0 0 192.168.112.138:7005 0.0.0.0:* LISTEN 4553/redis-server 1 tcp 0 0 192.168.112.138:17001 0.0.0.0:* LISTEN 4536/redis-server 1 [root@ying02 etc]# ps aux |grep redis root 2974 0.1 0.1 147352 2516 ? Ssl 8月26 0:05 redis-server 127.0.0.1:6380 root 4536 0.0 0.1 145308 2648 ? Ssl 00:22 0:00 redis-server 192.168.112.138:7001 [cluster] root 4545 0.1 0.1 145308 2652 ? Ssl 00:22 0:00 redis-server 192.168.112.138:7003 [cluster] root 4553 0.0 0.1 145308 2648 ? Ssl 00:23 0:00 redis-server 192.168.112.138:7005 [cluster] root 4565 0.0 0.0 112720 984 pts/0 R+ 00:23 0:00 grep --color=auto redis
在ying01机器上安装ruby2.2(只须要在ying01上安装)
[root@ying01 ~]# yum -y groupinstall "Development Tools" [root@ying01 ~]# yum -y install gdbm-devel libdb4-devel libffi-devel libyaml libyaml-devel ncurses-devel openssl-devel readline-devel tcl-deve
建立rpmbuild目录及其子目录
[root@ying01 ~]# mkdir -p rpmbuild/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS} [root@ying01 ~]# ls rpmbuild/ BUILD BUILDROOT RPMS SOURCES SPECS SRPMS
下载ruby的源码包,存放在rpmbuild/SOURCES目录下
[root@ying01 ~]# wget http://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.3.tar.gz -P rpmbuild/SOURCES
下载ruby的spec的文件,目的rpmbuild把其生成rpm文件
[root@ying01 ~]# wget https://raw.githubusercontent.com/tjinjin/automate-ruby-rpm/master/ruby22x.spec -P rpmbuild/SPECS [root@ying01 ~]# rpmbuild -bb rpmbuild/SPECS/ruby22x.spec
安装 ruby-2.2.3-1.el7.centos.x86_64.rpm ,并查看其版本号
[root@ying01 ~]# ls rpmbuild/RPMS/x86_64/ruby-2.2.3-1.el7.centos.x86_64.rpm rpmbuild/RPMS/x86_64/ruby-2.2.3-1.el7.centos.x86_64.rpm [root@ying01 ~]# du -sh !$ du -sh rpmbuild/RPMS/x86_64/ruby-2.2.3-1.el7.centos.x86_64.rpm 10M rpmbuild/RPMS/x86_64/ruby-2.2.3-1.el7.centos.x86_64.rpm [root@ying01 ~]# rpm -ivh rpmbuild/RPMS/x86_64/ruby-2.2.3-1.el7.centos.x86_64.rpm 准备中... ################################# [100%] 正在升级/安装... 1:ruby-2.2.3-1.el7.centos ################################# [100%] [root@ying01 ~]# ruby -v //查看版本号 ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-linux]
用gem安与redis中与ruby相关的文件 redis-trib.rb
[root@ying01 ~]# 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 1 seconds 1 gem installed
如今把redis-trib.rb 复制到/usr/bin/下,能够直接使用这个命令
[root@ying01 ~]# cp /usr/local/src/redis-4.0.9/src/redis-trib.rb /usr/bin/
用redis-trib.rb 命令分配主、从端口
[root@ying01 ~]# redis-trib.rb create --replicas 1 192.168.112.136:7000 192.168.112.136:7002 192.168.112.136:7004 192.168.112.138:7001 192.168.112.138:7003 192.168.112.138:7005 >>> Creating cluster >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 192.168.112.136:7000 192.168.112.138:7001 192.168.112.136:7002 Adding replica 192.168.112.138:7005 to 192.168.112.136:7000 Adding replica 192.168.112.136:7004 to 192.168.112.138:7001 Adding replica 192.168.112.138:7003 to 192.168.112.136:7002 M: 25cff1f2c0d7e296b8af969ac8c1de535e8ddf6c 192.168.112.136:7000 slots:0-5460 (5461 slots) master M: f2690645620ad4c4042536108e576158dec751b3 192.168.112.136:7002 slots:10923-16383 (5461 slots) master S: 022afc487ced1c543ee85c4f9ba591b377d5e9e1 192.168.112.136:7004 replicates 0ff8984070960a56f204d992d176ca7275e06c1d M: 0ff8984070960a56f204d992d176ca7275e06c1d 192.168.112.138:7001 slots:5461-10922 (5462 slots) master S: 578367da301f9d795854ce2ee3daf56d5971e7de 192.168.112.138:7003 replicates f2690645620ad4c4042536108e576158dec751b3 S: 0e107e02f89ef282470174bfa6bcf6cdfebc8271 192.168.112.138:7005 replicates 25cff1f2c0d7e296b8af969ac8c1de535e8ddf6c Can I set the above configuration? (type 'yes' to accept): yes //满意就 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.112.136:7000) M: 25cff1f2c0d7e296b8af969ac8c1de535e8ddf6c 192.168.112.136:7000 slots:0-5460 (5461 slots) master 1 additional replica(s) S: 578367da301f9d795854ce2ee3daf56d5971e7de 192.168.112.138:7003 slots: (0 slots) slave replicates f2690645620ad4c4042536108e576158dec751b3 M: 0ff8984070960a56f204d992d176ca7275e06c1d 192.168.112.138:7001 slots:5461-10922 (5462 slots) master 1 additional replica(s) S: 0e107e02f89ef282470174bfa6bcf6cdfebc8271 192.168.112.138:7005 slots: (0 slots) slave replicates 25cff1f2c0d7e296b8af969ac8c1de535e8ddf6c S: 022afc487ced1c543ee85c4f9ba591b377d5e9e1 192.168.112.136:7004 slots: (0 slots) slave replicates 0ff8984070960a56f204d992d176ca7275e06c1d M: f2690645620ad4c4042536108e576158dec751b3 192.168.112.136:7002 slots:10923-16383 (5461 slots) master 1 additional replica(s) [OK] All nodes agree about slots configuration. //成功的标志 >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. //成功的标志 [root@ying01 ~]# echo $? 0
开始以集群的方式登陆,建立不一样的值,观察存在哪一个服务器(端口)
[root@ying01 ~]# redis-cli -c -h 192.168.112.136 -p 7000 //-c 表示以集群的方式登陆,不然就是单机登陆 192.168.112.136:7000> set key1 aaa -> Redirected to slot [9189] located at 192.168.112.138:7001 OK 192.168.112.138:7001> set key2 bbb -> Redirected to slot [4998] located at 192.168.112.136:7000 OK 192.168.112.136:7000> set key3 ccc OK 192.168.112.136:7000> set key4 ddd -> Redirected to slot [13120] located at 192.168.112.136:7002 OK 192.168.112.136:7002> set key5 eee -> Redirected to slot [9057] located at 192.168.112.138:7001 OK 192.168.112.138:7001> get key3 -> Redirected to slot [935] located at 192.168.112.136:7000 "ccc" 192.168.112.136:7000> get key4 -> Redirected to slot [13120] located at 192.168.112.136:7002 "ddd" 192.168.112.136:7002>
检测集群状态 redis-trib.rb IP:port
[root@ying01 ~]# redis-trib.rb check 192.168.112.136:7000 //能够跟任何端口 >>> Performing Cluster Check (using node 192.168.112.136:7000) M: 25cff1f2c0d7e296b8af969ac8c1de535e8ddf6c 192.168.112.136:7000 slots:0-5460 (5461 slots) master 1 additional replica(s) S: 578367da301f9d795854ce2ee3daf56d5971e7de 192.168.112.138:7003 slots: (0 slots) slave replicates f2690645620ad4c4042536108e576158dec751b3 M: 0ff8984070960a56f204d992d176ca7275e06c1d 192.168.112.138:7001 slots:5461-10922 (5462 slots) master 1 additional replica(s) S: 0e107e02f89ef282470174bfa6bcf6cdfebc8271 192.168.112.138:7005 slots: (0 slots) slave replicates 25cff1f2c0d7e296b8af969ac8c1de535e8ddf6c S: 022afc487ced1c543ee85c4f9ba591b377d5e9e1 192.168.112.136:7004 slots: (0 slots) slave replicates 0ff8984070960a56f204d992d176ca7275e06c1d M: f2690645620ad4c4042536108e576158dec751b3 192.168.112.136:7002 slots:10923-16383 (5461 slots) master 1 additional replica(s) [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
以集群的方式登陆到redis下,一些重用操做:
192.168.112.136:7000> CLUSTER NODES 578367da301f9d795854ce2ee3daf56d5971e7de 192.168.112.138:7003@17003 slave f2690645620ad4c4042536108e576158dec751b3 0 1535304518521 5 connected 0ff8984070960a56f204d992d176ca7275e06c1d 192.168.112.138:7001@17001 master - 0 1535304516000 4 connected 5461-10922 25cff1f2c0d7e296b8af969ac8c1de535e8ddf6c 192.168.112.136:7000@17000 myself,master - 0 1535304516000 1 connected 0-5460 0e107e02f89ef282470174bfa6bcf6cdfebc8271 192.168.112.138:7005@17005 slave 25cff1f2c0d7e296b8af969ac8c1de535e8ddf6c 0 1535304518000 6 connected 022afc487ced1c543ee85c4f9ba591b377d5e9e1 192.168.112.136:7004@17004 slave 0ff8984070960a56f204d992d176ca7275e06c1d 0 1535304517513 4 connected f2690645620ad4c4042536108e576158dec751b3 192.168.112.136:7002@17002 master - 0 1535304519527 2 connected 10923-16383
192.168.112.136: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:1135 cluster_stats_messages_pong_sent:1094 cluster_stats_messages_sent:2229 cluster_stats_messages_ping_received:1089 cluster_stats_messages_pong_received:1135 cluster_stats_messages_meet_received:5 cluster_stats_messages_received:2229
在ying02机器上,新建一个7007的配置文件
[root@ying02 etc]# vim redis_7007.conf port 7007 bind 192.168.133.132 daemonize yes pidfile /var/run/redis_7007.pid dir /data/redis_data/7007 cluster-enabled yes cluster-config-file nodes_7007.conf cluster-node-timeout 10100 appendonly yes
建立目录,并启动
[root@ying02 etc]# mkdir /data/redis_data/7007 [root@ying02 etc]# redis-server /etc/redis_7007.conf 6060:C 27 Aug 01:37:20.129 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 6060:C 27 Aug 01:37:20.129 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=6060, just started 6060:C 27 Aug 01:37:20.129 # Configuration loaded [root@ying02 etc]# ps aux |grep redis root 2974 0.1 0.1 147352 2516 ? Ssl 8月26 0:09 redis-server 127.0.0.1:6380 root 4536 0.0 0.1 147356 3184 ? Ssl 00:22 0:04 redis-server 192.168.112.138:7001 [cluster] root 4545 0.0 0.1 147356 3068 ? Ssl 00:22 0:04 redis-server 192.168.112.138:7003 [cluster] root 4553 0.0 0.1 147356 3064 ? Ssl 00:23 0:04 redis-server 192.168.112.138:7005 [cluster] root 6061 0.2 0.1 145308 2648 ? Ssl 01:37 0:00 redis-server 192.168.112.138:7007 [cluster] root 6071 0.0 0.0 112720 984 pts/0 S+ 01:37 0:00 grep --color=auto redis
再回到ying01上,添加这个节点
192.168.112.136:7000> CLUSTER MEET 192.168.112.138 7007 OK 192.168.112.136:7000> CLUSTER NODES //发现7007为master 7bacdb18381ac0101abad973180a890cd2da2d39 192.168.112.138:7007@17007 master - 0 1535305180270 0 connected 578367da301f9d795854ce2ee3daf56d5971e7de 192.168.112.138:7003@17003 slave f2690645620ad4c4042536108e576158dec751b3 0 1535305182000 5 connected 0ff8984070960a56f204d992d176ca7275e06c1d 192.168.112.138:7001@17001 master - 0 1535305179263 4 connected 5461-10922 25cff1f2c0d7e296b8af969ac8c1de535e8ddf6c 192.168.112.136:7000@17000 myself,master - 0 1535305179000 1 connected 0-5460 0e107e02f89ef282470174bfa6bcf6cdfebc8271 192.168.112.138:7005@17005 slave 25cff1f2c0d7e296b8af969ac8c1de535e8ddf6c 0 1535305183291 6 connected 022afc487ced1c543ee85c4f9ba591b377d5e9e1 192.168.112.136:7004@17004 slave 0ff8984070960a56f204d992d176ca7275e06c1d 0 1535305182285 4 connected f2690645620ad4c4042536108e576158dec751b3 192.168.112.136:7002@17002 master - 0 1535305182000 2 connected 10923-16383 192.168.112.136:7000>
在ying01上新建7006配置文件
[root@ying01 etc]# vim redis_7006.conf port 7006 bind 192.168.112.136 daemonize yes pidfile /var/run/redis_7006.pid dir /data/redis_data/7006 cluster-enabled yes cluster-config-file nodes_7006.conf cluster-node-timeout 10100 appendonly yes
建立相应的数据目录,并开启7006的redis服务
[root@ying01 etc]# mkdir /data/redis_data/7006 [root@ying01 etc]# redis-server /etc/redis_7006.conf 29220:C 27 Aug 01:52:30.488 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 29220:C 27 Aug 01:52:30.488 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=29220, just started 29220:C 27 Aug 01:52:30.488 # Configuration loaded
再次登陆到集群下,增长7006的节点,此时7006也为master
[root@ying01 etc]# redis-cli -c -h 192.168.112.136 -p 7000 192.168.112.136:7000> CLUSTER MEET 192.168.112.136 7006 OK 192.168.112.136:7000> CLUSTER NODES 7bacdb18381ac0101abad973180a890cd2da2d39 192.168.112.138:7007@17007 master - 0 1535306149035 0 connected 578367da301f9d795854ce2ee3daf56d5971e7de 192.168.112.138:7003@17003 slave f2690645620ad4c4042536108e576158dec751b3 0 1535306148026 5 connected 0ff8984070960a56f204d992d176ca7275e06c1d 192.168.112.138:7001@17001 master - 0 1535306147000 4 connected 5461-10922 25cff1f2c0d7e296b8af969ac8c1de535e8ddf6c 192.168.112.136:7000@17000 myself,master - 0 1535306142000 1 connected 0-5460 0e107e02f89ef282470174bfa6bcf6cdfebc8271 192.168.112.138:7005@17005 slave 25cff1f2c0d7e296b8af969ac8c1de535e8ddf6c 0 1535306147000 6 connected 022afc487ced1c543ee85c4f9ba591b377d5e9e1 192.168.112.136:7004@17004 slave 0ff8984070960a56f204d992d176ca7275e06c1d 0 1535306146000 4 connected 288a9bcba1d561ae4bdb016794b46b098c52ebb5 192.168.112.136:7006@17006 master - 0 1535306147000 0 connected f2690645620ad4c4042536108e576158dec751b3 192.168.112.136:7002@17002 master - 0 1535306147018 2 connected 10923-16383 192.168.112.136:7000>
[root@ying01 etc]# redis-cli -c -h 192.168.112.136 -p 7006 //以7006端口登陆到集群 192.168.112.136:7006> CLUSTER REPLICATE 7bacdb18381ac0101abad973180a890cd2da2d39 //把7006设置7007的从,此id是7007的 OK 192.168.112.136:7006> CLUSTER NODES 25cff1f2c0d7e296b8af969ac8c1de535e8ddf6c 192.168.112.136:7000@17000 master - 0 1535306375000 1 connected 0-5460 0e107e02f89ef282470174bfa6bcf6cdfebc8271 192.168.112.138:7005@17005 slave 25cff1f2c0d7e296b8af969ac8c1de535e8ddf6c 0 1535306374000 1 connected 578367da301f9d795854ce2ee3daf56d5971e7de 192.168.112.138:7003@17003 slave f2690645620ad4c4042536108e576158dec751b3 0 1535306376198 2 connected f2690645620ad4c4042536108e576158dec751b3 192.168.112.136:7002@17002 master - 0 1535306377505 2 connected 10923-16383 288a9bcba1d561ae4bdb016794b46b098c52ebb5 192.168.112.136:7006@17006 myself,slave 7bacdb18381ac0101abad973180a890cd2da2d39 0 1535306372000 7 connected 0ff8984070960a56f204d992d176ca7275e06c1d 192.168.112.138:7001@17001 master - 0 1535306376500 4 connected 5461-10922 7bacdb18381ac0101abad973180a890cd2da2d39 192.168.112.138:7007@17007 master - 0 1535306374484 0 connected 022afc487ced1c543ee85c4f9ba591b377d5e9e1 192.168.112.136:7004@17004 slave 0ff8984070960a56f204d992d176ca7275e06c1d 0 1535306375492 4 connected 192.168.112.136:7006>
不能移走master,也不能移走当前节点的
192.168.112.136:7006> CLUSTER FORGET 288a9bcba1d561ae4bdb016794b46b098c52ebb5 //不可以在7006下移走7006 (error) ERR I tried hard but I can't forget myself... 192.168.112.136:7006> [root@ying01 etc]# redis-cli -c -h 192.168.112.136 -p 7000 //换一个节点登陆集群 192.168.112.136:7000> CLUSTER FORGET 288a9bcba1d561ae4bdb016794b46b098c52ebb5 OK 192.168.112.136:7000> CLUSTER NODES //此时就能移动7006 7bacdb18381ac0101abad973180a890cd2da2d39 192.168.112.138:7007@17007 master - 0 1535306852959 0 connected 578367da301f9d795854ce2ee3daf56d5971e7de 192.168.112.138:7003@17003 slave f2690645620ad4c4042536108e576158dec751b3 0 1535306851954 5 connected 0ff8984070960a56f204d992d176ca7275e06c1d 192.168.112.138:7001@17001 master - 0 1535306851000 4 connected 5461-10922 25cff1f2c0d7e296b8af969ac8c1de535e8ddf6c 192.168.112.136:7000@17000 myself,master - 0 1535306847000 1 connected 0-5460 0e107e02f89ef282470174bfa6bcf6cdfebc8271 192.168.112.138:7005@17005 slave 25cff1f2c0d7e296b8af969ac8c1de535e8ddf6c 0 1535306850000 6 connected 022afc487ced1c543ee85c4f9ba591b377d5e9e1 192.168.112.136:7004@17004 slave 0ff8984070960a56f204d992d176ca7275e06c1d 0 1535306850000 4 connected f2690645620ad4c4042536108e576158dec751b3 192.168.112.136:7002@17002 master - 0 1535306850947 2 connected 10923-16383 192.168.112.136:7000>
192.168.112.136:7000> CLUSTER SAVECONFIG OK 192.168.112.136:7000>
在ying02机器上,在端口下的nodes_700x的文件下,都保存了以前的设置
[root@ying02 etc]# vim /data/redis_data/7003/nodes_7003.conf 022afc487ced1c543ee85c4f9ba591b377d5e9e1 192.168.112.136:7004@17004 slave 0ff8984070960a56f204d992d176ca7275e06c1d 0 1535306360718 4 connected 7bacdb18381ac0101abad973180a890cd2da2d39 192.168.112.138:7007@17007 master - 0 1535306359000 0 connected 0e107e02f89ef282470174bfa6bcf6cdfebc8271 192.168.112.138:7005@17005 slave 25cff1f2c0d7e296b8af969ac8c1de535e8ddf6c 0 1535306359710 6 connected 578367da301f9d795854ce2ee3daf56d5971e7de 192.168.112.138:7003@17003 myself,slave f2690645620ad4c4042536108e576158dec751b3 0 1535306358000 5 connected 288a9bcba1d561ae4bdb016794b46b098c52ebb5 192.168.112.136:7006@17006 slave 7bacdb18381ac0101abad973180a890cd2da2d39 0 1535306361121 7 connected f2690645620ad4c4042536108e576158dec751b3 192.168.112.136:7002@17002 master - 0 1535306361120 2 connected 10923-16383 25cff1f2c0d7e296b8af969ac8c1de535e8ddf6c 192.168.112.136:7000@17000 master - 0 1535306358100 1 connected 0-5460 0ff8984070960a56f204d992d176ca7275e06c1d 192.168.112.138:7001@17001 master - 0 1535306360000 4 connected 5461-10922 vars currentEpoch 7 lastVoteEpoch 0