1、安装redis 3台(我这里用脚步安装的)
[root@ubuntu:~]# apt install redis # 3台
2、进入redis
[root@ubuntu:~]# redis-cli -a 123456 # 我这是用的脚本,我直接设置密码了
# 你也可以设置密码
[root@ubuntu:~]# vim /etc/redis/redis.conf
requirepass 123456
3、配置2台从服务器连接主服务器
[root@ubuntu:~]# redis-cli -a 123456
127.0.0.1:6379> REPLICAOF 10.0.0.212 6379
OK
127.0.0.1:6379> CONFIG set masterauth 123456
OK
4、主从配置完成。(如果要高可用,可以搭建Sentinel,可以我帮你安装)[root@ubuntu2:~]# redis-cli -a 123456 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 127.0.0.1:6379> info # Server redis_version:7.2.4 redis_git_sha1:00000000 redis_git_dirty:0 redis_build_id:bf11cda69fab264a redis_mode:standalone os:Linux 5.15.0-91-generic x86_64 arch_bits:64 monotonic_clock:POSIX clock_gettime multiplexing_api:epoll atomicvar_api:c11-builtin gcc_version:11.4.0 process_id:9528 process_supervised:systemd run_id:bec0bafd94756b179ded1253622f1c129a5fc7b1 tcp_port:6379 server_time_usec:1714051633784386 uptime_in_seconds:23142 uptime_in_days:0 hz:10 configured_hz:10 lru_clock:2775601 executable:/apps/redis/bin/redis-server config_file:/apps/redis/etc/redis.conf io_threads_active:0 listener0:name=tcp,bind=0.0.0.0,bind=-::1,port=6379 # Clients connected_clients:2 cluster_connections:0 maxclients:10000 client_recent_max_input_buffer:20480 client_recent_max_output_buffer:0 blocked_clients:0 tracking_clients:0 clients_in_timeout_table:0 total_blocking_keys:0 total_blocking_keys_on_nokey:0 # Memory used_memory:1206776 used_memory_human:1.15M used_memory_rss:8769536 used_memory_rss_human:8.36M used_memory_peak:1425424 used_memory_peak_human:1.36M used_memory_peak_perc:84.66% used_memory_overhead:931628 used_memory_startup:866016 used_memory_dataset:275148 used_memory_dataset_perc:80.75% allocator_allocated:1496344 allocator_active:1761280 allocator_resident:4169728 total_system_memory:2021871616 total_system_memory_human:1.88G used_memory_lua:31744 used_memory_vm_eval:31744 used_memory_lua_human:31.00K used_memory_scripts_eval:0 number_of_cached_scripts:0 number_of_functions:0 number_of_libraries:0 used_memory_vm_functions:32768 used_memory_vm_total:64512 used_memory_vm_total_human:63.00K used_memory_functions:184 used_memory_scripts:184 used_memory_scripts_human:184B maxmemory:0 maxmemory_human:0B maxmemory_policy:noeviction allocator_frag_ratio:1.18 allocator_frag_bytes:264936 allocator_rss_ratio:2.37 allocator_rss_bytes:2408448 rss_overhead_ratio:2.10 rss_overhead_bytes:4599808 mem_fragmentation_ratio:7.28 mem_fragmentation_bytes:7564584 mem_not_counted_for_evict:0 mem_replication_backlog:41012 mem_total_replication_buffers:41008 mem_clients_slaves:0 mem_clients_normal:24344 mem_cluster_links:0 mem_aof_buffer:0 mem_allocator:jemalloc-5.3.0 active_defrag_running:0 lazyfree_pending_objects:0 lazyfreed_objects:0 # Persistence loading:0 async_loading:0 current_cow_peak:0 current_cow_size:0 current_cow_size_age:0 current_fork_perc:0.00 current_save_keys_processed:0 current_save_keys_total:0 rdb_changes_since_last_save:0 rdb_bgsave_in_progress:0 rdb_last_save_time:1714032092 rdb_last_bgsave_status:ok rdb_last_bgsave_time_sec:0 rdb_current_bgsave_time_sec:-1 rdb_saves:1 rdb_last_cow_size:753664 rdb_last_load_keys_expired:0 rdb_last_load_keys_loaded: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_rewrites:0 aof_rewrites_consecutive_failures:0 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:4 total_commands_processed:2288 instantaneous_ops_per_sec:1 total_net_input_bytes:199516 total_net_output_bytes:1488065 total_net_repl_input_bytes:199066 total_net_repl_output_bytes:0 instantaneous_input_kbps:0.04 instantaneous_output_kbps:123.24 instantaneous_input_repl_kbps:0.01 instantaneous_output_repl_kbps:0.00 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:4 evicted_keys:0 evicted_clients:0 total_eviction_exceeded_time:0 current_eviction_exceeded_time:0 keyspace_hits:1 keyspace_misses:0 pubsub_channels:0 pubsub_patterns:0 pubsubshard_channels:0 latest_fork_usec:6838 total_forks:1 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 total_active_defrag_time:0 current_active_defrag_time:0 tracking_total_keys:0 tracking_total_items:0 tracking_total_prefixes:0 unexpected_error_replies:0 total_error_replies:1 dump_payload_sanitizations:0 total_reads_processed:2291 total_writes_processed:22732 io_threaded_reads_processed:0 io_threaded_writes_processed:0 reply_buffer_shrinks:7 reply_buffer_expands:3 eventloop_cycles:261078 eventloop_duration_sum:56567911 eventloop_duration_cmd_sum:20107 instantaneous_eventloop_cycles_per_sec:12 instantaneous_eventloop_duration_usec:453 acl_access_denied_auth:0 acl_access_denied_cmd:0 acl_access_denied_key:0 acl_access_denied_channel:0 # Replication role:slave # 从节点,另一台也是从节点 master_host:10.0.0.212 master_port:6379 master_link_status:up master_last_io_seconds_ago:1 master_sync_in_progress:0 slave_read_repl_offset:31834 slave_repl_offset:31834 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:6c2cd471fc3c969cfbb3b9f6851225a04778cc19 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:31834 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:31834 # CPU used_cpu_sys:39.914272 used_cpu_user:32.077390 used_cpu_sys_children:0.016718 used_cpu_user_children:0.004179 used_cpu_sys_main_thread:39.687808 used_cpu_user_main_thread:31.899536 # Modules # Errorstats errorstat_ERR:count=1 # Cluster cluster_enabled:0 # Keyspace db0:keys=1,expires=0,avg_ttl=0 # 查看主节点 127.0.0.1:6379> info role:master connected_slaves:2 # 两台从节点 slave0:ip=10.0.0.213,port=6379,state=online,offset=31946,lag=0 slave1:ip=10.0.0.214,port=6379,state=online,offset=31946,lag=0