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

Posted JAVA乐园

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis系列:单机主从模式搭建相关的知识,希望对你有一定的参考价值。

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

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

  • 如何同一台Linux服务器搭建Redis主从模式,则先参考【搭建单机版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.conf
cp /usr/local/redis/etc/redis.conf /usr/local/redis/etc/redis-6380.conf

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

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

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

#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

port 6379
dbfilename dump-6379.rdb
requirepass new2020
pidfile /var/run/redis_6379.pid
  • redis-6380.conf

port 6380
dbfilename dump-6380.rdb
requirepass new2020
pidfile /var/run/redis_6380.pid
replicaof 192.168.122.1 6379
masterauth new2020

0x03:验证是否搭建成功
  • 使用如下命令,启动主Redis
./redis-server /usr/local/redis/etc/redis-6379.conf 
    验证主Redis是否启动成功
./redis-cli -h 192.168.122.1 -p 6379

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

  • 使用如下命令,启动从Redis

./redis-server /usr/local/redis/etc/redis-6380.conf 
     验证从Redis是否启动成功

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

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

redis-6379使用info命令,出现以下信息
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系列:单机主从模式搭建

在redis-6380使用info命令,出现以下信息
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系列:单机主从模式搭建


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

0x04:Redis主从基本操作
  • 在主redis-6379写数据,在从redis-6380读数据
主redis-6379

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

从redis-6380

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

  • 在从redis-6380写数据

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

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

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

扫码二维码

获取更多精彩

Java乐园

有用!分享+在看☟

以上是关于Redis系列:单机主从模式搭建的主要内容,如果未能解决你的问题,请参考以下文章

Redis 主从哨兵模式搭建

Redis系列1-redis主从哨兵模式集群模式搭建

Redis系列1-redis主从哨兵模式集群模式搭建

redis单机主从搭建

Redis单机,主从集群,哨兵集群,分片集群的搭建

Redis单机,主从集群,哨兵集群,分片集群的搭建