redis主从
host1: master
192.168.40.130
host2: slave 
192.168.40.132
https://download.redis.io/releases/redis-6.2.6.tar.gz
编译: mkdir -p /usr/local/redis-6.2.6 make install PREFIX=/usr/local/redis-6.2.6 cp redis.conf /usr/local/redis-6.2.6/bin/ cd /usr/local/redis-6.2.6/bin/ vim redis.conf #bind 127.0.0.1 -::1 daemonize yes requirepass lzjasdqq ./redis-server redis.conf ./redis-cli pkill -9 redis vim /usr/lib/systemd/system/redis.service [Unit] Description=redis-server After=network.target [Service] Type=forking # ExecStart需要按照实际情况修改成自己的地址 ExecStart=/usr/local/redis-6.2.6/bin/redis-server /usr/local/redis-6.2.6/bin/redis.conf PrivateTmp=true [Install] WantedBy=multi-user.target useradd -r redis -s /sbin/nologin systemctl daemon-reload systemctl start redis 启动复制功能: 1、使用用户端启用: 在slave上: > SLAVAOF MASTER_IP MASTER_PORT 例: 127.0.0.1:6379> slaveof 192.168.1.64 6379 //成为从库 OK 2、使用配置配置(在从库上操作): # vim /etc/redis.conf # slaveof <masterip> <masterport> //修改此项如下 slaveof 192.168.1.63 6379 [root@host3 bin]# ./redis-cli 127.0.0.1:6379> auth lzjasdqq OK 127.0.0.1:6379> help slaveof SLAVEOF host port summary: Make the server a replica of another instance, or promote it as master. Deprecated starting with Redis 5. Use REPLICAOF instead. since: 1.0.0 group: server 127.0.0.1:6379> SLAVEOF 192.168.40.130 6379 OK 127.0.0.1:6379> info # Server redis_version:6.2.6 redis_git_sha1:00000000 # Replication role:slave master_host:192.168.40.130 master_port:6379 master_link_status:down master_last_io_seconds_ago:-1 master_sync_in_progress:1 master_link_status:down sysctl vm.overcommit_memory=1 replicaof 192.168.40.130 5379 # If the master is password protected (using the "requirepass" configuration # directive below) it is possible to tell the replica to authenticate before # starting the replication synchronization process, otherwise the master will # refuse the replica request. # masterauth lzjasdqq info # Replication role:slave master_host:192.168.40.130 master_port:6379 master_link_status:up 主: 127.0.0.1:6379> keys * (empty array) 127.0.0.1:6379> set a 1 OK 127.0.0.1:6379> set b 2 OK 127.0.0.1:6379> INCR b (integer) 3 127.0.0.1:6379> get a "1" 127.0.0.1:6379> get b "3" 127.0.0.1:6379> del b (integer) 1127.0.0.1:6379> keys * 1) "a" 2) "b" 127.0.0.1:6379> get a "1" 127.0.0.1:6379> get b "3" 127.0.0.1:6379> keys * 1) "a" 2) "b" 127.0.0.1:6379> del b (error) READONLY You can't write against a read only replica. 127.0.0.1:6379> keys * 1) "a" 从redis只能读,主redis可以读也可以写