redis 主从模式_Redis系列:单机主从模式搭建

论坛 期权论坛 脚本     
已经匿名di用户   2022-5-29 19:19   2906   0

无论哪种中间件的搭建,正常主从模式搭建需要搭建在两台不同的服务器上才是正规的主从搭建模式。因为由于资源的限制,今天来演示一下在同一台服务器上,基于端口不一致搭建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准备主从配置文件

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

cp /usr/local/redis/etc/redis.conf /usr/local/redis/etc/redis-6379.confcp /usr/local/redis/etc/redis.conf /usr/local/redis/etc/redis-6380.conf

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

63e5bf29b2ca4791dfef4aed9c8b45b8.png

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

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

#redis-6379.confbind 127.0.0.1 192.168.122.1#redis-6380.confbind 127.0.0.1 192.168.122.1

差异化修改

  • 主redis-6379.conf
port 6379dbfilename dump-6379.rdbrequirepass new2020pidfile /var/run/redis_6379.pid
  • 从redis-6380.conf
port 6380dbfilename dump-6380.rdbrequirepass new2020pidfile /var/run/redis_6380.pidreplicaof 192.168.122.1 6379masterauth new2020

0x03:验证是否搭建成功

  • 使用如下命令,启动主Redis
./redis-server /usr/local/redis/etc/redis-6379.conf 

验证主Redis是否启动成功

./redis-cli -h 192.168.122.1 -p 6379
eef8812fc32dd9927e1a0eeae877d56d.png
  • 使用如下命令,启动从Redis
./redis-server /usr/local/redis/etc/redis-6380.conf 

验证从Redis是否启动成功

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

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

192.168.122.1:6379> info# Serverredis_version:6.0.7redis_git_sha1:00000000redis_git_dirty:0redis_build_id:e15fc28d4c56c2dfredis_mode:standaloneos:Linux 3.10.0-1062.el7.x86_64 x86_64arch_bits:64multiplexing_api:epollatomicvar_api:atomic-builtingcc_version:9.3.1process_id:4848run_id:b2aed4fed2071bbfd2cf533e556e23646c6e2e86tcp_port:6379uptime_in_seconds:724uptime_in_days:0hz:10configured_hz:10lru_clock:7249429executable:/usr/local/redis/bin/./redis-serverconfig_file:/usr/local/redis/etc/redis-6379.confio_threads_active:0# Clientsconnected_clients:1client_recent_max_input_buffer:2client_recent_max_output_buffer:0blocked_clients:0tracking_clients:0clients_in_timeout_table:0# Memoryused_memory:1937064used_memory_human:1.85Mused_memory_rss:2805760used_memory_rss_human:2.68Mused_memory_peak:1937064used_memory_peak_human:1.85Mused_memory_peak_perc:100.08%used_memory_overhead:1885804used_memory_startup:803184used_memory_dataset:51260used_memory_dataset_perc:4.52%allocator_allocated:2053032allocator_active:2379776allocator_resident:6713344total_system_memory:1927323648total_system_memory_human:1.79Gused_memory_lua:37888used_memory_lua_human:37.00Kused_memory_scripts:0used_memory_scripts_human:0Bnumber_of_cached_scripts:0maxmemory:0maxmemory_human:0Bmaxmemory_policy:noevictionallocator_frag_ratio:1.16allocator_frag_bytes:326744allocator_rss_ratio:2.82allocator_rss_bytes:4333568rss_overhead_ratio:0.42rss_overhead_bytes:-3907584mem_fragmentation_ratio:1.45mem_fragmentation_bytes:870248mem_not_counted_for_evict:0mem_replication_backlog:1048576mem_clients_slaves:16986mem_clients_normal:16986mem_aof_buffer:0mem_allocator:jemalloc-5.1.0active_defrag_running:0lazyfree_pending_objects:0# Persistenceloading:0rdb_changes_since_last_save:0rdb_bgsave_in_progress:0rdb_last_save_time:1601084822rdb_last_bgsave_status:okrdb_last_bgsave_time_sec:0rdb_current_bgsave_time_sec:-1rdb_last_cow_size:262144aof_enabled:0aof_rewrite_in_progress:0aof_rewrite_scheduled:0aof_last_rewrite_time_sec:-1aof_current_rewrite_time_sec:-1aof_last_bgrewrite_status:okaof_last_write_status:okaof_last_cow_size:0module_fork_in_progress:0module_fork_last_cow_size:0# Statstotal_connections_received:46total_commands_processed:134instantaneous_ops_per_sec:1total_net_input_bytes:11349total_net_output_bytes:7679instantaneous_input_kbps:0.04instantaneous_output_kbps:0.00rejected_connections:0sync_full:1sync_partial_ok:0sync_partial_err:0expired_keys:0expired_stale_perc:0.00expired_time_cap_reached_count:0expire_cycle_cpu_milliseconds:9evicted_keys:0keyspace_hits:0keyspace_misses:0pubsub_channels:0pubsub_patterns:0latest_fork_usec:149migrate_cached_sockets:0slave_expires_tracked_keys:0active_defrag_hits:0active_defrag_misses:0active_defrag_key_hits:0active_defrag_key_misses:0tracking_total_keys:0tracking_total_items:0tracking_total_prefixes:0unexpected_error_replies:0total_reads_processed:391total_writes_processed:232io_threaded_reads_processed:0io_threaded_writes_processed:0# Replicationrole:masterconnected_slaves:1slave0:ip=127.0.0.1,port=6380,state=online,offset=168,lag=0master_replid:168adabf704ba8a93a9285e200093b84352d3ac7master_replid2:0000000000000000000000000000000000000000master_repl_offset:168second_repl_offset:-1repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:1repl_backlog_histlen:168# CPUused_cpu_sys:0.320768used_cpu_user:0.308592used_cpu_sys_children:0.001414used_cpu_user_children:0.000000# Modules# Clustercluster_enabled:0# Keyspacedb0:keys=1,expires=0,avg_ttl=0192.168.122.1:6379> 

