1. 程式人生 > 其它 >python中title函式的作用

python中title函式的作用

技術標籤:Redisredis

2.2:Redis 主從複製

2.2.1:拓撲圖

在這裡插入圖片描述

Master:192.168.1.201

Slave:192.168.1.202

2.2.2:臨時實現主從複製

通過命令列實現的主從複製關係,在重啟 Redis 後會失效;

Master 寫入測試資料

shell 指令碼:

[[email protected] ~]# vim redis.sh 
#!/bin/bash
NUM=`seq 1 1000`
PASS='123456'
for i in ${NUM};do
  redis-cli -h 127.0.0.1 -a ${PASS} set key-${i} value-${i} &>/dev/null
done

echo "資料寫入完成"

執行指令碼,寫入資料:

[[email protected] ~]# bash redis.sh        
資料寫入完成

檢視 Master 資料:

127.0.0.1:6379> GET key-111
"value-111"
127.0.0.1:6379> GET key-999
"value-999"

Slave 命令列配置

指定 Master 為 192.168.1.201:

127.0.0.1:6379> SLAVEOF 192.168.1.201 6379

設定 Master 連線的認證密碼:

127.0.0.1:6379> CONFIG SET masterauth 123456

驗證主從複製

檢視 Master 日誌:

[[email protected] ~]# tail -f /apps/redis/logs/redis_6379.log 
……
6538:M 04 Jan 11:04:25.156 * Slave 192.168.1.202:6379 asks for synchronization
6538:M 04 Jan 11:04:25.156 * Full resync requested by slave 192.168.1.202:6379
6538:M 04 Jan 11:04:26.684 * Starting BGSAVE for SYNC with target: slaves sockets
6538:M 04 Jan 11:04:26.686 * Background RDB transfer started by pid 7574
7574:C 04 Jan 11:04:26.694 * RDB: 6 MB of memory used by copy-on-write
6538:M 04 Jan 11:04:26.789 * Background RDB transfer terminated with success
6538:M 04 Jan 11:04:26.789 # Slave 192.168.1.202:6379 correctly received the streamed RDB file.
6538:M 04 Jan 11:04:26.789 * Streamed RDB transfer with slave 192.168.1.202:6379 succeeded (socket). Waiting for REPLCONF ACK from slave to enable streaming
6538:M 04 Jan 11:04:27.194 * Synchronization with slave 192.168.1.202:6379 succeeded

檢視 Slave 日誌:

[[email protected] ~]# tail -f /apps/redis/logs/redis_6379.log
……
2511:S 04 Jan 11:04:25.146 * Connecting to MASTER 192.168.1.201:6379
2511:S 04 Jan 11:04:25.146 * MASTER <-> SLAVE sync started
2511:S 04 Jan 11:04:25.147 * Non blocking connect for SYNC fired the event.
2511:S 04 Jan 11:04:25.150 * Master replied to PING, replication can continue...
2511:S 04 Jan 11:04:25.156 * Partial resynchronization not possible (no cached master)
2511:S 04 Jan 11:04:26.685 * Full resync from master: d8b79f11217860aeb81a74da06408af954e306f4:0
2511:S 04 Jan 11:04:26.692 * MASTER <-> SLAVE sync: receiving streamed RDB from master
2511:S 04 Jan 11:04:26.696 * MASTER <-> SLAVE sync: Flushing old data
2511:S 04 Jan 11:04:26.696 * MASTER <-> SLAVE sync: Loading DB in memory
2511:S 04 Jan 11:04:26.699 * MASTER <-> SLAVE sync: Finished with success
2511:S 04 Jan 11:04:26.701 * Background append only file rewriting started by pid 2519
2511:S 04 Jan 11:04:26.771 * AOF rewrite child asks to stop sending diffs.
2519:C 04 Jan 11:04:26.771 * Parent agreed to stop sending diffs. Finalizing AOF...
2519:C 04 Jan 11:04:26.771 * Concatenating 0.00 MB of AOF diff received from parent.
2519:C 04 Jan 11:04:26.771 * SYNC append only file rewrite performed
2519:C 04 Jan 11:04:26.772 * AOF rewrite: 6 MB of memory used by copy-on-write
2511:S 04 Jan 11:04:26.784 * Background AOF rewrite terminated with success
2511:S 04 Jan 11:04:26.784 * Residual parent diff successfully flushed to the rewritten AOF (0.00 MB)
2511:S 04 Jan 11:04:26.784 * Background AOF rewrite finished successfully

檢視 Slave 中同步過來的資料:

127.0.0.1:6379> GET key-1000
"value-1000"

Master 的 Replication 資訊:

