概述
21.9 redis介绍
1.Redis和Memcached类似,也属于k-v数据存储
2.Redis官网redis.io, 当前最新稳定版4.0.1
3.支持更多value类型,除了和string外,还支持hash、lists(链表)、sets(集合)和sorted sets(有序集合)
4.redis使用了两种文件格式:全量数据(RDB)和增量请求(aof)。全量数据格式是把内存中的数据写入磁盘,便于下次读取文件进行加载。增量请求文件则是把内存中的数据序列化为操作请求,用于读取文件进行replay得到数据,这种类似于mysql binlog。
5.redis的存储分为内存存储、磁盘存储和log文件三部分
21.10 redis安装
首先进入到src目录下面去
[root@liuhongwei-01 ~]# cd /usr/local/src
[root@liuhongwei-01 src]#
下载redis源码包
[root@liuhongwei-01 src]# wget http://download.redis.io/releases/redis-4.0.1.tar.gz
--2018-08-24 08:53:01-- http://download.redis.io/releases/redis-4.0.1.tar.gz
正在解析主机 download.redis.io (download.redis.io)...
解压包
[root@liuhongwei-01 src]# tar zxvf redis-4.0.1.tar.gz
redis-4.0.1/
进入到redis里面去,开始make编译
[root@liuhongwei-01 src]# cd redis-4.0.1
[root@liuhongwei-01 redis-4.0.1]# make
复制一下redis的配置文件
[root@liuhongwei-01 redis-4.0.1]# cp redis.conf /etc/redis.conf
修改一下配置文件:修改几个参数
[root@liuhongwei-01 redis-4.0.1]# vim /etc/redis.conf
# Redis configuration file example.
创建/data/redis
[root@liuhongwei-01 redis-4.0.1]# mkdir /data/redis
启动redis
[root@liuhongwei-01 redis-4.0.1]# redis-server /etc/redis.conf
[root@liuhongwei-01 redis-4.0.1]# ps aux|grep redis
root 5883 2.2 0.7 145240 7536 ? Ssl 09:09 0:00 redis-server 127.0.0.1:6379
root 5888 0.0 0.0 112664 964 pts/0 R+ 09:09 0:00 grep --color=auto redis
在etc/rc.local目录下添加二行配置
[root@liuhongwei-01 redis-4.0.1]# vim /etc/rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
sysctl vm.overcommit_memory=1
echo never > /sys/kernel/mm/transparent_hugepage/enabled
21.11 redis持久化
• Redis提供了两种持久化的方式,分别是RDB(Redis DataBase)和AOF(Append Only File)
• RDB,简而言之,就是在不同的时间点,将redis存储的数据生成快照并存储到磁盘等介质上。
• AOF,则是换了一个角度来实现持久化,那就是将redis执行过的所有写指令记录下来,在下次redis重新启动时,只要把这些写指令从前到后再重复执行一遍,就可以实现数据恢复了。
• 其实RDB和AOF两种方式也可以同时使用,在这种情况下,如果redis重启的话,则会优先采用AOF方式来进行数据恢复,这是因为AOF方式的数据恢复完整度更高。
• 如果你没有数据持久化的需求,也完全可以关闭RDB和AOF方式,这样的话,redis将变成一个纯内存数据库,就像memcache一样。
看一下配置文件
save 900 1 #表示每15分钟且至少有1个key改变,就触发一次持久化
save 300 10 #表示每5分钟且至少有10个key改变,就触发一次持久化
save 60 10000 #表示每60秒至少有10000个key改变,就触发一次持久
save “” #这样可以禁用rdb持久化
appendonly yes #如果是yes,则开启aof持久化
appendfilename “appendonly.aof” # 指定aof文件名字
appendfsync everysec #指定fsync()调用模式,有三种no(不调用fsync),always(每次写都会调用fsync),everysec(每秒钟调用一次fsync)。第一种最快,第二种数据最安全,但性能会差一些,第三种为这种方案,默认为第三种。
21.12 redis数据类型
Redis数据类型-string • string为最简单的类型,与Memcached一样的类型,一个key对应一个value,其支持的操作与Memcached的操作类似,它的功能更丰富。设置可以存二进制的对象。
示例:
链接redis命令 redis-cli。redis是可以设置密码的,但是没有设置密码之前直接输入redis-cli是可以直接进入的
[root@liuhongwei-01 redis-4.0.1]# redis-cli
127.0.0.1:6379>
127.0.0.1:6379> set mykey "aminglinux.com"
OK
127.0.0.1:6379> get mykey
"aminglinux.com"
127.0.0.1:6379> mset key1 1 key2 a key3 c
OK
127.0.0.1:6379> mget key1 key2 key3
1) "1"
2) "a"
3) "c"
Redis数据类型-list •list是一个链表结构,主要功能是push、pop、获取一个范围的所有值等等。操作中key理解为链表的名字。 • 使用 list 结构,我们可以轻松地实现最新消息排行等功能(比如新浪微博的 TimeLine )。list 的另一个应用就是消息队列,可以利用 list 的 push操作,将任务存在 list 中,然后工作线程再用pop操作将任务取出进行执行。
示例:
127.0.0.1:6379> LPUSH list1 "aminglinux"
(integer) 5
127.0.0.1:6379> LPUSH list1 "1 2 3"
(integer) 6
127.0.0.1:6379> LPUSH list1 aaa
(integer) 8
127.0.0.1:6379> LRANGE list1 0 -1
1) "x80x9c"
2) "aaa"
3) "1 2 3"
4) "aminglinux"
5) "bbbx80x9c"
6) "aaa"
7) "1 2 3"
8) "aminglinux"
127.0.0.1:6379>
127.0.0.1:6379> LPOP list1
"x80x9c"
127.0.0.1:6379> LRANGE list1 0 -1
1) "aaa"
2) "1 2 3"
3) "aminglinux"
4) "bbbx80x9c"
5) "aaa"
6) "1 2 3"
7) "aminglinux"
127.0.0.1:6379>
Redis数据类型-set • set是集合,和我们数学中的集合概念相似,对集合的操作有添加删除元素,有对多个集合求交并差等操作。操作中key理解为集合的名字。比如在微博应用中,可以将一个用户所有的关注人存在一个集合中,将其所有粉丝存在一个集合。因为 Redis 非常人性化的为集合提供了求交集、并集、差集等操作,那么就可以非常方便的实现如共同关注、共同喜好、二度好友等功能,对上面的所有集合操作,你还可以使用不同的命令选择将结果返回给客户端还是存集到一个新的集合中。
示例:
127.0.0.1:6379> SADD set1 a
(integer) 1
127.0.0.1:6379> SADD set1 b
(integer) 1
127.0.0.1:6379> SADD set1 c
(integer) 1
127.0.0.1:6379> SADD set1 d
(integer) 1
127.0.0.1:6379> SMEMBERS set1
1) "c"
2) "b"
3) "d"
4) "a"
127.0.0.1:6379> SADD set2 2
(integer) 1
127.0.0.1:6379> SADD set2 a
(integer) 1
127.0.0.1:6379> SADD set2 3
(integer) 1
127.0.0.1:6379> SADD set2 c
(integer) 1
127.0.0.1:6379> SMEMBERS set2
1) "c"
2) "3"
3) "2"
4) "a"
求集合的并集:
127.0.0.1:6379> SUNION set1 set2
1) "b"
2) "d"
3) "2"
4) "3"
5) "a"
6) "c"
127.0.0.1:6379>
求集合的交集:
127.0.0.1:6379> SINTER set1 set2
1) "c"
2) "a"
求集合的差集:
127.0.0.1:6379> SDIFF set1 set2
1) "b"
2) "d"
删除元素:
127.0.0.1:6379> SREM set1 a
(integer) 1
127.0.0.1:6379> SMEMBERS set1
1) "c"
2) "b"
3) "d"
127.0.0.1:6379> SREM set2 2
(integer) 1
127.0.0.1:6379> SMEMBERS set2
1) "c"
2) "3"
3) "a"
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 qqq
(integer) 1
127.0.0.1:6379> ZADD set3 22 "123 qwe"
(integer) 1
127.0.0.1:6379> ZADD set3 4 "aming"
(integer) 1
127.0.0.1:6379> ZRANGE set3 0 -1
1) "qqq"
2) "aming"
3) "abc"
4) "123 qwe"
这是有序排列,按照分数的大小关系去排列,从小到大排列
倒序排列:
127.0.0.1:6379> ZREVRANGE set3 0 -1
1) "123 qwe"
2) "abc"
3) "aming"
4) "qqq"
Redis数据类型-hash 在 Memcached 中,我们经常将一些结构化的信息打包成 hashmap,在客户端序列化后存储为一个字符串的值(一般是 JSON 格式),比如用户的昵称、年龄、性别、积分等
示例:
127.0.0.1:6379> HSET hash1 name hongwei
(integer) 1
127.0.0.1:6379> HSET hash1 age 18
(integer) 1
127.0.0.1:6379> HSET hash1 jobs it
(integer) 1
127.0.0.1:6379> HGETall hash1
1) "name"
2) "hongwei"
3) "age"
4) "18"
5) "jobs"
6) "it"
上面的奇数行表示key,偶数行表示value。
21.13/21.14/21.15 redis常用操作
Redis常用操作 (string, list)
首先set一个key值,当再次set一个值得时候,第二次赋值会覆盖
127.0.0.1:6379> set key1 hongwei
OK
127.0.0.1:6379> get key1
"hongwei"
127.0.0.1:6379> set key1 aming
OK
127.0.0.1:6379> get key1
"aming"
string用法
127.0.0.1:6379> SETNX key4 123
(integer) 1
127.0.0.1:6379> SETNX key4 234
(integer) 0
127.0.0.1:6379> get key4
"123"
127.0.0.1:6379>
•返回1 如果key4不存在直接创建key •返回0,如果key2存在,返回0
127.0.0.1:6379> SET key5 aaa ex 10
OK
127.0.0.1:6379> get key5
"aaa"
127.0.0.1:6379> get key5
(nil)
上面例子中,ex表示设置它的过期时间
list用法:
从左侧加入一个元素,先写入的放到最后面,后写入的放到最前面
127.0.0.1:6379> LPUSH list3 aaa
(integer) 1
127.0.0.1:6379> LPUSH list3 bbb
(integer) 2
127.0.0.1:6379> LRANGE list3 0 -1
1) "bbb"
2) "aaa"
127.0.0.1:6379>
从左侧取出一个元素
127.0.0.1:6379> LPOP list3
"bbb"
127.0.0.1:6379> LRANGE list3 0 -1
1) "aaa"
从右侧加入一个元素
127.0.0.1:6379> RPUSH list3 1
(integer) 2
127.0.0.1:6379> LRANGE list3 0 -1
1) "aaa"
2) "1"
从右侧取出一个元素
127.0.0.1:6379> RPOP list3
"1"
127.0.0.1:6379> LRANGE list3 0 -1
1) "aaa"
在aaa元素前面加上一个元素1
127.0.0.1:6379> LINSERT list3 before aaa 1
(integer) 2
127.0.0.1:6379> LRANGE list3 0 -1
1) "1"
2) "aaa"
在aaa元素后面加上一个元素2
127.0.0.1:6379> LINSERT list3 after aaa 2
(integer) 3
127.0.0.1:6379> LRANGE list3 0 -1
1) "1"
2) "aaa"
3) "2"
把第三个元素修改为abc
127.0.0.1:6379> LSET list3 2 abc
OK
127.0.0.1:6379> LRANGE list3 0 -1
1) "1"
2) "aaa"
3) "abc"
查看第一个元素,第二个元素,第三个元素
127.0.0.1:6379> LINDEX list3 0
"1"
127.0.0.1:6379> LINDEX list3 1
"aaa"
127.0.0.1:6379> LINDEX list3 3
(nil)
127.0.0.1:6379> LINDEX list3 2
"abc"
127.0.0.1:6379>
查看链表中有几个元素
127.0.0.1:6379> LLEN list3
(integer) 3
set用法:
向集合set中放入元素
127.0.0.1:6379> SADD seta aaa
(integer) 1
127.0.0.1:6379> SADD seta bbb
(integer) 1
查看集合中所有的元素
127.0.0.1:6379> SMEMBERS seta
1) "aaa"
2) "bbb"
删除元素
127.0.0.1:6379> SREM seta aaa
(integer) 1
127.0.0.1:6379> SMEMBERS seta
1) "bbb"
随机取出一个元素,删除
127.0.0.1:6379> SPOP seta
"bbb"
127.0.0.1:6379> SMEMBERS seta
1) "123"
2) "234"
3) "abc"
求差集,以seta为标准
127.0.0.1:6379> SDIFF seta setb
1) "234"
2) "abc"
求差集并且存储,存储到了setc里
127.0.0.1:6379> SDIFFSTORE setc seta setb
(integer) 2
127.0.0.1:6379> SMEMBERS setc
1) "234"
2) "abc"
求交集
127.0.0.1:6379> SADD setb 123
(integer) 1
127.0.0.1:6379> SADD setb aaa
(integer) 1
127.0.0.1:6379> SADD setb rrrr
(integer) 1
127.0.0.1:6379> SMEMBERS setb
1) "aaa"
2) "rrrr"
3) "123"
127.0.0.1:6379> SINTER seta setb
1) "123"
将交集储存在setd中
127.0.0.1:6379> SINTERSTORE setd seta setb
(integer) 1
127.0.0.1:6379> SMEMBERS setd
1) "123"
求并集
127.0.0.1:6379> SUNION seta setb
1) "123"
2) "234"
3) "abc"
4) "aaa"
5) "rrrr"
将并集储存在sete中
127.0.0.1:6379> SUNIONSTORE sete seta setb
(integer) 5
127.0.0.1:6379> SMEMBERS sete
1) "123"
2) "234"
3) "abc"
4) "aaa"
5) "rrrr"
判断一个元素是否属于一个集合。返回0不属于,返回1属于
127.0.0.1:6379> SISMEMBER seta aaa
(integer) 0
127.0.0.1:6379> SMEMBERS seta
1) "123"
2) "234"
3) "abc"
127.0.0.1:6379> SISMEMBER seta 123
(integer) 1
随机取出一个元素,但不删除
127.0.0.1:6379> SRANDMEMBER seta
"abc"
127.0.0.1:6379> SMEMBERS seta
1) "123"
2) "234"
3) "abc"
zet用法:
创建有序集合
127.0.0.1:6379> ZADD zsetb 12 123
(integer) 1
127.0.0.1:6379> ZADD zsetb 1 abc
(integer) 1
显示所有元素,按顺序显示
127.0.0.1:6379> ZRANGE zsetb 0 -1
1) "abc"
2) "123"
可以带上分值
2) "123"
127.0.0.1:6379> zrange zseta 0 -1 withscores
1) "-1"
2) "0"
3) "123"
4) "11"
删除指定元素
127.0.0.1:6379> ZREM zsetb 123
(integer) 1
127.0.0.1:6379> ZRANGE zsetb 0 -1
1) "abc"
返回元素的索引值,索引值从0开始,按score正向排序,索引值就是下标,第几个数字
127.0.0.1:6379> ZADD zsetb 13 weqwe
(integer) 1
127.0.0.1:6379> ZADD zsetb 14 12sds
(integer) 1
127.0.0.1:6379> ZADD zsetb 21 3434trtr
(integer) 1
127.0.0.1:6379> ZADD zsetb 5 qwerr
(integer) 1
127.0.0.1:6379> ZRANGE zsetb 0 -1
1) "abc"
2) "qwerr"
3) "weqwe"
4) "12sds"
5) "3434trtr"
127.0.0.1:6379> ZRANK zsetb 12sds
(integer) 3
同上,不同的是,按score反序排序
27.0.0.1:6379> ZREVRANK zsetb 12sds
(integer) 1
反序显示所有元素,并带分值
127.0.0.1:6379> ZREVRANGE zsetb 0 -1
1) "3434trtr"
2) "12sds"
3) "weqwe"
4) "qwerr"
5) "abc"
返回集合中所有元素的个数
127.0.0.1:6379> ZCARD zsetb
(integer) 5
返回分值范围1-10的元素个数
(integer) 0
127.0.0.1:6379> ZCOUNT zsetb 1 10
(integer) 2
127.0.0.1:6379> ZCOUNT zsetb 10 20
(integer) 2
127.0.0.1:6379> ZCOUNT zsetb 1 100
(integer) 5
删除索引范围0-2的元素,按score正向排序
127.0.0.1:6379> ZREMRANGEBYRANK zsetb 1 5
(integer) 4
127.0.0.1:6379> ZRANGE zsetb 0 -1
1) "abc"
hash用法:
建立hash
127.0.0.1:6379> HSET user1 name aming
(integer) 1
127.0.0.1:6379> HSET user1 age 20
(integer) 1
127.0.0.1:6379> HSET user1 job it
(integer) 1
127.0.0.1:6379> HGETALL uesr1
(empty list or set)
127.0.0.1:6379> HGETALL user1
1) "name"
2) "aming"
3) "age"
4) "20"
5) "job"
6) "it"
批量建立键值对
127.0.0.1:6379> HMSET user2 name hongwei age 20 jod worker
OK
127.0.0.1:6379> hmget user2 name age jod
1) "hongwei"
2) "20"
3) "worker"
删除指定filed
127.0.0.1:6379> hdel user2 jod
(integer) 1
打印所有的key
127.0.0.1:6379> hkeys user2
1) "name"
2) "age"
打印所有的values
127.0.0.1:6379> hvals user2
1) "hongwei"
2) "20"
查看hash有几个filed
127.0.0.1:6379> hlen user2
(integer) 2
21.16 redis操作键值
取出所有key
127.0.0.1:6379> keys *
1) "user2"
2) "key2"
3) "k1"
4) "k3"
5) "hash1"
6) "set2"
7) "lista"
8) "setb"
9) "zseta"
10) "set3"
11) "key4"
12) "seta"
13) "user1"
14) "mykey"
15) "list3"
16) "k2"
17) "setc"
18) "zsetb"
19) "sete"
20) "set1"
21) "setd"
22) "key3"
23) "key1"
24) "list1"
25) "list2"
模糊匹配
127.0.0.1:6379> keys my*
1) "mykey"
有name键 返回1 ,否则返回0
127.0.0.1:6379> EXISTS name
(integer) 0
127.0.0.1:6379> EXISTS hey3
(integer) 0
127.0.0.1:6379> EXISTS key3
(integer) 1
删除一个key //成功返回1 ,否则返回0
127.0.0.1:6379> del k1
(integer) 1
127.0.0.1:6379> del jjk
(integer) 0
设置key1 100s后过期
127.0.0.1:6379> EXPIRE key1 100
(integer) 1
// 查看键 还有多长时间过期,单位是s,当 key 不存在时,返回 -2 。 当 key 存在但没有设置剩余生存时间时,返回 -1 。 否则,返回 key 的剩余生存时间
127.0.0.1:6379> ttl kei1
(integer) -2
127.0.0.1:6379> ttl key1
(integer) 33
127.0.0.1:6379> ttl key1
(integer) 32
127.0.0.1:6379> ttl key1
(integer) 31
127.0.0.1:6379> ttl key1
(integer) 31
127.0.0.1:6379> ttl key1
(integer) 30
127.0.0.1:6379> ttl key1
(integer) 30
127.0.0.1:6379> ttl key1
(integer) 29
127.0.0.1:6379> ttl key1
(integer) 28
代表选择当前数据库,默认进入0 数据库,选择了0,在进入1是空的
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> keys *
(empty list or set)
把age 移动到1 数据库
127.0.0.1:6379[1]> select 0
OK
127.0.0.1:6379> move seta 1
(integer) 1
127.0.0.1:6379[1]> keys *
1) "seta"
取消key1的过期时间
127.0.0.1:6379> EXPIRE key2 100
(integer) 1
127.0.0.1:6379> ttl key2
(integer) 97
127.0.0.1:6379> ttl key2
(integer) 96
127.0.0.1:6379> persist key2
(integer) 1
127.0.0.1:6379> ttl key2
(integer) -1
随机返回一个key
127.0.0.1:6379> RANDOMKEY
"key3"
127.0.0.1:6379> RANDOMKEY
"setd"
重命名key
127.0.0.1:6379> keys set*
1) "set2"
2) "setb"
3) "set3"
4) "setc"
5) "sete"
6) "set1"
7) "setd"
127.0.0.1:6379> RENAME setd sett
OK
127.0.0.1:6379> keys set*
1) "set2"
2) "setb"
3) "set3"
4) "sett"
5) "setc"
6) "sete"
7) "set1"
返回键的类型
127.0.0.1:6379> type key1
none
127.0.0.1:6379> type sett
set
127.0.0.1:6379> type key2
string
127.0.0.1:6379> type zseta
zset
127.0.0.1:6379> type list1
list
Redis常用操作(服务)
回当前数据库中key的数目
127.0.0.1:6379> DBSIZE
(integer) 22
返回redis数据库状态信息
127.0.0.1:6379> info
# Server
redis_version:4.0.1
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:cefa031cc42cb327
redis_mode:standalone
os:Linux 3.10.0-327.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:4.8.5
process_id:5883
run_id:030b1bbda62aeeca896b4981c68da95453b7b787
tcp_port:6379
uptime_in_seconds:7590
uptime_in_days:0
hz:10
lru_clock:8353878
executable:/usr/local/src/redis-4.0.1/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:831160
used_memory_human:811.68K
used_memory_rss:7983104
used_memory_rss_human:7.61M
used_memory_peak:831160
used_memory_peak_human:811.68K
used_memory_peak_perc:100.11%
used_memory_overhead:816470
used_memory_startup:765600
used_memory_dataset:14690
used_memory_dataset_perc:22.41%
total_system_memory:1033498624
total_system_memory_human:985.62M
used_memory_lua:37888
used_memory_lua_human:37.00K
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
mem_fragmentation_ratio:9.60
mem_allocator:jemalloc-4.0.3
active_defrag_running:0
lazyfree_pending_objects:0
# Persistence
loading:0
rdb_changes_since_last_save:6
rdb_bgsave_in_progress:0
rdb_last_save_time:1535079908
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:0
rdb_current_bgsave_time_sec:-1
rdb_last_cow_size:6594560
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:3363
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:5
total_commands_processed:226
instantaneous_ops_per_sec:0
total_net_input_bytes:7983
total_net_output_bytes:54981
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:2
evicted_keys:0
keyspace_hits:106
keyspace_misses:13
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:4759
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:a13867f1887510a6afac572ae2e52f138ec0e5b7
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:12.14
used_cpu_user:5.20
used_cpu_sys_children:0.21
used_cpu_user_children:0.00
# Cluster
cluster_enabled:0
# Keyspace
db0:keys=22,expires=0,avg_ttl=0
db1:keys=1,expires=0,avg_ttl=0
清空当前数据库中所有的键
127.0.0.1:6379> flushall
OK
127.0.0.1:6379> keys *
(empty list or set)
保存数据到 rdb文件中,在后台运行
127.0.0.1:6379> BGSAVE
Background saving started
作用同上,但是在前台运行
127.0.0.1:6379> save
OK
获取所有配置参数。这些参数就是我们在配置文件配置的参数
127.0.0.1:6379> CONFIG get *
1) "dbfilename"
2) "dump.rdb"
3) "requirepass"
4) ""
5) "masterauth"
6) ""
7) "cluster-announce-ip"
8) ""
9) "unixsocket"
10) ""
11) "logfile"
12) "/var/log/redis.log"
13) "pidfile"
14) "/var/run/redis_6379.pid"
15) "slave-announce-ip"
16) ""
17) "maxmemory"
18) "0"
19) "maxmemory-samples"
20) "5"
21) "timeout"
22) "0"
23) "active-defrag-threshold-lower"
24) "10"
25) "active-defrag-threshold-upper"
26) "100"
27) "active-defrag-ignore-bytes"
28) "104857600"
29) "active-defrag-cycle-min"
30) "25"
31) "active-defrag-cycle-max"
32) "75"
33) "auto-aof-rewrite-percentage"
34) "100"
35) "auto-aof-rewrite-min-size"
36) "67108864"
37) "hash-max-ziplist-entries"
38) "512"
39) "hash-max-ziplist-value"
40) "64"
41) "list-max-ziplist-size"
42) "-2"
43) "list-compress-depth"
44) "0"
45) "set-max-intset-entries"
46) "512"
47) "zset-max-ziplist-entries"
48) "128"
49) "zset-max-ziplist-value"
50) "64"
51) "hll-sparse-max-bytes"
52) "3000"
53) "lua-time-limit"
54) "5000"
55) "slowlog-log-slower-than"
56) "10000"
57) "latency-monitor-threshold"
58) "0"
59) "slowlog-max-len"
60) "128"
61) "port"
62) "6379"
63) "cluster-announce-port"
64) "0"
65) "cluster-announce-bus-port"
66) "0"
67) "tcp-backlog"
68) "511"
69) "databases"
70) "16"
71) "repl-ping-slave-period"
72) "10"
73) "repl-timeout"
74) "60"
75) "repl-backlog-size"
76) "1048576"
77) "repl-backlog-ttl"
78) "3600"
79) "maxclients"
80) "10000"
81) "watchdog-period"
82) "0"
83) "slave-priority"
84) "100"
85) "slave-announce-port"
86) "0"
87) "min-slaves-to-write"
88) "0"
89) "min-slaves-max-lag"
90) "10"
91) "hz"
92) "10"
93) "cluster-node-timeout"
94) "15000"
95) "cluster-migration-barrier"
96) "1"
97) "cluster-slave-validity-factor"
98) "10"
99) "repl-diskless-sync-delay"
100) "5"
101) "tcp-keepalive"
102) "300"
103) "cluster-require-full-coverage"
104) "yes"
105) "no-appendfsync-on-rewrite"
106) "no"
107) "slave-serve-stale-data"
108) "yes"
109) "slave-read-only"
110) "yes"
111) "stop-writes-on-bgsave-error"
112) "yes"
113) "daemonize"
114) "yes"
115) "rdbcompression"
116) "yes"
117) "rdbchecksum"
118) "yes"
119) "activerehashing"
120) "yes"
121) "activedefrag"
122) "no"
123) "protected-mode"
124) "yes"
125) "repl-disable-tcp-nodelay"
126) "no"
127) "repl-diskless-sync"
128) "no"
129) "aof-rewrite-incremental-fsync"
130) "yes"
131) "aof-load-truncated"
132) "yes"
133) "aof-use-rdb-preamble"
134) "no"
135) "lazyfree-lazy-eviction"
136) "no"
137) "lazyfree-lazy-expire"
138) "no"
139) "lazyfree-lazy-server-del"
140) "no"
141) "slave-lazy-flush"
142) "no"
143) "maxmemory-policy"
144) "noeviction"
145) "loglevel"
146) "notice"
147) "supervised"
148) "no"
149) "appendfsync"
150) "everysec"
151) "syslog-facility"
152) "local0"
153) "appendonly"
154) "yes"
155) "dir"
156) "/data/redis"
157) "save"
158) "900 1 300 10 60 10000"
159) "client-output-buffer-limit"
160) "normal 0 0 0 slave 268435456 67108864 60 pubsub 33554432 8388608 60"
161) "unixsocketperm"
162) "0"
163) "slaveof"
164) ""
165) "notify-keyspace-events"
166) ""
167) "bind"
168) "127.0.0.1"
127.0.0.1:6379>
获取配置参数
127.0.0.1:6379> CONFIG get bind
1) "bind"
2) "127.0.0.1"
127.0.0.1:6379> CONFIG get dir
1) "dir"
2) "/data/redis"
更改配置参数
127.0.0.1:6379> CONFIG set timeout 1000
OK
127.0.0.1:6379> CONFIG get timeout
1) "timeout"
2) "1000"
数据恢复: 首先定义或者确定dir目录和dbfilename,然后把备份的rdb文件放到dir目录下面,重启redis服务即可恢复数据
127.0.0.1:6379> CONFIG get dir
1) "dir"
2) "/data/redis"
127.0.0.1:6379> CONFIG get dbfilename
1) "dbfilename"
2) "dump.rdb"
首先要获取到上面的二个值
21.17 redis安全设置
•设置监听ip • bind 127.0.0.1 2.2.2.2//可以是多个ip,用空格分隔 • 设置监听端口 • port 16000
还可以设置密码 设置密码步骤:
打开配置文件/etc/redis.conf,添加密码
然后重启redis
[root@liuhongwei-01 redis-4.0.1]# killall redis-server
[root@liuhongwei-01 redis-4.0.1]# redis-server /etc/redis.conf
[root@liuhongwei-01 redis-4.0.1]#
当然我们设置密码后,也可以这样登录,但是运行keys *的时候报错了
[root@liuhongwei-01 redis-4.0.1]# redis-cli
127.0.0.1:6379> keys *
(error) NOAUTH Authentication required.
使用密码登录加上-a选项
[root@liuhongwei-01 redis-4.0.1]# redis-cli -a woshi
127.0.0.1:6379> keys *
1) "user1"
2) "zseta"
•将config命令改名
• rename-command CONFIG aming
• 禁掉config命令
• rename-command CONFIG “”
21.18 redis慢查询日志
mysql有慢查询日志,redis也有慢查询日志
• 编辑配置文件/etc/redis.conf
1000是微秒
长度是128条日志
为了演示实验,把1000微秒改为10微秒
然后重启redis服务
[root@liuhongwei-01 redis-4.0.1]# killall redis-server
[root@liuhongwei-01 redis-4.0.1]# redis-server /etc/redis.conf
列出所有的慢查询日志
127.0.0.1:6379> slowlog get
1) 1) (integer) 1
2) (integer) 1535088565
3) (integer) 536
4) 1) "COMMAND"
5) "127.0.0.1:44010"
6) ""
2) 1) (integer) 0
2) (integer) 1535088565
3) (integer) 11
4) 1) "AUTH"
2) "woshi10086"
5) "127.0.0.1:44010"
6) ""
再次执行几条命令看一下慢查询日志
127.0.0.1:6379> keys *
1) "zseta"
2) "user1"
127.0.0.1:6379> get key3
(nil)
127.0.0.1:6379> slowlog get
1) 1) (integer) 3
2) (integer) 1535088647
3) (integer) 20
4) 1) "keys"
2) "*"
5) "127.0.0.1:44010"
6) ""
2) 1) (integer) 2
2) (integer) 1535088599
3) (integer) 35
4) 1) "slowlog"
2) "get"
5) "127.0.0.1:44010"
6) ""
3) 1) (integer) 1
2) (integer) 1535088565
3) (integer) 536
4) 1) "COMMAND"
5) "127.0.0.1:44010"
6) ""
4) 1) (integer) 0
2) (integer) 1535088565
3) (integer) 11
4) 1) "AUTH"
2) "woshi10086"
5) "127.0.0.1:44010"
6) ""
只列出2条
127.0.0.1:6379> slowlog get 2
1) 1) (integer) 4
2) (integer) 1535088658
3) (integer) 30
4) 1) "slowlog"
2) "get"
5) "127.0.0.1:44010"
6) ""
2) 1) (integer) 3
2) (integer) 1535088647
3) (integer) 20
4) 1) "keys"
2) "*"
5) "127.0.0.1:44010"
6) ""
127.0.0.1:6379>
slowlog len //查看慢查询日志条数
127.0.0.1:6379> slowlog len
(integer) 7
21.19 php安装redis扩展
首先进入到src目录下
[root@liuhongwei-01 redis-4.0.1]# cd /usr/local/src
[root@liuhongwei-01 src]#
下载包
[root@liuhongwei-01 src]# wget https://coding.net/u/aminglinux/p/yuanke_centos7/git/raw/master/21NOSQL/phpredis.zip
--2018-08-24 13:39:41-- https://coding.net/u/aminglinux/p/yuanke_centos7/git/raw/master/21NOSQL/phpredis.zip
正在解析主机 coding.net (coding.net)... 123.59.94.101, 123.59.89.67, 123.59.94.73, ...
正在连接 coding.net (coding.net)|123.59.94.101|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:未指定 [application/octet-stream]
正在保存至: “phpredis.zip”
[ <=> ] 224,818 --.-K/s 用时 0.1s
2018-08-24 13:39:47 (1.71 MB/s) - “phpredis.zip” 已保存 [224818]
解压
[root@liuhongwei-01 src]# unzip phpredis.zip
然后开始编译
[root@liuhongwei-01 src]# cd phpredis-develop
[root@liuhongwei-01 phpredis-develop]#
[root@liuhongwei-01 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@liuhongwei-01 phpredis-develop]#
[root@liuhongwei-01 phpredis-develop]# ./configure --with-php-config=/usr/local/php-fpm/bin/php-config
[root@liuhongwei-01 phpredis-develop]# make
[root@liuhongwei-01 phpredis-develop]# make install
Installing shared extensions: /usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20131226/
编译php.ini配置文件增加一行
[root@liuhongwei-01 phpredis-develop]# vim /usr/local/php-fpm/etc/php.ini
查看是否加载了redis。so文件,
[root@liuhongwei-01 phpredis-develop]# /usr/local/php-fpm/bin/php -m
[PHP Modules]
Core
ctype
curl
date
dom
ereg
exif
fileinfo
filter
ftp
gd
hash
iconv
json
libxml
mbstring
mcrypt
memcache
mysql
mysqli
openssl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
redis
Reflection
session
SimpleXML
soap
SPL
sqlite3
standard
tokenizer
xml
xmlreader
xmlwriter
zlib
[Zend Modules]
为了让redis生效还要重启php-fpm
[root@liuhongwei-01 phpredis-develop]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
[root@liuhongwei-01 phpredis-develop]#
21.20 redis存储session
• vim /usr/local/php-fpm/etc/php.ini//更改或增加 |
session.save_handler = "redis" session.save_path = "tcp://127.0.0.1:6379" |
• 或者apache虚拟主机配置文件中也可以这样配置: |
php_value session.save_handler " redis" |
或者php-fpm配置文件对应的pool中增加 |
php_value[session.save_handler] = redis php_value[session.save_path] = " tcp://127.0.0.1:6379 " |
因为之前在memcached中使用的最后一种方面,这次也是这种方法
[root@liuhongwei-01 phpredis-develop]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
为了方便测试还需要把密码给关闭
重启redis服务
[root@liuhongwei-01 ~]# killall redis-server
[root@liuhongwei-01 ~]# redis-server /etc/redis.conf
然后是测试吧redis存储到session中
[root@liuhongwei-01 ~]# curl localhost/session.php
1535095749<br><br>1535095749<br><br>vueo4ehbjlqmk615t5b4egd5t1
[root@liuhongwei-01 ~]# curl localhost/session.php
1535095750<br><br>1535095750<br><br>bonvhii83l7e72o257j60hskb2
[root@liuhongwei-01 ~]# curl localhost/session.php
1535095751<br><br>1535095751<br><br>8tehp18dqnir4dabdm7b25f6a4
[root@liuhongwei-01 ~]# curl localhost/session.php
1535095751<br><br>1535095751<br><br>1pr7b8tlhf7jd35nka1dv3gfm1
[root@liuhongwei-01 ~]# curl localhost/session.php
1535095752<br><br>1535095752<br><br>nbobda00itmrkoqhodhpi785r3
[root@liuhongwei-01 ~]#
可以看到redis存了好多session
[root@liuhongwei-01 ~]# redis-cli
127.0.0.1:6379> keys *
1) "PHPREDIS_SESSION:nbobda00itmrkoqhodhpi785r3"
2) "PHPREDIS_SESSION:8tehp18dqnir4dabdm7b25f6a4"
3) "zseta"
4) "PHPREDIS_SESSION:1pr7b8tlhf7jd35nka1dv3gfm1"
5) "PHPREDIS_SESSION:vueo4ehbjlqmk615t5b4egd5t1"
6) "PHPREDIS_SESSION:bonvhii83l7e72o257j60hskb2"
7) "user1"
127.0.0.1:6379>
127.0.0.1:6379> get PHPREDIS_SESSION:bonvhii83l7e72o257j60hskb2
"TEST|i:1535095750;TEST3|i:1535095750;"
127.0.0.1:6379>
21.21 redis主从配置
为了节省资源,我们可以在一台机器上启动两个redis服务
拷贝一下配置文件,然后命令为redis2.conf
[root@liuhongwei-01 ~]# cp /etc/redis.conf /etc/redis2.conf
然后编辑redis2.conf配置文件,需要修改port,dir,pidfile,logfile
[root@liuhongwei-01 ~]# vim /etc/redis2.conf
还需要添加一行
注:如果主的redis设置了密码,还需要添加密码
masterauth 后面跟上密码
然后创建/data/redis2目录
[root@liuhongwei-01 ~]# mkdir /data/redis2
接着就是启动redis
[root@liuhongwei-01 ~]# redis-server /etc/redis2.conf
[root@liuhongwei-01 ~]# ps aux|grep redis2
root 12818 0.0 0.5 151528 5116 pts/1 S+ 15:38 0:00 vim /etc/redis2.conf
root 12905 0.0 0.0 112664 964 pts/2 R+ 15:51 0:00 grep --color=auto redis2
[root@liuhongwei-01 ~]# netstat -lntp
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 12735/redis-server
tcp 0 0 127.0.0.1:6380 0.0.0.0:* LISTEN 12898/redis-server
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 759/rpcbind
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 12778/nginx: master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1033/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2327/master
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 12778/nginx: master
tcp 0 0 0.0.0.0:10051 0.0.0.0:* LISTEN 2331/zabbix_server
tcp6 0 0 :::3306 :::* LISTEN 2283/mysqld
tcp6 0 0 :::111 :::* LISTEN 759/rpcbind
tcp6 0 0 :::22 :::* LISTEN 1033/sshd
tcp6 0 0 ::1:25 :::* LISTEN 2327/master
tcp6 0 0 :::10051 :::* LISTEN 2331/zabbix_server
然后登陆一下从的redis
[root@liuhongwei-01 ~]# redis-cli -h 127.0.0.1 -p 6380
127.0.0.1:6380> heys *
(error) ERR unknown command 'heys'
127.0.0.1:6380> keys *
1) "user1"
2) "zseta"
127.0.0.1:6380>
测试:在主上创建新的key 在从上查看
[root@liuhongwei-01 ~]# redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> set key1 aaa
OK
127.0.0.1:6379> get key1
"aaa"
127.0.0.1:6379> keys *
1) "zseta"
2) "key1"
3) "user1"
127.0.0.1:6379>
[root@liuhongwei-01 ~]# redis-cli -h 127.0.0.1 -p 6380
127.0.0.1:6380> keys *
1) "key1"
2) "user1"
3) "zseta"
因为从的机器设置了只读模式,所以在从上的redis是不能创建key的
[root@liuhongwei-01 ~]# redis-cli -h 127.0.0.1 -p 6380
127.0.0.1:6380> keys *
1) "key1"
2) "user1"
3) "zseta"
127.0.0.1:6380> set key1 111
(error) READONLY You can't write against a read only slave.
127.0.0.1:6380>
21.22 redis集群介绍
21.23/21.24 redis集群搭建配置
安装ruby2.2 (只需要一台机器上运行)
[root@liuhongwei-01 ~]# yum -y groupinstall "Development Tools"
已加载插件:fastestmirror
没有安装组信息文件
Maybe run: yum groups mark convert (see man yum)
然后在主机器上。创建7000 7002 7004的conf,加入如下内容
[root@liuhongwei-01 ~]# vim /etc/redis_7000.conf
port 7000
bind 192.168.93.129
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
~
[root@liuhongwei-01 ~]# vim /etc/redis_7002.conf
port 7002
bind 192.168.93.129
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
[root@liuhongwei-01 ~]# vim /etc/redis_7002.conf
port 7004
bind 192.168.93.129
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
第二台机器上也要编写7001 7003 7005配置文件
[root@hongwei-02 ~]# vim /etc/redis_7001.conf
port 7001
bind 192.168.93.128
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
[root@hongwei-02 ~]# vim /etc/redis_7001.conf
port 7003
bind 192.168.93.128
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
~
[root@hongwei-02 ~]# vim /etc/redis_7005.conf
port 7005
bind 192.168.93.128
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@liuhongwei-01 ~]# mkdir /data/redis_data
[root@liuhongwei-01 ~]# mkdir /data/redis_data/{7000,7002,7004}
[root@liuhongwei-01 ~]#
在从上创建目录
[root@hongwei-02 ~]# mkdir /data/redis_data/
[root@hongwei-02 ~]# mkdir /data/redis_data/{7001,7003,7005}
[root@hongwei-02 ~]#
创建完目录后,在主机器上启动
[root@liuhongwei-01 ~]# redis-server /etc/redis_7000.conf
25054:C 24 Aug 18:31:25.596 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
25054:C 24 Aug 18:31:25.597 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=25054, just started
25054:C 24 Aug 18:31:25.597 # Configuration loaded
[root@liuhongwei-01 ~]# redis-server /etc/redis_7002.conf
25059:C 24 Aug 18:31:45.098 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
25059:C 24 Aug 18:31:45.098 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=25059, just started
25059:C 24 Aug 18:31:45.098 # Configuration loaded
[root@liuhongwei-01 ~]# redis-server /etc/redis_7004.conf
25064:C 24 Aug 18:31:49.435 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
25064:C 24 Aug 18:31:49.436 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=25064, just started
25064:C 24 Aug 18:31:49.436 # Configuration loaded
[root@liuhongwei-01 ~]#
[root@liuhongwei-01 ~]# ps aux|grep redis
root 12735 0.1 0.9 147404 9488 ? Ssl 15:25 0:21 redis-server 127.0.0.1:6379
root 12898 0.2 0.9 147288 9396 ? Ssl 15:51 0:19 redis-server 127.0.0.1:6380
root 25055 0.1 0.7 145244 7568 ? Ssl 18:31 0:00 redis-server 192.168.93.129:7000 [cluster]
root 25060 0.1 0.7 145244 7568 ? Ssl 18:31 0:00 redis-server 192.168.93.129:7002 [cluster]
root 25065 0.1 0.7 145244 7572 ? Ssl 18:31 0:00 redis-server 192.168.93.129:7004 [cluster]
root 25073 0.0 0.0 112668 960 pts/2 S+ 18:32 0:00 grep --color=auto redis
第二台也要启动,第二台机器要安装redis服务
scp -r redis-4.0.1 root@192.168.93.128:/usr/local/src
[root@hongwei-02 ~]# cd /usr/local/src
[root@hongwei-02 src]# cd redis-4.0.1/
[root@hongwei-02 redis-4.0.1]# make install
cd src && make install
make[1]: 进入目录“/usr/local/src/redis-4.0.1/src”
Hint: It's a good idea to run 'make test' ;)
INSTALL install
INSTALL install
INSTALL install
INSTALL install
INSTALL install
make[1]: 离开目录“/usr/local/src/redis-4.0.1/src”
[root@hongwei-02 redis-4.0.1]#
[root@hongwei-02 redis-4.0.1]# redis-server /etc/redis_7001.conf
3237:C 24 Aug 18:36:32.658 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3237:C 24 Aug 18:36:32.658 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=3237, just started
3237:C 24 Aug 18:36:32.658 # Configuration loaded
[root@hongwei-02 redis-4.0.1]# redis-server /etc/redis_7003.conf
3242:C 24 Aug 18:36:35.945 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3242:C 24 Aug 18:36:35.945 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=3242, just started
3242:C 24 Aug 18:36:35.945 # Configuration loaded
[root@hongwei-02 redis-4.0.1]# redis-server /etc/redis_7005.conf
3247:C 24 Aug 18:36:39.082 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3247:C 24 Aug 18:36:39.082 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=3247, just started
3247:C 24 Aug 18:36:39.083 # Configuration loaded
[root@hongwei-02 redis-4.0.1]# ps aux|grep redis
root 3238 0.6 0.7 145244 7568 ? Ssl 18:36 0:00 redis-server 192.168.93.128:7001 [cluster]
root 3243 0.1 0.7 145244 7568 ? Ssl 18:36 0:00 redis-server 192.168.93.128:7003 [cluster]
root 3248 0.6 0.7 145244 7568 ? Ssl 18:36 0:00 redis-server 192.168.93.128:7005 [cluster]
root 3253 0.0 0.0 112664 964 pts/0 R+ 18:37 0:00 grep --color=auto redis
[root@hongwei-02 redis-4.0.1]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 192.168.93.128:17001 0.0.0.0:* LISTEN 3238/redis-server 1
tcp 0 0 192.168.93.128:17003 0.0.0.0:* LISTEN 3243/redis-server 1
tcp 0 0 192.168.93.128:17005 0.0.0.0:* LISTEN 3248/redis-server 1
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 747/rpcbind
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 2048/nginx: master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 985/sshd
tcp 0 0 192.168.93.128:7001 0.0.0.0:* LISTEN 3238/redis-server 1
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2096/master
tcp 0 0 192.168.93.128:7003 0.0.0.0:* LISTEN 3243/redis-server 1
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 2048/nginx: master
tcp 0 0 192.168.93.128:7005 0.0.0.0:* LISTEN 3248/redis-server 1
tcp6 0 0 :::3306 :::* LISTEN 2265/mysqld
tcp6 0 0 :::111 :::* LISTEN 747/rpcbind
tcp6 0 0 :::22 :::* LISTEN 985/sshd
tcp6 0 0 ::1:25 :::* LISTEN 2096/master
[root@hongwei-02 redis-4.0.1]#
在做个实验之前要关闭iptables规则,和selinux
[root@hongwei-02 redis-4.0.1]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
[root@hongwei-02 redis-4.0.1]# getenforce
Disabled
[root@hongwei-02 redis-4.0.1]#
[root@liuhongwei-01 src]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
[root@liuhongwei-01 src]# getenforce
Disabled
[root@liuhongwei-01 src]#
然后yum install ruby
[root@liuhongwei-01 ~]# yum -y install gdbm-devel libdb4-devel libffi-devel libyaml libyaml-devel ncurses-devel openssl-devel readline-devel tcl-deve
[root@liuhongwei-01 ~]# cd /root/
[root@liuhongwei-01 ~]# mkdir -p rpmbuild/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS}
[root@liuhongwei-01 ~]# wget http://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.3.tar.gz -P rpmbuild/SOURCES
[root@liuhongwei-01 ~]# wget https://raw.githubusercontent.com/tjinjin/automate-ruby-rpm/master/ruby22x.spec -P rpmbuild/SPECS
[root@liuhongwei-01 ~]# rpmbuild -bb rpmbuild/SPECS/ruby22x.spec
[root@liuhongwei-01 ~]# yum -y localinstall rpmbuild/RPMS/x86_64/ruby-2.2.3-1.el7.centos.x86_64.rpm
查看ruby版本
[root@liuhongwei-01 ~]# ruby -v
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-linux]
[root@liuhongwei-01 ~]# gem install redis
拷贝一下redis-trib.rb,到/usr/local/bin下。
[root@liuhongwei-01 ~]# cp /usr/local/src/redis-4.0.1/src/redis-trib.rb /usr/bin/
[root@liuhongwei-01 ~]# redis-trib.rb create --replicas 1 192.168.93.129:7000 192.168.93.129:7002 192.168.93.129:7004 192.168.93.128:7001 192.168.93.128:7003 192.168.93.128:7005
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.93.129:7000
192.168.93.128:7001
192.168.93.129:7002
Adding replica 192.168.93.128:7003 to 192.168.93.129:7000
Adding replica 192.168.93.129:7004 to 192.168.93.128:7001
Adding replica 192.168.93.128:7005 to 192.168.93.129:7002
M: dda093ad3607902d38372fac6536d458ba8674b8 192.168.93.129:7000
slots:0-5460 (5461 slots) master
M: 8783b7e996f849923045a663ac32241548b6555c 192.168.93.129:7002
slots:10923-16383 (5461 slots) master
S: fa31f01721a1da745d6a84d57ed1d508a77e3e05 192.168.93.129:7004
replicates e1d0aa5366b26db7d830480f20410cb75c371a4b
M: e1d0aa5366b26db7d830480f20410cb75c371a4b 192.168.93.128:7001
slots:5461-10922 (5462 slots) master
S: f05d8ba12ce2cb2ae75a4366e571fb165491d135 192.168.93.128:7003
replicates dda093ad3607902d38372fac6536d458ba8674b8
S: 8b8992b2eabd0e89e6728dddf89993e1e212d024 192.168.93.128:7005
replicates 8783b7e996f849923045a663ac32241548b6555c
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
看到二个绿色的ok表明成功
上面的操作后,表明集群搭建成功
21.25 redis集群操作
选择任何一个端口链接redis
[root@liuhongwei-01 ~]# redis-cli -c -h 192.168.93.129 -p 7000
192.168.93.129:7000>
-c说明以集群的方式登录
然后创建一个key
192.168.93.129:7000> set key1 123
-> Redirected to slot [9189] located at 192.168.93.128:7001
OK
192.168.93.128:7001>
接着在创建key看看到那个集群端口
192.168.93.128:7001> set key2 abc
-> Redirected to slot [4998] located at 192.168.93.129:7000
OK
192.168.93.129:7000> set key3 qwer
OK
192.168.93.129:7000> set key3 qwe
OK
192.168.93.129:7000> set key6 1212
OK
192.168.93.129:7000> set key5 sss
-> Redirected to slot [9057] located at 192.168.93.128:7001
OK
192.168.93.128:7001>
它会自动选择端口,没有提示的,表明到了本地端口
检测集群状态
[root@liuhongwei-01 ~]# redis-trib.rb check 192.168.93.129:7000
>>> Performing Cluster Check (using node 192.168.93.129:7000)
M: dda093ad3607902d38372fac6536d458ba8674b8 192.168.93.129:7000
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: fa31f01721a1da745d6a84d57ed1d508a77e3e05 192.168.93.129:7004
slots: (0 slots) slave
replicates e1d0aa5366b26db7d830480f20410cb75c371a4b
M: 8783b7e996f849923045a663ac32241548b6555c 192.168.93.129:7002
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: f05d8ba12ce2cb2ae75a4366e571fb165491d135 192.168.93.128:7003
slots: (0 slots) slave
replicates dda093ad3607902d38372fac6536d458ba8674b8
M: e1d0aa5366b26db7d830480f20410cb75c371a4b 192.168.93.128:7001
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 8b8992b2eabd0e89e6728dddf89993e1e212d024 192.168.93.128:7005
slots: (0 slots) slave
replicates 8783b7e996f849923045a663ac32241548b6555c
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered
列出节点
192.168.93.129:7000> cluster nodes
dda093ad3607902d38372fac6536d458ba8674b8 192.168.93.129:7000@17000 myself,master - 0 1535110554000 1 connected 0-5460
fa31f01721a1da745d6a84d57ed1d508a77e3e05 192.168.93.129:7004@17004 slave e1d0aa5366b26db7d830480f20410cb75c371a4b 0 1535110557313 4 connected
8783b7e996f849923045a663ac32241548b6555c 192.168.93.129:7002@17002 master - 0 1535110559357 2 connected 10923-16383
f05d8ba12ce2cb2ae75a4366e571fb165491d135 192.168.93.128:7003@17003 slave dda093ad3607902d38372fac6536d458ba8674b8 0 1535110560376 5 connected
e1d0aa5366b26db7d830480f20410cb75c371a4b 192.168.93.128:7001@17001 master - 0 1535110558334 4 connected 5461-10922
8b8992b2eabd0e89e6728dddf89993e1e212d024 192.168.93.128:7005@17005 slave 8783b7e996f849923045a663ac32241548b6555c 0 1535110558129 6 connected
查看集群信息
192.168.93.129: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:808
cluster_stats_messages_pong_sent:866
cluster_stats_messages_sent:1674
cluster_stats_messages_ping_received:861
cluster_stats_messages_pong_received:808
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:1674
添加节点
[root@hongwei-02 ~]# cp /etc/redis_7003.conf /etc/redis_7007.conf
[root@hongwei-02 ~]# vim !$
vim /etc/redis_7007.conf
port 7007
bind 192.168.93.128
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@hongwei-02 ~]# mkdir /data/redis_data/7007
[root@hongwei-02 ~]# redis-server /etc/redis_7007.conf
2466:C 25 Aug 09:51:07.153 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
2466:C 25 Aug 09:51:07.154 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=2466, just started
2466:C 25 Aug 09:51:07.154 # Configuration loaded
192.168.93.129:7000> cluster meet 192.168.93.128 7007
OK
192.168.93.129:7000> cluster nodes
dda093ad3607902d38372fac6536d458ba8674b8 192.168.93.129:7000@17000 myself,master - 0 1535110934000 1 connected 0-5460
2aca66e579b187a5ae1d5bcbfcec74a66041a1fe 192.168.93.128:7007@17007 master - 0 1535110933296 0 connected
fa31f01721a1da745d6a84d57ed1d508a77e3e05 192.168.93.129:7004@17004 slave e1d0aa5366b26db7d830480f20410cb75c371a4b 0 1535110933296 4 connected
8783b7e996f849923045a663ac32241548b6555c 192.168.93.129:7002@17002 master - 0 1535110935325 2 connected 10923-16383
f05d8ba12ce2cb2ae75a4366e571fb165491d135 192.168.93.128:7003@17003 slave dda093ad3607902d38372fac6536d458ba8674b8 0 1535110934308 5 connected
e1d0aa5366b26db7d830480f20410cb75c371a4b 192.168.93.128:7001@17001 master - 0 1535110934000 4 connected 5461-10922
8b8992b2eabd0e89e6728dddf89993e1e212d024 192.168.93.128:7005@17005 slave 8783b7e996f849923045a663ac32241548b6555c 0 1535110936343 6 connected
192.168.93.129:7000>
将当前节点设置为指定节点的从
先要登录要设置的机器redis
[root@liuhongwei-01 ~]# redis-cli -c -h 192.168.93.128 -p 7007
192.168.93.128:7007> cluster nodes
192.168.93.128:7007> cluster replicate 8783b7e996f849923045a663ac32241548b6555c
OK
192.168.93.128:7007>
把7007设置为7002的从
192.168.93.128:7007> cluster nodes
e1d0aa5366b26db7d830480f20410cb75c371a4b 192.168.93.128:7001@17001 master - 0 1535162277849 4 connected 5461-10922
f05d8ba12ce2cb2ae75a4366e571fb165491d135 192.168.93.128:7003@17003 slave dda093ad3607902d38372fac6536d458ba8674b8 0 1535162275000 1 connected
dda093ad3607902d38372fac6536d458ba8674b8 192.168.93.129:7000@17000 master - 0 1535162277000 1 connected 0-5460
8b8992b2eabd0e89e6728dddf89993e1e212d024 192.168.93.128:7005@17005 slave 8783b7e996f849923045a663ac32241548b6555c 0 1535162277000 2 connected
fa31f01721a1da745d6a84d57ed1d508a77e3e05 192.168.93.129:7004@17004 slave e1d0aa5366b26db7d830480f20410cb75c371a4b 0 1535162278057 4 connected
8783b7e996f849923045a663ac32241548b6555c 192.168.93.129:7002@17002 master - 0 1535162275095 2 connected 10923-16383
2aca66e579b187a5ae1d5bcbfcec74a66041a1fe 192.168.93.128:7007@17007 myself,slave 8783b7e996f849923045a663ac32241548b6555c 0 1535162275000 0 connected
192.168.93.128:7007>
移除某个节点
192.168.93.128:7007> cluster forget 2aca66e579b187a5ae1d5bcbfcec74a66041a1fe
(error) ERR I tried hard but I can't forget myself...
192.168.93.128:7007>
报错了,表明我们登录的是要移除的本身,移除不了,需要登录不同的端口号
192.168.93.129:7000> cluster forget 2aca66e579b187a5ae1d5bcbfcec74a66041a1fe
OK
192.168.93.129:7000> cluster nodes
dda093ad3607902d38372fac6536d458ba8674b8 192.168.93.129:7000@17000 myself,master - 0 1535111461000 1 connected 0-5460
fa31f01721a1da745d6a84d57ed1d508a77e3e05 192.168.93.129:7004@17004 slave e1d0aa5366b26db7d830480f20410cb75c371a4b 0 1535111464000 4 connected
8783b7e996f849923045a663ac32241548b6555c 192.168.93.129:7002@17002 master - 0 1535111464172 2 connected 10923-16383
f05d8ba12ce2cb2ae75a4366e571fb165491d135 192.168.93.128:7003@17003 slave dda093ad3607902d38372fac6536d458ba8674b8 0 1535111467129 5 connected
e1d0aa5366b26db7d830480f20410cb75c371a4b 192.168.93.128:7001@17001 master - 0 1535111465089 4 connected 5461-10922
8b8992b2eabd0e89e6728dddf89993e1e212d024 192.168.93.128:7005@17005 slave 8783b7e996f849923045a663ac32241548b6555c 0 1535111466109 6 connected
保存配置文件
192.168.93.129:7000> cluster saveconfig
OK
回到从上看一下有没有增加文件
[root@hongwei-02 ~]# ls -l /data/redis_data/7001
总用量 12
-rw-r--r-- 1 root root 87 8月 25 09:42 appendonly.aof
-rw-r--r-- 1 root root 175 8月 25 09:35 dump.rdb
-rw-r--r-- 1 root root 953 8月 25 09:57 nodes_7001.conf
[root@hongwei-02 ~]#
[root@hongwei-02 ~]# cat !$
cat /data/redis_data/7001/nodes_7001.conf
2aca66e579b187a5ae1d5bcbfcec74a66041a1fe 192.168.93.128:7007@17007 slave 8783b7e996f849923045a663ac32241548b6555c 0 1535162236659 2 connected
dda093ad3607902d38372fac6536d458ba8674b8 192.168.93.129:7000@17000 master - 0 1535162236000 1 connected 0-5460
8783b7e996f849923045a663ac32241548b6555c 192.168.93.129:7002@17002 master - 0 1535162238000 2 connected 10923-16383
fa31f01721a1da745d6a84d57ed1d508a77e3e05 192.168.93.129:7004@17004 slave e1d0aa5366b26db7d830480f20410cb75c371a4b 0 1535162238000 4 connected
f05d8ba12ce2cb2ae75a4366e571fb165491d135 192.168.93.128:7003@17003 slave dda093ad3607902d38372fac6536d458ba8674b8 0 1535162238697 5 connected
e1d0aa5366b26db7d830480f20410cb75c371a4b 192.168.93.128:7001@17001 myself,master - 0 1535162234000 4 connected 5461-10922
8b8992b2eabd0e89e6728dddf89993e1e212d024 192.168.93.128:7005@17005 slave 8783b7e996f849923045a663ac32241548b6555c 0 1535162237677 6 connected
vars currentEpoch 6 lastVoteEpoch 0
[root@hongwei-02 ~]#
转载于:https://my.oschina.net/u/3851487/blog/1933851
最后
以上就是优秀黑米为你收集整理的2018-08-24 课后笔记21.9 redis介绍21.10 redis安装21.11 redis持久化21.12 redis数据类型21.13/21.14/21.15 redis常用操作21.16 redis操作键值21.17 redis安全设置 21.18 redis慢查询日志21.19 php安装redis扩展21.20 redis存储session21.21 redis主从配置21.22 redis集群介绍21.23/21.24 redis集群搭建配置21.25 redis集群操作的全部内容,希望文章能够帮你解决2018-08-24 课后笔记21.9 redis介绍21.10 redis安装21.11 redis持久化21.12 redis数据类型21.13/21.14/21.15 redis常用操作21.16 redis操作键值21.17 redis安全设置 21.18 redis慢查询日志21.19 php安装redis扩展21.20 redis存储session21.21 redis主从配置21.22 redis集群介绍21.23/21.24 redis集群搭建配置21.25 redis集群操作所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复