在以上信息可以看到

5ec876eded5f43437dd19e5b58f112d9.png

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

192.168.122.1:6380> info# Serverredis_version:6.0.7redis_git_sha1:00000000redis_git_dirty:0redis_build_id:e15fc28d4c56c2dfredis_mode:standaloneos:Linux 3.10.0-1062.el7.x86_64 x86_64arch_bits:64multiplexing_api:epollatomicvar_api:atomic-builtingcc_version:9.3.1process_id:5288run_id:589ba5025a924c2d962bbbe0a8f85913d4d2c50atcp_port:6380uptime_in_seconds:295uptime_in_days:0hz:10configured_hz:10lru_clock:7249597executable:/usr/local/redis/bin/./redis-serverconfig_file:/usr/local/redis/etc/redis-6380.confio_threads_active:0# Clientsconnected_clients:2client_recent_max_input_buffer:2client_recent_max_output_buffer:0blocked_clients:0tracking_clients:0clients_in_timeout_table:0# Memoryused_memory:1936344used_memory_human:1.85Mused_memory_rss:2859008used_memory_rss_human:2.73Mused_memory_peak:1936344used_memory_peak_human:1.85Mused_memory_peak_perc:100.08%used_memory_overhead:1885860used_memory_startup:803208used_memory_dataset:50484used_memory_dataset_perc:4.46%allocator_allocated:2191304allocator_active:2502656allocator_resident:4780032total_system_memory:1927323648total_system_memory_human:1.79Gused_memory_lua:37888used_memory_lua_human:37.00Kused_memory_scripts:0used_memory_scripts_human:0Bnumber_of_cached_scripts:0maxmemory:0maxmemory_human:0Bmaxmemory_policy:noevictionallocator_frag_ratio:1.14allocator_frag_bytes:311352allocator_rss_ratio:1.91allocator_rss_bytes:2277376rss_overhead_ratio:0.60rss_overhead_bytes:-1921024mem_fragmentation_ratio:1.51mem_fragmentation_bytes:965168mem_not_counted_for_evict:0mem_replication_backlog:1048576mem_clients_slaves:0mem_clients_normal:33972mem_aof_buffer:0mem_allocator:jemalloc-5.1.0active_defrag_running:0lazyfree_pending_objects:0# Persistenceloading:0rdb_changes_since_last_save:0rdb_bgsave_in_progress:0rdb_last_save_time:1601084822rdb_last_bgsave_status:okrdb_last_bgsave_time_sec:-1rdb_current_bgsave_time_sec:-1rdb_last_cow_size:0aof_enabled:0aof_rewrite_in_progress:0aof_rewrite_scheduled:0aof_last_rewrite_time_sec:-1aof_current_rewrite_time_sec:-1aof_last_bgrewrite_status:okaof_last_write_status:okaof_last_cow_size:0module_fork_in_progress:0module_fork_last_cow_size:0# Statstotal_connections_received:1total_commands_processed:30instantaneous_ops_per_sec:0total_net_input_bytes:649total_net_output_bytes:10505instantaneous_input_kbps:0.00instantaneous_output_kbps:0.04rejected_connections:0sync_full:0sync_partial_ok:0sync_partial_err:0expired_keys:0expired_stale_perc:0.00expired_time_cap_reached_count:0expire_cycle_cpu_milliseconds:0evicted_keys:0keyspace_hits:0keyspace_misses:0pubsub_channels:0pubsub_patterns:0latest_fork_usec:0migrate_cached_sockets:0slave_expires_tracked_keys:0active_defrag_hits:0active_defrag_misses:0active_defrag_key_hits:0active_defrag_key_misses:0tracking_total_keys:0tracking_total_items:0tracking_total_prefixes:0unexpected_error_replies:0total_reads_processed:32total_writes_processed:295io_threaded_reads_processed:0io_threaded_writes_processed:0# Replicationrole:slavemaster_host:192.168.122.1master_port:6379master_link_status:upmaster_last_io_seconds_ago:7master_sync_in_progress:0slave_repl_offset:406slave_priority:100slave_read_only:1connected_slaves:0master_replid:168adabf704ba8a93a9285e200093b84352d3ac7master_replid2:0000000000000000000000000000000000000000master_repl_offset:406second_repl_offset:-1repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:1repl_backlog_histlen:406# CPUused_cpu_sys:0.095399used_cpu_user:0.166949used_cpu_sys_children:0.000000used_cpu_user_children:0.000000# Modules# Clustercluster_enabled:0# Keyspacedb0:keys=1,expires=0,avg_ttl=0192.168.122.1:6380> 

在以上信息可以看到

1fba98fc8dd9d6da01be7fa45a1dd969.png

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

0x04:Redis主从基本操作

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

主redis-6379

9ba04b199adc8daf97f5f3b02100391c.png

从redis-6380

88de8f3163f6299fc503d02ae696ae88.png
  • 在从redis-6380写数据
8aa2c5eb4287b60923c3ad8ff13ce580.png

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

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:81
帖子:4969
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP