Redis 主从搭建

Redis主从搭建 7.2.5

文章目录

  • [一. 同主机搭建Redis主从](#一. 同主机搭建Redis主从)
    • [1. 环境介绍](#1. 环境介绍)
    • [2. 环境前准备工作](#2. 环境前准备工作)
    • [3. 安装 Redis 7.2.5](#3. 安装 Redis 7.2.5)
    • [4. redis 配置修改并且启动](#4. redis 配置修改并且启动)
      • [4.1 修改配置文件](#4.1 修改配置文件)
      • [4.2 编写启动脚本](#4.2 编写启动脚本)
    • [5. 开启主从](#5. 开启主从)
      • [5.1 开启](#5.1 开启)
      • [5.2 主库实例查看主从信息](#5.2 主库实例查看主从信息)
      • [5.3 从库实例查看主从信息](#5.3 从库实例查看主从信息)
      • [5.4 验证主从配置是否生效](#5.4 验证主从配置是否生效)
    • [6. 解除 192.168.1.100:16372 实例主从](#6. 解除 192.168.1.100:16372 实例主从)
  • [二. 跨节点部署Redis主从](#二. 跨节点部署Redis主从)
    • [1. 环境介绍](#1. 环境介绍)
    • [2. 修改配置文件](#2. 修改配置文件)
      • [2.1 master](#2.1 master)
      • [2.2 slave 1](#2.2 slave 1)
      • [2.3 slave 2](#2.3 slave 2)
    • 3.启动Redis
      • [3.1 master](#3.1 master)
      • [3.2 slave 1](#3.2 slave 1)
      • [3.3 slave 2](#3.3 slave 2)
    • [4. 开启主从](#4. 开启主从)
      • [4.1 slave 1](#4.1 slave 1)
      • [4.2 slave 2](#4.2 slave 2)
      • [4.3 master](#4.3 master)
    • [5. 验证主从配置是否生效](#5. 验证主从配置是否生效)

一. 同主机搭建Redis主从

1. 环境介绍

同机器 一主两从

操作系统 Centos 7
内核版本 Linux 3.10.0-957.el7.x86_64
主机名称 master-salve
IP地址 192.168.1.100
端口 master: 16370 slave1: 16371 slave2: 16372
Redis 7.2.5

2. 环境前准备工作

sh 复制代码
# 关闭防火墙
systemctl stop firewalld
systemctl disable firewalld

# 修改 hostname
hostnamectl set-hostname xxxx  # 修改后退出当前终端重新连接即可

# 更新下软件源
# 地址 https://developer.aliyun.com/mirror/

# 时区调整,时间校准
date -R
timedatectl set-timezone Asia/Shanghai
yum -y install ntp
ntpdate ntp1.aliyun.com

# 关闭 selinux: 
sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0

3. 安装 Redis 7.2.5

sh 复制代码
# 下载地址
http://download.redis.io/releases/

# 下载
wget http://download.redis.io/releases/redis-7.2.5.tar.gz
# 解压
tar -xf redis-7.2.5.tar.gz -C /opt/redis

# 编译
make MALLOC=libc

# 安装 注意如果安装提示python3不存在 执行 yum install -y python3
make install PREFIX=/usr/local/redis

# 配置环境变量
vi /etc/profile.d/redis.sh

#!/bin/bash

export REDIS_HOME=/usr/local/redis
export PATH=$PATH:$REDIS_HOME/bin 

# 使其配置生效
source /etc/profile.d/redis.sh

4. redis 配置修改并且启动

4.1 修改配置文件

sh 复制代码
# 创建redis数据存储目录
mkdir -p /usr/local/redis/data/1637{0..2}
# 创建redis配置文件存储目录
mkdir -p /usr/local/redis/conf/1637{0..2}
# 创建redis日志存储目录
mkdir -p /usr/local/redis/log/1637{0..2}

# 创建redis pid存储目录
mkdir /usr/local/redis/run


# redis配置文件 16370
# 开启保护模式
protected-mode yes
# 添加本机的ip
bind 192.168.1.100   
# 端口  
port 16370
# pid存储目录
pidfile /usr/local/redis/run/redis_16370.pid   
# 日志存储目录
logfile /usr/local/redis/log/16370/redis_16370.log 
# 数据存储目录,目录要提前创建好
dir /usr/local/redis/data/16370
# 设置实例的验证口令
requirepass dongdong
# 以认证的方式连接到master。 如果master中使用了"密码保护",slave必须交付正确的授权密码,才能连接成功。
# 此配置项中值需要和master机器的"requirepass"保持一致
masterauth dongdong
# 配置RDB持久化策略
save 900 1
save 300 10
save 60 10000
# 配置AOF持久化
appendonly yes 
appendfsync everysec
# 守护进程
daemonize yes  



# redis配置文件 16371
# 开启保护模式
protected-mode yes
# 添加本机的ip
bind 192.168.1.100   
# 端口  
port 16371
# pid存储目录
pidfile /usr/local/redis/run/redis_16371.pid   
# 日志存储目录
logfile /usr/local/redis/log/16370/redis_16371.log 
# 数据存储目录,目录要提前创建好
dir /usr/local/redis/data/16371
# 设置实例的验证口令
requirepass dongdong
# 以认证的方式连接到master。 如果master中使用了"密码保护",slave必须交付正确的授权密码,才能连接成功。
# 此配置项中值需要和master机器的"requirepass"保持一致
masterauth dongdong
# 配置RDB持久化策略
save 900 1
save 300 10
save 60 10000
# 配置AOF持久化
appendonly yes 
appendfsync everysec
# 守护进程
daemonize yes  



# redis配置文件 16372
# 开启保护模式
protected-mode yes
# 添加本机的ip
bind 192.168.1.100   
# 端口  
port 16372
# pid存储目录
pidfile /usr/local/redis/run/redis_16372.pid   
# 日志存储目录
logfile /usr/local/redis/log/16370/redis_16372.log 
# 数据存储目录,目录要提前创建好
dir /usr/local/redis/data/16372
# 设置实例的验证口令
requirepass dongdong
# 以认证的方式连接到master。 如果master中使用了"密码保护",slave必须交付正确的授权密码,才能连接成功。
# 此配置项中值需要和master机器的"requirepass"保持一致
masterauth dongdong
# 配置RDB持久化策略
save 900 1
save 300 10
save 60 10000
# 配置AOF持久化
appendonly yes 
appendfsync everysec
# 守护进程
daemonize yes  




# 复制下面命令即可
cat > /usr/local/redis/conf/redis_16370.conf <<EOF 
# 开启保护模式
protected-mode yes
# 添加本机的ip
bind 192.168.1.100   
# 端口  
port 16370
# pid存储目录
pidfile /usr/local/redis/run/redis_16370.pid   
# 日志存储目录
logfile /usr/local/redis/log/16370/redis_16370.log 
# 数据存储目录,目录要提前创建好
dir /usr/local/redis/data/16370
# 设置实例的验证口令
requirepass dongdong
# 以认证的方式连接到master。 如果master中使用了"密码保护",slave必须交付正确的授权密码,才能连接成功。
# 此配置项中值需要和master机器的"requirepass"保持一致
masterauth dongdong
# 配置RDB持久化策略
save 900 1
save 300 10
save 60 10000
# 配置AOF持久化
appendonly yes 
appendfsync everysec
# 守护进程
daemonize yes 
EOF



cat > /usr/local/redis/conf/redis_16371.conf <<EOF 
# 开启保护模式
protected-mode yes
# 添加本机的ip
bind 192.168.1.100   
# 端口  
port 16371
# pid存储目录
pidfile /usr/local/redis/run/redis_16371.pid   
# 日志存储目录
logfile /usr/local/redis/log/16370/redis_16371.log 
# 数据存储目录,目录要提前创建好
dir /usr/local/redis/data/16371
# 设置实例的验证口令
requirepass dongdong
# 以认证的方式连接到master。 如果master中使用了"密码保护",slave必须交付正确的授权密码,才能连接成功。
# 此配置项中值需要和master机器的"requirepass"保持一致
masterauth dongdong
# 配置RDB持久化策略
save 900 1
save 300 10
save 60 10000
# 配置AOF持久化
appendonly yes 
appendfsync everysec
# 守护进程
daemonize yes 
EOF

cat > /usr/local/redis/conf/redis_16372.conf <<EOF 
# 开启保护模式
protected-mode yes
# 添加本机的ip
bind 192.168.1.100   
# 端口  
port 16372
# pid存储目录
pidfile /usr/local/redis/run/redis_16372.pid   
# 日志存储目录
logfile /usr/local/redis/log/16370/redis_16372.log 
# 数据存储目录,目录要提前创建好
dir /usr/local/redis/data/16372
# 设置实例的验证口令
requirepass dongdong
# 以认证的方式连接到master。 如果master中使用了"密码保护",slave必须交付正确的授权密码,才能连接成功。
# 此配置项中值需要和master机器的"requirepass"保持一致
masterauth dongdong
# 配置RDB持久化策略
save 900 1
save 300 10
save 60 10000
# 配置AOF持久化
appendonly yes 
appendfsync everysec
# 守护进程
daemonize yes 
EOF

4.2 编写启动脚本

sh 复制代码
# 启动脚本
cat > /usr/local/redis/bin/master-slave_start.sh << EOF
#!/bin/bash



redis-server /usr/local/redis/conf/redis_16370.conf
redis-server /usr/local/redis/conf/redis_16371.conf
redis-server /usr/local/redis/conf/redis_16372.conf
EOF


# 赋予可执行权限
chmod +x /usr/local/redis/bin/master-slave_start.sh


# 关闭脚本
vi /usr/local/redis/bin/master-slave_shutdown.sh

#!/bin/bash

kill -9 $(ps -ef | grep 1637 | grep -v grep | awk '{print $2}')

# 赋予可执行权限
chmod +x /usr/local/redis/bin/master-slave_shutdown.sh

5. 开启主从

5.1 开启

sh 复制代码
# 以"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 主库实例查看主从信息

sh 复制代码
 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 从库实例查看主从信息

sh 复制代码
# 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 验证主从配置是否生效

sh 复制代码
# "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 实例主从

sh 复制代码
# 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主从

1. 环境介绍

操作系统 Centos 7 Centos 7 Centos 7
内核版本 Linux 3.10.0-957.el7.x86_64 Linux 3.10.0-957.el7.x86_64 Linux 3.10.0-957.el7.x86_64
主机名称 master slave1 slave2
IP 192.168.1.100 192.168.1.200 192.168.1.250
端口 16380 16381 16382

2. 修改配置文件

2.1 master

sh 复制代码
# 创建redis数据存储目录
mkdir -p /usr/local/redis/data/16380
# 创建redis配置文件存储目录
mkdir -p /usr/local/redis/conf/16380
# 创建redis日志存储目录
mkdir -p /usr/local/redis/log/16380

# 创建redis pid存储目录
mkdir /usr/local/redis/run


# 复制下面命令即可
cat > /usr/local/redis/conf/redis_16380.conf<<EOF 
# 开启保护模式
protected-mode yes
# 添加本机的ip
bind 192.168.1.100   
# 端口  
port 16380
# pid存储目录
pidfile /usr/local/redis/run/redis_16380.pid   
# 日志存储目录
logfile /usr/local/redis/log/redis_16380.log 
# 数据存储目录,目录要提前创建好
dir /usr/local/redis/data/16380
# 设置实例的验证口令
requirepass dongdong
# 以认证的方式连接到master。 如果master中使用了"密码保护",slave必须交付正确的授权密码,才能连接成功。
# 此配置项中值需要和master机器的"requirepass"保持一致
masterauth dongdong
# 配置RDB持久化策略
save 900 1
save 300 10
save 60 10000
# 配置AOF持久化
appendonly yes 
appendfsync everysec
# 守护进程
daemonize yes 
EOF

2.2 slave 1

sh 复制代码
# 创建redis数据存储目录
mkdir -p /usr/local/redis/data/16381
# 创建redis配置文件存储目录
mkdir -p /usr/local/redis/conf/16381
# 创建redis日志存储目录
mkdir -p /usr/local/redis/log/16381

# 创建redis pid存储目录
mkdir /usr/local/redis/run


# 复制下面命令即可
cat > /usr/local/redis/conf/redis_16381.conf <<EOF 
# 开启保护模式
protected-mode yes
# 添加本机的ip
bind 192.168.1.200   
# 端口  
port 16381
# pid存储目录
pidfile /usr/local/redis/run/redis_16381.pid   
# 日志存储目录
logfile /usr/local/redis/log/redis_16381.log 
# 数据存储目录,目录要提前创建好
dir /usr/local/redis/data/16381
# 设置实例的验证口令
requirepass dongdong
# 以认证的方式连接到master。 如果master中使用了"密码保护",slave必须交付正确的授权密码,才能连接成功。
# 此配置项中值需要和master机器的"requirepass"保持一致
masterauth dongdong
# 配置RDB持久化策略
save 900 1
save 300 10
save 60 10000
# 配置AOF持久化
appendonly yes 
appendfsync everysec
# 守护进程
daemonize yes 
EOF

2.3 slave 2

sh 复制代码
# 创建redis数据存储目录
mkdir -p /usr/local/redis/data/16382
# 创建redis配置文件存储目录
mkdir -p /usr/local/redis/conf/16382
# 创建redis日志存储目录
mkdir -p /usr/local/redis/log/16382

# 创建redis pid存储目录
mkdir /usr/local/redis/run


# 复制下面命令即可
cat > /usr/local/redis/conf/redis_16382.conf <<EOF 
# 开启保护模式
protected-mode yes
# 添加本机的ip
bind 192.168.1.250   
# 端口  
port 16382
# pid存储目录
pidfile /usr/local/redis/run/redis_16382.pid   
# 日志存储目录
logfile /usr/local/redis/log/redis_16382.log 
# 数据存储目录,目录要提前创建好
dir /usr/local/redis/data/16382
# 设置实例的验证口令
requirepass dongdong
# 以认证的方式连接到master。 如果master中使用了"密码保护",slave必须交付正确的授权密码,才能连接成功。
# 此配置项中值需要和master机器的"requirepass"保持一致
masterauth dongdong
# 配置RDB持久化策略
save 900 1
save 300 10
save 60 10000
# 配置AOF持久化
appendonly yes 
appendfsync everysec
# 守护进程
daemonize yes 
EOF

3.启动Redis

3.1 master

sh 复制代码
redis-server  /usr/local/redis/conf/redis_16380.conf

3.2 slave 1

sh 复制代码
redis-server  /usr/local/redis/conf/redis_16381.conf

3.3 slave 2

sh 复制代码
redis-server  /usr/local/redis/conf/redis_16382.conf

4. 开启主从

4.1 slave 1

sh 复制代码
# 从库开启主从
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

sh 复制代码
# 从库开启主从
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

sh 复制代码
# 查看 主从信息
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. 验证主从配置是否生效

sh 复制代码
# "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.
相关推荐
tatasix20 分钟前
MySQL UPDATE语句执行链路解析
数据库·mysql
秋意钟25 分钟前
缓存雪崩、缓存穿透【Redis】
redis
南城花随雪。33 分钟前
硬盘(HDD)与固态硬盘(SSD)详细解读
数据库
儿时可乖了34 分钟前
使用 Java 操作 SQLite 数据库
java·数据库·sqlite
懒是一种态度36 分钟前
Golang 调用 mongodb 的函数
数据库·mongodb·golang
简 洁 冬冬37 分钟前
046 购物车
redis·购物车
天海华兮38 分钟前
mysql 去重 补全 取出重复 变量 函数 和存储过程
数据库·mysql
雯0609~1 小时前
网页F12:缓存的使用(设值、取值、删除)
前端·缓存
soulteary1 小时前
突破内存限制:Mac Mini M2 服务器化实践指南
运维·服务器·redis·macos·arm·pika
gma9992 小时前
Etcd 框架
数据库·etcd