目录
一、Redis介绍
二、安装Redis
三、Redis 持久化
四、Redis 的数据类型
五、Redis常用操作
六、Redis安全设置
七、Redis慢查询日志
八、PHP中使用redis – 安装扩展模块
九、PHP中使用redis – 存储session
十、Redis主从配置
十一、Redis集群
一、Redis介绍
Redis和Memcached类似,也属于k-v数据存储
Redis官网redis.io,当前最新稳定版4.0.1
支持更多value类型,除了和string外,还支持hash、lists(链表)、sets(集合)和sorted sets(有序集合)
redis使用了两种文件格式:全量数据(RDB)和增量请求(aof)。全量数据格式是把内存中的数据写入磁盘,便于下次读取文件进行加载。增量请求文件则是把内存中的数据序列化为操作请求,用于读取文件进行replay得到数据,这种类似于mysql binlog。
redis的存储分为内存存储、磁盘存储和log文件三部分
二、安装Redis
#下载并解压安装包
[root@minglinux-01 /usr/local/src] wget http://download.redis.io/releases/redis-4.0.1.tar.gz
[root@minglinux-01 /usr/local/src] tar -zxvf redis-4.0.1.tar.gz
#编译安装Redis,无需.configure检测系统配置和生成makefile文件,直接make && make install即可
[root@minglinux-01 /usr/local/src/redis-4.0.1] make && make install
[root@minglinux-01 /usr/local/src/redis-4.0.1] echo $?
0
[root@minglinux-01 /usr/local/src/redis-4.0.1] redis- #敲两次Tab,出来以下内容则表示Redis安装成功了
redis-benchmark redis-check-rdb redis-sentinel
redis-check-aof redis-cli redis-server
[root@minglinux-01 /usr/local/src/redis-4.0.1] which redis-cli #查看安装路径
/usr/local/bin/redis-cli
#拷贝配置文件
[root@minglinux-01 /usr/local/src/redis-4.0.1] cp redis.conf /etc/redis.conf
#修改配置文件如下
daemonize no 修改为 daemonize yes #yes让redis后台启动
logfile "/var/log/redis.log" #指定日志文件路径
dir /data/redis #指定RDB和AOF文件的存放目录
appendonly no 修改为 appendonly yes #开启AOF增量请求
#创建 RDB 和 AOF 文件的存放目录
[root@minglinux-01 /usr/local/src/redis-4.0.1] mkdir /data/redis
#启动Redis
[root@minglinux-01 /usr/local/src/redis-4.0.1] redis-server /etc/redis.conf
[root@minglinux-01 /usr/local/src/redis-4.0.1] ps aux |grep redis
root 5215 0.5 0.4 145296 7548 ? Ssl 15:26 0:00 redis-server 127.0.0.1:6379
root 5220 0.0 0.0 112720 984 pts/1 S+ 15:26 0:00 grep --color=auto redis
#查看Redis日志,有如下警告
[root@minglinux-01 /usr/local/src/redis-4.0.1] cat /var/log/redis.log
5214:C 18 Feb 15:26:44.222 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5214:C 18 Feb 15:26:44.222 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=5214, just started
5214:C 18 Feb 15:26:44.222 # Configuration loaded
5215:M 18 Feb 15:26:44.230 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 4.0.1 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 5215
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
5215:M 18 Feb 15:26:44.267 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
5215:M 18 Feb 15:26:44.268 # Server initialized
5215:M 18 Feb 15:26:44.268 # 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.
5215:M 18 Feb 15:26:44.268 # 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.
5215:M 18 Feb 15:26:44.268 * Ready to accept connections
#在当前目录执行两条命令
[root@minglinux-01 /usr/local/src/redis-4.0.1] sysctl vm.overcommit_memory=1
vm.overcommit_memory = 1
[root@minglinux-01 /usr/local/src/redis-4.0.1] echo never > /sys/kernel/mm/transparent_hugepage/enabled
# 修改内核参数,使其开机自动执行
[root@minglinux-01 /usr/local/src/redis-4.0.1] echo "sysctl vm.overcommit_memory=1" >> /etc/rc.local
[root@minglinux-01 /usr/local/src/redis-4.0.1] echo "echo never > /sys/kernel/mm/transparent_hugepage/enabled" >> /etc/rc.local
三、Redis 持久化
- Redis持久化方式
Redis提供了两种持久化的方式,分别是RDB(Redis DataBase)和AOF(Append Only File)
RDB,简而言之,就是在不同的时间点,将redis存储的数据生成快照并存储到磁盘等介质上。
AOF,则是换了一个角度来实现持久化,那就是将redis执行过的所有写指令记录下来,在下次redis重新启动时,只要把这些写指令从前到后再重复执行一遍,就可以实现数据恢复了。
其实RDB和AOF两种方式也可以同时使用,在这种情况下,如果redis重启的话,则会优先采用AOF方式来进行数据恢复,这是因为AOF方式的数据恢复完整度更高。
如果你没有数据持久化的需求,也完全可以关闭RDB和AOF方式,这样的话,redis将变成一个纯内存数据库,就像memcache一样。
- Redis持久化相关参数
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)。第一种最快,第二种数据最安全,但性能会差一些,第三种为折中方案,默认为第三种。
四、Redis 的数据类型
- Redis数据类型-string
string为最简单的类型,与Memcached一样的类型,一个key对应一个value,其支持的操作与Memcached的操作类似,它的功能更丰富。设置可以存二进制的对象。示例如下:
[root@minglinux-01 /usr/local/src/redis-4.0.1] redis-cli
127.0.0.1:6379> set mykey "xiaoming"
OK
127.0.0.1:6379> get mykey
"xiaoming"
127.0.0.1:6379> mset key1 a key2 b key3 c
OK
127.0.0.1:6379> mget key1 mykey
1) "a"
2) "xiaoming"
- Redis数据类型-list
list是一个链表结构,主要功能是push、pop、获取一个范围的所有值等等。操作中key理解为链表的名字。
使用 list 结构,我们可以轻松地实现最新消息排行等功能(比如新浪微博的 TimeLine )。list 的另一个应用就是消息队列,可以利用 list 的 push操作,将任务存在 list 中,然后工作线程再用pop操作将任务取出进行执行。
示例:
127.0.0.1:6379> LPUSH list1 "xiaoming"
(integer) 1
127.0.0.1:6379> LPUSH list1 "123"
(integer) 2
127.0.0.1:6379> LPUSH list1 "456"
(integer) 3
127.0.0.1:6379> LPUSH list1 "789"
(integer) 4
127.0.0.1:6379> LRANGE list1 0 -1 #0表示第一个元素, -1表示最后一个元素
1) "789"
2) "456"
3) "123"
4) "xiaoming"
127.0.0.1:6379> LRANGE list1 0 2
1) "789"
2) "456"
3) "123"
127.0.0.1:6379> LPOP list1
"789"
127.0.0.1:6379> LRANGE list1 0 -1
1) "456"
2) "123"
3) "xiaoming"
- 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> SMEMBERS set1 #获取集合全部元素
1) "a"
2) "c"
3) "b"
127.0.0.1:6379> SADD set2 a
(integer) 1
127.0.0.1:6379> SADD set2 c
(integer) 1
127.0.0.1:6379> SADD set2 d
(integer) 1
127.0.0.1:6379> SMEMBERS set2
1) "d"
2) "a"
3) "c"
127.0.0.1:6379> SUNION set1 set2 #并集
1) "a"
2) "b"
3) "c"
4) "d"
127.0.0.1:6379> SINTER set1 set2 #交集
1) "a"
2) "c"
127.0.0.1:6379> SDIFF set1 set2 #差集
1) "b"
127.0.0.1:6379> SREM set1 a #删除元素
(integer) 1
127.0.0.1:6379> SMEMBERS set1
1) "c"
2) "b"
- Redis数据类型-sort set
sorted set是有序集合,它比set多了一个权重参数score,使得集合中的元素能够按 score 进行有序排列,比如一个存储全班同学成绩的 Sorted Sets,其集合 value 可以是同学的学号,而 score 就可以是其考试得分,这样在数据插入集合的时候,就已经进行了天然的排序。
示例:
127.0.0.1:6379> ZADD set3 2 a
(integer) 1
127.0.0.1:6379> ZADD set3 10 "b"
(integer) 1
127.0.0.1:6379> ZADD set3 5 "c"
(integer) 1
127.0.0.1:6379> ZADD set3 8 "d"
(integer) 1
127.0.0.1:6379> ZRANGE set3 0 -1
1) "a"
2) "c"
3) "d"
4) "b"
127.0.0.1:6379> ZREVRANGE set3 0 -1 #倒序
1) "b"
2) "d"
3) "c"
4) "a"
- Redis数据类型-hash
在 Redis 中,我们经常将一些结构化的信息打包成 hashmap,在客户端序列化后存储为一个字符串的值(一般是 JSON 格式),比如用户的昵称、年龄、性别、积分等。
示例:
127.0.0.1:6379> HSET hash1 name xiaoming
(integer) 1
127.0.0.1:6379> HSET hash1 age 19
(integer) 1
127.0.0.1:6379> HSET hash1 job IT
(integer) 1
127.0.0.1:6379> HGET hash1 name
"xiaoming"
127.0.0.1:6379> HGET hash1 age
"19"
127.0.0.1:6379> HGET hash1 job
"IT"
127.0.0.1:6379> HGETALL hash1 #获取全部元素,奇数行为key,偶数行为value
1) "name"
2) "xiaoming"
3) "age"
4) "19"
5) "job"
6) "IT"
五、Redis常用操作
- string相关操作
127.0.0.1:6379> set key1 hello
OK
127.0.0.1:6379> set key2 world
OK
127.0.0.1:6379> set key1 linux #第二次赋值会覆盖
OK
127.0.0.1:6379> get key1
"linux"
127.0.0.1:6379> SETNX key1 a
(integer) 0 #key1已存在,返回0
127.0.0.1:6379> get key1
"linux"
127.0.0.1:6379> SETNX key4 d
(integer) 1 #key4不存在,返回1并创建key4为d
127.0.0.1:6379> get key4
"d"
127.0.0.1:6379> set key3 abc
OK
127.0.0.1:6379> SETEX key3 10 123 #给key3设置过期时间为10s,值为123
OK
127.0.0.1:6379> get key3 #key3已经存在,会覆盖为新值
"123"
- list相关操作
127.0.0.1:6379> LPUSH list3 a #LPUSH从左侧加入一个元素
(integer) 1
127.0.0.1:6379> LPUSH list3 b
(integer) 2
127.0.0.1:6379> LRANGE list3 0 -1
1) "b"
2) "a"
127.0.0.1:6379> LPOP list3 #LPOP从左侧取出第一个元素
"b"
127.0.0.1:6379> RPUSH list3 c #RPUSH从右侧加入一个元素
(integer) 2
127.0.0.1:6379> LRANGE list3 0 -1
1) "a"
2) "c"
127.0.0.1:6379> RPOP list3 #RPOP从右侧取出第一个元素
"c"
127.0.0.1:6379> LRANGE list3 0 -1
1) "a"
127.0.0.1:6379> LPUSH list4 aa bb cc dd
(integer) 4
127.0.0.1:6379> LINSERT list4 before bb 123 #在链表list4中bb前面(左边)插入元素123
(integer) 5
127.0.0.1:6379> LRANGE list4 0 -1
1) "dd"
2) "cc"
3) "123"
4) "bb"
5) "aa"
127.0.0.1:6379> LSET list4 0 DD #把第1个元素修改为DD
OK
127.0.0.1:6379> LRANGE list4 0 -1
1) "DD"
2) "cc"
3) "123"
4) "bb"
5) "aa"
127.0.0.1:6379> LINDEX list4 4 #查看list4第5个元素
"aa"
127.0.0.1:6379> LLEN list4 #查看list4中有几个元素
(integer) 5
- set相关操作
[root@minglinux-01 /usr/local/src/redis-4.0.1] redis-server /etc/redis.conf
[root@minglinux-01 /usr/local/src/redis-4.0.1] redis-cli
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) "bbb"
2) "aaa"
127.0.0.1:6379> SADD seta ccc #向集合中放入元素
(integer) 1
127.0.0.1:6379> SPOP seta #随机取出一个元素,删除
"ccc"
127.0.0.1:6379> SMEMBERS seta #查看集合中的所有元素
1) "bbb"
2) "aaa"
127.0.0.1:6379> srem seta aaa #删除元素
(integer) 1
127.0.0.1:6379> SMEMBERS seta
1) "bbb"
127.0.0.1:6379> SADD setb aaa
(integer) 1
127.0.0.1:6379> SADD setb bbb
(integer) 1
127.0.0.1:6379> SADD setb ccc
(integer) 1
127.0.0.1:6379> SMEMBERS setb
1) "bbb"
2) "ccc"
3) "aaa"
127.0.0.1:6379> SMEMBERS seta
1) "bbb"
2) "ddd"
127.0.0.1:6379> SDIFF seta setb #求差集,以seta为标准
1) "ddd"
127.0.0.1:6379> SDIFFSTORE setc seta setb #求差集并将结果存储到了setc里
(integer) 1
127.0.0.1:6379> SMEMBERS setc
1) "ddd"
127.0.0.1:6379> SINTER seta setb #求交集
1) "bbb"
127.0.0.1:6379> SINTERSTORE setd seta setb #将交集存储到setd
(integer) 1
127.0.0.1:6379> SMEMBERS setd
1) "bbb"
127.0.0.1:6379> SUNION seta setb #求并集
1) "bbb"
2) "ccc"
3) "ddd"
4) "aaa"
127.0.0.1:6379> SUNIONSTORE sete seta setb #求并集并存储到sete
(integer) 4
127.0.0.1:6379> SMEMBERS sete
1) "bbb"
2) "ccc"
3) "ddd"
4) "aaa"
127.0.0.1:6379> SMEMBERS seta #判断一个元素是否属于一个集合
1) "bbb"
2) "ddd"
127.0.0.1:6379> SISMEMBER seta aaa
(integer) 0 #不在则返回0
127.0.0.1:6379> SISMEMBER seta bbb
(integer) 1 #在则返回1
127.0.0.1:6379> SRANDMEMBER seta #随机取出一个元素,但不删除
"bbb"
127.0.0.1:6379> SMEMBERS seta
1) "bbb"
2) "ddd"
- sort set相关操作
zadd zseta 11 123 //创建有序集合
zrange zseta 0 -1 //显示所有元素,按顺序显示
zrange zseta 0 -1 withscores //可以带上分值
zrem zseta 222 //删除指定元素
zrank zseta 222 //返回元素的索引值,索引值从0开始,按score正向排序
zrevrank zseta 222 //同上,不同的是,按score反序排序
zrevrange zseta 0 -1 //反序显示所有元素,并带分值
zcard zseta //返回集合中所有元素的个数
zcount zseta 1 10 // 返回分值范围1-10的元素个数
zrangebyscore zseta 1 10 // 返回分值范围1-10的元素
zremrangebyrank zseta 0 2 //删除索引范围0-2的元素,按score正向排序
zremrangebyscore zseta 1 10 //删除分值范围1-10的元素
- hash 相关操作
hset user1 name xiaoming //建立hash
hset user1 age 7
hset user1 job it
hgetall user1 //打印user1所有信息
hmset user2 name xiaoming age 7 job it //批量建立键值对
hmget user2
hmget user2 name age job
hdel user2 job //删除指定filed
hkeys user2 //打印所有的key
hvals user2 //打印所有的values
hlen user2 //查看hash有几个filed
- Redis常用操作(键值)
keys * //取出所有key
keys my* //模糊匹配
exists name //有name键 返回1 ,否则返回0;
del key1 // 删除一个key,成功返回1 ,否则返回0;
EXPIRE key1 100 //设置key1 100s后过期
ttl key // 查看键 还有多长时间过期,单位是s,当 key 不存在时,返回 -2 。 当 key 存在但没有设置剩余生存时间时,返回 -1 。 否则,返回 key 的剩余生存时间。
select 0 //代表选择当前数据库,默认进入0 数据库
move age 1 // 把age 移动到1 数据库
persist key1 //取消key1的过期时间
randomkey //随机返回一个key
rename oldname newname //重命名key
type key1 //返回键的类型
- Redis常用操作(服务)
dbsize //返回当前数据库中key的数目
info //返回redis数据库状态信息
flushdb //清空当前数据库中所有的键
flushall //清空所有数据库中的所有的key
bgsave //保存数据到 rdb文件中,在后台运行
save //作用同上,但是在前台运行
config get * //获取所有配置参数
config get dir //获取配置参数
config set dir //更改配置参数
数据恢复: 首先定义或者确定dir目录和dbfilename,然后把备份的rdb文件放到dir目录下面,重启redis服务即可恢复数据
六、Redis安全设置
设置监听ip
bind 127.0.0.1 2.2.2.2 ##可以是多个ip,用空格分隔
设置监听端口
port 16000
连接
redis-cli -h 127.0.0.1 -p 16000
设置密码 #修改/etc/redis.conf
requirepass 123456>com
redis-cli -a '123456>com'
将config命令改名,让别人不易猜到进而利用
rename-command CONFIG ming
禁掉config命令
rename-command CONFIG ""
[root@minglinux-01 ~] vim /etc/redis.conf #修改配置文件设置密码
[root@minglinux-01 ~] systemctl restart redis
Failed to restart redis.service: Unit not found.
[root@minglinux-01 ~]
[root@minglinux-01 ~] killall redis-server
[root@minglinux-01 ~] redis-server /etc/redis.conf
[root@minglinux-01 ~] redis-cli
127.0.0.1:6379> keys *
(error) NOAUTH Authentication required. #没有权限查看
[root@minglinux-01 ~] redis-cli -a '123456>com' #使用密码登录
127.0.0.1:6379> keys *
1) "setb"
2) "key2"
3) "set3"
4) "key4"
5) "hash1"
6) "list4"
7) "list3"
8) "mykey"
9) "set1"
10) "set2"
11) "setd"
12) "sete"
13) "setc"
14) "key1"
15) "list1"
16) "seta"
七、Redis慢查询日志
编辑配置文件/etc/redis.conf
针对慢查询日志,可以设置两个参数,一个是执行时长,单位是微秒,另一个是慢查询日志的长度。当一个新的命令被写入日志时,最老的一条会从命令日志队列中被移除。
默认设置:
slowlog-log-slower-than 10000 //单位ms,表示慢于10000ms则记录日志
slowlog-max-len 128 //定义日志长度,表示最多存128条
慢查询操作:
slowlog get //列出所有的慢查询日志,可以修改配置文件慢于10ms记录日志来测试,需重启redis
slowlog get 2 //只列出2条
slowlog len //查看慢查询日志条数
八、PHP中使用redis – 安装扩展模块
#下载并解压安装包
[root@minglinux-01 /usr/local/src] wget https://coding.net/u/aminglinux/p/yuanke_centos7/git/raw/master/21NOSQL/phpredis.zip
[root@minglinux-01 /usr/local/src] unzip phpredis.zip
#安装
[root@minglinux-01 /usr/local/src] cd phpredis-develop/
[root@minglinux-01 /usr/local/src/phpredis-develop] /usr/local/php-fpm/bin/phpize #生成./configure 文件
[root@minglinux-01 /usr/local/src/phpredis-develop] ./configure --with-php-config=/usr/local/php-fpm/bin/php-config
[root@minglinux-01 /usr/local/src/phpredis-develop] make
[root@minglinux-01 /usr/local/src/phpredis-develop] make install
#修改/usr/local/php.ini配置文件,增加extension=redis.so
[root@minglinux-01 /usr/local/src/phpredis-develop] vim /usr/local/php-fpm/etc/php.ini
#重启php-fpm服务,检查是否正常加载redis模块
[root@minglinux-01 ~] /etc/init.d/php-fpm reload
[root@minglinux-01 ~] /usr/local/php-fpm/sbin/php-fpm -m | grep redis
redis
九、PHP中使用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_value session.save_path " tcp://127.0.0.1:6379"
或者php-fpm配置文件对应的pool中增加:
php_value[session.save_handler] = redis
php_value[session.save_path] = " tcp://127.0.0.1:6379 "
- 修改配置文件
[root@minglinux-01 ~] vim /usr/local/php-fpm/etc/php-fpm.d/ming.conf
#修改两行如下:
···
13 php_value[session.save_handler] = redis
14 php_value[session.save_path] = "tcp://127.0.0.1:6379"
···
# 修改完重启php-fpm服务
/etc/init.d/php-fpm reload
- 测试
#为了方便测试,需要取消redis的密码验证
[root@minglinux-01 ~] vim /etc/redis.conf
#requirepass 123456>com
[root@minglinux-01 ~] killall redis-server
redis-server: no process found
[root@minglinux-01 ~] redis-server /etc/redis.conf
[root@minglinux-01 ~] cd /data/wwwroot/default/ #站点根目录
[root@minglinux-01 /data/wwwroot/default] ls
1.php index.html
[root@minglinux-01 /data/wwwroot/default] cat 1.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();
?>
[root@minglinux-01 /data/wwwroot/default] curl localhost/1.php #访问测试页
1550909799<br><br>1550909799<br><br>6od0en830spiddnd6su1pnv2c6
[root@minglinux-01 /data/wwwroot/default] curl localhost/1.php
1550909801<br><br>1550909801<br><br>6nncc6vhrlc10193ie9qvdm097
[root@minglinux-01 /data/wwwroot/default] curl localhost/1.php
1550909802<br><br>1550909802<br><br>76mrrc38cmjrbf3is86abs8ee1
[root@minglinux-01 /data/wwwroot/default] redis-cli #命令行连接redis,可以查看到该key以及对应的值
127.0.0.1:6379> keys *
1) "list1"
2) "list4"
3) "sete"
4) "key2"
5) "mykey"
6) "setd"
7) "PHPREDIS_SESSION:6od0en830spiddnd6su1pnv2c6"
8) "set3"
9) "set1"
10) "seta"
11) "PHPREDIS_SESSION:6nncc6vhrlc10193ie9qvdm097"
12) "hash1"
13) "PHPREDIS_SESSION:76mrrc38cmjrbf3is86abs8ee1"
14) "key1"
15) "set2"
16) "setb"
17) "list3"
18) "key4"
19) "setc"
#如果想用php连接redis cluster,需要使用predis扩展
#安装方法类似phpredis,predis扩展地址https://github.com/nrk/predis
十、Redis主从配置
为了节省资源,我们可以在一台机器上启动两个redis服务
cp /etc/redis.conf /etc/redis2.conf
vim /etc/redis2.conf //需要修改port,dir,pidfile,logfile
还要增加一行
slaveof 127.0.0.1 6379
如果主上设置了密码,还需要增加
masterauth password>com //设置主的密码
启动之前不要忘记创建新的dir目录
redis-server /etc/redis2.conf
测试:在主上创建新的key,在从上查看
注意:redis主从和mysql主从不一样,redis主从不用事先同步数据,它会自动同步过去
[root@minglinux-01 ~] cp /etc/redis.conf /etc/redis2.conf #拷贝一下配置文件
[root@minglinux-01 ~] vim !$
vim /etc/redis2.conf
#编辑redis2.conf修改如下4行,增加1行
port 6380
pidfile /var/run/redis_6380.pid
logfile "/var/log/redis2.log"
dir /data/redis2
slaveof 127.0.0.1 6379 #增加改行
#如果主上设置了密码,还需要增加
masterauth password>com //password是主的密码
[root@minglinux-01 ~] mkdir /data/redis2 #创建从的dir目录
[root@minglinux-01 ~] redis-server /etc/redis2.conf #启动redis2服务
[root@minglinux-01 ~] ps aux |grep redis
root 2080 0.1 0.5 147344 9864 ? Ssl 12:01 0:14 redis-server 127.0.0.1:6379
root 2947 0.3 0.5 147344 9744 ? Ssl 15:34 0:00 redis-server 127.0.0.1:6380
root 2954 0.0 0.0 112724 980 pts/1 S+ 15:34 0:00 grep --color=auto redis
[root@minglinux-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:25 0.0.0.0:* LISTEN 1491/master
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 2045/nginx: master
tcp 0 0 0.0.0.0:39004 0.0.0.0:* LISTEN 891/rpc.statd
tcp 0 0 0.0.0.0:2049 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:10051 0.0.0.0:* LISTEN 918/zabbix_server
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 2080/redis-server 1
tcp 0 0 127.0.0.1:6380 0.0.0.0:* LISTEN 2947/redis-server 1
tcp 0 0 0.0.0.0:42767 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 2045/nginx: master
tcp 0 0 0.0.0.0:20048 0.0.0.0:* LISTEN 930/rpc.mountd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 879/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1491/master
tcp6 0 0 :::36414 :::* LISTEN -
tcp6 0 0 :::2049 :::* LISTEN -
tcp6 0 0 :::10051 :::* LISTEN 918/zabbix_server
tcp6 0 0 :::3306 :::* LISTEN 1483/mysqld
tcp6 0 0 :::39786 :::* LISTEN 891/rpc.statd
tcp6 0 0 :::111 :::* LISTEN 1/systemd
tcp6 0 0 :::20048 :::* LISTEN 930/rpc.mountd
tcp6 0 0 :::22 :::* LISTEN 879/sshd
#测试
[root@minglinux-01 ~] redis-cli -h 127.0.0.1 -p 6380
127.0.0.1:6380> keys * #从上实时自动同步主上的数据
1) "sete"
2) "set1"
3) "setc"
4) "list1"
5) "mykey"
6) "PHPREDIS_SESSION:utup3roqgk9m2ens1sv7bb9015"
7) "key4"
8) "key2"
9) "seta"
10) "setb"
11) "hash1"
12) "set2"
13) "setd"
14) "set3"
15) "key1"
16) "list3"
17) "list4"
127.0.0.1:6380> set key1 111 #配置文件中规定slave-read-only yes,slave只能读不能写
(error) READONLY You can't write against a read only slave.
十一、Redis集群
1.Redis集群简介
多个redis节点网络互联,数据共享
所有的节点都是一主一从(可以是多个从),其中从不提供服务,仅作为备用
不支持同时处理多个键(如mset/mget),因为redis需要把键均匀分布在各个节点上,并发量很高的情况下同时创建键值会降低性能并导致不可预测的行为。
支持在线增加、删除节点
客户端可以连任何一个主节点进行读写

