一 redis 主從複製
1.1 主從複製介紹
原理:slave向master 發送sync命令,
master會創建一個新的進程
缺點:
網絡繁忙 :傳送數據 (加大帶寬)
系統繁忙:cpu損耗 ( 提高硬件配置)
1.2 redis 主從複製,結構分類
一主一從
一主多從
主從從
1.3 配置redis主從複製 並驗證
顯示主從複製信息:
192.168.4.51:6351> info replication
# Replication
role:master # redis 服務運行後,默認都是master服務
connected_slaves:0 #
永舊配置
[root@host52 ~]# vim /etc/redis/6379.conf
slaveof 192.168.4.51 6351 #280行
手動將主機192.168.4.52配置爲192.168.4.51的從庫: #這樣配置後,臨時有效,當服務器重起以後,配置失效
[root@host52 ~]# redis-cli -h 192.168.4.52 -p 6352
192.168.4.52:6352> SLAVEOF 192.168.4.51 6351
OK
192.168.4.52:6352> info replication
# Replication
role:slave
master_host:192.168.4.51
master_port:6351
master_link_status:up # 只有up 了 主機52才能同步主機51的數據
master_last_io_seconds_ago:1
master_sync_in_progress:0
192.168.4.51:6351> info replication #在51查看信息
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.4.52,port=6352,state=online,offset=140,lag=0
master_replid:f3f84f8b8de96eef68a65a97eb024a43ef455933
master_replid2:0000000000000000000000000000000000000000
一主多從
在以上實驗的基礎上面將主機53配置成主機51的從
在主機53上操作
[root@host53 ~]# vim /etc/redis/6379.conf
slaveof 192.168.4.51 6351 #280行
192.168.4.53:6353> info replication
# Replication
role:slave
master_host:192.168.4.51
master_port:6351
master_link_status:up
master_last_io_seconds_ago:6
重起服務
[root@host53 ~]# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped
[root@host53 ~]# /etc/init.d/redis_6379 start
主從從
把主機54配置爲主機53的從
在主機54上操作
[root@host53 ~]# vim /etc/redis/6379.conf
slaveof 192.168.4.53 6353 #280行
驗證
[root@host50 ~]# redis-cli -h 192.168.4.51 -p 6351
192.168.4.51:6351> set x 101
OK
192.168.4.51:6351> set y 102
OK
[root@host50 ~]# redis-cli -h 192.168.4.52/53/54 -p 6352/6353/6354
keys *
把從服務器恢復爲獨立的服務器
[root@host52 ~]# redis-cli -h 192.168.4.52 -p 6352 #l臨時有效
192.168.4.52:6352> SLAVEOF no one
永久恢復:
[root@host52 ~]# vim /etc/redis/6379.conf
#slaveof 192.168.4.51 6351 註釋掉
1.4 配置有密碼的redis 主從複製結構
55主服務器
1設置服務器的連接密碼
[root@host55 ~]# /etc/init.d/redis_6379 stop
[root@host55 ~]# vim /etc/redis/6379.conf
requirepass 123456 # 把密碼設置爲123456
[root@host55 ~]# redis-cli -h 192.168.4.55 -p 6355 -a 123456
192.168.4.55:6355> keys *
(empty list or set)
192.168.4.55:6355> set x 99
OK
192.168.4.55:6355> get x
"99"
192.168.4.55:6355>
[root@host55 ~]# vim +43 /etc/init.d/redis_6379
$CLIEXEC -h 192.168.4.55 -p 6355 -a 123456 shutdown # 是它能用腳本關閉服務
56 從服務機器
[root@host56 ~]# vim +281 /etc/redis/6379.conf
slaveof 192.168.4.55 6355 # 指定服務器的ip 和端口
masterauth 123456 # 指定主服務器的redi 的登陸密碼
[root@host56 ~]# redis-cli -h 192.168.4.56 -p 6356
192.168.4.56:6356> info replication # 查看信息
# Replication
role:slave
master_host:192.168.4.55
master_port:6355
master_link_status:up
master_last_io_seconds_ago:3
master_sync_in_progress:0
slave_repl_offset:14
1.5哨兵服務 :監視主服務機器,主服務機器宕機後 ,把對應的從服務其 升級爲 主服務機器
在主機58上運行哨兵服務
1 安裝redis軟件包
2創建哨兵服務的配置文件
[root@host58 redis-4.0.8]# vim sentinel.conf
bind 0.0.0.0 哨兵服務的ip 地址 0.0.0.0 表示所有
port 26379
sentinel monitor redisser 192.168.4.55 6355 1
sentinel auth-pass redisser 123456
# sentinel monitor <master-name> <ip> <redis-port> <quorum>
給監視主服務機器起名字 主服務器的ip 地址 主服務器的端口 哨兵服務器的臺數(這裏就只有58是哨兵服務,只要有一臺哨兵服務機器監控到主服務掛掉舊切換)
# sentinel auth-pass <master-name> <password> # 如果主庫設置了密碼,
3啓動哨兵服務
[root@host58 redis-4.0.8]# redis-sentinel /etc/sentinel.conf
4測試哨兵服務
[root@host55 ~]# /etc/init.d/redis_6379 stop #把主機55 服務停止
[root@host56 ~]# redis-cli -h 192.168.4.56 -p 6356 # 登陸主機56
192.168.4.56:6356> info replication # 查看信息
# Replication
role:master # 主機56變爲主機
connected_slaves:0
master_replid:a15e8774538fb27c3299639141539e2789a4b848
master_replid2:46ce2bb65b0ef6f03d6bd114a74ebb2670f6ec46
[root@host55 ~]# /etc/init.d/redis_6379 start #重新在啓動55
[root@host55 ~]# redis-cli -h 192.168.4.55 -p 6355 -a 123456
192.168.4.55:6355> info replication #查看55的信息
# Replication
role:slave #角色變爲從
master_host:192.168.4.56
master_port:6356
master_link_status:up
master_last_io_seconds_ago:0
192.168.4.55:6355> keys * #主機 56後面寫的數據 在主機55啓動redis服務後同樣能同步到主機55
1) "z"
2) "x"
3) "y"
192.168.4.56:6356> info replication # 主機56查看信息
# Replication
role:master # 主機56保持爲主
connected_slaves:1
slave0:ip=192.168.4.55,port=6355,state=online,offset=124956,lag=1
master_replid:a15e8774538fb27c3299639141539e2789a4b848
master_replid2:46ce2bb65b0ef6f03d6bd114a74ebb2670f6ec46
master_repl_offset:124956
second_repl_offset:91256
二數據持久話
2.1RDB方式實現數據化持久化
2.1.1 RDB介紹
當redis 停止服務時,在數據庫目錄下面會產生dump.rdb文件存到硬盤中
當redis 重起服務時,dump。rbd 會調用到內存裏
redis 數據文件 : dump.rbd
2.1.2 使用RDB文件恢復數據
[root@host51 ~]# redis-cli -h 192.168.4.51 -p 6351
192.168.4.51:6351> set x 1
OK
192.168.4.51:6351> set y 2
OK
192.168.4.51:6351> set z 4
192.168.4.51:6351> save # 快速寫進硬盤裏 # bgsave 表示不阻塞寫進硬盤
OK
[root@host51 ~]# ls /var/lib/redis/6379/
dump.rdb
[root@host51 ~]# cp /var/lib/redis/6379/dump.rdb /root # 一定要從先備份 到 /root下
[root@host51 ~]# scp /root/dump.rdb [email protected]:/root # 一定要先傳送到/root 下,不要直接/var/lib/redis/6379/該目錄下,否則恢復不成功
主機52上面的操作
[root@host52 ~]# /etc/init.d/redis_6379 stop
[root@host52 ~]# rm -rf /var/lib/redis/6379/*
[root@host52 ~]# cp /root/dump.rdb /var/lib/redis/6379/
[root@host52 ~]# /etc/init.d/redis_6379 start
[root@host52 ~]# redis-cli -h 192.168.4.52 -p 6352
192.168.4.52:6352> keys * # 數據和主機51 一樣
1) "y"
2) "z"
3) "x"
2.1.3 與RDB文件相關的配置
[root@host51 ~]# vim /etc/redis/6379.conf
dbfilename dump.rdb 文件名
數據從內存保存到硬盤的頻率
save 900 1 900秒內且有一次修改
save 300 10 300秒內且有一次修改
save 60 10000 60秒內且有一次修改
2.1.4使用RDB實現數據持久化的優點和缺點
優點
高性能的持久化實現---
比較適合大規模的數據恢復
缺點:
意外宕機時,最後一次持久化的數據會丟失
2.2AOF 方式實現數據持久化 (如果aof 和RBD同時啓用,,則默認用AOF )
2.1.1AOF 介紹
類似於mysql 的binlog日誌文件
只做追加操作的文件,記錄redis服務所有寫操作,不斷的將新的寫操作,追加到
2.1.2 使用AOF文件恢復數據
[root@host51 ~]# vim /etc/redis/6379.conf
appendonly yes
appendfilename "appendonly.aof"
appendfsync always
[root@host51 ~]# /etc/init.d/redis_6379 stop
[root@host51 ~]# /etc/init.d/redis_6379 start #開啓了aof後,在重起redis 服務後,之前的數據會丟失
紅色爲額外加的,,,針對 開啓了aof後,在重起redis 服務後,之前的數據會丟失 的解決方法
{ *** 在命令行上面啓用AOF,不會覆蓋已有的數據
[root@host53 ~]# redis-cli -h 192.168.4.53 -p 6353
192.168.4.53:6353> keys *
1) "a1"
2) "y"
3) "v2"
192.168.4.53:6353> config set appendonly yes # 啓用aof 臨時設置
OK
192.168.4.53:6353> config rewrite # 寫進配置文件裏,永久設置
192.168.4.53:6353> keys * 有效數據
1) "a1"
2) "y"
3) "v2"
192.168.4.53:6353> save # 寫進硬盤裏
[root@host53 ~]# ls /var/lib/redis/6379/ # 含有
appendonly.aof dump.rdb
[root@host53 ~]# /etc/init.d/redis_6379 stop
[root@host53 6379]# /etc/init.d/redis_6379 start
[root@host53 6379]# redis-cli -h 192.168.4.53 -p 6353
192.168.4.53:6353> keys * # 數據沒有丟失
1) "a1"
2) "y"
3) "v2"
}
[root@host51 ~]# ls /var/lib/redis/6379/
appendonly.aof dump.rdb
[root@host51 ~]# redis-cli -h 192.168.4.51 -p 6351
192.168.4.51:6351> keys *
192.168.4.51:6351> set i 88
OK
192.168.4.51:6351> set j 99
[root@host51 ~]# cat /var/lib/redis/6379/appendonly.aof # 擁有記錄數據寫入
[root@host51 ~]# cp /var/lib/redis/6379/appendonly.aof /root/
[root@host51 ~]# scp /root/appendonly.aof [email protected]:/root
在主機52上操作
[root@host52 ~]# vim /etc/redis/6379.conf
appendonly yes
appendfilename "appendonly.aof"
appendfsync always
[root@host52 ~]# /etc/init.d/redis_6379 stop
[root@host52 ~]# rm -rf /var/lib/redis/6379/*
[root@host52 ~]# cp /root/appendonly.aof /var/lib/redis/6379/
[root@host52 ~]# /etc/init.d/redis_6379 start
[root@host52 ~]# redis-cli -h 192.168.4.52 -p 6352
192.168.4.52:6352> keys * # 擁有跟主機 51數據一樣
1) "j"
2) "i"
修復AOF 文件
[root@host53 6379]# vim /var/lib/redis/6379/appendonly.aof #故意把文件修改錯
aaaa
[root@host53 6379]# /etc/init.d/redis_6379 stop
[root@host53 6379]# /etc/init.d/redis_6379 start
[root@host53 6379]# redis-check-aof --fix /var/lib/redis/6379/appendonly.aof
[root@host53 6379]# rm -rf /var/run/redis_6379.pid
[root@host53 6379]# /etc/init.d/redis_6379 start
[root@host53 6379]# redis-cli -h 192.168.4.53 -p 6353
192.168.4.53:6353> keys *
1) "a1"
2) "x"
3) "y"
4) "v2"
5) "v1"
2.1.3 與AOF文件相關的配置
appendfsync always //有新寫的操作立即記錄 即是寫入appendonly.aof文件中,也寫入磁盤裏
appendfsync everysec //每秒記錄一次 即是寫入appendonly.aof文件中,也寫入磁盤裏
appendfsync no //從不記錄 ,,指的是隻時寫入appendonly.aof文件中,不寫入磁盤裏
[root@host53 6379]# vim +280 /etc/redis/6379.conf
auto-aof-rewrite-percentage 100 當瘦身到50M 的文件增加到 100 的時候 繼續對文件進行瘦身 ,, ,,50+50=100
auto-aof-rewrite-min-size 64mb 當文件達到 64MB 時,,進行文件瘦身 假設瘦身到 50M
2.1.4使用AOF實現數據持久化的優點和缺點
優點:
可以靈活化設置持久化方式
出現意外宕機時,僅僅可能丟失1秒的數據
缺點 :
執行fsync策略時的速度可能比rdb方式慢
持久化文件體積大與rdb
三數據類型
3.1字符類型 string 字符串
192.168.4.51:6351> setrange tel 3 *** 從第4個字符修改爲***
(integer) 11
192.168.4.51:6351> get tel
"199***28888"
192.168.4.51:6351> set x 888
OK
192.168.4.51:6351> strlen tel 顯示變量的 個數
(integer) 11
192.168.4.51:6351> strlen x
192.168.4.51:6351> append x 88 給變量追加88
(integer) 5
192.168.4.51:6351> append x2 88
(integer) 2
192.168.4.51:6351> get x2
"88"
192.168.4.51:6351> get y
"997"
192.168.4.51:6351> decr y y值減掉1
(integer) 996
192.168.4.51:6351> DECRBY y 50 y值減掉 50
(integer) 946
192.168.4.51:6351> decr v1 v1沒值時,,,會賦值爲0
(integer) -1
192.168.4.51:6351> get v1
"-1"
一次輸出多個變量值
192.168.4.51:6351> mget x y
1) "88888"
2) "946"
192.168.4.51:6351> getrange key start end
192.168.4.51:6351> set hostname www.tedu.com
OK
192.168.4.51:6351> get hostname
"www.tedu.com"
192.168.4.51:6351> getrange key 0 2
""
192.168.4.51:6351> getrange hostname 0 2
"www"
192.168.4.51:6351> getrange hostname 1 2
"ww"
192.168.4.51:6351> getrange hostname -2 -1
"om"
192.168.4.51:6351> get x
"88888"
192.168.4.51:6351> incr x #自加1
(integer) 88889
192.168.4.51:6351> incrby x 3 加3
(integer) 88892
setbit 節省空間 單位是位,,1字節(byte)=8位(bit)
192.168.4.51:6351> setbit tom 1 0
(integer) 0
192.168.4.51:6351> setbit tom 2 1
(integer) 0
192.168.4.51:6351> setbit tom 3 0
192.168.4.51:6351> bitcount tom # 顯示多少個1
(integer) 1
192.168.4.51:6351> setbit tom 4 1
(integer) 0
192.168.4.51:6351> bitcount tom
(integer) 2
192.168.4.51:6351> set pay 7
OK
192.168.4.51:6351> get pay
"7"
192.168.4.51:6351> INCRBYFLOAT pay 1.5 # 加小數
"8.5"
192.168.4.51:6351> mset v3 99 v4 88
OK
192.168.4.51:6351> mget v3 v4
1) "99"
2) "88"
管理字符類型的數據命令: set mset get mget setrange strlen append getrange decr decrby incr incrby incrbyfloat setbit bitcount
數據來源 :程序員所寫的代碼php 連接redis 服務機器
3.2列表類型 list
列表介紹: 讓一個變量存儲多個值, 輸出時先進後出
192.168.4.51:6351> lpush stuname a b c
(integer) 3
192.168.4.51:6351> lpush stuname d e f
(integer) 6
192.168.4.51:6351> LRANGE stuname 0 -1
1) "f"
2) "e"
3) "d"
4) "c"
5) "b"
6) "a"
192.168.4.51:6351> LRANGE stuname 0 1
1) "f"
2) "e"
192.168.4.51:6351> LRANGE stuname -3 -1
1) "c"
2) "b"
3) "a"
192.168.4.51:6351> type stuname
list
192.168.4.51:6351> lpop stuname # 移除列表頭元素數據
"f"
192.168.4.51:6351> LRANGE stuname 0 -1
1) "e"
2) "d"
3) "c"
4) "b"
5) "a"
192.168.4.51:6351> llen stuname
(integer) 5
192.168.4.51:6351>
192.168.4.51:6351> lindex stuname 0
"e"
192.168.4.51:6351> lindex stuname 2
"c"
192.168.4.51:6351> lindex stuname -1
"a"
192.168.4.51:6351> -5
(error) ERR unknown command '-5'
192.168.4.51:6351> lindex stuname -4
"d"
192.168.4.51:6351> LRANGE stuname 0 -1
1) "e"
2) "d"
3) "c"
4) "b"
5) "a"
192.168.4.51:6351> lset stuname 0 E
OK
192.168.4.51:6351> lset stuname 1 D
OK
192.168.4.51:6351> LRANGE stuname 0 -1
1) "E"
2) "D"
3) "c"
4) "b"
5) "a"
192.168.4.51:6351> RPUSH stuname tom lucy 在末尾加上 tom lucy
(integer) 7
192.168.4.51:6351> LRANGE stuname 0 -1
1) "E"
2) "D"
3) "c"
4) "b"
5) "a"
6) "tom"
7) "lucy"
192.168.4.51:6351> RPOP stuname #刪除末尾的值
"lucy"
192.168.4.51:6351> lrange stuname 0 -1
1) "E"
2) "D"
3) "c"
4) "b"
5) "a"
6) "tom"
管理列表類型數據的命令 :lpush lrange lpop llen lindex lset rpush rpop
3.3 hash 類型 hash
一個變量可以對應多個filed,一個filed 對應一個value
比使用string 類型存儲更節能內存
管理hash 類型數據的命令:hset hget hmset hmget hkeys hvals hgetall hdel
192.168.4.51:6351> HSET wz baidu 'www.baidu.com'
(integer) 1
192.168.4.51:6351> hset wz goole 'www.g.cn'
(integer) 1
192.168.4.51:6351> type wz
hash
192.168.4.51:6351> HGET wz baidu
"www.baidu.com"
192.168.4.51:6351> HGET wz goole
"www.g.cn"
192.168.4.51:6351> hmset wz jd www.jd.com sina www.sina.com
OK
192.168.4.51:6351> hmget wz jd
1) "www.jd.com"
192.168.4.51:6351> hmget wz jd sina
1) "www.jd.com"
2) "www.sina.com"
192.168.4.51:6351> hkeys site
1) "baidu"
2) "sina"
192.168.4.51:6351> HVALS site
1) "www.baidu.com"
2) "www.sina.com"
192.168.4.51:6351> hgetall site
1) "baidu"
2) "www.baidu.com"
3) "sina"
4) "www.sina.com"
192.168.4.51:6351> hkeys wz
1) "baidu"
2) "goole"
3) "jd"
4) "sina"
192.168.4.51:6351> HGETALL wz
1) "baidu"
2) "www.baidu.com"
3) "goole"
4) "www.g.cn"
5) "jd"
6) "www.jd.com"
7) "sina"
8) "www.sina.com"
192.168.4.51:6351> HDEL wz jd baidu
(integer) 2
192.168.4.51:6351> HGETALL wz
1) "goole"
2) "www.g.cn"
3) "sina"
4) "www.sina.com"
192.168.4.51:6351>