127.0.0.1:6379> INFO Replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.1.202,port=6379,state=online,offset=420,lag=1
master_replid:d8b79f11217860aeb81a74da06408af954e306f4
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:420
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:104857600
repl_backlog_first_byte_offset:1
repl_backlog_histlen:420

Slave 的 Replication 資訊:

127.0.0.1:6379> INFO Replication
# Replication
role:slave
master_host:192.168.1.201
master_port:6379
master_link_status:up
master_last_io_seconds_ago:4
master_sync_in_progress:0
slave_repl_offset:448
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:d8b79f11217860aeb81a74da06408af954e306f4
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:448
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:104857600
repl_backlog_first_byte_offset:1
repl_backlog_histlen:448

2.2.3:永久實現主從複製

一般採用編輯配置檔案的方式來儲存主從複製關係,再次啟動 Redis 時,主從複製關係仍然存在;

編輯 Slave 配置檔案

設定 Master 為 192.168.1.201,並配置 Master 連線密碼:

[[email protected] ~]# vim /apps/redis/etc/redis.conf
slaveof 192.168.1.201 6379
masterauth 123456

重啟 Slave 的 Redis 程序

[[email protected] ~]# systemctl restart redis

驗證主從複製

檢視主從複製關係:

master_link_status:up,在 Slave 重啟後主從複製關係仍然存在;

127.0.0.1:6379> INFO Replication
# Replication
role:slave
master_host:192.168.1.201
master_port:6379
master_link_status:up
master_last_io_seconds_ago:5
master_sync_in_progress:0
slave_repl_offset:728
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:d8b79f11217860aeb81a74da06408af954e306f4
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:728
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:104857600
repl_backlog_first_byte_offset:631
repl_backlog_histlen:98

Master 新增資料:

127.0.0.1:6379> SET key-1001 value-1001
OK
127.0.0.1:6379> SET key-test value-test
OK

Slave 驗證同步資料:

127.0.0.1:6379> GET key-1001
"value-1001"
127.0.0.1:6379> GET key-test
"value-test"

2.2.4:模擬 Master 不可用

驗證 Slave 的只讀狀態

配置檔案中設定了 slave-read-only yes,所以 Slave 節點為只讀:

127.0.0.1:6379> SET foo bar
(error) READONLY You can't write against a read only slave.

停止 Master 的 Redis 程序

停止 Redis 程序:

[[email protected] ~]# systemctl stop redis

Slave 日誌中顯示已失去與 Master 的連線:

2590:S 04 Jan 11:20:40.223 # Connection with master lost.
2590:S 04 Jan 11:20:40.223 * Caching the disconnected master state.
2590:S 04 Jan 11:20:40.314 * Connecting to MASTER 192.168.1.201:6379
2590:S 04 Jan 11:20:40.314 * MASTER <-> SLAVE sync started
2590:S 04 Jan 11:20:40.315 # Error condition on socket for SYNC: Connection refused

Slave 的 Replication 狀態:

master_link_status:down,主從複製關係已 down;

127.0.0.1:6379> INFO Replication
# Replication
role:slave
master_host:192.168.1.201
master_port:6379
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:1413
master_link_down_since_seconds:103
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:d8b79f11217860aeb81a74da06408af954e306f4
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1413
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:104857600
repl_backlog_first_byte_offset:631
repl_backlog_histlen:783

手動將 Slave 提升為 Master

手動取消之前的主從複製關係:

127.0.0.1:6379> SLAVEOF no one
OK

Slave 日誌:

2590:M 04 Jan 11:23:12.376 # Setting secondary replication ID to d8b79f11217860aeb81a74da06408af954e306f4, valid up to offset: 1414. New replication ID is 37c7c687adda7c9d53578ff8aa16e403413c8a8f
2590:M 04 Jan 11:23:12.376 * Discarding previously cached master state.
2590:M 04 Jan 11:23:12.376 * MASTER MODE enabled (user request from 'id=4 addr=127.0.0.1:52112 fd=7 name= age=596 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=slaveof')

驗證 Slave 已提升為 Master

檢視當前的 Replicaiton 狀態:

角色已成為 master,且之前的 master_replid 已成為 master_replid2;

127.0.0.1:6379> INFO Replication
# Replication
role:master
connected_slaves:0
master_replid:37c7c687adda7c9d53578ff8aa16e403413c8a8f
master_replid2:d8b79f11217860aeb81a74da06408af954e306f4
master_repl_offset:1413
second_repl_offset:1414
repl_backlog_active:1
repl_backlog_size:104857600
repl_backlog_first_byte_offset:631
repl_backlog_histlen:783

驗證寫入操作:

127.0.0.1:6379> SET foo bar
OK
127.0.0.1:6379> GET foo
"bar"

此時之前的 Slave 已經可以單獨對外提供服務了;

2.2.5:將之前的 Master 作為 Slave 新增進來

啟動 Redis 並指定 Master

啟動 192.168.1.201(原 Master)的 Redis 程序,並設為 192.168.1.202 的 Slave:

[[email protected] ~]# systemctl start redis
[[email protected] ~]# redis-cli
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> SLAVEOF 192.168.1.202 6379
OK
127.0.0.1:6379> CONFIG SET masterauth 123456
OK

驗證新的主從複製關係

Master 的 Replication 狀態:

127.0.0.1:6379> INFO Replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.1.201,port=6379,state=online,offset=1607,lag=0
master_replid:37c7c687adda7c9d53578ff8aa16e403413c8a8f
master_replid2:d8b79f11217860aeb81a74da06408af954e306f4
master_repl_offset:1607
second_repl_offset:1414
repl_backlog_active:1
repl_backlog_size:104857600
repl_backlog_first_byte_offset:631
repl_backlog_histlen:977

Slave 的 Replication 狀態:

127.0.0.1:6379> INFO Replication
# Replication
role:slave
master_host:192.168.1.202
master_port:6379
master_link_status:up
master_last_io_seconds_ago:10
master_sync_in_progress:0
slave_repl_offset:1635
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:37c7c687adda7c9d53578ff8aa16e403413c8a8f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1635
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:104857600
repl_backlog_first_byte_offset:1468
repl_backlog_histlen:168

Master 日誌:

2590:M 04 Jan 11:32:15.306 * Slave 192.168.1.201:6379 asks for synchronization
2590:M 04 Jan 11:32:15.306 * Full resync requested by slave 192.168.1.201:6379
2590:M 04 Jan 11:32:16.768 * Starting BGSAVE for SYNC with target: slaves sockets
2590:M 04 Jan 11:32:16.769 * Background RDB transfer started by pid 2615
2615:C 04 Jan 11:32:16.776 * RDB: 6 MB of memory used by copy-on-write
2590:M 04 Jan 11:32:16.877 * Background RDB transfer terminated with success
2590:M 04 Jan 11:32:16.877 # Slave 192.168.1.201:6379 correctly received the streamed RDB file.
2590:M 04 Jan 11:32:16.877 * Streamed RDB transfer with slave 192.168.1.201:6379 succeeded (socket). Waiting for REPLCONF ACK from slave to enable streaming
2590:M 04 Jan 11:32:17.486 * Synchronization with slave 192.168.1.201:6379 succeeded

Slave 日誌:

7621:S 04 Jan 11:32:15.302 * Connecting to MASTER 192.168.1.202:6379
7621:S 04 Jan 11:32:15.302 * MASTER <-> SLAVE sync started
7621:S 04 Jan 11:32:15.303 * Non blocking connect for SYNC fired the event.
7621:S 04 Jan 11:32:15.304 * Master replied to PING, replication can continue...
7621:S 04 Jan 11:32:15.306 * Partial resynchronization not possible (no cached master)
7621:S 04 Jan 11:32:16.769 * Full resync from master: 37c7c687adda7c9d53578ff8aa16e403413c8a8f:1467
7621:S 04 Jan 11:32:16.778 * MASTER <-> SLAVE sync: receiving streamed RDB from master
7621:S 04 Jan 11:32:16.782 * MASTER <-> SLAVE sync: Flushing old data
7621:S 04 Jan 11:32:16.783 * MASTER <-> SLAVE sync: Loading DB in memory
7621:S 04 Jan 11:32:16.786 * MASTER <-> SLAVE sync: Finished with success
7621:S 04 Jan 11:32:16.787 * Background append only file rewriting started by pid 7626
7621:S 04 Jan 11:32:17.080 * AOF rewrite child asks to stop sending diffs.
7626:C 04 Jan 11:32:17.081 * Parent agreed to stop sending diffs. Finalizing AOF...
7626:C 04 Jan 11:32:17.081 * Concatenating 0.00 MB of AOF diff received from parent.
7626:C 04 Jan 11:32:17.081 * SYNC append only file rewrite performed
7626:C 04 Jan 11:32:17.082 * AOF rewrite: 6 MB of memory used by copy-on-write
7621:S 04 Jan 11:32:17.158 * Background AOF rewrite terminated with success
7621:S 04 Jan 11:32:17.159 * Residual parent diff successfully flushed to the rewritten AOF (0.00 MB)
7621:S 04 Jan 11:32:17.159 * Background AOF rewrite finished successfully

驗證 Slave 資料:

127.0.0.1:6379> GET foo
"bar"
127.0.0.1:6379> GET key-222
"value-222"

Master 的切換會導致 master_replid 發生變化,Slave 之前的 master_replid 就和當前 Master 不一致,從而會引發所有 Slave 的全量同步。