前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Redis系列:单机主从模式搭建

Redis系列:单机主从模式搭建

作者头像
BUG弄潮儿
发布2020-09-28 09:55:12
5640
发布2020-09-28 09:55:12
举报
文章被收录于专栏:JAVA乐园

无论哪种中间件的搭建,正常主从模式搭建需要搭建在两台不同的服务器上才是正规的主从搭建模式。因为由于资源的限制,今天来演示一下在同一台服务器上,基于端口不一致搭建Redis的单机主从模式。

  • 如果是不同的Linux服务器搭建Redis主从模式,只需准备两台Linux服务器即可,例如两台Liunx服务器的IP地址为192.168.79.129和192.168.79.130。在这里两台Linux安装单机模式部署两台Redis,参考【Redis系列:Linux下部署Redis 6.x 版本】
  • 如何同一台Linux服务器搭建Redis主从模式,则先参考【Redis系列:Linux下部署Redis 6.x 版本】搭建单机版Redis,然后准备两个Redis的配置文件,例如分别为redis-6379.conf和redis-6380.conf,分别代表端口为6379的redis服务和端口为6380的redis服务。

0x01:基于单机版的redis.conf准备主从配置文件

执行以下命令复制两个配置文件

代码语言:javascript
复制
cp /usr/local/redis/etc/redis.conf /usr/local/redis/etc/redis-6379.conf
cp /usr/local/redis/etc/redis.conf /usr/local/redis/etc/redis-6380.conf

其中redis-6379.conf主,redis-6380.conf从。

0x02:修改redis-6379.conf和redis-6380.conf配置文件

分别先修改redis-6379.conf和redis-6380.conf配置文件的bind选项,绑定自己的IP地址。因为是同一台Linux服务器,所以这个配置项是一样的。

代码语言:javascript
复制
#redis-6379.conf
bind 127.0.0.1 192.168.122.1
#redis-6380.conf
bind 127.0.0.1 192.168.122.1

差异化修改

  • 主redis-6379.conf
代码语言:javascript
复制
port 6379
dbfilename dump-6379.rdb
requirepass new2020
pidfile /var/run/redis_6379.pid
  • 从redis-6380.conf
代码语言:javascript
复制
port 6380
dbfilename dump-6380.rdb
requirepass new2020
pidfile /var/run/redis_6380.pid
replicaof 192.168.122.1 6379
masterauth new2020

0x03:验证是否搭建成功

  • 使用如下命令,启动主Redis
代码语言:javascript
复制
./redis-server /usr/local/redis/etc/redis-6379.conf 

验证主Redis是否启动成功

代码语言:javascript
复制
./redis-cli -h 192.168.122.1 -p 6379
  • 使用如下命令,启动从Redis
代码语言:javascript
复制
./redis-server /usr/local/redis/etc/redis-6380.conf 

验证从Redis是否启动成功

  • 验证redis-6379与redis-6380是否形成主从模式

在redis-6379使用info命令,出现以下信息

代码语言:javascript
复制
192.168.122.1:6379> info
# Server
redis_version:6.0.7
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:e15fc28d4c56c2df
redis_mode:standalone
os:Linux 3.10.0-1062.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:9.3.1
process_id:4848
run_id:b2aed4fed2071bbfd2cf533e556e23646c6e2e86
tcp_port:6379
uptime_in_seconds:724
uptime_in_days:0
hz:10
configured_hz:10
lru_clock:7249429
executable:/usr/local/redis/bin/./redis-server
config_file:/usr/local/redis/etc/redis-6379.conf
io_threads_active:0

# Clients
connected_clients:1
client_recent_max_input_buffer:2
client_recent_max_output_buffer:0
blocked_clients:0
tracking_clients:0
clients_in_timeout_table:0

# Memory
used_memory:1937064
used_memory_human:1.85M
used_memory_rss:2805760
used_memory_rss_human:2.68M
used_memory_peak:1937064
used_memory_peak_human:1.85M
used_memory_peak_perc:100.08%
used_memory_overhead:1885804
used_memory_startup:803184
used_memory_dataset:51260
used_memory_dataset_perc:4.52%
allocator_allocated:2053032
allocator_active:2379776
allocator_resident:6713344
total_system_memory:1927323648
total_system_memory_human:1.79G
used_memory_lua:37888
used_memory_lua_human:37.00K
used_memory_scripts:0
used_memory_scripts_human:0B
number_of_cached_scripts:0
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
allocator_frag_ratio:1.16
allocator_frag_bytes:326744
allocator_rss_ratio:2.82
allocator_rss_bytes:4333568
rss_overhead_ratio:0.42
rss_overhead_bytes:-3907584
mem_fragmentation_ratio:1.45
mem_fragmentation_bytes:870248
mem_not_counted_for_evict:0
mem_replication_backlog:1048576
mem_clients_slaves:16986
mem_clients_normal:16986
mem_aof_buffer:0
mem_allocator:jemalloc-5.1.0
active_defrag_running:0
lazyfree_pending_objects:0

# Persistence
loading:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1601084822
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:0
rdb_current_bgsave_time_sec:-1
rdb_last_cow_size:262144
aof_enabled:0
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
module_fork_in_progress:0
module_fork_last_cow_size:0