2.Redis集群环境搭建
场景设置:
两台机器,分别开启三个Redis服务(端口)
A机器上三个端口7000,7002,7004,全部为主
B机器上三个端口7001,7003,7005,全部为从
都要关闭防火墙,和selinux
两台机器上都要编译安装redis,然后编辑并复制3个不同的redis.conf,分别设置不同的端口号、dir等参数,还需要增加cluster相关参数,然后分别启动6个redis服务
具体redis配置文件到https://coding.net/u/aminglinux/p/yuanke_centos7/git/tree/master/21NOSQL下载或者查看
#A机器
[root@minglinux-01 ~] vim /etc/redis_7000.conf
[root@minglinux-01 ~] vim /etc/redis_7002.conf
[root@minglinux-01 ~] vim /etc/redis_7004.conf
[root@minglinux-01 ~] mkdir -p /data/redis_data/{7000,7002,7004}
[root@minglinux-01 ~] redis-server /etc/redis_7000.conf
3109:C 24 Feb 16:39:35.738 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3109:C 24 Feb 16:39:35.739 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=3109, just started
3109:C 24 Feb 16:39:35.739 # Configuration loaded
[root@minglinux-01 ~] redis-server /etc/redis_7002.conf
3114:C 24 Feb 16:39:59.005 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3114:C 24 Feb 16:39:59.005 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=3114, just started
3114:C 24 Feb 16:39:59.005 # Configuration loaded
[root@minglinux-01 ~] redis-server /etc/redis_7004.conf
3124:C 24 Feb 16:40:03.483 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3124:C 24 Feb 16:40:03.483 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=3124, just started
3124:C 24 Feb 16:40:03.483 # Configuration loaded
[root@minglinux-01 ~] ps aux | grep redis
root 2080 0.1 0.5 147344 9864 ? Ssl 12:01 0:20 redis-server 127.0.0.1:6379
root 2947 0.1 0.5 147344 9828 ? Ssl 15:34 0:05 redis-server 127.0.0.1:6380
root 3110 0.1 0.4 145300 7584 ? Ssl 16:39 0:00 redis-server 192.168.162.130:7000 [cluster]
root 3115 0.1 0.4 145300 7588 ? Ssl 16:39 0:00 redis-server 192.168.162.130:7002 [cluster]
root 3125 0.1 0.4 145300 7588 ? Ssl 16:40 0:00 redis-server 192.168.162.130:7004 [cluster]
root 3130 0.0 0.0 112724 980 pts/1 S+ 16:41 0:00 grep --color=auto redis
[root@minglinux-01 ~] iptables -nvL
Chain INPUT (policy ACCEPT 150K packets, 50M 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 147K packets, 50M bytes)
pkts bytes target prot opt in out source destination
[root@minglinux-01 ~] getenforce
Disabled
#B机器
#若未安装redis则先编译安装redis
[root@minglinux-02 ~] vim /etc/redis_7001.conf
[root@minglinux-02 ~] vim /etc/redis_7003.conf
[root@minglinux-02 ~] vim /etc/redis_7005.conf
[root@minglinux-02 ~] mkdir -p /data/redis_data/{7001,7003,7005}
[root@minglinux-02 ~] redis-server /etc/redis_7001.conf
28397:C 24 Feb 16:42:09.090 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
28397:C 24 Feb 16:42:09.090 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=28397, just started
28397:C 24 Feb 16:42:09.090 # Configuration loaded
[root@minglinux-02 ~] redis-server /etc/redis_7003.conf
28402:C 24 Feb 16:42:16.499 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
28402:C 24 Feb 16:42:16.500 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=28402, just started
28402:C 24 Feb 16:42:16.500 # Configuration loaded
[root@minglinux-02 ~] redis-server /etc/redis_7005.conf
28407:C 24 Feb 16:42:29.043 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
28407:C 24 Feb 16:42:29.043 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=28407, just started
28407:C 24 Feb 16:42:29.043 # Configuration loaded
[root@minglinux-02 ~] ps aux |grep redis
root 28398 0.1 0.4 145300 7592 ? Ssl 16:42 0:00 redis-server 192.168.162.132:7001 [cluster]
root 28403 0.1 0.4 145300 7584 ? Ssl 16:42 0:00 redis-server 192.168.162.132:7003 [cluster]
root 28408 0.3 0.4 145300 7584 ? Ssl 16:42 0:00 redis-server 192.168.162.132:7005 [cluster]
root 28413 0.0 0.0 112720 980 pts/1 S+ 16:42 0:00 grep --color=auto redis
[root@minglinux-02 ~] iptables -nvL
Chain INPUT (policy ACCEPT 167K packets, 242M 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 157K packets, 10M bytes)
pkts bytes target prot opt in out source destination
[root@minglinux-02 ~] getenforce
Disabled
安装ruby2.2:(master)
#当前系统yum源中的ruby2.0,我们需要安装2.2版本
#安装ruby2.2 (只需要一台机器上运行)
#安装yum开发工具组
[root@minglinux-01 ~] yum -y groupinstall "Development Tools"
#安装依赖包
[root@minglinux-01 ~] yum -y install gdbm-devel libdb4-devel libffi-devel libyaml libyaml-devel ncurses-devel openssl-devel readline-devel tcl-deve
#创建制作rpm包的目录
[root@minglinux-01 ~] mkdir -p rpmbuild/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS}
#下载Ruby的源码包
[root@minglinux-01 ~] wget http://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.3.tar.gz -P rpmbuild/SOURCES
#下载specs文件,用于制作rpm包
[root@minglinux-01 ~] wget https://raw.githubusercontent.com/tjinjin/automate-ruby-rpm/master/ruby22x.spec -P rpmbuild/SPECS
#制作rpm包
[root@minglinux-01 ~] rpmbuild -bb rpmbuild/SPECS/ruby22x.spec
#安装Ruby2.2
[root@minglinux-01 ~] yum -y localinstall rpmbuild/RPMS/x86_64/ruby-2.2.3-1.el7.centos.x86_64.rpm
#编译安装(这里我使用编译安装)
[root@minglinux-01 /usr/local/src] wget http://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.4.tar.gz
[root@minglinux-01 /usr/local/src] tar -zxvf ruby-2.2.3.tar.gz
[root@minglinux-01 /usr/local/src] cd ruby-2.2.4/
[root@minglinux-01 /usr/local/src/ruby-2.2.4] ./configure --prefix=/usr/local/ruby
[root@minglinux-01 /usr/local/src/ruby-2.2.4] make && make install
[root@minglinux-01 /usr/local/src/ruby-2.2.4] ruby -v
-bash: ruby: 未找到命令
[root@minglinux-01 /usr/local/src/ruby-2.2.4] /usr/local/ruby/bin/ruby --version
ruby 2.2.10p489 (2018-03-28 revision 63023) [x86_64-linux]
[root@minglinux-01 /usr/local/src/ruby-2.2.4] ln -s /usr/local/ruby/bin/ruby /usr/bin/ruby
3.Redis集群配置
#安装Redis配置集群的工具
[root@minglinux-01 /usr/local/src/ruby-2.2.3] gem install redis
-bash: /usr/bin/gem: 没有那个文件或目录
#我使用yum安装gem会使ruby版本变成2.0,所以我手动安装rubygems-3.0.2,参考https://rubygems.org/pages/download
[root@minglinux-01 /usr/local/sbin] ln -s /usr/local/ruby/bin/gem /usr/bin/gem
[root@minglinux-01 /usr/local/sbin] gem install redis
Successfully installed redis-4.1.0
Parsing documentation for redis-4.1.0
Done installing documentation for redis after 1 seconds
1 gem installed
#将redis-trib.rb加入环境变量目录下,方便直接使用
[root@minglinux-01 ~] cp /usr/local/src/redis-4.0.1/src/redis-trib.rb /usr/bin/
[root@minglinux-01 ~] redis-trib.rb create --replicas 1 192.168.162.130:7000 192.168.162.130:7002 192.168.162.130:7004 192.168.162.132:7001 192.168.162.132:7003 192.168.162.132:7005
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.162.130:7000
192.168.162.132:7001
192.168.162.130:7002
Adding replica 192.168.162.132:7003 to 192.168.162.130:7000
Adding replica 192.168.162.130:7004 to 192.168.162.132:7001
Adding replica 192.168.162.132:7005 to 192.168.162.130:7002
M: e8674ae599d4cda5ed03238a5f6e172d81e5752c 192.168.162.130:7000
slots:0-5460 (5461 slots) master
M: e7a1e4061c158d06afe0b52c61e8ada732591024 192.168.162.130:7002
slots:10923-16383 (5461 slots) master
S: 68d7203f01f1223149ee3610d6173488b3997136 192.168.162.130:7004
replicates b921960225c67bd494e517947d32c8841fc5d868
M: b921960225c67bd494e517947d32c8841fc5d868 192.168.162.132:7001
slots:5461-10922 (5462 slots) master
S: b38a01c21936ef19f699b7b8b0f4db1a54887c9a 192.168.162.132:7003
replicates e8674ae599d4cda5ed03238a5f6e172d81e5752c
S: 4acfd2a3405bc4a2d2093126ec1eb36e72f680cd 192.168.162.132:7005
replicates e7a1e4061c158d06afe0b52c61e8ada732591024
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.162.130:7000)
M: e8674ae599d4cda5ed03238a5f6e172d81e5752c 192.168.162.130:7000
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 68d7203f01f1223149ee3610d6173488b3997136 192.168.162.130:7004
slots: (0 slots) slave
replicates b921960225c67bd494e517947d32c8841fc5d868
M: e7a1e4061c158d06afe0b52c61e8ada732591024 192.168.162.130:7002
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: b38a01c21936ef19f699b7b8b0f4db1a54887c9a 192.168.162.132:7003
slots: (0 slots) slave
replicates e8674ae599d4cda5ed03238a5f6e172d81e5752c
S: 4acfd2a3405bc4a2d2093126ec1eb36e72f680cd 192.168.162.132:7005
slots: (0 slots) slave
replicates e7a1e4061c158d06afe0b52c61e8ada732591024
M: b921960225c67bd494e517947d32c8841fc5d868 192.168.162.132:7001
slots:5461-10922 (5462 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@minglinux-01 ~] echo $?
0
4.Redis集群操作
redis-cli -c -h 192.168.162.130 -p 7000//-c说明以集群的方式登录
任意一个节点都可以创建key,或者查看key(演示)
redis-trib.rb check 192.168.162.130:7000//检测集群状态
cluster nodes//列出节点
cluster info//查看集群信息
cluster meet ip port //添加节点
cluster forget node_id //移除某个节点
cluster replicate node_id//将当前节点设置为指定节点的从
cluster saveconfig//保存配置文件
- 创建数据:
[root@minglinux-01 ~] redis-cli -c -h 192.168.162.130 -p 7000
#-c:=cluster,表示以集群方式连接
#不以集群方式连接就相当于操作单机的redis
192.168.162.130:7000> set key5 225
-> Redirected to slot [9057] located at 192.168.162.130:7004
OK ##该操作被重定向到192.168.162.130:7004
192.168.162.130:7004> set key6 225
-> Redirected to slot [4866] located at 192.168.162.130:7000
OK #重定向
192.168.162.130:7000> set key7 225
OK #无提示则是在本地创建
192.168.162.130:7000> set key8 225
-> Redirected to slot [13004] located at 192.168.162.130:7002
OK
- 查看数据:
192.168.162.130:7004> get key8
-> Redirected to slot [13004] located at 192.168.162.130:7002
"225"
192.168.162.130:7002> get key7
-> Redirected to slot [803] located at 192.168.162.130:7000
"225"
192.168.162.130:7000> get key6
"225"
192.168.162.130:7000> get key5
-> Redirected to slot [9057] located at 192.168.162.130:7004
"225"
- 检测集群状态:
[root@minglinux-01 ~] redis-trib.rb check 192.168.162.130:7002
>>> Performing Cluster Check (using node 192.168.162.130:7002)
M: e7a1e4061c158d06afe0b52c61e8ada732591024 192.168.162.130:7002
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: b921960225c67bd494e517947d32c8841fc5d868 192.168.162.132:7001
slots: (0 slots) slave
replicates 68d7203f01f1223149ee3610d6173488b3997136
S: 4acfd2a3405bc4a2d2093126ec1eb36e72f680cd 192.168.162.132:7005
slots: (0 slots) slave
replicates e7a1e4061c158d06afe0b52c61e8ada732591024
M: 68d7203f01f1223149ee3610d6173488b3997136 192.168.162.130:7004
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: e8674ae599d4cda5ed03238a5f6e172d81e5752c 192.168.162.130:7000
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: b38a01c21936ef19f699b7b8b0f4db1a54887c9a 192.168.162.132:7003
slots: (0 slots) slave
replicates e8674ae599d4cda5ed03238a5f6e172d81e5752c
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
- 列出所有节点:
[root@minglinux-01 ~] redis-cli -c -h 192.168.162.130 -p 7000
192.168.162.130:7000> CLUSTER NODES
68d7203f01f1223149ee3610d6173488b3997136 192.168.162.130:7004@17004 master - 0 1551078312879 7 connected 5461-10922
e8674ae599d4cda5ed03238a5f6e172d81e5752c 192.168.162.130:7000@17000 myself,master - 0 1551078311000 1 connected 0-5460
4acfd2a3405bc4a2d2093126ec1eb36e72f680cd 192.168.162.132:7005@17005 slave e7a1e4061c158d06afe0b52c61e8ada732591024 0 1551078311871 6 connected
e7a1e4061c158d06afe0b52c61e8ada732591024 192.168.162.130:7002@17002 master - 0 1551078309853 2 connected 10923-16383
b921960225c67bd494e517947d32c8841fc5d868 192.168.162.132:7001@17001 slave 68d7203f01f1223149ee3610d6173488b3997136 0 1551078310863 7 connected
b38a01c21936ef19f699b7b8b0f4db1a54887c9a 192.168.162.132:7003@17003 slave e8674ae599d4cda5ed03238a5f6e172d81e5752c 0 1551078310000 5 connected
- 查看集群信息:
192.168.162.130: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:7
cluster_my_epoch:1
cluster_stats_messages_ping_sent:1979
cluster_stats_messages_pong_sent:1951
cluster_stats_messages_sent:3930
cluster_stats_messages_ping_received:1951
cluster_stats_messages_pong_received:1971
cluster_stats_messages_received:3922
- 添加节点:
#先在slave创建redis_7007.conf并启动
[root@minglinux-02 ~] cd /etc/
[root@minglinux-02 /etc] cp redis_7001.conf redis_7007.conf
[root@minglinux-02 /etc] vim !$
vim redis_7007.conf
port 7007
bind 192.168.162.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@minglinux-02 /etc] mkdir /data/redis_data/7007
[root@minglinux-02 /etc] redis-server /etc/redis_7007.conf
79937:C 25 Feb 15:12:45.345 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
79937:C 25 Feb 15:12:45.345 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=79937, just started
79937:C 25 Feb 15:12:45.345 # Configuration loaded
192.168.162.130:7000> CLUSTER MEET 192.168.162.132 7007 #master上执行添加节点操作
OK
192.168.162.130:7000> CLUSTER NODES #7007是master身份
68d7203f01f1223149ee3610d6173488b3997136 192.168.162.130:7004@17004 master - 0 1551079130022 7 connected 5461-10922
e8674ae599d4cda5ed03238a5f6e172d81e5752c 192.168.162.130:7000@17000 myself,master - 0 1551079125000 1 connected 0-5460
9457a06ff5bd90d702a05fc6fcf93616338be609 192.168.162.132:7007@17007 master - 0 1551079128000 0 connected
4acfd2a3405bc4a2d2093126ec1eb36e72f680cd 192.168.162.132:7005@17005 slave e7a1e4061c158d06afe0b52c61e8ada732591024 0 1551079127001 6 connected
e7a1e4061c158d06afe0b52c61e8ada732591024 192.168.162.130:7002@17002 master - 0 1551079125991 2 connected 10923-16383
b921960225c67bd494e517947d32c8841fc5d868 192.168.162.132:7001@17001 slave 68d7203f01f1223149ee3610d6173488b3997136 0 1551079128009 7 connected
b38a01c21936ef19f699b7b8b0f4db1a54887c9a 192.168.162.132:7003@17003 slave e8674ae599d4cda5ed03238a5f6e172d81e5752c 0 1551079129016 5 connected
再添加一个节点:
[root@minglinux-01 /etc] cp redis_7000.conf redis_7006.conf
[root@minglinux-01 /etc] vim !$
vim redis_7006.conf
1 port 7006
2 bind 192.168.162.130
3 daemonize yes
4 pidfile /var/run/redis_7006.pid
5 dir /data/redis_data/7006
6 cluster-enabled yes
7 cluster-config-file nodes_7006.conf
8 cluster-node-timeout 10100
9 appendonly yes
[root@minglinux-01 /etc] mkdir /data/redis_data/7006
[root@minglinux-01 /etc] redis-server /etc/redis_7006.conf
8517:C 25 Feb 15:24:31.574 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
8517:C 25 Feb 15:24:31.574 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=8517, just started
8517:C 25 Feb 15:24:31.574 # Configuration loaded
[root@minglinux-01 /etc] redis-cli -c -h 192.168.162.130 -p 7000
192.168.162.130:7000> CLUSTER MEET 192.168.162.130 7006
OK
192.168.162.130:7000> CLUSTER NODES #新添加的7006也是master身份,使用该方式添加的新节点都是以master身份存在
8ed5022913f6242843c3e47df21b0db7fb35c32c 192.168.162.130:7006@17006 master - 0 1551079548000 0 connected
68d7203f01f1223149ee3610d6173488b3997136 192.168.162.130:7004@17004 master - 0 1551079544727 7 connected 5461-10922
e8674ae599d4cda5ed03238a5f6e172d81e5752c 192.168.162.130:7000@17000 myself,master - 0 1551079545000 1 connected 0-5460
9457a06ff5bd90d702a05fc6fcf93616338be609 192.168.162.132:7007@17007 master - 0 1551079546000 0 connected
4acfd2a3405bc4a2d2093126ec1eb36e72f680cd 192.168.162.132:7005@17005 slave e7a1e4061c158d06afe0b52c61e8ada732591024 0 1551079546141 6 connected
e7a1e4061c158d06afe0b52c61e8ada732591024 192.168.162.130:7002@17002 master - 0 1551079547753 2 connected 10923-16383
b921960225c67bd494e517947d32c8841fc5d868 192.168.162.132:7001@17001 slave 68d7203f01f1223149ee3610d6173488b3997136 0 1551079548762 7 connected
b38a01c21936ef19f699b7b8b0f4db1a54887c9a 192.168.162.132:7003@17003 slave e8674ae599d4cda5ed03238a5f6e172d81e5752c 0 1551079547000 5 connected
- 将当前节点设置为指定节点的从:
#将7006设置为7007的从
[root@minglinux-01 /etc] redis-cli -c -h 192.168.162.130 -p 7006
192.168.162.130:7006> CLUSTER REPLICATE 9457a06ff5bd90d702a05fc6fcf93616338be609 #7007的node_id
OK
192.168.162.130:7006> CLUSTER NODES
8ed5022913f6242843c3e47df21b0db7fb35c32c 192.168.162.130:7006@17006 myself,slave 9457a06ff5bd90d702a05fc6fcf93616338be609 0 1551079862000 8 connected
68d7203f01f1223149ee3610d6173488b3997136 192.168.162.130:7004@17004 master - 0 1551079863090 7 connected 5461-10922
4acfd2a3405bc4a2d2093126ec1eb36e72f680cd 192.168.162.132:7005@17005 slave e7a1e4061c158d06afe0b52c61e8ada732591024 0 1551079860000 2 connected
e7a1e4061c158d06afe0b52c61e8ada732591024 192.168.162.130:7002@17002 master - 0 1551079862066 2 connected 10923-16383
b38a01c21936ef19f699b7b8b0f4db1a54887c9a 192.168.162.132:7003@17003 slave e8674ae599d4cda5ed03238a5f6e172d81e5752c 0 1551079862000 1 connected
b921960225c67bd494e517947d32c8841fc5d868 192.168.162.132:7001@17001 slave 68d7203f01f1223149ee3610d6173488b3997136 0 1551079862068 7 connected
9457a06ff5bd90d702a05fc6fcf93616338be609 192.168.162.132:7007@17007 master - 0 1551079861054 0 connected
e8674ae599d4cda5ed03238a5f6e172d81e5752c 192.168.162.130:7000@17000 master - 0 1551079864099 1 connected 0-5460
- 移除某节点:
192.168.162.130:7006> CLUSTER FORGET 9457a06ff5bd90d702a05fc6fcf93616338be609
(error) ERR Can't forget my master!
#不能移除master节点
192.168.162.130:7006> CLUSTER FORGET 8ed5022913f6242843c3e47df21b0db7fb35c32c
(error) ERR I tried hard but I can't forget myself...
#也不能移除当前所在节点
[root@minglinux-01 /etc] redis-cli -c -h 192.168.162.130 -p 7000
192.168.162.130:7000> CLUSTER FORGET 8ed5022913f6242843c3e47df21b0db7fb35c32c
OK
192.168.162.130:7000> CLUSTER NODES #7006节点已成功移除
68d7203f01f1223149ee3610d6173488b3997136 192.168.162.130:7004@17004 master - 0 1551080305000 7 connected 5461-10922
e8674ae599d4cda5ed03238a5f6e172d81e5752c 192.168.162.130:7000@17000 myself,master - 0 1551080305000 1 connected 0-5460
9457a06ff5bd90d702a05fc6fcf93616338be609 192.168.162.132:7007@17007 master - 0 1551080307000 0 connected
4acfd2a3405bc4a2d2093126ec1eb36e72f680cd 192.168.162.132:7005@17005 slave e7a1e4061c158d06afe0b52c61e8ada732591024 0 1551080308596 6 connected
e7a1e4061c158d06afe0b52c61e8ada732591024 192.168.162.130:7002@17002 master - 0 1551080305572 2 connected 10923-16383
b921960225c67bd494e517947d32c8841fc5d868 192.168.162.132:7001@17001 slave 68d7203f01f1223149ee3610d6173488b3997136 0 1551080307587 7 connected
b38a01c21936ef19f699b7b8b0f4db1a54887c9a 192.168.162.132:7003@17003 slave e8674ae599d4cda5ed03238a5f6e172d81e5752c 0 1551080306580 5 connected
- 保存配置文件
192.168.162.130:7000> CLUSTER SAVECONFIG
OK
#保存后会在每个节点的dir目录下生成一个 .conf 结尾的配置文件
[root@minglinux-01 /etc] cat /data/redis_data/7000/nodes_7000.conf
8ed5022913f6242843c3e47df21b0db7fb35c32c 192.168.162.130:7006@17006 slave 9457a06ff5bd90d702a05fc6fcf93616338be609 0 1551080431701 0 connected
68d7203f01f1223149ee3610d6173488b3997136 192.168.162.130:7004@17004 master - 0 1551080430000 7 connected 5461-10922
e8674ae599d4cda5ed03238a5f6e172d81e5752c 192.168.162.130:7000@17000 myself,master - 0 1551080431000 1 connected 0-5460
9457a06ff5bd90d702a05fc6fcf93616338be609 192.168.162.132:7007@17007 master - 0 1551080430694 0 connected
4acfd2a3405bc4a2d2093126ec1eb36e72f680cd 192.168.162.132:7005@17005 slave e7a1e4061c158d06afe0b52c61e8ada732591024 0 1551080432709 6 connected
e7a1e4061c158d06afe0b52c61e8ada732591024 192.168.162.130:7002@17002 master - 0 1551080431000 2 connected 10923-16383
b921960225c67bd494e517947d32c8841fc5d868 192.168.162.132:7001@17001 slave 68d7203f01f1223149ee3610d6173488b3997136 0 1551080429080 7 connected
b38a01c21936ef19f699b7b8b0f4db1a54887c9a 192.168.162.132:7003@17003 slave e8674ae599d4cda5ed03238a5f6e172d81e5752c 0 1551080429000 5 connected
vars currentEpoch 8 lastVoteEpoch 7
网友评论