Redis和Memcached相似,也是NoSQL的一種,是一個基於內存的高性能key-value(k-v)數據庫。php
Redis支持string
、list
、set
、zset
(sorted set
)和hash
類型數據,這些數據類型都支持push/pop、add/remove及取交集、並集和差集及更豐富的操做,對這些數據的操做都是原子性的。html
與memcached同樣,爲了保證效率,數據都是緩存在內存中。區別的是redis會週期性的把更新的數據寫入磁盤或者把修改操做寫入追加的記錄文件,而且在此基礎上實現了master-slave(主從)同步。redis的出現,很大程度補償了memcached這類key/value存儲的不足,在部 分場合能夠對關係數據庫起到很好的補充做用。它提供了Java,C/C++,C#,PHP,JavaScript,Perl,Object-C,Python,Ruby,Erlang等客戶端,使用很方便。node
數據能夠從主服務器向任意數量的從服務器上同步,從服務器能夠是關聯其餘從服務器的主服務器。這使得Redis可執行單層樹複製。存盤能夠有意無心的對數據進行寫操做。因爲徹底實現了發佈/訂閱機制,使得從數據庫在任何地方同步樹時,可訂閱一個頻道並接收主服務器完整的消息發佈記錄。同步對讀取操做的可擴展性和數據冗餘頗有幫助。mysql
此外,Redis使用了兩種文件格式:全量數據(RDB)和增量請求(aof)。全量數據格式是把內存中的數據寫入磁盤,便於下次讀取文件時進行加載;增量請求則是把內存中的數據序列轉化爲操做請求,用於讀取文件進行replay獲得數據,這種相似於mysql的binlog。Redis的存儲分爲內存存儲、磁盤存儲和log文件三部分。linux
Redis官網:https://redis.io/ ,最新版本4.0.11nginx
# cd /usr/local/src/# wget http://download.redis.io/releases/redis-4.0.11.tar.gz# tar zxf redis-4.0.11.tar.gz# cd redis-4.0.11# make && make install# echo $?0# redis- #按兩次Tab鍵出現下面內容就說明安裝成功redis-benchmark redis-check-rdb redis-sentinel redis-check-aof redis-cli redis-server
# cp redis.conf /etc/redis.conf# vim /etc/redis.conf #作下面更改daemonize yes #後臺啓動logfile "/var/log/redis.log" #設置日誌存放路徑dir /data/redis #設置RDB存放目錄改成 appendonly yes# mkdir /data/redis
# redis-server /etc/redis.conf# ps aux |grep redisroot 11920 0.2 0.2 145308 2188 ? Ssl 22:51 0:00 redis-server 127.0.0.1:6379# less /var/log/redis.log #提示修改內核參數,若是不去修改也沒很大影響11920:M 20 Jul 22:51:40.532 # 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 's ysctl vm.overcommit_memory=1' for this to take effect. 11920:M 20 Jul 22:51:40.533 # 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.# vim /etc/rc.local #修改內核參數,並開機執行sysctl vm.overcommit_memory=1echo never > /sys/kernel/mm/transparent_hugepage/enabled
Redis提供了兩種持久化的方式:RDB
(Redis DataBase)和AOF
Append Only File)。git
RDB,簡而言之,就是在不一樣的時間點,將redis存儲的數據生成快照並存儲到磁盤等介質上。github
AOF,則是將redis執行過的全部寫指令記錄下來,在下次redis從新啓動時,只要把這些寫指令從前到後再重複執行一遍,就能夠實現數據恢復了。redis
RDB和AOF兩種方式也能夠同時使用,在這種狀況下,若是redis重啓,則會優先採用AOF方式來進行數據恢復,這是由於AOF方式的數據恢復完整度更高。若是沒有數據持久化的需求,也能夠徹底關閉RDB和AOF,這樣的話,redis將變成一個純內存數據庫,就像memcached同樣。算法
# cat /etc/redis.conf save 900 1 #表示900s內發生了1次更改save 300 10 #表示300s內發生了10次更改save 60 10000 #表示60s內發生了10000次更改
決定RDB何時將數據寫入磁盤是由這些參數決定的。三選一,咱們就保持默認便可。
# cat /etc/redis.conf #作下面更改save ""#save 900 1#save 300 10#save 60 10000
# cat /etc/redis.conf# appendfsync always #一直記錄,每次有變動就記錄appendfsync everysec #每秒記錄一次# appendfsync no #每隔一段時間記錄一次,根據系統裏面算法決定,不安全
前面已經講過,Redis的數據類型有五種:string
、list
、set
、zset
、hash
string
是最簡單的類型,與Memcached同樣的類型,一個key對應一個value,其支持的操做與Memcached的操做相似,不過功能更豐富,能夠存二進制的對象。
# redis-cli #進入redis命令行127.0.0.1:6379> set mykey "123" OK 127.0.0.1:6379> get mykey"123"127.0.0.1:6379> MSET k1 1 k2 2 k3 3 #支持Tab鍵將命令能夠自動補全OK 127.0.0.1:6379> mget k1 k2 k3 mykey 1) "1"2) "2"3) "3"4) "123"
list
是一個鏈表結構,主要功能是push、pop、獲取一個範圍的全部值等。操做中key可理解爲鏈表的名字。
使用list結構,能夠輕鬆實現最新消息排行等功能(好比新浪微博的TimeLine)。list的另外一個應用就是消息隊列,能夠利用list的push操做將任務存在list中,而後工做線程再用pop操做將任務取出執行。
127.0.0.1:6379> LPUSH list1 "lzx" #LPUSH表示推動去(integer) 1 127.0.0.1:6379> LPUSH list1 "123"(integer) 2 127.0.0.1:6379> LPUSH list1 "aaa"(integer) 3 127.0.0.1:6379> LRANGE list1 0 -1 #0表示第一位,-1表示最後一位1) "aaa"2) "123"3) "lzx" #最早推動去的排在後面127.0.0.1:6379> LPOP list1 #LPOP表示取出來,取出最前面的"aaa"127.0.0.1:6379> LRANGE list1 0 -1 1) "123"2) "lzx" #取出來的數據就不在list1中了
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) "b"3) "c"127.0.0.1:6379> SADD set2 1(integer) 1 127.0.0.1:6379> SADD set2 2(integer) 1 127.0.0.1:6379> SADD set2 c(integer) 1 127.0.0.1:6379> SMEMBERS set2 1) "2"2) "c"3) "1"127.0.0.1:6379> SUNION set1 set2 #SUNION表示求並集1) "2"2) "b"3) "c"4) "a"5) "1"127.0.0.1:6379> SINTER set1 set2 #SINTER表示求交集1) "c"127.0.0.1:6379> SDIFF set1 set2 #SDIFF表示求差集,針對set1求差集,去除交集的部分1) "a"2) "b"127.0.0.1:6379> SREM set1 b #SREM表示刪除元素(integer) 1 127.0.0.1:6379> SMEMBERS set1 1) "a"2) "c"
zset
是有序集合,它比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 "cde 123"(integer) 1 127.0.0.1:6379> ZADD set3 24 "lzx"(integer) 1 127.0.0.1:6379> ZADD set3 4 "linux"(integer) 1 127.0.0.1:6379> ZRANGE set3 0 -1 #順序排列set3,0表示第一位,-1表示最後一位1) "cde 123"2) "linux"3) "abc"4) "lzx"127.0.0.1:6379> ZREVRANGE set3 0 -1 #倒序排列set31) "lzx"2) "abc"3) "linux"4) "cde 123"
hash
,Redis和Memcached相似,在Memcached中,咱們常常將一些結構化的信息打包成hashmap,在客戶端序列化後存儲爲一個字符串的值(通常是JSON格式),好比用戶的暱稱、年齡、性別、積分等。
127.0.0.1:6379> HSET hash1 name lzx (integer) 1 127.0.0.1:6379> HSET hash1 age 20(integer) 1 127.0.0.1:6379> HSET hash1 job it //添加了3對子元素,每一對子元素均可以看作是一個key-value(integer) 1 127.0.0.1:6379> HSET hash1 name lzx(integer) 1 127.0.0.1:6379> HSET hash1 age 20(integer) 1 127.0.0.1:6379> HSET hash1 job it(integer) 1 127.0.0.1:6379> HGET hash1 name #查看hash1中name"lzx"127.0.0.1:6379> HGET hash1 age"20"127.0.0.1:6379> HGET hash1 job"it"127.0.0.1:6379> HGETALL hash1 #查看hash1所有1) "name"2) "lzx"3) "age"4) "20"5) "job"6) "it"
127.0.0.1:6379> set key1 lzx OK 127.0.0.1:6379> set key2 123 OK 127.0.0.1:6379> set key1 aaa OK 127.0.0.1:6379> get key1 "aaa" #第二次賦值將以前賦值覆蓋
127.0.0.1:6379> SETNX key1 linux #key1存在,因此返回0(integer) 0 127.0.0.1:6379> get key1 "aaa" #而且不改變key1的value127.0.0.1:6379> SETNX key3 111 #key3不存在,因此直接建立,返回1(integer) 1 127.0.0.1:6379> get key3"111"
127.0.0.1:6379> set key3 111 ex 10 #給key3設置過時時間,爲10s,set時ex不可省略OK 127.0.0.1:6379> get key3 #過10s以後查看,value爲空(nil)127.0.0.1:6379> SETEX key3 1000 222 #SETEX設置過時時間爲1000s,後面222爲value,若key3已存在則覆蓋OK 127.0.0.1:6379> get key3"222"
127.0.0.1:6379> LPUSH list2 aaa(integer) 1 127.0.0.1:6379> LPUSH list2 bbb(integer) 2 127.0.0.1:6379> LPUSH list2 ccc(integer) 3 127.0.0.1:6379> LRANGE list2 0 -1 1) "ccc"2) "bbb"3) "aaa"127.0.0.1:6379> LPOP list2 #取出的是最後加入的值"ccc"127.0.0.1:6379> LRANGE list2 0 -1 #取出的值已經不在list2中1) "bbb"2) "aaa"127.0.0.1:6379> RPOP list2 #取出最早加入的值,跟LPOP相反"aaa"
127.0.0.1:6379> LRANGE list2 0 -1 1) "bbb"127.0.0.1:6379> LINSERT list2 before bbb 111 #LINSERT表示插入,在bbb前面插入111(integer) 2 127.0.0.1:6379> LINSERT list2 after bbb ddd #在bbb後面插入ddd(integer) 3 127.0.0.1:6379> LINSERT list2 after ddd fff(integer) 4 127.0.0.1:6379> LRANGE list2 0 -1 1) "111"2) "bbb"3) "ddd"4) "fff"
127.0.0.1:6379> LSET list2 1 123 #LSET表示修改,將第2個值改成123OK 127.0.0.1:6379> LSET list2 3 abc #將第4個值改成abcOK 127.0.0.1:6379> LRANGE list2 0 -1 1) "111"2) "123"3) "ddd"4) "abc"
127.0.0.1:6379> LINDEX list2 1 #LINDEX表示查看,查看第2個值"123"127.0.0.1:6379> LINDEX list2 3 #查看第4個值"abc"
127.0.0.1:6379> LLEN list2 #LLEN查看列表中有幾個元素(integer) 4 #表示list2中有4個元素
127.0.0.1:6379> SADD set1 aaa #SADD表示添加元素,添加元素aaa(integer) 1 127.0.0.1:6379> SADD set1 bbb(integer) 1 127.0.0.1:6379> SADD set1 ccc(integer) 1 127.0.0.1:6379> SMEMBERS set1 #SMEMBERS表示查看集合中全部元素1) "bbb"2) "aaa"3) "ccc"
127.0.0.1:6379> SREM set1 aaa #SREM表示刪除元素,刪除元素aaa(integer) 1 127.0.0.1:6379> SMEMBERS set1 1) "bbb"2) "ccc"
127.0.0.1:6379> SPOP set1 1 #SPOP表示取出元素,1表示第2個元素1) "ccc"127.0.0.1:6379> SMEMBERS set1 1) "bbb" #取出的元素就再也不在set1中
127.0.0.1:6379> SADD set1 aaa(integer) 1 127.0.0.1:6379> SADD set1 ccc(integer) 1 127.0.0.1:6379> SADD set1 222(integer) 1 127.0.0.1:6379> SMEMBERS set1 1) "bbb"2) "aaa"3) "222"4) "ccc"127.0.0.1:6379> SADD set2 aaa(integer) 1 127.0.0.1:6379> SADD set2 ccc(integer) 1 127.0.0.1:6379> SADD set2 111(integer) 1 127.0.0.1:6379> SMEMBERS set2 1) "aaa"2) "111"3) "ccc"127.0.0.1:6379> SDIFF set1 set2 #SDIFF表示求差集,set1在前面就是以set1爲標準1) "bbb"2) "222"127.0.0.1:6379> SDIFF set2 set1 1) "111"
127.0.0.1:6379> SDIFFSTORE set3 set1 set2 #SDIFFSTORE表示求出差集並保存到新集合中,set3爲新集合(integer) 2 127.0.0.1:6379> SMEMBERS set3 1) "bbb"2) "222"
127.0.0.1:6379> SINTER set1 set2 #SINTER表示求交集1) "aaa"2) "ccc"
127.0.0.1:6379> SINTERSTORE set4 set1 set2 #SINTERSTORE表示求交集並保存到新集合中,set4爲新集合(integer) 2 127.0.0.1:6379> SMEMBERS set4 1) "aaa"2) "ccc"
127.0.0.1:6379> SUNION set1 set2 #SUNION表示求並集1) "ccc"2) "222"3) "bbb"4) "aaa"5) "111"
127.0.0.1:6379> SUNIONSTORE set5 set1 set2 #SUNIONSTORE表示求並集並保存到新集合中,set5爲新集合(integer) 5 127.0.0.1:6379> SMEMBERS set5 1) "ccc"2) "222"3) "bbb"4) "aaa"5) "111"
127.0.0.1:6379> SISMEMBER set1 1 #SISMEMBER表示判斷一個元素是否在集合中(integer) 0 #返回0表示不存在127.0.0.1:6379> SISMEMBER set1 aaa(integer) 1 #返回1表示存在
127.0.0.1:6379> SRANDMEMBER set1 #SRANDMEMBER表示隨機取出元素,但不刪除"ccc"127.0.0.1:6379> SRANDMEMBER set1 2 #2表示隨機取出2個元素1) "ccc"2) "222"127.0.0.1:6379> SMEMBERS set1 1) "bbb"2) "aaa"3) "222"4) "ccc"
127.0.0.1:6379> ZADD zset1 11 123 #ZADD表示添加元素(integer) 1 127.0.0.1:6379> ZADD zset1 2 lab(integer) 1 127.0.0.1:6379> ZADD zset1 25 k (integer) 1 127.0.0.1:6379> ZRANGE zset1 0 -1 #ZRANGE表示查看元素,按順序顯示,0表示第一位,-1表示最後一位1) "lab"2) "123"3) "k"
127.0.0.1:6379> ZREM zset1 123 #ZREM表示刪除元素,123爲具體的值(輸入score(11)則不對)(integer) 1 127.0.0.1:6379> ZRANGE zset1 0 -1 1) "lab"2) "k"
127.0.0.1:6379> ZADD zset1 9 la(integer) 1 127.0.0.1:6379> ZADD zset1 100 sss(integer) 1 127.0.0.1:6379> ZRANGE zset1 0 -1 1) "lab"2) "la"3) "k"4) "sss"127.0.0.1:6379> ZRANK zset1 k #ZRANK返回元素的索引值,索引值從0開始,按sore正向排序,score即value前面的值,如la的sore是9,sss的sore爲100(integer) 2
127.0.0.1:6379> ZREVRANK zset1 k #ZREVRANK返回元素的索引值,索引值從0開始,按score反向排序(integer) 1
127.0.0.1:6379> ZREVRANGE zset1 0 -1 #ZREVRANGE表示反序排序1) "sss"2) "k"3) "la"4) "lab"
127.0.0.1:6379> ZCARD zset1 #ZCARD返回元素個數(integer) 4
127.0.0.1:6379> ZCOUNT zset1 1 10 #ZCOUNT返回score範圍中的元素個數,1 10表示範圍爲1-10(integer) 2 #1-10的元素個數爲2
127.0.0.1:6379> ZRANGEBYSCORE zset1 1 10 #ZRANGEBYSCORE返回score範圍中的元素1) "lab"2) "la"
127.0.0.1:6379> ZREMRANGEBYRANK zset1 0 1 #ZREMRANGEBYRANK表示刪除索引範圍中的元素,0 1表示索引從第一位到第二位(integer) 2 127.0.0.1:6379> ZRANGE zset1 0 -1 1) "k"2) "sss"
127.0.0.1:6379> ZADD zset1 6 111(integer) 1 127.0.0.1:6379> ZADD zset1 31 all(integer) 1 127.0.0.1:6379> ZRANGE zset1 0 -1 1) "111"2) "k"3) "all"4) "sss"127.0.0.1:6379> ZREMRANGEBYSCORE zset1 1 10 #ZREMRANGEBYSCORE表示刪除score(分值)範圍中的元素,1 10表示範圍爲1-10(integer) 1 127.0.0.1:6379> ZRANGE zset1 0 -1 1) "k"2) "all"3) "sss"
127.0.0.1:6379> HSET usera name lzx #HSET創建hash(integer) 1 127.0.0.1:6379> HGET usera name #HGET查看"lzx"
127.0.0.1:6379> HMSET userb name lzx age 20 job it #HMSET批量建立鍵值對OK 127.0.0.1:6379> HMGET userb name age job #HMGET批量查看1) "lzx"2) "20"3) "it"127.0.0.1:6379> HGETALL userb #HGETALL查看hash中全部鍵值對,這樣查看也能夠1) "name"2) "lzx"3) "age"4) "20"5) "job"6) "it"
127.0.0.1:6379> HDEL userb age #HDEL刪除對應的鍵值對(integer) 1 127.0.0.1:6379> HGETALL userb 1) "name"2) "lzx"3) "job"4) "it" #age對應的鍵值對消失
127.0.0.1:6379> HKEYS userb #HKEYS顯示hash中全部的key1) "name"2) "job"
127.0.0.1:6379> HVALS userb #HVALS顯示hash中全部的value1) "lzx"2) "it"
127.0.0.1:6379> HLEN userb #HLEN查看hash中有多少個鍵值對(integer) 2
127.0.0.1:6379> KEYS * #KEYS列出redis中全部的key 1) "key1" 2) "k2" 3) "set3" 4) "k3" 5) "seta" 6) "mykey" 7) "key2" 8) "set5" 9) "zset1"10) "k1"11) "set2"12) "set4"13) "list1"14) "userb"15) "usera"16) "hash1"17) "set1"18) "list2"
127.0.0.1:6379> KEYS my* #模糊匹配1) "mykey"
127.0.0.1:6379> EXISTS key1 #EXISTS查看是否存在某個鍵,存在返回1,不存在返回0(integer) 1
127.0.0.1:6379> DEL key1 #DEL刪除某個鍵(integer) 1 127.0.0.1:6379> EXISTS key1(integer) 0
127.0.0.1:6379> EXPIRE k2 10 #EXPIRE給某個鍵設置過時時間,10表示10s(integer) 1 127.0.0.1:6379> GET k2"2"127.0.0.1:6379> GET k2 #過10s以後查看,k2的value爲空(nil)
127.0.0.1:6379> EXPIRE key2 100(integer) 1 127.0.0.1:6379> TTL key2 #TTL查看鍵的過時時間(integer) 94 127.0.0.1:6379> TTL key2(integer) 90
127.0.0.1:6379> SELECT 1 #SELECT選擇庫,總共有16個庫,默認爲0庫OK 127.0.0.1:6379[1]> KEYS * #[1]表示如今在1庫(empty list or set) #1庫沒有數據127.0.0.1:6379[1]> SELECT 0 OK 127.0.0.1:6379> KEYS * 1) "set3" 2) "k3" 3) "seta" 4) "mykey" 5) "set5" 6) "zset1" 7) "k1" 8) "set2" 9) "set4"10) "list1"11) "userb"12) "usera"13) "hash1"14) "set1"15) "list2"
127.0.0.1:6379> MOVE set2 1 #MOVE移動數據到另外一個庫(integer) 1 127.0.0.1:6379> SELECT 1 OK 127.0.0.1:6379[1]> KEYS * 1) "set2"
127.0.0.1:6379> EXPIRE set1 100(integer) 1 127.0.0.1:6379> TTL set1(integer) 94 127.0.0.1:6379> PERSIST set1 #PERSIST取消過時時間(integer) 1 127.0.0.1:6379> TTL set1(integer) -1 #-1表示沒有過時時間,即永不過時
127.0.0.1:6379> RANDOMKEY #隨機返回一個key"userb"127.0.0.1:6379> RANDOMKEY"k3"127.0.0.1:6379> RANDOMKEY"k1"
127.0.0.1:6379> RENAME set1 setc #RENAME表示重命名keyOK 127.0.0.1:6379> KEYS set* 1) "set3"2) "seta"3) "set5"4) "set4"5) "setc"
127.0.0.1:6379> TYPE zset1 #TYPE查看key類型zset 127.0.0.1:6379> TYPE list1 list 127.0.0.1:6379> TYPE setcset
127.0.0.1:6379> DBSIZE #DBSIZE返回當前數據庫中key的數量(integer) 14
127.0.0.1:6379> INFO #返回redis數據庫狀態信息# Serverredis_version:4.0.11 redis_git_sha1:00000000 redis_git_dirty:0 redis_build_id:f1b08454f8b4e56c redis_mode:standalone os:Linux 3.10.0-693.el7.x86_64 x86_64 arch_bits:64 multiplexing_api:epoll atomicvar_api:atomic-builtin gcc_version:4.8.5 process_id:36457 run_id:d580da83e18929d6b37d826991dc705172de928c tcp_port:6379 uptime_in_seconds:24729 uptime_in_days:0 hz:10 lru_clock:3752638 executable:/usr/local/src/redis-4.0.11/redis-server config_file:/etc/redis.conf# Clientsconnected_clients:1 client_longest_output_list:0 client_biggest_input_buf:0 blocked_clients:0# Memoryused_memory:851096 used_memory_human:831.15K used_memory_rss:7970816 used_memory_rss_human:7.60M used_memory_peak:851096 used_memory_peak_human:831.15K used_memory_peak_perc:100.08% used_memory_overhead:837134 used_memory_startup:786584 used_memory_dataset:13962 used_memory_dataset_perc:21.64% total_system_memory:3958075392 total_system_memory_human:3.69G used_memory_lua:37888 used_memory_lua_human:37.00K maxmemory:0 maxmemory_human:0B maxmemory_policy:noeviction mem_fragmentation_ratio:9.36 mem_allocator:jemalloc-4.0.3 active_defrag_running:0 lazyfree_pending_objects:0# Persistenceloading:0 rdb_changes_since_last_save:3 rdb_bgsave_in_progress:0 rdb_last_save_time:1530478637 rdb_last_bgsave_status:ok rdb_last_bgsave_time_sec:0 rdb_current_bgsave_time_sec:-1 rdb_last_cow_size:6545408 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:3560 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# Statstotal_connections_received:2 total_commands_processed:211 instantaneous_ops_per_sec:0 total_net_input_bytes:7390 total_net_output_bytes:24177 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:4 expired_stale_perc:0.00 expired_time_cap_reached_count:0 evicted_keys:0 keyspace_hits:97 keyspace_misses:10 pubsub_channels:0 pubsub_patterns:0 latest_fork_usec:927 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# Replicationrole:master connected_slaves:0 master_replid:91c7968e04d1852516098681ea3bbd3e052b4252 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# CPUused_cpu_sys:17.50 used_cpu_user:8.47 used_cpu_sys_children:0.32 used_cpu_user_children:0.00# Clustercluster_enabled:0# Keyspacedb0:keys=14,expires=0,avg_ttl=0 db1:keys=1,expires=0,avg_ttl=0
127.0.0.1:6379> FLUSHDB #FLUSHDB清空當前數據庫中全部的keyOK 127.0.0.1:6379> KEYS *(empty list or set)
127.0.0.1:6379> FLUSHALL #FLUSHALL清空全部庫中全部的keyOK 127.0.0.1:6379> KEYS *(empty list or set)127.0.0.1:6379> SELECT 1 OK 127.0.0.1:6379[1]> KEYS *(empty list or set)
127.0.0.1:6379> BGSAVE #保存數據到磁盤,在後臺運行Background saving started
127.0.0.1:6379> save #保存數據到磁盤,在前臺運行OK
127.0.0.1:6379> CONFIG GET * #獲取全部配置參數
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"
127.0.0.1:6379> CONFIG SET timeout 100 #更改配置參數OK 127.0.0.1:6379> CONFIG GET timeout1) "timeout"2) "100"
redis數據恢復:首先定義或者肯定dir
目錄和dbfilename
,而後把備份的rdb文件放到dir目錄下面,重啓redis服務便可恢復數據。
# vim /etc/redis.confbind 127.0.0.1 192.168.1.1 #設置內網IP,能夠是多個,用空格分隔
# vim /etc/redis.confport 16000 #不要設置爲默認端口6379
# vim /etc/redis.confrequirepass 123123 #密碼爲123123# redis-cli -a '123123' #重啓服務以後再次登陸redis
# vim /etc/redis.confrename-command CONFIG lzx #把config命令改成lzx命令,保存退出重啓服務
# vim /etc/redis.confrename-command CONFIG "" #保存退出重啓服務
# vim /etc/redis.conf #默認配置slowlog-log-slower-than 10000 //單位:μs,即10ms slowlog-max-len 128
針對慢查詢日誌,能夠設置兩個參數,一個是執行時長,另外一個是慢查詢日誌的長度。當一個新的命令被寫入日誌時,最老的一條會從命令日誌隊列中移除出去。
在redis命令行下,
slowlog get #列出全部的慢查詢日誌slowlog get 2 #只列出2條slowlog len #查看慢查詢日誌條數
# cd /usr/local/src/# wget https://codeload.github.com/phpredis/phpredis/zip/develop# mv develop phpredis.zip# unzip phpredis.zip# cd phpredis-develop/# /usr/local/php-fpm/bin/phpize
# ./configure --with-php-config=/usr/local/php-fpm/bin/php-config # echo $?0# make# echo $?0# make install# echo $?0# vim /usr/local/php-fpm/etc/php.ini #增長一行extension=redis.so# /usr/local/php-fpm/bin/php -m[PHP Modules]Core ctypecurldatedom ereg exif fileinfo filterftpgdhashiconvjson libxml mbstring mcrypt mysql openssl pcre PDO pdo_sqlite Phar posix redis #有redis說明沒問題Reflection session SimpleXML soap SPL sqlite3 standard tokenizer xml xmlreader xmlwriter zlib[Zend Modules]# /etc/init.d/php-fpm restart #重啓php-fpm服務Gracefully shutting down php-fpm . doneStarting php-fpm done
# vim /usr/local/php-fpm/etc/php-fpm.confphp_value[session.save_handler] = redis php_value[session.save_path] = "tcp://127.0.0.1:6379"
# vim session.php #寫入下面內容<?php session_start();if (!isset($_SESSION['TEST'])) {$_SESSION['TEST'] = time();}$_SESSION['TEST3'] = time();print $_SESSION['TEST'];print "
";print $_SESSION['TEST3'];print "
";print session_id();?># mv session.php /usr/local/nginx/html/
# curl localhost/session.php 1530489156<br><br>1530489156<br><br>st5k899dgb370g9ul54kalhf14# curl localhost/session.php1530489158<br><br>1530489158<br><br>nt89nio8q5s81imr51op8as6i4# curl localhost/session.php1530489159<br><br>1530489159<br><br>fjn7oi5tn0dmus2fds18earvr6# curl localhost/session.php1530489160<br><br>1530489160<br><br>gpc9vtajh3sdsv7h0hufmembb7# curl localhost/session.php1530489163<br><br>1530489163<br><br>q03c4qq5j6ts781d5su3lmqr73# redis-cli127.0.0.1:6379> KEYS * 1) "PHPREDIS_SESSION:gpc9vtajh3sdsv7h0hufmembb7"2) "PHPREDIS_SESSION:q03c4qq5j6ts781d5su3lmqr73"3) "PHPREDIS_SESSION:nt89nio8q5s81imr51op8as6i4"4) "PHPREDIS_SESSION:st5k899dgb370g9ul54kalhf14"5) "PHPREDIS_SESSION:fjn7oi5tn0dmus2fds18earvr6"127.0.0.1:6379> GET PHPREDIS_SESSION:fjn7oi5tn0dmus2fds18earvr6 #查詢對應key的value"TEST|i:1530489159;TEST3|i:1530489159;" #查出來的這個value能夠和上面curl出來的對應
這就說明,咱們上面的配置沒有問題,Redis中保存了session。
Redis的主從配置比MySQL的主從配置要簡單,這裏爲了方便測試,我直接在一臺機器上配置。
# cp /etc/redis.conf /etc/redis2.conf# vim !$port 6380 #不能與主衝突pidfile /var/run/redis_6380.pid #pid文件不能相同logfile "/var/log/redis2.log" #日誌文件不能相同dir /data/redis2 #dir不能相同slaveof 127.0.0.1 6379 #說明它是哪一個的從,salveof 主IP 主服務端口
若是主上設置了密碼,那麼從也須要增長一行:
masterauth 123123 #設置從的密碼,假設主的密碼爲123123
# mkdir /data/redis2# redis-server /etc/redis2.conf# ps aux |grep redisroot 36457 0.1 0.2 147356 9900 ? Ssl 7月01 0:39 redis-server 127.0.0.1:6379 root 81816 1.0 0.2 147356 9720 ? Ssl 08:16 0:00 redis-server 127.0.0.1:6380
從不須要手動去同步數據,它會自動同步主上面的數據:
# redis-cli127.0.0.1:6379> KEYS *(empty list or set)127.0.0.1:6379> set key1 10 OK 127.0.0.1:6379> KEYS * 1) "key1"127.0.0.1:6379> set key2 100 OK# redis-cli -h 127.0.0.1 -p 6380127.0.0.1:6380> KEYS * 1) "key1"2) "key2" #能夠看到剛剛在主上建立的key,在從上能夠看到127.0.0.1:6380> CONFIG GET dir1) "dir"2) "/data/redis2"127.0.0.1:6380> CONFIG GET dbfilename 1) "dbfilename"2) "dump.rdb"127.0.0.1:6380> set key3 aaa(error) READONLY You can't write against a read only slave. #slave上不能夠寫入數據,配置文件裏面有定義
整個過程十分簡單,Redis主從配置完成。
當數據量達到很大時,單臺的Redis服務器是沒法知足需求的,這時候,就須要對Redis進行集羣,由多臺機器組成分佈式的Redis集羣,這樣新增節點很是方便。
Redis集羣有如下特色:
1. 多個redis節點網絡互聯,數據共享 2. 全部的節點都是一主一從(也能夠是一主多從),其中從不提供服務,僅做爲備用 3. 不支持同時處理多個key(如MSET/MGET),由於redis須要把key均勻分佈在各個節點上, 併發量很高的狀況下同時建立key-value會下降性能並致使不可預測的行爲 4. 支持在線增長、刪除節點 5. 客戶端能夠連任何一個主節點進行讀寫
場景設置:
兩臺機器,關閉防火牆和SElinux,分別開啓三個redis服務(端口) 機器A:192.168.100.150 7000 7002 7004 機器B:192.168.100.160 7001 7003 7005
# cd /etc/# vim redis_7000.confport 7000 bind 192.168.100.150 daemonize yespidfile /var/run/redis_7000.piddir /data/redis_data/7000 cluster-enabled yescluster-config-file nodes_7000.conf cluster-node-timeout 10100 appendonly yes
# vim redis_7002.confport 7002 bind 192.168.100.150 daemonize yespidfile /var/run/redis_7002.piddir /data/redis_data/7002 cluster-enabled yescluster-config-file nodes_7002.conf cluster-node-timeout 10100 appendonly yes
# vim redis_7004.confport 7004 bind 192.168.100.150 daemonize yespidfile /var/run/redis_7004.piddir /data/redis_data/7004 cluster-enabled yescluster-config-file nodes_7004.conf cluster-node-timeout 10100 appendonly yes
# mkdir -p /data/redis_data/{7000,7002,7004}# redis-server /etc/redis_7000.conf #啓動7000端口的redis服務13375:C 23 Aug 21:37:11.272 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo13375:C 23 Aug 21:37:11.272 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=13375, just started13375:C 23 Aug 21:37:11.272 # Configuration loaded# redis-server /etc/redis_7002.conf #啓動7002端口的redis服務13380:C 23 Aug 21:38:18.483 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo13380:C 23 Aug 21:38:18.483 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=13380, just started13380:C 23 Aug 21:38:18.483 # Configuration loaded# redis-server /etc/redis_7004.conf #啓動7004端口的redis服務13385:C 23 Aug 21:38:23.564 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo13385:C 23 Aug 21:38:23.564 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=13385, just started13385:C 23 Aug 21:38:23.564 # Configuration loaded # ps aux |grep redisroot 13376 0.1 0.1 145312 7572 ? Ssl 21:37 0:00 redis-server 192.168.100.150:7000 [cluster]root 13381 0.1 0.1 145312 7572 ? Ssl 21:38 0:00 redis-server 192.168.100.150:7002 [cluster]root 13386 0.1 0.1 145312 7576 ? Ssl 21:38 0:00 redis-server 192.168.100.150:7004 [cluster]
# cd /etc/# vim redis_7001.confport 7001 bind 192.168.100.160 daemonize yespidfile /var/run/redis_7001.piddir /data/redis_data/7001 cluster-enabled yescluster-config-file nodes_7001.conf cluster-node-timeout 10100 appendonly yes
# vim redis_7003.confport 7003 bind 192.168.100.160 daemonize yespidfile /var/run/redis_7003.piddir /data/redis_data/7003 cluster-enabled yescluster-config-file nodes_7003.conf cluster-node-timeout 10100 appendonly yes
# vim redis_7005.confport 7005 bind 192.168.100.160 daemonize yespidfile /var/run/redis_7005.piddir /data/redis_data/7005 cluster-enabled yescluster-config-file nodes_7005.conf cluster-node-timeout 10100 appendonly yes
# mkdir -p /data/redis_data/{7001,7003,7005}# redis-server /etc/redis_7001.conf #啓動7001端口的redis服務1855:C 23 Aug 21:39:43.723 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo1855:C 23 Aug 21:39:43.723 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=1855, just started1855:C 23 Aug 21:39:43.723 # Configuration loaded# redis-server /etc/redis_7003.conf ##啓動7003端口的redis服務1860:C 23 Aug 21:39:48.375 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo1860:C 23 Aug 21:39:48.376 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=1860, just started1860:C 23 Aug 21:39:48.376 # Configuration loaded# redis-server /etc/redis_7005.conf #啓動7005端口的redis服務1865:C 23 Aug 21:39:52.655 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo1865:C 23 Aug 21:39:52.656 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=1865, just started1865:C 23 Aug 21:39:52.656 # Configuration loaded# ps aux |grep redisroot 1856 0.1 0.1 145312 7576 ? Ssl 21:39 0:00 redis-server 192.168.100.160:7001 [cluster]root 1861 0.1 0.1 145312 7576 ? Ssl 21:39 0:00 redis-server 192.168.100.160:7003 [cluster]root 1866 0.1 0.1 145312 7576 ? Ssl 21:39 0:00 redis-server 192.168.100.160:7005 [cluster]
# yum -y groupinstall "Development Tools"# yum install -y gdbm-devel libdb4-devel libffi-devel libyaml libyaml-devel ncurses-devel openssl-devel readline-devel tcl-devel# mkdir -p rpmbuild/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS}# wget http://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.3.tar.gz -P rpmbuild/SOURCES# wget http://raw.githubusercontent.com/tjinjin/automate-ruby-rpm/master/ruby22x.spec -P rpmbuild/SPECS# rpmbuild -bb rpmbuild/SPECS/ruby22x.spec # 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# yum localinstall -y rpmbuild/RPMS/x86_64/ruby-2.2.3-1.el7.centos.x86_64.rpm #也能夠rpm -ivh方式安裝# 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
# cp /usr/local/src/redis-4.0.11/src/redis-trib.rb /usr/bin/# redis-trib.rb create --replicas 1 192.168.100.150:7000 192.168.100.150:7002 192.168.100.150:7004 192.168.100.160:7001 192.168.100.160:7003 192.168.100.160:7005 #上面的一切都是爲了這條命令可以順利執行>>> Creating cluster>>> Performing hash slots allocation on 6 nodes... Using 3 masters: 192.168.100.150:7000 192.168.100.160:7001 192.168.100.150:7002 #三個主分別是7000,7001,7002Adding replica 192.168.100.160:7005 to 192.168.100.150:7000 #7005是7000的從Adding replica 192.168.100.150:7004 to 192.168.100.160:7001 #7004是7001的從Adding replica 192.168.100.160:7003 to 192.168.100.150:7002 #7003是7002的從M: c007437678a379cd57e811843811e5b047337457 192.168.100.150:7000 slots:0-5460 (5461 slots) master M: 6d53d46c401de9804c1fd48c07685f22d693f39c 192.168.100.150:7002 slots:10923-16383 (5461 slots) master S: 9866e7651c9e26a533112e96f578477293de9670 192.168.100.150:7004 replicates 0550a444f45604dfc1f01191df67f513d5c5fc5b M: 0550a444f45604dfc1f01191df67f513d5c5fc5b 192.168.100.160:7001 slots:5461-10922 (5462 slots) master S: 3cb24dc6d295fa9c261e36df67fda11151de7ef7 192.168.100.160:7003 replicates 6d53d46c401de9804c1fd48c07685f22d693f39c S: bfc152ca2c9d140d980e3b8c3c1868239e32efe8 192.168.100.160:7005 replicates c007437678a379cd57e811843811e5b047337457 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.100.150:7000)M: c007437678a379cd57e811843811e5b047337457 192.168.100.150:7000 slots:0-5460 (5461 slots) master 1 additional replica(s)S: bfc152ca2c9d140d980e3b8c3c1868239e32efe8 192.168.100.160:7005 slots: (0 slots) slave replicates c007437678a379cd57e811843811e5b047337457 S: 3cb24dc6d295fa9c261e36df67fda11151de7ef7 192.168.100.160:7003 slots: (0 slots) slave replicates 6d53d46c401de9804c1fd48c07685f22d693f39c M: 0550a444f45604dfc1f01191df67f513d5c5fc5b 192.168.100.160:7001 slots:5461-10922 (5462 slots) master 1 additional replica(s)S: 9866e7651c9e26a533112e96f578477293de9670 192.168.100.150:7004 slots: (0 slots) slave replicates 0550a444f45604dfc1f01191df67f513d5c5fc5b M: 6d53d46c401de9804c1fd48c07685f22d693f39c 192.168.100.150: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. #執行成功,集羣搭建完畢# echo $?0
# redis-cli -c -h 192.168.100.150 -p 7000 #-c表示使用集羣方式登陸192.168.100.150:7000> set key1 123 -> Redirected to slot [9189] located at 192.168.100.160:7001 #說明定位到7001上OK 192.168.100.160:7001> set key2 abc -> Redirected to slot [4998] located at 192.168.100.150:7000 #說明又定位到7000上OK 192.168.100.150:7000> set key3 aaa #沒有提示表示回到了本機OK 192.168.100.150:7000> get key1 -> Redirected to slot [9189] located at 192.168.100.160:7001 "123"192.168.100.160:7001> get key2 -> Redirected to slot [4998] located at 192.168.100.150:7000"abc"
192.168.100.150:7000> CLUSTER NODES #列出節點bfc152ca2c9d140d980e3b8c3c1868239e32efe8 192.168.100.160:7005@17005 slave c007437678a379cd57e811843811e5b047337457 0 1535036884135 6 connected 3cb24dc6d295fa9c261e36df67fda11151de7ef7 192.168.100.160:7003@17003 slave 6d53d46c401de9804c1fd48c07685f22d693f39c 0 1535036885139 5 connected 0550a444f45604dfc1f01191df67f513d5c5fc5b 192.168.100.160:7001@17001 master - 0 1535036885140 4 connected 5461-10922 c007437678a379cd57e811843811e5b047337457 192.168.100.150:7000@17000 myself,master - 0 1535036884000 1 connected 0-5460 9866e7651c9e26a533112e96f578477293de9670 192.168.100.150:7004@17004 slave 0550a444f45604dfc1f01191df67f513d5c5fc5b 0 1535036885000 4 connected 6d53d46c401de9804c1fd48c07685f22d693f39c 192.168.100.150:7002@17002 master - 0 1535036886147 2 connected 10923-16383 192.168.100.150: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:1021 cluster_stats_messages_pong_sent:967 cluster_stats_messages_sent:1988 cluster_stats_messages_ping_received:962 cluster_stats_messages_pong_received:1021 cluster_stats_messages_meet_received:5 cluster_stats_messages_received:1988 192.168.100.150:7000> CLUSTER MEET 192.168.100.160 7007 #添加節點,進行這一步以前,先在B機器上配置好7007服務OK 192.168.100.150:7000> CLUSTER NODES bfc152ca2c9d140d980e3b8c3c1868239e32efe8 192.168.100.160:7005@17005 slave c007437678a379cd57e811843811e5b047337457 0 1535037337457 6 connected 247db306c69342ce8642e2a276199349aa25c6e7 192.168.100.160:7007@17007 master - 0 1535037335448 0 connected #以master身份添加進來3cb24dc6d295fa9c261e36df67fda11151de7ef7 192.168.100.160:7003@17003 slave 6d53d46c401de9804c1fd48c07685f22d693f39c 0 1535037335000 5 connected 0550a444f45604dfc1f01191df67f513d5c5fc5b 192.168.100.160:7001@17001 master - 0 1535037335000 4 connected 5461-10922 c007437678a379cd57e811843811e5b047337457 192.168.100.150:7000@17000 myself,master - 0 1535037333000 1 connected 0-5460 9866e7651c9e26a533112e96f578477293de9670 192.168.100.150:7004@17004 slave 0550a444f45604dfc1f01191df67f513d5c5fc5b 0 1535037336000 4 connected 6d53d46c401de9804c1fd48c07685f22d693f39c 192.168.100.150:7002@17002 master - 0 1535037337000 2 connected 10923-16383 192.168.100.150:7000> CLUSTER MEET 192.168.100.150 7006 #進行這一步以前,先在A機器上配置好7006服務OK 192.168.100.150:7000> CLUSTER NODES bfc152ca2c9d140d980e3b8c3c1868239e32efe8 192.168.100.160:7005@17005 slave c007437678a379cd57e811843811e5b047337457 0 1535037595784 6 connected 247db306c69342ce8642e2a276199349aa25c6e7 192.168.100.160:7007@17007 master - 0 1535037596790 0 connected 3cb24dc6d295fa9c261e36df67fda11151de7ef7 192.168.100.160:7003@17003 slave 6d53d46c401de9804c1fd48c07685f22d693f39c 0 1535037595000 5 connected 0550a444f45604dfc1f01191df67f513d5c5fc5b 192.168.100.160:7001@17001 master - 0 1535037596087 4 connected 5461-10922 c007437678a379cd57e811843811e5b047337457 192.168.100.150:7000@17000 myself,master - 0 1535037596000 1 connected 0-5460 9866e7651c9e26a533112e96f578477293de9670 192.168.100.150:7004@17004 slave 0550a444f45604dfc1f01191df67f513d5c5fc5b 0 1535037595079 4 connected 6d53d46c401de9804c1fd48c07685f22d693f39c 192.168.100.150:7002@17002 master - 0 1535037595000 2 connected 10923-16383 a9fbe573f52ac2aa272963ff89131f0246055cfc 192.168.100.150:7006@17006 master - 0 1535037597793 0 connected #以master身份添加進來# redis-cli -c -h 192.168.100.150 -p 7006192.168.100.150:7006> CLUSTER REPLICATE 247db306c69342ce8642e2a276199349aa25c6e7 #將當前節點設置爲指定節點的從,更改7006爲7007的從,後面隨機字符串爲node_idOK 192.168.100.150:7006> CLUSTER NODES 0550a444f45604dfc1f01191df67f513d5c5fc5b 192.168.100.160:7001@17001 master - 0 1535037791000 4 connected 5461-10922 bfc152ca2c9d140d980e3b8c3c1868239e32efe8 192.168.100.160:7005@17005 slave c007437678a379cd57e811843811e5b047337457 0 1535037790000 1 connected 6d53d46c401de9804c1fd48c07685f22d693f39c 192.168.100.150:7002@17002 master - 0 1535037790000 2 connected 10923-16383 3cb24dc6d295fa9c261e36df67fda11151de7ef7 192.168.100.160:7003@17003 slave 6d53d46c401de9804c1fd48c07685f22d693f39c 0 1535037791000 2 connected a9fbe573f52ac2aa272963ff89131f0246055cfc 192.168.100.150:7006@17006 myself,slave 247db306c69342ce8642e2a276199349aa25c6e7 0 1535037791000 0 connected c007437678a379cd57e811843811e5b047337457 192.168.100.150:7000@17000 master - 0 1535037790742 1 connected 0-5460 247db306c69342ce8642e2a276199349aa25c6e7 192.168.100.160:7007@17007 master - 0 1535037791749 7 connected 9866e7651c9e26a533112e96f578477293de9670 192.168.100.150:7004@17004 slave 0550a444f45604dfc1f01191df67f513d5c5fc5b 0 1535037792751 4 connected 192.168.100.150:7006> CLUSTER FORGET 247db306c69342ce8642e2a276199349aa25c6e7 #刪除指定節點,但沒法刪除master節點,並且不能刪除自己節點(登陸節點)(error) ERR Can't forget my master!192.168.100.150:7006> CLUSTER FORGET bfc152ca2c9d140d980e3b8c3c1868239e32efe8 #刪除7005節點OK 192.168.100.150:7006> CLUSTER NODES 0550a444f45604dfc1f01191df67f513d5c5fc5b 192.168.100.160:7001@17001 master - 0 1535038150000 4 connected 5461-10922 6d53d46c401de9804c1fd48c07685f22d693f39c 192.168.100.150:7002@17002 master - 0 1535038151864 2 connected 10923-16383 3cb24dc6d295fa9c261e36df67fda11151de7ef7 192.168.100.160:7003@17003 slave 6d53d46c401de9804c1fd48c07685f22d693f39c 0 1535038150860 2 connected a9fbe573f52ac2aa272963ff89131f0246055cfc 192.168.100.150:7006@17006 myself,slave 247db306c69342ce8642e2a276199349aa25c6e7 0 1535038151000 0 connected c007437678a379cd57e811843811e5b047337457 192.168.100.150:7000@17000 master - 0 1535038152867 1 connected 0-5460 247db306c69342ce8642e2a276199349aa25c6e7 192.168.100.160:7007@17007 master - 0 1535038151000 7 connected 9866e7651c9e26a533112e96f578477293de9670 192.168.100.150:7004@17004 slave 0550a444f45604dfc1f01191df67f513d5c5fc5b 0 1535038151000 4 connected# ls /data/redis_data/7000appendonly.aof dump.rdb nodes_7000.conf# redis-cli -c -h 192.168.100.150 -p 7000192.168.100.150:7000> CLUSTER SAVECONFIG #保存當前全部配置OK# cat /data/redis_data/7000/nodes_7000.conf bfc152ca2c9d140d980e3b8c3c1868239e32efe8 192.168.100.160:7005@17005 slave c007437678a379cd57e811843811e5b047337457 0 1535038376000 6 connected 247db306c69342ce8642e2a276199349aa25c6e7 192.168.100.160:7007@17007 master - 0 1535038377000 7 connected 3cb24dc6d295fa9c261e36df67fda11151de7ef7 192.168.100.160:7003@17003 slave 6d53d46c401de9804c1fd48c07685f22d693f39c 0 1535038376000 5 connected 0550a444f45604dfc1f01191df67f513d5c5fc5b 192.168.100.160:7001@17001 master - 0 1535038376777 4 connected 5461-10922 c007437678a379cd57e811843811e5b047337457 192.168.100.150:7000@17000 myself,master - 0 1535038375000 1 connected 0-5460 9866e7651c9e26a533112e96f578477293de9670 192.168.100.150:7004@17004 slave 0550a444f45604dfc1f01191df67f513d5c5fc5b 0 1535038378080 4 connected 6d53d46c401de9804c1fd48c07685f22d693f39c 192.168.100.150:7002@17002 master - 0 1535038378081 2 connected 10923-16383 a9fbe573f52ac2aa272963ff89131f0246055cfc 192.168.100.150:7006@17006 slave 247db306c69342ce8642e2a276199349aa25c6e7 0 1535038377779 7 connected vars currentEpoch 7 lastVoteEpoch 0 #文件內容發生變化,全部節點的配置文件都會發生變化
# redis-trib.rb check 192.168.100.150:7000>>> Performing Cluster Check (using node 192.168.100.150:7000)M: c007437678a379cd57e811843811e5b047337457 192.168.100.150:7000 slots:0-5460 (5461 slots) master 1 additional replica(s)S: bfc152ca2c9d140d980e3b8c3c1868239e32efe8 192.168.100.160:7005 slots: (0 slots) slave replicates c007437678a379cd57e811843811e5b047337457 M: 247db306c69342ce8642e2a276199349aa25c6e7 192.168.100.160:7007 slots: (0 slots) master 1 additional replica(s)S: 3cb24dc6d295fa9c261e36df67fda11151de7ef7 192.168.100.160:7003 slots: (0 slots) slave replicates 6d53d46c401de9804c1fd48c07685f22d693f39c M: 0550a444f45604dfc1f01191df67f513d5c5fc5b 192.168.100.160:7001 slots:5461-10922 (5462 slots) master 1 additional replica(s)S: 9866e7651c9e26a533112e96f578477293de9670 192.168.100.150:7004 slots: (0 slots) slave replicates 0550a444f45604dfc1f01191df67f513d5c5fc5b M: 6d53d46c401de9804c1fd48c07685f22d693f39c 192.168.100.150:7002 slots:10923-16383 (5461 slots) master 1 additional replica(s)S: a9fbe573f52ac2aa272963ff89131f0246055cfc 192.168.100.150:7006 slots: (0 slots) slave replicates 247db306c69342ce8642e2a276199349aa25c6e7[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.
更多資料參考: