天天看點

redis 配置主從同步

redis 配置主從同步很簡單,主伺服器不需要單獨的設定,隻需要設定好 rdb 和aof 即可,我們這裡aof設定為 everysec;

有兩種方式配置主從,第一種在從伺服器配置檔案 redis.conf 配置 參數  slaveof masterIp masterPort 即可,第二種 在運作的從伺服器執行指令  slaveof masterIp masterPort 即可;(redis的主從鍊可以設定多個即從伺服器還可以設定從伺服器)。

redis 配置主從同步

配置好後 可以在主伺服器 通過指令  ./redis-cli info 檢視伺服器目前的主從同步資訊 

./redis-cli info           
# Server                             #伺服器的資訊
redis_version:4.0.10                #redis伺服器版本
redis_git_sha1:00000000            #Git SHA1
redis_git_dirty:0                     #Git dirty flag
redis_build_id:d2228e273f4aff2f       #redis build id
redis_mode:standalone                 #運作模式,單機或叢集 
os:Linux 3.10.0-862.el7.x86_64 x86_64 #redis伺服器主控端作業系統
arch_bits:64                      #架構64位
multiplexing_api:epoll                #redis所使用的事件處理模型
atomicvar_api:atomic-builtin        
gcc_version:4.8.5                           #編譯redis時gcc版本
process_id:6816                     #redis伺服器程序的pid
run_id:3f3ec65cf6ce6c4307d23f8ce5e1c437c31cde58 #redis伺服器的随機辨別符(sentinel和叢集)
tcp_port:6379                    #redis伺服器監聽端口
uptime_in_seconds:7437               #redis伺服器啟動總時間,機關秒
uptime_in_days:0                     #redis伺服器啟動總時間,機關天
hz:10                               #redis内部排程頻率(關閉timeout用戶端,删除過期key)
lru_clock:8197344                #自增時間,用于LRU管理
executable:/root/redis-server
config_file:/usr/local/redis/conf/redis.conf        #配置檔案路徑

# Clients                         #已連接配接用戶端資訊
connected_clients:1                   #已經連接配接用戶端數量(不包括slave連接配接的用戶端)
client_longest_output_list:0          #目前連接配接的用戶端當中,最長的輸出清單   
client_biggest_input_buf:0            #目前用戶端當中,最大輸入緩存。
blocked_clients:0                    #正在等待阻塞指令的用戶端數量


# Memory                          #記憶體資訊
used_memory:2995728                   #由redis配置設定器配置設定的記憶體總量,機關位元組
used_memory_human:2.86M               #以可讀方式傳回redis已配置設定的記憶體總量
used_memory_rss:4636672              #從作業系統角度,傳回redis已配置設定記憶體總量
used_memory_rss_human:4.42M           #以可讀方式傳回redis已配置設定的記憶體總量
used_memory_peak:15517016             #redis的記憶體消耗峰值(以位元組為機關)
used_memory_peak_human:14.80M         #以可讀方式傳回redis記憶體消耗峰值
used_memory_peak_perc:19.31%
used_memory_overhead:1934516
used_memory_startup:786608
used_memory_dataset:1061212
used_memory_dataset_perc:48.04%
total_system_memory:2229866496
total_system_memory_human:2.08G
used_memory_lua:37888                 #lua引擎所使用的記憶體大小(機關位元組)
used_memory_lua_human:37.00K
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
mem_fragmentation_ratio:1.55         #used_memory_rss和used_memory比率,小于1表示使用了swap,大于1表示碎片多,redis進行增加删除的動作,會引起記憶體碎片化
mem_allocator:jemalloc-4.0.3         #編譯時指定的redis的記憶體配置設定器。越好的配置設定器記憶體碎片化率越低,低版本建議更新
active_defrag_running:0
lazyfree_pending_objects:0

# Persistence                      #rdb和aof的持久化相關資訊              
loading:0                           #伺服器是否正在載入持久化檔案
rdb_changes_since_last_save:0        #有多少個已經寫入的指令還未被持久化
rdb_bgsave_in_progress:0             #伺服器是否正在建立rdb檔案
rdb_last_save_time:1534918159        #已經有多長時間沒有進行持久化了
rdb_last_bgsave_status:ok            #最後一次的rdb持久化是否成功
rdb_last_bgsave_time_sec:0           #最後一次生成rdb檔案耗時秒數
rdb_current_bgsave_time_sec:-1       #如果伺服器正在建立rdb檔案,那麼目前這個記錄就是建立操作耗時秒數
rdb_last_cow_size:438272
aof_enabled:1                       #是否開啟了aof
aof_rewrite_in_progress:0          #辨別aof的rewrite操作是否進行中
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok         #上次bgrewriteaof操作的狀态
aof_last_write_status:ok             #上一次aof寫入狀态
aof_last_cow_size:581632
aof_current_size:2634673
aof_base_size:2473591
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:220209     #新建立的連結個數,如果過多,會影響性能
total_commands_processed:447805       #redis處理的指令數
instantaneous_ops_per_sec:0         #redis目前的qps,redis内部較實時的每秒執行指令數
total_net_input_bytes:13682780        #redis網絡入口流量位元組數
total_net_output_bytes:10737650       #redis網絡出口流量位元組數
instantaneous_input_kbps:0.02         #redis網絡入口kps
instantaneous_output_kbps:0.00        #redis網絡出口kps
rejected_connections:0                #拒絕的連接配接個數,redis連接配接個數已經達到maxclients限制。
sync_full:1                           #主從完全同步成功次數
sync_partial_ok:0                 #主從部分同步成功次數
sync_partial_err:1                  #主從部分同步失敗次數
expired_keys:0                    #運作以來過期的key的數量
expired_stale_perc:0.00
expired_time_cap_reached_count:0
evicted_keys:0                        #運作以來剔除(超過maxmemory)的key的數量
keyspace_hits:0                     #命中次數
keyspace_misses:0                     #沒命中次數
pubsub_channels:0                     #目前使用中的頻道數量
pubsub_patterns:0                   #目前使用的模式數量
latest_fork_usec:401
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:1
slave0:ip=192.168.50.167,port=6380,state=online,offset=7672755,lag=0
master_replid:2757c33a80f71d262335ecb6ce2c6fdca96ee09e
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:7672755           #主從同步偏移量(通過主從對比判斷主從同步是否一緻)
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:6624180
repl_backlog_histlen:1048576

# CPU
used_cpu_sys:23.63
used_cpu_user:13.16
used_cpu_sys_children:0.31
used_cpu_user_children:0.14

# Cluster              #叢集相關資訊
cluster_enabled:0

# Keyspace           #資料庫相關資訊
db0:keys=1,expires=0,avg_ttl=0         #db0的key數量以及帶有生存周期的key的個數,平均存活時間           

然後在主伺服器設定一個值,在從伺服器能查詢到即說明設定成功。

繼續閱讀