# Stats
total_connections_received:46
total_commands_processed:134
instantaneous_ops_per_sec:1
total_net_input_bytes:11349
total_net_output_bytes:7679
instantaneous_input_kbps:0.04
instantaneous_output_kbps:0.00
rejected_connections:0
sync_full:1
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
expired_stale_perc:0.00
expired_time_cap_reached_count:0
expire_cycle_cpu_milliseconds:9
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:149
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
tracking_total_keys:0
tracking_total_items:0
tracking_total_prefixes:0
unexpected_error_replies:0
total_reads_processed:391
total_writes_processed:232
io_threaded_reads_processed:0
io_threaded_writes_processed:0

# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6380,state=online,offset=168,lag=0
master_replid:168adabf704ba8a93a9285e200093b84352d3ac7
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:168
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:168

# CPU
used_cpu_sys:0.320768
used_cpu_user:0.308592
used_cpu_sys_children:0.001414
used_cpu_user_children:0.000000

# Modules

# Cluster
cluster_enabled:0

# Keyspace
db0:keys=1,expires=0,avg_ttl=0
192.168.122.1:6379> 

在以上信息可以看到

在redis-6380使用info命令,出现以下信息

代码语言:javascript
复制
192.168.122.1:6380> info
# Server
redis_version:6.0.7
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:e15fc28d4c56c2df
redis_mode:standalone
os:Linux 3.10.0-1062.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:9.3.1
process_id:5288
run_id:589ba5025a924c2d962bbbe0a8f85913d4d2c50a
tcp_port:6380
uptime_in_seconds:295
uptime_in_days:0
hz:10
configured_hz:10
lru_clock:7249597
executable:/usr/local/redis/bin/./redis-server
config_file:/usr/local/redis/etc/redis-6380.conf
io_threads_active:0

# Clients
connected_clients:2
client_recent_max_input_buffer:2
client_recent_max_output_buffer:0
blocked_clients:0
tracking_clients:0
clients_in_timeout_table:0

# Memory
used_memory:1936344
used_memory_human:1.85M
used_memory_rss:2859008
used_memory_rss_human:2.73M
used_memory_peak:1936344
used_memory_peak_human:1.85M
used_memory_peak_perc:100.08%
used_memory_overhead:1885860
used_memory_startup:803208
used_memory_dataset:50484
used_memory_dataset_perc:4.46%
allocator_allocated:2191304
allocator_active:2502656
allocator_resident:4780032
total_system_memory:1927323648
total_system_memory_human:1.79G
used_memory_lua:37888
used_memory_lua_human:37.00K
used_memory_scripts:0
used_memory_scripts_human:0B
number_of_cached_scripts:0
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
allocator_frag_ratio:1.14
allocator_frag_bytes:311352
allocator_rss_ratio:1.91
allocator_rss_bytes:2277376
rss_overhead_ratio:0.60
rss_overhead_bytes:-1921024
mem_fragmentation_ratio:1.51
mem_fragmentation_bytes:965168
mem_not_counted_for_evict:0
mem_replication_backlog:1048576
mem_clients_slaves:0
mem_clients_normal:33972
mem_aof_buffer:0
mem_allocator:jemalloc-5.1.0
active_defrag_running:0
lazyfree_pending_objects:0

# Persistence
loading:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1601084822
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:-1
rdb_current_bgsave_time_sec:-1
rdb_last_cow_size:0
aof_enabled:0
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
module_fork_in_progress:0
module_fork_last_cow_size:0

# Stats
total_connections_received:1
total_commands_processed:30
instantaneous_ops_per_sec:0
total_net_input_bytes:649
total_net_output_bytes:10505
instantaneous_input_kbps:0.00
instantaneous_output_kbps:0.04
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
expired_stale_perc:0.00
expired_time_cap_reached_count:0
expire_cycle_cpu_milliseconds:0
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:0
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
tracking_total_keys:0
tracking_total_items:0
tracking_total_prefixes:0
unexpected_error_replies:0
total_reads_processed:32
total_writes_processed:295
io_threaded_reads_processed:0
io_threaded_writes_processed:0

# Replication
role:slave
master_host:192.168.122.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:7
master_sync_in_progress:0
slave_repl_offset:406
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:168adabf704ba8a93a9285e200093b84352d3ac7
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:406
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:406

# CPU
used_cpu_sys:0.095399
used_cpu_user:0.166949
used_cpu_sys_children:0.000000
used_cpu_user_children:0.000000

# Modules

# Cluster
cluster_enabled:0

# Keyspace
db0:keys=1,expires=0,avg_ttl=0
192.168.122.1:6380> 

在以上信息可以看到

说明redis-6379与redis-6380形成主从模式

0x04:Redis主从基本操作

  • 在主redis-6379写数据,在从redis-6380读数据

主redis-6379

从redis-6380

  • 在从redis-6380写数据

直接报错,显示不能进行写操作

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-09-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 BUG弄潮儿 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 Redis®
腾讯云数据库 Redis®(TencentDB for Redis®)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档