Redis
1、RDB和AOF的優(yōu)缺點(diǎn)
RDB優(yōu)點(diǎn):
-
比如: 可以在最近的24小時(shí)內(nèi),每小時(shí)備份一次RDB文件,并且在每個(gè)月的每一天,也備份一個(gè) ROB文件。這樣的話,即使遇上問(wèn)題,也可以隨時(shí)將數(shù)據(jù)集還原到不同的版本。
-
RDB可以最大化Redis的性能,父進(jìn)程在保存 RDB文件時(shí)唯一要做的就是fork出一個(gè)子進(jìn)程,然后 這個(gè)子進(jìn)程就會(huì)處理接下來(lái)的所有保存工作,父進(jìn)程無(wú)須執(zhí)行任何磁盤(pán)工/0操作。
-
RDB在大量數(shù)據(jù),比如幾個(gè)G的數(shù)據(jù),恢復(fù)的速度比AOF的快
RDB缺點(diǎn):
-
如果你需要盡量避免在服務(wù)器故障時(shí)丟失數(shù)據(jù),那么RDB不適合你。雖然Redis允許你設(shè)置不同的 保存點(diǎn)(save point)來(lái)控制保存RDB文件的頻率,但是,因?yàn)镽OB文件需要保存整個(gè)數(shù)據(jù)集的狀態(tài),所以它并不是一個(gè)輕松的操作。因此你可能會(huì)至少5分鐘才保存一次RDB文件。在這種情況 下,一旦發(fā)生故障停機(jī),你就可能會(huì)丟失好幾分鐘的數(shù)據(jù)。 (不能保證數(shù)據(jù)一點(diǎn)不丟失)
-
當(dāng)數(shù)據(jù)量非常大的時(shí)候,從父進(jìn)程fork子進(jìn)程進(jìn)行保存至RDB文件時(shí)需要一點(diǎn)時(shí)間,可能是毫秒或 者秒,取決于磁盤(pán)IO性能
-
在數(shù)據(jù)集比較龐大時(shí),fork()可能會(huì)非常耗時(shí),造成服務(wù)器在一定時(shí)間內(nèi)停止處理客戶端﹔如果數(shù) 據(jù)集非常巨大,并且CPU時(shí)間非常緊張的話,那么這種停止時(shí)間甚至可能會(huì)長(zhǎng)達(dá)整整一秒或更久。
-
雖然 AOF重寫(xiě)也需要進(jìn)行fork(),但無(wú)論AOF重寫(xiě)的執(zhí)行間隔有多長(zhǎng),數(shù)據(jù)的持久性都不會(huì)有任何損失。
AOF 優(yōu)點(diǎn):
-
-
由于該機(jī)制對(duì)日志文件的寫(xiě)入操作采用的是append模式,因此在寫(xiě)入過(guò)程中不需要seek, 即使出現(xiàn)宕機(jī)現(xiàn)象,也不會(huì)破壞日志文件中已經(jīng)存在的內(nèi)容。然而如果本次操作只是寫(xiě)入了一半數(shù)據(jù)就出現(xiàn)了系統(tǒng)崩潰問(wèn)題,不用擔(dān)心,在Redis下一次啟動(dòng)之前,可以通過(guò) redis-check-aof 工具來(lái)解決數(shù)據(jù)一致性的問(wèn)題
-
Redis可以在 AOF文件體積變得過(guò)大時(shí),自動(dòng)地在后臺(tái)對(duì)AOF進(jìn)行重寫(xiě):重寫(xiě)后的新AOF文件包含了恢復(fù)當(dāng)前數(shù)據(jù)集所需的最小命令集合。整個(gè)重寫(xiě)操作是絕對(duì)安全的,因?yàn)镽edis在創(chuàng)建新 AOF文件的過(guò)程中,append模式不斷的將修改數(shù)據(jù)追加到現(xiàn)有的 AOF文件里面,即使重寫(xiě)過(guò)程中發(fā)生停 機(jī),現(xiàn)有的 AOF文件也不會(huì)丟失。而一旦新AOF文件創(chuàng)建完畢,Redis就會(huì)從舊AOF文件切換到新 AOF文件,并開(kāi)始對(duì)新AOF文件進(jìn)行追加操作。
-
AOF包含一個(gè)格式清晰、易于理解的日志文件用于記錄所有的修改操作。事實(shí)上,也可以通過(guò)該文件完成數(shù)據(jù)的重建AOF文件有序地保存了對(duì)數(shù)據(jù)庫(kù)執(zhí)行的所有寫(xiě)入操作,這些寫(xiě)入操作以Redis協(xié)議的格式保存,因此 AOF文件的內(nèi)容非常容易被人讀懂,對(duì)文件進(jìn)行分析(parse)也很輕松。導(dǎo)出(export)AOF文件 也非常簡(jiǎn)單:舉個(gè)例子,如果你不小心執(zhí)行了FLUSHALL.命令,但只要AOF文件未被重寫(xiě),那么只 要停止服務(wù)器,移除 AOF文件末尾的FLUSHAL命令,并重啟Redis ,就可以將數(shù)據(jù)集恢復(fù)到 FLUSHALL執(zhí)行之前的狀態(tài)。
AOF缺點(diǎn):
-
-
AOF 在恢復(fù)大數(shù)據(jù)集時(shí)的速度比 RDB 的恢復(fù)速度要慢
-
根據(jù)fsync策略不同,AOF速度可能會(huì)慢于RDB
-
bug 出現(xiàn)的可能性更多
2、master和slave同步過(guò)程
環(huán)境:
兩臺(tái)機(jī)器
10.0.0.8 master
10.0.0.18 slave
[root@centos8 ~]#yum -y install redis
[root@centos8 ~]#systemctl start redis
[root@centos18 ~]#systemctl start redis
[root@centos8 ~]#vim /etc/redis.conf
69 bind 0.0.0.0
88 protected-mode yes
308 replica-serve-stale-data yes
127.0.0.1:6379> CONFIG SET requirepass 123456
OK
#重啟服務(wù)
[root@centos8 ~]#systemctl restart redis
#從節(jié)點(diǎn)進(jìn)行設(shè)置
[root@centos18 ~]#redis-cli -h 10.0.0.8 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.0.0.8:6379>
#搭建主從節(jié)點(diǎn)
#登錄本機(jī),輸入主服務(wù)器的IP和端口號(hào)
#在slave上設(shè)置master的IP和端口,4.0版之前的指令為slaveof
127.0.0.1:6379> REPLICAOF 10.0.0.8 6379 #仍可使用SLAVEOF MasterIP Port
OK
#輸入主服務(wù)器的密碼進(jìn)行同步,進(jìn)行全量復(fù)制
#在slave上設(shè)置master的密碼,才可以同步
127.0.0.1:6379> CONFIG SET masterauth 123456
OK
127.0.0.1:6379> INFO replication
# Replication #角色變?yōu)閟lave
role:slave
master_host:10.0.0.8 #指向master
master_port:6379
master_link_status:up
master_last_io_seconds_ago:8
master_sync_in_progress:0
slave_repl_offset:42
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:b69908f23236fb20b810d198f7f4539f795e0ee5
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:42
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:42
[root@centos18 ~]#vim /etc/redis.conf
.......
# replicaof <masterip> <masterport>
replicaof 10.0.0.8 6379 #指定master的IP和端口號(hào)
# If the master is password protected (using the "requirepass" configuration
# directive below) it is possible to tell the replica to authenticate before
# starting the replication synchronization process, otherwise the master will
# refuse the replica request.
# masterauth <master-password>
masterauth 123456 #如果密碼需要設(shè)置
.......
注:主從搭建完畢,搭建成功,從節(jié)點(diǎn)只能讀不能寫(xiě)
3、哨兵的使用和實(shí)現(xiàn)機(jī)制
#前提是已經(jīng)實(shí)現(xiàn)了一主兩從的搭建
環(huán)境
master 10.0.0.8
slave1 10.0.0.18
slave2 10.0.0.28
#搭建一主兩從
#三臺(tái)機(jī)器安裝redis服務(wù)并啟動(dòng)
[root@centos8 ~]#yum -y install redis
[root@centos8 ~]#systemctl start redis
[root@centos18 ~]#yum -y install redis
[root@centos18 ~]#systemctl start redis
[root@centos28 ~]#yum -y install redis
[root@centos28 ~]#systemctl start redis
#主節(jié)點(diǎn)進(jìn)行配置
[root@centos8 ~]#vim /etc/redis.conf
69 bind 0.0.0.0
88 protected-mode yes
308 replica-serve-stale-data yes
324 replica-read-only yes
355 repl-diskless-sync no
367 repl-diskless-sync-delay 5
#設(shè)置連接密碼
[root@centos8 ~]#redis-cli
127.0.0.1:6379> CONFIG SET requirepass 123456
OK
#重啟服務(wù)
[root@centos8 ~]#systemctl restart redis
#從節(jié)點(diǎn)進(jìn)行設(shè)置
#查看是否能夠遠(yuǎn)程連接10.0.0.8
[root@centos18 ~]#redis-cli -h 10.0.0.8 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.0.0.8:6379>
#搭建主從節(jié)點(diǎn)
#登錄本機(jī),輸入主服務(wù)器的IP和端口號(hào)
#在slave上設(shè)置master的IP和端口,4.0版之前的指令為slaveof
127.0.0.1:6379> REPLICAOF 10.0.0.8 6379 #仍可使用SLAVEOF MasterIP Port
OK
#輸入主服務(wù)器的密碼進(jìn)行同步,進(jìn)行全量復(fù)制
#在slave上設(shè)置master的密碼,才可以同步
127.0.0.1:6379> CONFIG SET masterauth 123456
OK
127.0.0.1:6379> INFO replication
# Replication #角色變?yōu)閟lave
role:slave
master_host:10.0.0.8 #指向master
master_port:6379
master_link_status:up
master_last_io_seconds_ago:8
master_sync_in_progress:0
slave_repl_offset:42
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:b69908f23236fb20b810d198f7f4539f795e0ee5
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:42
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:42
#在所有slave節(jié)點(diǎn)保存配置到redis.conf
[root@centos18 ~]#vim /etc/redis.conf
.......
# replicaof <masterip> <masterport>
replicaof 10.0.0.8 6379 #指定master的IP和端口號(hào)
# If the master is password protected (using the "requirepass" configuration
# directive below) it is possible to tell the replica to authenticate before
# starting the replication synchronization process, otherwise the master will
# refuse the replica request.
# masterauth <master-password>
masterauth 123456 #如果密碼需要設(shè)置
.......
#查看是否能夠遠(yuǎn)程連接10.0.0.8
[root@centos28 ~]#redis-cli -h 10.0.0.8 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.0.0.8:6379>
#搭建主從節(jié)點(diǎn)
#登錄本機(jī),輸入主服務(wù)器的IP和端口號(hào)
#在slave上設(shè)置master的IP和端口,4.0版之前的指令為slaveof
127.0.0.1:6379> REPLICAOF 10.0.0.8 6379 #仍可使用SLAVEOF MasterIP Port
OK
#輸入主服務(wù)器的密碼進(jìn)行同步,進(jìn)行全量復(fù)制
#在slave上設(shè)置master的密碼,才可以同步
127.0.0.1:6379> CONFIG SET masterauth 123456
OK
127.0.0.1:6379> INFO replication
# Replication #角色變?yōu)閟lave
role:slave
master_host:10.0.0.8 #指向master
master_port:6379
master_link_status:up
master_last_io_seconds_ago:8
master_sync_in_progress:0
slave_repl_offset:42
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:b69908f23236fb20b810d198f7f4539f795e0ee5
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:42
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:42
#在所有slave節(jié)點(diǎn)保存配置到redis.conf
[root@centos28 ~]#vim /etc/redis.conf
.......
# replicaof <masterip> <masterport>
replicaof 10.0.0.8 6379 #指定master的IP和端口號(hào)
# If the master is password protected (using the "requirepass" configuration
# directive below) it is possible to tell the replica to authenticate before
# starting the replication synchronization process, otherwise the master will
# refuse the replica request.
# masterauth <master-password>
masterauth 123456 #如果密碼需要設(shè)置
.......
#所有主從節(jié)點(diǎn)的redis.conf中關(guān)鍵配置
#三臺(tái)機(jī)器全部配置成以下三步
[root@centos8 ~]#vim /etc/redis.conf
bind 0.0.0.0
masterauth "123456"
requirepass "123456"
[root@centos8 ~]#systemctl restart redis
[root@centos18 ~]#vim /etc/redis.conf
bind 0.0.0.0
masterauth "123456"
requirepass "123456"
[root@centos18 ~]#systemctl restart redis
[root@centos28 ~]#vim /etc/redis.conf
bind 0.0.0.0
masterauth "123456"

