目錄php
9、redis介紹
10、redis安裝
11、redis持久化
12、redis數據類型
十3、redis經常使用操做
十4、redis操做鍵值
十5、redis安全設置
十6、redis慢查詢日誌
十7、php安裝redis擴展
十8、redis存儲session
十9、redis主從配置
二10、redis集羣介紹
二11、redis集羣搭建配置
二12、redis集羣操做
二十3、擴展html
Redis和Memcached相似,也屬於k-v數據存儲。node
Redis官網redis.io, 截止於(2018-08-23)當前最新穩定版4.0.11。mysql
支持更多value類型,除了和string外,還支持hash、lists(鏈表)、sets(集合)和sorted sets(有序集合)linux
redis使用了兩種文件格式:全量數據(RDB)和增量請求(aof)。全量數據格式是把內存中的數據寫入磁盤,便於下次讀取文件進行加載。增量請求文件則是把內存中的數據序列化爲操做請求,用於讀取文件進行replay獲得數據,這種相似於mysql binlog。nginx
redis的存儲分爲內存存儲、磁盤存儲和log文件三部分git
1.下載redis穩定安裝包,當前最新4.0.11github
[root@lnmp-server src]# cd /usr/local/src/ [root@lnmp-server src]# wget http://download.redis.io/releases/redis-4.0.11.tar.gz --2018-08-23 21:23:11-- http://download.redis.io/releases/redis-4.0.11.tar.gz Resolving download.redis.io (download.redis.io)... 109.74.203.151 Connecting to download.redis.io (download.redis.io)|109.74.203.151|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 1739656 (1.7M) [application/x-gzip] Saving to: ‘redis-4.0.11.tar.gz’ 100%[==========================================>] 1,739,656 838KB/s in 2.0s 2018-08-23 21:23:14 (838 KB/s) - ‘redis-4.0.11.tar.gz’ saved [1739656/1739656]
2.解壓軟件包redis
[root@lnmp-server src]# tar -zxvf redis-4.0.11.tar.gz
3.編譯安裝sql
[root@lnmp-server src]# cd redis-4.0.11/ [root@lnmp-server redis-4.0.11]# make cd src && make all make[1]: Entering directory `/usr/local/src/redis-4.0.11/src' CC Makefile.dep make[1]: Leaving directory `/usr/local/src/redis-4.0.11/src' make[1]: Entering directory `/usr/local/src/redis-4.0.11/src' rm -rf redis-server redis-sentinel redis-cli redis-benchmark redis-check-rdb redis-check-aof *.o *.gcda *.gcno *.gcov redis.info lcov-html Makefile.dep dict-benchmark ...中間略... Hint: It's a good idea to run 'make test' ;) make[1]: Leaving directory `/usr/local/src/redis-4.0.11/src' [root@lnmp-server redis-4.0.11]# make install cd src && make install make[1]: Entering directory `/usr/local/src/redis-4.0.11/src' CC Makefile.dep make[1]: Leaving directory `/usr/local/src/redis-4.0.11/src' make[1]: Entering directory `/usr/local/src/redis-4.0.11/src' Hint: It's a good idea to run 'make test' ;) INSTALL install INSTALL install INSTALL install INSTALL install INSTALL install make[1]: Leaving directory `/usr/local/src/redis-4.0.11/src'
4.新建配置文件
[root@lnmp-server redis-4.0.11]# cp redis.conf /etc/redis.conf [root@lnmp-server redis-4.0.11]# ls !$ ls /etc/redis.conf /etc/redis.conf
5.編輯配置文件
[root@lnmp-server redis-4.0.11]# vim /etc/redis.conf #修改以下配置 daemonize yes logfile "/var/log/redis.log" dir /data/redis_data/ appendonly yes [root@lnmp-server redis-4.0.11]# mkdir /data/redis_data #內核優化參數,添加到/etc/rc.local [root@lnmp-server redis-4.0.11]# vim /etc/rc.local #redis optimize parameter sysctl vm.overcommit_memory=1 echo never > /sys/kernel/mm/transparent_hugepage/enabled #須要給/etc/rc.d/rc.local賦予執行權限。不然,/etc/rc.local中的任務沒法執行 [root@lnmp-server redis-4.0.11]# ls -l /etc/rc.local lrwxrwxrwx. 1 root root 13 Apr 20 23:18 /etc/rc.local -> rc.d/rc.local [root@lnmp-server redis-4.0.11]# chmod a+x /etc/rc.d/rc.local [root@lnmp-server redis-4.0.11]# ls -l !$ ls -l /etc/rc.d/rc.local -rwxr-xr-x. 1 root root 587 Aug 23 21:53 /etc/rc.d/rc.local
6.啓動redis並驗證
[root@lnmp-server redis-4.0.11]# redis-server /etc/redis.conf [root@lnmp-server redis-4.0.11]# ps aux | grep redis root 4914 0.1 0.2 145264 2180 ? Ssl 21:57 0:00 redis-server 127.0.0.1:6379 root 4919 0.0 0.0 112660 972 pts/0 S+ 21:58 0:00 grep --color=auto redis
Redis提供了兩種持久化的方式,分別是RDB(Redis DataBase)和AOF(Append Only File)
RDB,簡而言之,就是在不一樣的時間點,將redis存儲的數據生成快照並存儲到磁盤等介質上。
RDB主要受配置文件中的如下幾項影響
# save "" # save 900 1 表示每15分鐘且至少有1個key改變,就觸發一次持久化 save 900 1 # save 300 10 表示每5分鐘且至少有10個key改變,就觸發一次持久化 save 300 10 # save 60 10000 表示每60秒至少有10000個key改變,就觸發一次持久 save 60 10000 # 註釋其餘三項,取消註釋 # save "",這樣能夠禁用rdb持久化
AOF,則是換了一個角度來實現持久化,那就是將redis執行過的全部寫指令記錄下來,在下次redis從新啓動時,只要把這些寫指令從前到後再重複執行一遍,就能夠實現數據恢復了。
AOF主要受配置文件中的如下幾項影響
#若是是yes,則開啓aof持久化 appendonly yes # 指定aof文件名字 appendfilename "appendonly.aof" #指定fsync()調用模式,有三種no(不調用fsync),always(每次寫都會調用fsync),everysec(每秒鐘調用一次fsync)。第一種最快,第二種數據最安全,但性能會差一些,第三種爲這種方案,默認爲第三種。 # appendfsync always appendfsync everysec # appendfsync no
其實RDB和AOF兩種方式也能夠同時使用,在這種狀況下,若是redis重啓的話,則會優先採用AOF方式來進行數據恢復,這是由於AOF方式的數據恢復完整度更高。
若是你沒有數據持久化的需求,也徹底能夠關閉RDB和AOF方式,這樣的話,redis將變成一個純內存數據庫,就像memcache同樣。
1.Redis數據類型-string
string爲最簡單的類型,與Memcached同樣的類型,一個key對應一個value,其支持的操做與Memcached的操做相似,它的功能更豐富。設置能夠存二進制的對象。
簡單演示
[root@lnmp-server redis-4.0.11]# redis-cli 127.0.0.1:6379> set mykey "kennminn.net" OK 127.0.0.1:6379> get mykey "kennminn.net" # 批量設置key 127.0.0.1:6379> mset k1 a k2 b k3 c OK 127.0.0.1:6379> mget k1 k2 k3 1) "a" 2) "b" 3) "c" 127.0.0.1:6379>
2.Redis數據類型-list
list是一個鏈表結構,主要功能是push、pop、獲取一個範圍的全部值等等。操做中key理解爲鏈表的名字。
使用 list 結構,咱們能夠輕鬆地實現最新消息排行等功能(好比新浪微博的 TimeLine )。list 的另外一個應用就是消息隊列,能夠利用list的push操做,將任務存在list中,而後工做線程再用pop操做將任務取出進行執行。
簡單演示
127.0.0.1:6379> LPUSH list1 "kennminn" (integer) 1 127.0.0.1:6379> LPUSH list1 "1 2 3" (integer) 2 127.0.0.1:6379> LPUSH list1 "huannan huaihua" (integer) 3 127.0.0.1:6379> LRANGE list1 0 10 1) "huannan huaihua" 2) "1 2 3" 3) "kennminn" 127.0.0.1:6379> LPOP list1 "huannan huaihua" 127.0.0.1:6379> LPOP list1 "1 2 3" 127.0.0.1:6379> LPOP list1 "kennminn" 127.0.0.1:6379> LPOP list1 (nil) 127.0.0.1:6379>
3.Redis數據類型-set
set是集合,和咱們數學中的集合概念類似,對集合的操做有添加刪除元素,有對多個集合求交併差等操做。操做中key理解爲集合的名字。好比在微博應用中,能夠將一個用戶全部的關注人存在一個集合中,將其全部粉絲存在一個集合。由於 Redis 很是人性化的爲集合提供了求交集、並集、差集等操做,那麼就能夠很是方便的實現如共同關注、共同喜愛、二度好友等功能,對上面的全部集合操做,你還可使用不一樣的命令選擇將結果返回給客戶端仍是存集到一個新的集合中。
簡單演示
# set1添加元素 127.0.0.1:6379> SADD set1 aa (integer) 1 127.0.0.1:6379> SADD set1 bb (integer) 1 127.0.0.1:6379> SADD set1 cc (integer) 1 127.0.0.1:6379> SADD set1 dd (integer) 1 # set1的元素 127.0.0.1:6379> SMEMBERS set1 1) "bb" 2) "dd" 3) "cc" 4) "aa" # 從set1刪除aa元素 127.0.0.1:6379> SREM set1 aa (integer) 1 127.0.0.1:6379> SMEMBERS set1 1) "bb" 2) "dd" 3) "cc" # 添加set2 127.0.0.1:6379> SADD set2 a1 2 bb (integer) 3 127.0.0.1:6379> SMEMBERS set2 1) "2" 2) "bb" 3) "a1" # 取set1和set2的交集 127.0.0.1:6379> SINTER set1 set2 1) "bb" # 取set1和set2的並集 127.0.0.1:6379> SUNION set1 set2 1) "bb" 2) "cc" 3) "dd" 4) "2" 5) "a1" # 取set1和set2的差集 127.0.0.1:6379> SDIFF set1 set2 1) "cc" 2) "dd" 127.0.0.1:6379>
4.Redis數據類型-sort set
sorted set是有序集合,它比set多了一個權重參數score,使得集合中的元素可以按 score 進行有序排列,好比一個存儲全班同窗成績的 Sorted Sets,其集合 value 能夠是同窗的學號,而 score 就能夠是其考試得分,這樣在數據插入集合的時候,就已經進行了自然的排序。
簡單演示
127.0.0.1:6379> ZADD set3 12 abc (integer) 1 127.0.0.1:6379> ZADD set3 2 "guanzhou" (integer) 1 127.0.0.1:6379> ZADD set3 10 "shenzhen" (integer) 1 127.0.0.1:6379> ZADD set3 11 "huizhou" (integer) 1 127.0.0.1:6379> ZRANGE set3 0 -1 1) "guanzhou" 2) "shenzhen" 3) "huizhou" 4) "abc" # 反序 127.0.0.1:6379> Zrevrange set3 0 -1 1) "abc" 2) "huizhou" 3) "shenzhen" 4) "guanzhou" 127.0.0.1:6379>
5.Redis數據類型-hash
在 Memcached 中,咱們常常將一些結構化的信息打包成 hashmap,在客戶端序列化後存儲爲一個字符串的值(通常是 JSON 格式),好比用戶的暱稱、年齡、性別、積分等。
127.0.0.1:6379> hset h1 name nico (integer) 1 127.0.0.1:6379> hget h1 name "nico" 127.0.0.1:6379> hset h1 age 6 (integer) 1 127.0.0.1:6379> hget h1 age "6" 127.0.0.1:6379> hgetall h1 1) "name" 2) "nico" 3) "age" 4) "6" 127.0.0.1:6379>
1.Redis經常使用操做 (string, list)
127.0.0.1:6379> set k1 nico OK 127.0.0.1:6379> get k1 "nico" # 再次賦值會覆蓋原來的值 127.0.0.1:6379> set k1 aliva OK 127.0.0.1:6379> get k1 "aliva" # 若是key存在,返回0 127.0.0.1:6379> setnx k1 kennminn (integer) 0 127.0.0.1:6379> setnx k3 kennminn (integer) 0 # 若是不存在,則返回1並建立相應元素 127.0.0.1:6379> setnx k4 kennminn (integer) 1 # 給元素設置過時時間 127.0.0.1:6379> setex k5 5 aaaa OK # 5s之後再次查k5的值 127.0.0.1:6379> get k5 (nil) # mset一次設置多個元素 127.0.0.1:6379> mset key1 111 key2 222 key3 333 OK 127.0.0.1:6379> mget kye1 key2 key3 1) (nil) 2) "222" 3) "333" 127.0.0.1:6379> LPUSH list_a 1 (integer) 1 127.0.0.1:6379> LPUSH list_a 2 (integer) 2 127.0.0.1:6379> LRANGE list_a 0 -1 1) "2" 2) "1" 127.0.0.1:6379> LPOP list_a "2" 127.0.0.1:6379> RPUSH list_a 3 (integer) 2 127.0.0.1:6379> LRANGE list_a 0 -1 1) "1" 2) "3" 127.0.0.1:6379> rpop list_a "3" 127.0.0.1:6379>
2.Redis經常使用操做(list, set)
127.0.0.1:6379> LRANGE list_a 0 -1 1) "1" # 元素值爲1的前面增長元素0 127.0.0.1:6379> LINSERT list_a before 1 0 (integer) 2 127.0.0.1:6379> LRANGE list_a 0 -1 1) "0" 2) "1" # 第二行元素的值賦值爲aaa 127.0.0.1:6379> lset list_a 1 aaa OK 127.0.0.1:6379> LRANGE list_a 0 -1 1) "0" 2) "aaa" 127.0.0.1:6379> LINDEX list_a 0 "0" 127.0.0.1:6379> LLEN list_a (integer) 2 127.0.0.1:6379> SADD set_a aaa (integer) 1 127.0.0.1:6379> SMEMBERS set_a 1) "aaa" 127.0.0.1:6379> SREM set_a aaa (integer) 1 127.0.0.1:6379> SMEMBERS set_a (empty list or set) 127.0.0.1:6379> SADD set_a 111 (integer) 1 127.0.0.1:6379> SADD set_a 222 (integer) 1 127.0.0.1:6379> SADD set_a 333 (integer) 1 127.0.0.1:6379> SADD set_a 444 (integer) 1 127.0.0.1:6379> SADD set_a 555 (integer) 1 127.0.0.1:6379> SMEMBERS set_a 1) "111" 2) "222" 3) "333" 4) "444" 5) "555" 127.0.0.1:6379> SPOP set_a "111" 127.0.0.1:6379> SMEMBERS set_a 1) "222" 2) "333" 3) "444" 4) "555" 127.0.0.1:6379> SADD set_b 333 (integer) 1 127.0.0.1:6379> SADD set_b 444 (integer) 1 127.0.0.1:6379> SADD set_b aaa (integer) 1 127.0.0.1:6379> SADD set_b bbb (integer) 1 127.0.0.1:6379> SDIFF set_a set_b 1) "222" 2) "555" 127.0.0.1:6379> SDIFFSTORE set_c set_a set_b (integer) 2 127.0.0.1:6379> SMEMBERS set_c 1) "222" 2) "555" 127.0.0.1:6379> SINTER set_a set_b 1) "333" 2) "444" 127.0.0.1:6379> SINTERSTORE set_d set_a set_b (integer) 2 127.0.0.1:6379> SMEMBERS set_d 1) "333" 2) "444" 127.0.0.1:6379> SUNION set_a set_b 1) "222" 2) "333" 3) "444" 4) "aaa" 5) "bbb" 6) "555" 127.0.0.1:6379> SUNIONSTORE set_e set_a set_b (integer) 6 127.0.0.1:6379> SMEMBERS set_e 1) "222" 2) "333" 3) "444" 4) "aaa" 5) "bbb" 6) "555" 127.0.0.1:6379>
3.Redis經常使用操做(set, zset)
127.0.0.1:6379> SMEMBERS set_e 1) "222" 2) "333" 3) "444" 4) "aaa" 5) "bbb" 6) "555" 127.0.0.1:6379> 127.0.0.1:6379> SMEMBERS set_a 1) "222" 2) "333" 3) "444" 4) "555" # 判斷一個元素是否屬於集合set_a 127.0.0.1:6379> SISMEMBER set_a 222 (integer) 1 127.0.0.1:6379> SISMEMBER set_a 111 (integer) 0 # 隨機取出一個元素,但不刪除 127.0.0.1:6379> SRANDMEMBER set_a "222" 127.0.0.1:6379> ZADD zset_a 11 aaaa (integer) 1 # 按順序顯示全部元素 127.0.0.1:6379> ZRANGE zset_a 0 -1 1) "aaaa" # 帶分值按順序顯示全部元素 127.0.0.1:6379> ZRANGE zset_a 0 -1 withscores 1) "aaaa" 2) "11" # 刪除指定元素 127.0.0.1:6379> ZREM zset_a 11 (integer) 0 # 返回元素的索引值,索引從0開始,按分值正序 127.0.0.1:6379> ZRANK zset_a aaaa (integer) 0 127.0.0.1:6379> ZADD zset_a 13 2345 (integer) 1 127.0.0.1:6379> ZADD zset_a 9 bb2345 (integer) 1 127.0.0.1:6379> ZRANGE zset_a 0 -1 1) "bb2345" 2) "aaaa" 3) "2345" 127.0.0.1:6379> ZRANk zset_a aaaa (integer) 1 127.0.0.1:6379> ZREVRANGE zset_a 0 -1 1) "2345" 2) "aaaa" 3) "bb2345" # 返回集合中元素的個數 127.0.0.1:6379> ZCARD zset_a (integer) 3 # 返回分值範圍1-10之間的元素 127.0.0.1:6379> ZCOUNT zset_a 1 10 (integer) 1 127.0.0.1:6379> ZRANGEBYSCORE zset_a 0 2 (empty list or set) 127.0.0.1:6379> ZADD zset_a 12 aaa (integer) 1 127.0.0.1:6379> ZADD zset_a 13 bbb (integer) 1 127.0.0.1:6379> ZREMRANGEBYSCORE zset_a 1 10 (integer) 1 127.0.0.1:6379>
4.Redis經常使用操做(hash)
# 新建hash 127.0.0.1:6379> hset user1 name aming (integer) 0 127.0.0.1:6379> hset user1 age 30 (integer) 0 127.0.0.1:6379> hset user1 job it (integer) 0 127.0.0.1:6379> hgetall user1 1) "name" 2) "aming" 3) "age" 4) "30" 5) "job" 6) "it" # 批量添加鍵值對 127.0.0.1:6379> hmset user2 name aming age 30 job it OK 127.0.0.1:6379> hmget user2 name age job 1) "aming" 2) "30" 3) (nil) # 刪除指定鍵 127.0.0.1:6379> hdel user2 age (integer) 1 # 打印打有鍵 127.0.0.1:6379> HKEYS user2 1) "name" 2) "job" # 打印全部值 127.0.0.1:6379> HVALS user2 1) "aming" 2) "it" # 查看hash有幾個field 127.0.0.1:6379> HLEN user2 (integer) 2 127.0.0.1:6379>
# 取出全部key 127.0.0.1:6379> keys * 1) "set_c" 2) "k3" 3) "set_b" 4) "k2" 5) "k1" 6) "key2" 7) "set_a" 8) "user2" 9) "set1" 10) "k4" 11) "set_e" 12) "set_d" 13) "key3" 14) "h1" 15) "list1" 16) "user1" 17) "list_a" 18) "zset_a" 19) "key1" 20) "mykey" 21) "set3" 22) "set2" # 模糊匹配 127.0.0.1:6379> keys k* 1) "k3" 2) "k2" 3) "k1" 4) "key2" 5) "k4" 6) "key3" 7) "key1" # 有name爲set1的集合,返回1,沒有返回0 127.0.0.1:6379> EXISTS set1 (integer) 1 127.0.0.1:6379> EXISTS aaaa (integer) 0 # 刪除一個key,成功爲1,不成功返回0 127.0.0.1:6379> del set1 (integer) 1 # 設置key的超時時間 127.0.0.1:6379> EXPIRE k2 5 (integer) 1 # 查看鍵 還有多長時間過時,單位是s,當 key 不存在時,返回 -2 。 當 key 存在但沒有設置剩餘生存時間時,返回 -1 。 不然,返回 key 的剩餘生存時間。 127.0.0.1:6379> ttl k2 (integer) -2 127.0.0.1:6379> expire k3 100 (integer) 1 127.0.0.1:6379> ttl k3 (integer) 94 127.0.0.1:6379> # 表明選擇當前數據庫,默認進入0 數據庫 127.0.0.1:6379> select 0 OK # 把age 移動到1 數據庫, 當前因數據庫1不存在, 移動不成功 127.0.0.1:6379> MOVE age 1 (integer) 0 # 取消key3的過時時間 127.0.0.1:6379> expire key3 100 (integer) 1 127.0.0.1:6379> persist key3 (integer) 1 # 隨機地返回key 127.0.0.1:6379> randomkey "key3" # 重命名key 127.0.0.1:6379> rename key3 key4 OK # 查看key的類型 127.0.0.1:6379> type key4 string # 返回當前數據庫中key的數目 127.0.0.1:6379> dbsize (integer) 19 # 返回數據庫的狀態信息 127.0.0.1:6379> info # Server redis_version:4.0.11 redis_git_sha1:00000000 redis_git_dirty:0 redis_build_id:379f2baee845925 redis_mode:standalone os:Linux 3.10.0-693.el7.x86_64 x86_64 arch_bits:64 multiplexing_api:epoll atomicvar_api:atomic-builtin gcc_version:4.8.5 process_id:4914 run_id:98e0aafbc301473e5cd79d449aff2b8b656b33e9 tcp_port:6379 uptime_in_seconds:40854 uptime_in_days:0 hz:10 lru_clock:8346862 executable:/usr/local/src/redis-4.0.11/redis-server config_file:/etc/redis.conf # Clients connected_clients:1 client_longest_output_list:0 client_biggest_input_buf:0 blocked_clients:0 # Memory used_memory:851128 used_memory_human:831.18K used_memory_rss:2510848 used_memory_rss_human:2.39M used_memory_peak:851128 used_memory_peak_human:831.18K used_memory_peak_perc:100.07% used_memory_overhead:837262 used_memory_startup:786584 used_memory_dataset:13866 used_memory_dataset_perc:21.48% total_system_memory:1023713280 total_system_memory_human:976.29M used_memory_lua:37888 used_memory_lua_human:37.00K maxmemory:0 maxmemory_human:0B maxmemory_policy:noeviction mem_fragmentation_ratio:2.95 mem_allocator:jemalloc-4.0.3 active_defrag_running:0 lazyfree_pending_objects:0 # Persistence loading:0 rdb_changes_since_last_save:5 rdb_bgsave_in_progress:0 rdb_last_save_time:1535072813 rdb_last_bgsave_status:ok rdb_last_bgsave_time_sec:0 rdb_current_bgsave_time_sec:-1 rdb_last_cow_size:348160 aof_enabled:1 aof_rewrite_in_progress:0 aof_rewrite_scheduled:0 aof_last_rewrite_time_sec:-1 aof_current_rewrite_time_sec:-1 aof_last_bgrewrite_status:ok aof_last_write_status:ok aof_last_cow_size:0 aof_current_size:3081 aof_base_size:0 aof_pending_rewrite:0 aof_buffer_length:0 aof_rewrite_buffer_length:0 aof_pending_bio_fsync:0 aof_delayed_fsync:0 # Stats total_connections_received:3 total_commands_processed:166 instantaneous_ops_per_sec:0 total_net_input_bytes:6218 total_net_output_bytes:33240 instantaneous_input_kbps:0.00 instantaneous_output_kbps:0.00 rejected_connections:0 sync_full:0 sync_partial_ok:0 sync_partial_err:0 expired_keys:3 expired_stale_perc:0.00 expired_time_cap_reached_count:0 evicted_keys:0 keyspace_hits:75 keyspace_misses:13 pubsub_channels:0 pubsub_patterns:0 latest_fork_usec:302 migrate_cached_sockets:0 slave_expires_tracked_keys:0 active_defrag_hits:0 active_defrag_misses:0 active_defrag_key_hits:0 active_defrag_key_misses:0 # Replication role:master connected_slaves:0 master_replid:a6c48034ad413a880216a547ea633dd19acf1042 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:0 second_repl_offset:-1 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0 # CPU used_cpu_sys:34.93 used_cpu_user:26.09 used_cpu_sys_children:0.02 used_cpu_user_children:0.01 # Cluster cluster_enabled:0 # Keyspace db0:keys=19,expires=0,avg_ttl=0 127.0.0.1:6379> # 清空當前數據庫的全部key 127.0.0.1:6379> flushdb # 清空全部數據庫的全部key 127.0.0.1:6379> flushall # 保存數據到 rdb文件中,在後臺運行 127.0.0.1:6379> BGSAVE Background saving started # 保存數據到rdb文件中,在前臺運行 127.0.0.1:6379> save # 獲取全部配置參數 127.0.0.1:6379> config get * # 獲取配置參數 127.0.0.1:6379> config get dir 1) "dir" 2) "/data/redis_data" # 更改配置參數 config set dir 值 # 數據恢復: 首先定義或者肯定dir目錄和dbfilename,而後把備份的rdb文件放到dir目錄下面,重啓redis服務便可恢復數據
1.設置監聽ip
[root@lnmp-server redis-4.0.11]# vim /etc/redis.conf # 修改成信任的ip,能夠是多個ip,用空格分隔 # bind 192.168.1.100 10.0.0.1 # bind 127.0.0.1 ::1
# 可將默認端口修改成其餘不知名端口高端口 # port 6379 port 16000
3.設置密碼
# 可取消此句註釋,並設置密碼爲123456 # requirepass foobared requirepass 123456
4.將config命令更名
# security of read only slaves using 'rename-command' to shadow all the # administrative / dangerous commands. # 添加此行 # 若是想禁用config命令,能夠添加此行,可是將重命名的命令設置爲空 # rename-command CONFIG "" rename-command config aming slave-read-only yes
5.驗證
# 關閉redis [root@lnmp-server redis-4.0.11]# redis-cli shutdown 5750:M 24 Aug 09:52:45.308 # User requested shutdown... 5750:M 24 Aug 09:52:45.308 * Saving the final RDB snapshot before exiting. 5750:M 24 Aug 09:52:45.310 * DB saved on disk 5750:M 24 Aug 09:52:45.310 * Removing the pid file. 5750:M 24 Aug 09:52:45.310 # Redis is now ready to exit, bye bye... # 啓動redis [root@lnmp-server redis-4.0.11]# redis-server /etc/redis.conf [root@lnmp-server redis-4.0.11]# netstat -nltup Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1152/nginx: master tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1096/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1230/master tcp 0 0 127.0.0.1:16000 0.0.0.0:* LISTEN 5765/redis-server 1 tcp6 0 0 :::22 :::* LISTEN 1096/sshd tcp6 0 0 ::1:25 :::* LISTEN 1230/master tcp6 0 0 :::3306 :::* LISTEN 1367/mysqld udp 0 0 127.0.0.1:323 0.0.0.0:* 696/chronyd udp6 0 0 ::1:323 :::* 696/chronyd [root@lnmp-server redis-4.0.11]# redis-cli -a 123456 -p 16000 Warning: Using a password with '-a' option on the command line interface may not be safe. 127.0.0.1:16000> keys * 1) "user1" 2) "set_e" 3) "key1" 4) "k1" 5) "list_a" 6) "set_a" 7) "set_c" 8) "key2" 9) "list1" 10) "set3" 11) "set_b" 12) "set_d" 13) "key4" 14) "user2" 15) "k4" 16) "h1" 17) "set2" 18) "zset_a" 19) "mykey" 127.0.0.1:16000> CONFIG GET dir (error) ERR unknown command `CONFIG`, with args beginning with: `GET`, `dir`, 127.0.0.1:16000> aming get dir 1) "dir" 2) "/data/redis_data"
編輯/etc/redis.conf
[root@lnmp-server redis-4.0.11]# vim /etc/redis.conf # 針對慢查詢日誌,能夠設置兩個參數,一個是執行時長,單位是微秒,另外一個是慢查詢日誌的長度。當一個新的命令被寫入日誌時,最老的一條會從命令日誌隊列中被移除。 # 默認慢查詢日誌已經開啓 # a value of zero forces the logging of every command. # 單位ms,表示慢於10000ms則記錄日誌 slowlog-log-slower-than 10000 # There is no limit to this length. Just be aware that it will consume memory. # You can reclaim memory used by the slow log with SLOWLOG RESET. # 定義日誌長度,表示最多存128條 slowlog-max-len 128
slowlog相關的命令
# 列出全部的慢查詢日誌 slowlog get # 只列出2條 slowlog get 2 # 查看慢查詢日誌條數 slowlog len
1.下載redis的php擴展
[root@lnmp-server redis-4.0.11]# cd /usr/local/src/ [root@lnmp-server redis-4.0.11]# cd /usr/local/src/ [root@lnmp-server src]# wget https://codeload.github.com/phpredis/phpredis/zip/develop --2018-08-24 10:19:53-- https://codeload.github.com/phpredis/phpredis/zip/develop Resolving codeload.github.com (codeload.github.com)... 54.251.140.56, 13.229.189.0, 13.250.162.133 Connecting to codeload.github.com (codeload.github.com)|54.251.140.56|:443... connected. HTTP request sent, awaiting response... 200 OK Length: unspecified [application/zip] Saving to: ‘develop’ [ <=> ] 240,041 402KB/s in 0.6s 2018-08-24 10:19:55 (402 KB/s) - ‘develop’ saved [240041] [root@lnmp-server src]# mv develop phpredis-develop.zip
2.解壓軟件包
[root@lnmp-server src]# unzip phpredis-develop.zip Archive: phpredis-develop.zip 1d997873750e5fdef1fe368b5296f4427dab3ea0 creating: phpredis-develop/ ...中間略... inflating: phpredis-develop/tests/regenerateSessionId.php inflating: phpredis-develop/tests/startSession.php
3.配置
[root@lnmp-server src]# cd phpredis-develop/ [root@lnmp-server phpredis-develop]# /usr/local/php-fpm/bin/phpize Configuring for: PHP Api Version: 20131106 Zend Module Api No: 20131226 Zend Extension Api No: 220131226
4.配置、編譯安裝
[root@lnmp-server phpredis-develop]# ./configure --with-php-config=/usr/local/php-fpm/bin/php-config checking for grep that handles long lines and -e... /usr/bin/grep checking for egrep... /usr/bin/grep -E checking for a sed that does not truncate output... /usr/bin/sed ...中間略... creating libtool appending configuration tag "CXX" to libtool configure: creating ./config.status config.status: creating config.h [root@lnmp-server phpredis-develop]# make /bin/sh /usr/local/src/phpredis-develop/libtool --mode=compile cc -I. -I/usr/local/src/phpredis-develop -DPHP_ATOM_INC -I/usr/local/src/phpredis-develop/include -I/usr/local/src/phpredis-develop/main -I/usr/local/src/phpredis-develop -I/usr/local/php-fpm/include/php -I/usr/local/php-fpm/include/php/main -I/usr/local/php-fpm/include/php/TSRM -I/usr/local/php-fpm/include/php/Zend -I/usr/local/php-fpm/include/php/ext -I/usr/local/php-fpm/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /usr/local/src/phpredis-develop/redis.c -o redis.lo ...中間略... See any operating system documentation about shared libraries for more information, such as the ld(1) and ld.so(8) manual pages. ---------------------------------------------------------------------- Build complete. Don't forget to run 'make test'. [root@lnmp-server phpredis-develop]# make install Installing shared extensions: /usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20131226/
5.配置/usr/local/php-fpm/etc/php.ini
/usr/local/php-fpm/etc/php.ini # 添加下行 extension=redis.so [root@lnmp-server phpredis-develop]# /usr/local/php-fpm/sbin/php-fpm -m | grep redis redis # 重啓php-fpm [root@lnmp-server phpredis-develop]# /etc/init.d/php-fpm restart Gracefully shutting down php-fpm . done Starting php-fpm done
1.修改php-fpm主配置文件
vim /usr/local/php-fpm/etc/php.ini # 更改或增長 session.save_handler = "redis" session.save_path = "tcp://127.0.0.1:6379"
2.apache虛擬主機配置文件中也能夠這樣配置:
php_value session.save_handler " redis" php_value session.save_path " tcp://127.0.0.1:6379"
3.php-fpm配置文件對應的pool中增長:
php_value[session.save_handler] = redis php_value[session.save_path] = " tcp://127.0.0.1:6379 "
這裏以第1種方法,修改php-fpm主配置文件爲例
1.修改配置文件後重啓php-tpm
2.新建測試文件
[root@lnmp-server src]# vim /usr/local/nginx/html/bbb.com/session.php # session.php內容可參考https://coding.net/u/aminglinux/p/yuanke_centos7/git/blob/master/21NOSQL/session.php #內容以下 <?php session_start(); if (!isset($_SESSION['TEST'])) { $_SESSION['TEST'] = time(); } $_SESSION['TEST3'] = time(); print $_SESSION['TEST']; print "<br><br>"; print $_SESSION['TEST3']; print "<br><br>"; print session_id(); ?>
3.測試
[root@lnmp-server src]# curl localhost/session.php 1535079050<br><br>1535079050<br><br>qra2l77cbvqca62ljtoibpjcl6 [root@lnmp-server src]# redis-cli 127.0.0.1:6379> keys * 1) "set_d" 2) "user1" 3) "set2" # 說明已經保存到redis中了 4) "PHPREDIS_SESSION:qra2l77cbvqca62ljtoibpjcl6" 5) "set3" 6) "list_a" 7) "PHPREDIS_SESSION:uo3tmvekflcp0q051u0rjp5i25" 8) "set_e" 9) "set_a" 10) "list1" 11) "mykey" 12) "key4" 13) "set_b" 14) "k4" 15) "key1" 16) "set_c" 17) "k1" 18) "user2" 19) "zset_a" 20) "key2" 21) "h1" 127.0.0.1:6379>
爲了節省資源,咱們能夠在一臺機器上啓動兩個redis服務
1.新建從配置文件
[root@lnmp-server src]# cp /etc/redis.conf /etc/redis1.conf [root@lnmp-server src]# vim /etc/redis1.conf # port,dir,pidfile,logfile port 6380 dir /data/redis1_data/ pidfile /var/run/redis_6380.pid logfile "/var/log/redis1.log" # 增長 slaveof 127.0.0.1 6379 # 若是主上設置了密碼,還須要增長 # masterauth aminglinux>com 設置主的密碼 # 啓動以前不要忘記建立新的dir目錄 [root@lnmp-server src]# mkdir /data/redis1_data/
2.啓動從redis
[root@lnmp-server src]# redis-server /etc/redis1.conf [root@lnmp-server src]# netstat -nltup Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 1482/redis-server 1 tcp 0 0 127.0.0.1:6380 0.0.0.0:* LISTEN 1690/redis-server 1 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1133/nginx: master tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1077/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1348/master tcp6 0 0 :::22 :::* LISTEN 1077/sshd tcp6 0 0 ::1:25 :::* LISTEN 1348/master tcp6 0 0 :::3306 :::* LISTEN 1308/mysqld udp 0 0 127.0.0.1:323 0.0.0.0:* 689/chronyd udp6 0 0 ::1:323 :::* 689/chronyd [root@lnmp-server src]#
3.測試
# 登陸從redis,查看數據,主上的數據自動同步過來了 [root@lnmp-server src]# redis-cli -p 6380 127.0.0.1:6380> keys * 1) "k4" 2) "mykey" 3) "key4" 4) "user2" 5) "list_a" 6) "set_d" 7) "set2" 8) "set_a" 9) "k1" 10) "user1" 11) "set_e" 12) "key1" 13) "list1" 14) "set_c" 15) "h1" 16) "zset_a" 17) "set_b" 18) "key2" 19) "set3"
多個redis節點網絡互聯,數據共享
全部的節點都是一主一從(能夠是多個從),其中從不提供服務,僅做爲備用
不支持同時處理多個鍵(如mset/mget),由於redis須要把鍵均勻分佈在各個節點上,併發量很高的狀況下同時建立鍵值會下降性能並致使不可預測的行爲。
支持在線增長、刪除節點
客戶端能夠連任何一個主節點進行讀寫
場景設置:
兩臺機器,分別開啓三個Redis服務(端口)
A機器上三個端口7000,7002,7004,所有爲主
B機器上三個端口7001,7003,7005,所有爲從
兩臺機器上都要編譯安裝redis,而後編輯並複製3個不一樣的redis.conf,分別設置不一樣的端口號、dir等參數,還須要增長cluster相關參數,而後分別啓動6個redis服務
1.分別在兩臺機器上編譯安裝redis, 各建3個配置文件,配置文件可參考https://coding.net/u/aminglinux/p/yuanke_centos7/git/tree/master/21NOSQL。分別啓動各reids
[root@redis-master src]# ps aux | grep redis root 18786 0.0 0.9 147312 9624 ? Ssl 14:31 0:02 redis-server 127.0.0.1:7000 [cluster] root 18792 0.0 0.9 147312 9624 ? Ssl 14:31 0:02 redis-server 127.0.0.1:7002 [cluster] root 18798 0.0 0.9 147312 9624 ? Ssl 14:31 0:02 redis-server 127.0.0.1:7004 [cluster] root 18861 0.0 0.0 112660 972 pts/0 R+ 15:19 0:00 grep --color=auto redis [root@redis-slave src]# ps aux | grep redis root 4830 0.0 0.9 147312 9624 ? Ssl 14:32 0:02 redis-server 127.0.0.1:7001 [cluster] root 4835 0.0 0.9 147312 9624 ? Ssl 14:32 0:02 redis-server 127.0.0.1:7003 [cluster] root 4840 0.0 0.9 147312 9624 ? Ssl 14:32 0:02 redis-server 127.0.0.1:7005 [cluster] root 18868 0.0 0.0 112660 972 pts/0 R+ 15:19 0:00 grep --color=auto redis
2.安裝ruby2.2
# 若是未安裝開發工具,須要安裝一下開發工具 # yum groupinstall "Development Tools" -y # 安裝依賴包 [root@redis-slave src]# yum -y install gdbm-devel libdb4-devel libffi-devel libyaml libyaml-devel ncurses-devel openssl-devel readline-devel tcl-deve [root@redis-master src]# cd /root/ [root@redis-master ~]# mkdir -p rpmbuild/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS} [root@redis-master ~]# wget http://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.3.tar.gz -P rpmbuild/SOURCES --2018-08-24 15:32:43-- http://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.3.tar.gz Resolving cache.ruby-lang.org (cache.ruby-lang.org)... 151.101.229.178, 2a04:4e42:36::434 Connecting to cache.ruby-lang.org (cache.ruby-lang.org)|151.101.229.178|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 16626772 (16M) [application/x-tar] Saving to: ‘rpmbuild/SOURCES/ruby-2.2.3.tar.gz’ 100%[====================================================================================>] 16,626,772 6.94MB/s in 2.3s 2018-08-24 15:32:53 (6.94 MB/s) - ‘rpmbuild/SOURCES/ruby-2.2.3.tar.gz’ saved [16626772/16626772] [root@redis-master ~]# wget https://raw.githubusercontent.com/tjinjin/automate-ruby-rpm/master/ruby22x.spec -P rpmbuild/SPECS --2018-08-24 15:33:18-- https://raw.githubusercontent.com/tjinjin/automate-ruby-rpm/master/ruby22x.spec Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.228.133 Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.228.133|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 4484 (4.4K) [text/plain] Saving to: ‘rpmbuild/SPECS/ruby22x.spec’ 100%[====================================================================================>] 4,484 --.-K/s in 0s 2018-08-24 15:33:29 (41.0 MB/s) - ‘rpmbuild/SPECS/ruby22x.spec’ saved [4484/4484] [root@redis-master ~]# rpmbuild -bb rpmbuild/SPECS/ruby22x.spec + exit 0 [root@redis-master ~]# ls rpmbuild/RPMS/x86_64/ ruby-2.2.3-1.el7.centos.x86_64.rpm ruby-debuginfo-2.2.3-1.el7.centos.x86_64.rpm ruby-2.2.3-1.el7.centos.x86_64.rpm ruby-debuginfo-2.2.3-1.el7.centos.x86_64.rpm [root@redis-master ~]# yum -y localinstall rpmbuild/RPMS/x86_64/ruby-2.2.3-1.el7.centos.x86_64.rpm [root@redis-master ~]# rpm -q ruby ruby-2.2.3-1.el7.centos.x86_64 [root@redis-master ~]# gem install redis Fetching: redis-4.0.2.gem (100%) Successfully installed redis-4.0.2 Parsing documentation for redis-4.0.2 Installing ri documentation for redis-4.0.2 Done installing documentation for redis after 0 seconds 1 gem installed
3.Redis集羣配置
[root@redis-master ~]# cp /usr/local/src/redis-4.0.11/src/redis-trib.rb /usr/bin/ # 配置集羣 [root@redis-master ~]# redis-trib.rb create --replicas 1 192.168.1.45:7000 192.168.1.45:7002 192.168.1.45:7004 192.168.1.46:7001 192.168.1.46:7003 192.168.1.46:7005 >>> Creating cluster >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 192.168.1.45:7000 192.168.1.46:7001 192.168.1.45:7002 Adding replica 192.168.1.46:7005 to 192.168.1.45:7000 Adding replica 192.168.1.45:7004 to 192.168.1.46:7001 Adding replica 192.168.1.46:7003 to 192.168.1.45:7002 M: 65fff908dc0736d0cabea55d6dd5908f5655b38a 192.168.1.45:7000 slots:0-5460 (5461 slots) master M: 45876690d891e369cc1ffd0ae61b2f6b4c59e70b 192.168.1.45:7002 slots:10923-16383 (5461 slots) master S: 4837f52c7731531c7c6ea903f0c81b28b6956d21 192.168.1.45:7004 replicates 31abbf90e210d813bbfa6e6cecbd1d616d579461 M: 31abbf90e210d813bbfa6e6cecbd1d616d579461 192.168.1.46:7001 slots:5461-10922 (5462 slots) master S: 215d5b451cbe5e3e68ba131f64acf6e1b6fe62e7 192.168.1.46:7003 replicates 45876690d891e369cc1ffd0ae61b2f6b4c59e70b S: 24cbab3b253d8a3e89978a402873bfb3a72a8d21 192.168.1.46:7005 replicates 65fff908dc0736d0cabea55d6dd5908f5655b38a Can I set the above configuration? (type 'yes' to accept): yes >>> Nodes configuration updated >>> Assign a different config epoch to each node >>> Sending CLUSTER MEET messages to join the cluster Waiting for the cluster to join.... >>> Performing Cluster Check (using node 192.168.1.45:7000) M: 65fff908dc0736d0cabea55d6dd5908f5655b38a 192.168.1.45:7000 slots:0-5460 (5461 slots) master 1 additional replica(s) M: 45876690d891e369cc1ffd0ae61b2f6b4c59e70b 192.168.1.45:7002 slots:10923-16383 (5461 slots) master 1 additional replica(s) M: 31abbf90e210d813bbfa6e6cecbd1d616d579461 192.168.1.46:7001 slots:5461-10922 (5462 slots) master 1 additional replica(s) S: 4837f52c7731531c7c6ea903f0c81b28b6956d21 192.168.1.45:7004 slots: (0 slots) slave replicates 31abbf90e210d813bbfa6e6cecbd1d616d579461 S: 24cbab3b253d8a3e89978a402873bfb3a72a8d21 192.168.1.46:7005 slots: (0 slots) slave replicates 65fff908dc0736d0cabea55d6dd5908f5655b38a S: 215d5b451cbe5e3e68ba131f64acf6e1b6fe62e7 192.168.1.46:7003 slots: (0 slots) slave replicates 45876690d891e369cc1ffd0ae61b2f6b4c59e70b [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
1.鏈接redis集羣
# -c以集羣的方式登陸,-h 主機地址,-p 端口號 [root@redis-master ~]# redis-cli -c -h 192.168.1.45 -p 7000 192.168.1.45:7000> [root@redis-master ~]# redis-cli -c -h 192.168.1.45 -p 7000 192.168.1.45:7000> set k1 "nico" -> Redirected to slot [12706] located at 192.168.1.45:7002 OK 192.168.1.45:7002> get k1 "nico" [root@redis-master ~]# redis-cli -c -h 192.168.1.45 -p 7002 192.168.1.45:7002> get k1 "nico"
2.經常使用操做
# 檢測集羣狀態 [root@redis-master ~]# redis-trib.rb check 192.168.1.45:7000 >>> Performing Cluster Check (using node 192.168.1.45:7000) M: 65fff908dc0736d0cabea55d6dd5908f5655b38a 192.168.1.45:7000 slots:0-5460 (5461 slots) master 1 additional replica(s) M: 45876690d891e369cc1ffd0ae61b2f6b4c59e70b 192.168.1.45:7002 slots:10923-16383 (5461 slots) master 1 additional replica(s) M: 31abbf90e210d813bbfa6e6cecbd1d616d579461 192.168.1.46:7001 slots:5461-10922 (5462 slots) master 1 additional replica(s) S: 4837f52c7731531c7c6ea903f0c81b28b6956d21 192.168.1.45:7004 slots: (0 slots) slave replicates 31abbf90e210d813bbfa6e6cecbd1d616d579461 S: 24cbab3b253d8a3e89978a402873bfb3a72a8d21 192.168.1.46:7005 slots: (0 slots) slave replicates 65fff908dc0736d0cabea55d6dd5908f5655b38a S: 215d5b451cbe5e3e68ba131f64acf6e1b6fe62e7 192.168.1.46:7003 slots: (0 slots) slave replicates 45876690d891e369cc1ffd0ae61b2f6b4c59e70b [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. # 列出節點 [root@redis-master releasetools]# redis-cli -c -h 192.168.1.45 -p 7000 192.168.1.45:7000> cluster nodes 45876690d891e369cc1ffd0ae61b2f6b4c59e70b 192.168.1.45:7002@17002 master - 0 1535103063059 2 connected 10923-16383 31abbf90e210d813bbfa6e6cecbd1d616d579461 192.168.1.46:7001@17001 master - 0 1535103061000 4 connected 5461-10922 65fff908dc0736d0cabea55d6dd5908f5655b38a 192.168.1.45:7000@17000 myself,master - 0 1535103060000 1 connected 0-5460 4837f52c7731531c7c6ea903f0c81b28b6956d21 192.168.1.45:7004@17004 slave 31abbf90e210d813bbfa6e6cecbd1d616d579461 0 1535103062000 4 connected 24cbab3b253d8a3e89978a402873bfb3a72a8d21 192.168.1.46:7005@17005 slave 65fff908dc0736d0cabea55d6dd5908f5655b38a 0 1535103062356 6 connected 215d5b451cbe5e3e68ba131f64acf6e1b6fe62e7 192.168.1.46:7003@17003 slave 45876690d891e369cc1ffd0ae61b2f6b4c59e70b 0 1535103060341 5 connected # 查看集羣信息 192.168.1.45:7000> cluster info cluster_state:ok cluster_slots_assigned:16384 cluster_slots_ok:16384 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:6 cluster_size:3 cluster_current_epoch:6 cluster_my_epoch:1 cluster_stats_messages_ping_sent:4232 cluster_stats_messages_pong_sent:4159 cluster_stats_messages_sent:8391 cluster_stats_messages_ping_received:4154 cluster_stats_messages_pong_received:4232 cluster_stats_messages_meet_received:5 cluster_stats_messages_received:8391 # cluster meet ip port 添加節點 # cluster forget node_id 移除節點 # cluster replicate node_id 將當前節點設置爲指定節點的從節點 # 新增長2個配置文件,並啓動 [root@redis-master releasetools]# redis-server /etc/redis7006.conf [root@redis-master releasetools]# redis-server /etc/redis7007.conf [root@redis-master releasetools]# ps aux | grep redis root 34213 0.2 1.1 151408 11928 ? Ssl 16:18 0:13 redis-server 192.168.1.45:7000 [cluster] root 34264 0.2 1.1 151408 11936 ? Ssl 16:20 0:13 redis-server 192.168.1.45:7002 [cluster] root 34269 0.2 1.1 151408 11928 ? Ssl 16:20 0:14 redis-server 192.168.1.45:7004 [cluster] root 34504 0.0 0.9 147312 9624 ? Ssl 17:52 0:00 redis-server 192.168.1.45:7006 [cluster] root 34509 0.2 0.9 147312 9704 ? Ssl 17:52 0:00 redis-server 192.168.1.45:7007 [cluster] root 34515 0.0 0.0 112660 968 pts/0 R+ 17:52 0:00 grep --color=auto redis [root@redis-master releasetools]# redis-cli -c -h 192.168.1.45 -p 7000 192.168.1.45:7000> cluster nodes 45876690d891e369cc1ffd0ae61b2f6b4c59e70b 192.168.1.45:7002@17002 master - 0 1535104403139 2 connected 10923-16383 31abbf90e210d813bbfa6e6cecbd1d616d579461 192.168.1.46:7001@17001 master - 0 1535104402000 4 connected 5461-10922 65fff908dc0736d0cabea55d6dd5908f5655b38a 192.168.1.45:7000@17000 myself,master - 0 1535104402000 1 connected 0-5460 4837f52c7731531c7c6ea903f0c81b28b6956d21 192.168.1.45:7004@17004 slave 31abbf90e210d813bbfa6e6cecbd1d616d579461 0 1535104402129 4 connected 24cbab3b253d8a3e89978a402873bfb3a72a8d21 192.168.1.46:7005@17005 slave 65fff908dc0736d0cabea55d6dd5908f5655b38a 0 1535104400117 6 connected 215d5b451cbe5e3e68ba131f64acf6e1b6fe62e7 192.168.1.46:7003@17003 slave 45876690d891e369cc1ffd0ae61b2f6b4c59e70b 0 1535104403138 5 connected # 添加節點, 193.192.168.1.45:7000> CLUSTER MEET 192.168.1.45 7006 OK 192.168.1.45:7000> CLUSTER MEET 192.168.1.45 7007 OK 192.168.1.45:7000> cluster nodes 6157feade2af15a81a982ab467fc9746514552e3 192.168.1.45:7007@17007 slave 65fff908dc0736d0cabea55d6dd5908f5655b38a 0 1535104467000 1 connected 45876690d891e369cc1ffd0ae61b2f6b4c59e70b 192.168.1.45:7002@17002 master - 0 1535104465000 2 connected 10923-16383 31abbf90e210d813bbfa6e6cecbd1d616d579461 192.168.1.46:7001@17001 master - 0 1535104467106 4 connected 5461-10922 65fff908dc0736d0cabea55d6dd5908f5655b38a 192.168.1.45:7000@17000 myself,master - 0 1535104464000 1 connected 0-5460 4837f52c7731531c7c6ea903f0c81b28b6956d21 192.168.1.45:7004@17004 slave 31abbf90e210d813bbfa6e6cecbd1d616d579461 0 1535104466000 4 connected 24cbab3b253d8a3e89978a402873bfb3a72a8d21 192.168.1.46:7005@17005 slave 65fff908dc0736d0cabea55d6dd5908f5655b38a 0 1535104467813 6 connected 215d5b451cbe5e3e68ba131f64acf6e1b6fe62e7 192.168.1.46:7003@17003 slave 45876690d891e369cc1ffd0ae61b2f6b4c59e70b 0 1535104466000 5 connected 81885a03578dbd587136c233b48aee633703286e 192.168.1.45:7006@17006 master - 0 1535104466000 0 connected # 刪除節點 192.168.1.45:7000> CLUSTER FORGET 6157feade2af15a81a982ab467fc9746514552e3 OK 192.168.1.45:7000> cluster nodes 45876690d891e369cc1ffd0ae61b2f6b4c59e70b 192.168.1.45:7002@17002 master - 0 1535104692665 2 connected 10923-16383 31abbf90e210d813bbfa6e6cecbd1d616d579461 192.168.1.46:7001@17001 master - 0 1535104689000 4 connected 5461-10922 65fff908dc0736d0cabea55d6dd5908f5655b38a 192.168.1.45:7000@17000 myself,master - 0 1535104688000 1 connected 0-5460 4837f52c7731531c7c6ea903f0c81b28b6956d21 192.168.1.45:7004@17004 slave 31abbf90e210d813bbfa6e6cecbd1d616d579461 0 1535104691000 4 connected 24cbab3b253d8a3e89978a402873bfb3a72a8d21 192.168.1.46:7005@17005 slave 65fff908dc0736d0cabea55d6dd5908f5655b38a 0 1535104690652 6 connected 215d5b451cbe5e3e68ba131f64acf6e1b6fe62e7 192.168.1.46:7003@17003 slave 45876690d891e369cc1ffd0ae61b2f6b4c59e70b 0 1535104691000 5 connected 81885a03578dbd587136c233b48aee633703286e 192.168.1.45:7006@17006 master - 0 1535104691658 0 connected # cluster saveconfig 保存配置文件 192.168.1.45:7000> cluster saveconfig OK
redis哨兵 https://segmentfault.com/a/1190000002680804 http://www.cnblogs.com/jaycekon/p/6237562.html