# 以"192.168.1.100:16370"实例为主库,以"192.168.1.100:16371"实例和"192.168.1.100:16372"实例为从库。 # -a 参数指定密码 # 从库实例开启主从 redis-cli -h 192.168.1.100 -p 16371 -a dongdong slaveof 192.168.1.100 16370 redis-cli -h 192.168.1.100 -p 16372 -a dongdong slaveof 192.168.1.100 16370 # Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. # 警告:在命令行界面上使用带有“-a”或“-u”选项的密码可能不安全。 可以忽略 # 解决方式 # redis-cli -h 192.168.1.100 -p 16371 -a dongdong --no-auth-warning slaveof 192.168.1.100 16370
5.2 主库实例查看主从信息
redis-cli -h 192.168.1.100 -p 16370 -a dongdong info replication [root@master-slave ~]# redis-cli -h 192.168.1.100 -p 16370 -a dongdong info replication Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. # Replication role:master connected_slaves:2 slave0:ip=192.168.1.100,port=16371,state=online,offset=8428,lag=0 slave1:ip=192.168.1.100,port=16372,state=online,offset=8428,lag=1 master_failover_state:no-failover master_replid:a292603e2c96a6f451055e6d84a60795f81f2928 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:8428 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:8428
5.3 从库实例查看主从信息
# 192.168.1.100:16371 查看主从信息 redis-cli -h 192.168.1.100 -p 16371 -a dongdong info replication [root@master-slave ~]# redis-cli -h 192.168.1.100 -p 16371 -a dongdong info replication Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. # Replication role:slave master_host:192.168.1.100 master_port:16370 master_link_status:up master_last_io_seconds_ago:0 master_sync_in_progress:0 slave_read_repl_offset:8680 slave_repl_offset:8680 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:a292603e2c96a6f451055e6d84a60795f81f2928 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:8680 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:15 repl_backlog_histlen:8666
5.4 验证主从配置是否生效
# "192.168.1.100:16370"实例主库的11号数据库创建测试数据 redis-cli -h 192.168.1.100 -p 16370 -a dongdong -n 11 [root@master-slave ~]# redis-cli -h 192.168.1.100 -p 16370 -a dongdong -n 11 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.1.100:16370[11]> keys * (empty array) 192.168.1.100:16370[11]> set name dongdong OK 192.168.1.100:16370[11]> set age 18 OK 192.168.1.100:16370[11]> keys * 1) "age" 2) "name" # "192.168.1.100:16371"实例从库的11号数据库查看是否同步主库的11号数据库 redis-cli -h 192.168.1.100 -p 16371 -a dongdong -n 11 [root@master-slave ~]# redis-cli -h 192.168.1.100 -p 16371 -a dongdong -n 11 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.1.100:16371[11]> keys * 1) "name" 2) "age" 192.168.1.100:16371[11]> get name "dongdong" 192.168.1.100:16371[11]> get age "18" 192.168.1.100:16371[11]> # "192.168.1.100:16372"实例从库的11号数据库查看是否同步主库的11号数据库 redis-cli -h 192.168.1.100 -p 16372 -a dongdong -n 11 [root@master-slave ~]# redis-cli -h 192.168.1.100 -p 16372 -a dongdong -n 11 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.1.100:16372[11]> keys * 1) "age" 2) "name" 192.168.1.100:16372[11]> get name "dongdong" 192.168.1.100:16372[11]> get age "18" 192.168.1.100:16372[11]>
6. 解除 192.168.1.100:16372 实例主从
# 192.168.1.100:16372 查看主从信息 redis-cli -h 192.168.1.100 -p 16372 -a dongdong info replication [root@master-slave ~]# redis-cli -h 192.168.1.100 -p 16372 -a dongdong info replication Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. # Replication role:slave master_host:192.168.1.100 master_port:16370 master_link_status:up master_last_io_seconds_ago:4 master_sync_in_progress:0 slave_read_repl_offset:10003 slave_repl_offset:10003 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:a292603e2c96a6f451055e6d84a60795f81f2928 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:10003 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:155 repl_backlog_histlen:9849 # 192.168.1.100:16370 查看主从信息 redis-cli -h 192.168.1.100 -p 16370 -a dongdong info replication [root@master-slave ~]# redis-cli -h 192.168.1.100 -p 16370 -a dongdong info replication Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. # Replication role:master connected_slaves:2 slave0:ip=192.168.1.100,port=16371,state=online,offset=10157,lag=0 slave1:ip=192.168.1.100,port=16372,state=online,offset=10157,lag=0 master_failover_state:no-failover master_replid:a292603e2c96a6f451055e6d84a60795f81f2928 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:10157 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:10157 # 解除 192.168.1.100:16372 主从信息 redis-cli -h 192.168.1.100 -p 16372 -a dongdong slaveof no one [root@master-slave ~]# redis-cli -h 192.168.1.100 -p 16372 -a dongdong slaveof no one Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. OK # 再次查看 192.168.1.100:16370 查看主从信息 redis-cli -h 192.168.1.100 -p 16370 -a dongdong info replication [root@master-slave ~]# redis-cli -h 192.168.1.100 -p 16370 -a dongdong info replication Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. # Replication role:master connected_slaves:1 slave0:ip=192.168.1.100,port=16371,state=online,offset=10731,lag=0 master_failover_state:no-failover master_replid:a292603e2c96a6f451055e6d84a60795f81f2928 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:10731 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:10731 # 只剩下一个 从节点了 # 把192.168.1.100:16372 添加回来 redis-cli -h 192.168.1.100 -p 16372 -a dongdong slaveof 192.168.1.100 16370 [root@master-slave ~]# redis-cli -h 192.168.1.100 -p 16372 -a dongdong slaveof 192.168.1.100 16370 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. OK # 此时再看192.168.1.100:16370 查看主从信息 redis-cli -h 192.168.1.100 -p 16370 -a dongdong info replication [root@master-slave ~]# redis-cli -h 192.168.1.100 -p 16370 -a dongdong info replication Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. # Replication role:master connected_slaves:2 slave0:ip=192.168.1.100,port=16371,state=online,offset=10941,lag=0 slave1:ip=192.168.1.100,port=16372,state=online,offset=10941,lag=0 master_failover_state:no-failover master_replid:a292603e2c96a6f451055e6d84a60795f81f2928 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:10941 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:10941 # slaves 变成2了
# 从库开启主从 redis-cli -h 192.168.1.200 -p 16381 -a dongdong slaveof 192.168.1.100 16380 [root@slave1 ~]# redis-cli -h 192.168.1.200 -p 16381 -a dongdong slaveof 192.168.1.100 16380 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. OK # 从库查看主从信息 redis-cli -h 192.168.1.200 -p 16381 -a dongdong info replication [root@slave1 ~]# redis-cli -h 192.168.1.200 -p 16381 -a dongdong info replication Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. # Replication role:slave master_host:192.168.1.100 master_port:16380 master_link_status:up master_last_io_seconds_ago:3 master_sync_in_progress:0 slave_read_repl_offset:532 slave_repl_offset:532 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:7330881025ee2709ee6c9c32ea3fcc9b6649893d master_replid2:0000000000000000000000000000000000000000 master_repl_offset:532 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:15 repl_backlog_histlen:518
4.2 slave 2
# 从库开启主从 redis-cli -h 192.168.1.250 -p 16382 -a dongdong slaveof 192.168.1.100 16380 [root@slave2 ~]# redis-cli -h 192.168.1.250 -p 16382 -a dongdong slaveof 192.168.1.100 16380 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. OK # 从库查看主从信息 redis-cli -h 192.168.1.250 -p 16382 -a dongdong info replication [root@slave2 ~]# redis-cli -h 192.168.1.250 -p 16382 -a dongdong info replication Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. # Replication role:slave master_host:192.168.1.100 master_port:16380 master_link_status:up master_last_io_seconds_ago:10 master_sync_in_progress:0 slave_read_repl_offset:560 slave_repl_offset:560 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:7330881025ee2709ee6c9c32ea3fcc9b6649893d master_replid2:0000000000000000000000000000000000000000 master_repl_offset:560 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:211 repl_backlog_histlen:350
4.3 master
# 查看 主从信息 redis-cli -h 192.168.1.100 -p 16380 -a dongdong info replication [root@master ~]# redis-cli -h 192.168.1.100 -p 16380 -a dongdong info replication Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. # Replication role:master connected_slaves:2 slave0:ip=192.168.1.200,port=16381,state=online,offset=336,lag=0 slave1:ip=192.168.1.250,port=16382,state=online,offset=336,lag=1 master_failover_state:no-failover master_replid:7330881025ee2709ee6c9c32ea3fcc9b6649893d master_replid2:0000000000000000000000000000000000000000 master_repl_offset:336 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:336
5. 验证主从配置是否生效
# "master主机名"实例主库的11号数据库创建测试数据 redis-cli -h 192.168.1.100 -p 16380 -a dongdong -n 11 [root@master ~]# redis-cli -h 192.168.1.100 -p 16380 -a dongdong -n 11 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.1.100:16380[11]> keys * (empty array) 192.168.1.100:16380[11]> set name dongdong OK 192.168.1.100:16380[11]> set age 18 OK 192.168.1.100:16380[11]> keys * 1) "name" 2) "age" 192.168.1.100:16380[11]> # "slave1主机名"实例从库的11号数据库查看是否同步主库的11号数据库 redis-cli -h 192.168.1.200 -p 16381 -a dongdong -n 11 [root@slave1 ~]# redis-cli -h 192.168.1.200 -p 16381 -a dongdong -n 11 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.1.200:16381[11]> get name "dongdong" 192.168.1.200:16381[11]> get age "18" 192.168.1.200:16381[11]> key * (error) ERR unknown command 'key', with args beginning with: '*' 192.168.1.200:16381[11]> keys * 1) "age" 2) "name" 192.168.1.200:16381[11]> # "slave2主机名"实例从库的11号数据库查看是否同步主库的11号数据库 redis-cli -h 192.168.1.250 -p 16382 -a dongdong -n 11 [root@slave2 ~]# redis-cli -h 192.168.1.250 -p 16382 -a dongdong -n 11 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.1.250:16382[11]> get name "dongdong" 192.168.1.250:16382[11]> get age "18" 192.168.1.250:16382[11]> keys * 1) "age" 2) "name" 192.168.1.250:16382[11]> # 如果针对 slave1 或者 slave2 进行写入操作 则会报错 错误信息 只能在master操作哦 (error) READONLY You can't write against a read only replica.