redis-server --port 6300[从服务器端口号] --slaveof 127.0.0.1[主服务器IP地址] 6379[端口号] --masterauth[主服务器密码(如果有的话)] # 从服务器 redis-server --port 6300 --slaveof 127.0.0.1 6379 # 客户端 [root@10-7-189-100 ~]# redis-cli -p 6300 127.0.0.1:6300> select 1 OK # 从主服务器上复制过来的数据 127.0.0.1:6300[1]> keys * 1) "1" # 只能读数据不能写入 127.0.0.1:6300[1]> set 1 2 (error) READONLY You can't write against a read only slave. 127.0.0.1:6300[1]> get 1 "2"
方式二(redis命令行)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# 当然也可以在服务启动后在命令行执行slaveof IP PORT 绑定主服务 # 客户端 127.0.0.1:6300[1]> slaveof 127.0.0.1 6379 OK # 主服务器 28696:M 08 Jan 16:53:10.291 * Synchronization with slave 127.0.0.1:6300 succeeded # 当然也可以执行slaveof no one来解除主从 # 客户端 127.0.0.1:6300[1]> slaveof no one OK # 服务器端 28135:M 08 Jan 16:51:34.637 # Connection with slave 127.0.0.1:6300 lost.