目录
一、 同主机搭建redis 集群
1、 情况介绍
一台机器 一主两从集群搭建
操纵系统Centos 7内核版本3.10.0-957.21.3.el7.x86_64IP地址master: 7001slave1: 7002 slave2: 7003Redis7.2.5 2、 情况前准备工作
- # 关闭防火墙
- 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
- # 下载地址
- http://download.redis.io/releases/
- # 下载
- wget http://download.redis.io/releases/redis-7.2.5.tar.gz
- # 解压
- tar -xvf redis-7.2.5.tar.gz -C /opt/redis
- # 进入目录
- cd /opt/redis/redis-7.2.5
- # 编译
- 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 修改设置文件
4.2 编写启动脚本
- # 启动脚本
- cat > /usr/local/redis/bin/master-slave_start.sh << EOF
- #!/bin/bash
- redis-server /usr/local/redis/conf/redis_7001.conf
- redis-server /usr/local/redis/conf/redis_7002.conf
- redis-server /usr/local/redis/conf/redis_7003.conf
- EOF
- # 赋予可执行权限
- chmod +x /usr/local/redis/bin/redis_cluster_start.sh
- # 关闭脚本
- vi /usr/local/redis/bin/redis_cluster_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 开启
- # 以"192.168.2.1:7001"实例为主库,以"192.168.2.1:7002"实例和"192.168.2.1:7003"实例为从库。
- # -a 参数指定密码
- # 从库实例开启主从
- redis-cli -h 192.168.2.1 -p 7002 -a 你的密码 slaveof 192.168.2.1 7001
- redis-cli -h 192.168.2.1 -p 7003 -a 你的密码 slaveof 192.168.2.1 7001
-
- # 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.2.1 -p 7002 -a 你的密码 --no-auth-warning slaveof 192.168.2.1 7001
复制代码 5.2 主库实例查看主从信息
- redis-cli -h 192.168.2.1 -p 7001 -a 你的密码 info replication
-
- [root@master-slave ~]# redis-cli -h 192.168.2.1 -p 7001 -a 你的密码 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.2.1,port=7002,state=online,offset=8428,lag=0
- slave1:ip=192.168.2.1,port=7003,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.2.1:7002 查看主从信息
- redis-cli -h 192.168.2.1 -p 7002 -a 你的密码 info replication
- [root@master-slave ~]# redis-cli -h 192.168.2.1 -p 7002 -a 你的密码 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.2.1
- master_port:7001
- 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.2.1:7001"实例主库的11号数据库创建测试数据
- redis-cli -h 192.168.2.1 -p 7001 -a 你的密码 -n 11
- [root@master-slave ~]# redis-cli -h 192.168.2.1 -p 7001 -a 你的密码 -n 11
- Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
- 192.168.2.1:7001[11]> keys *
- (empty array)
- 192.168.2.1:7001[11]> set name xiaoxiao
- OK
- 192.168.2.1:7001[11]> set age 18
- OK
- 192.168.2.1:7001[11]> keys *
- 1) "age"
- 2) "name"
- # "192.168.2.1:7002"实例从库的11号数据库查看是否同步主库的11号数据库
- redis-cli -h 192.168.2.1 -p 7002 -a 你的密码 -n 11
- [root@master-slave ~]# redis-cli -h 192.168.2.1 -p 7002 -a 你的密码 -n 11
- Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
- 192.168.2.1:7002[11]> keys *
- 1) "name"
- 2) "age"
- 192.168.2.1:7002[11]> get name
- "zhangsan"
- 192.168.2.1:7002[11]> get age
- "18"
- 192.168.2.1:7002[11]>
- # "192.168.2.1:7003"实例从库的11号数据库查看是否同步主库的11号数据库
- redis-cli -h 192.168.2.1 -p 7003 -a 你的密码 -n 11
- [root@master-slave ~]# redis-cli -h 192.168.2.1 -p 7003 -a 你的密码 -n 11
- Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
- 192.168.2.1:7003[11]> keys *
- 1) "age"
- 2) "name"
- 192.168.2.1:7003[11]> get name
- "zhangsan"
- 192.168.2.1:7003[11]> get age
- "18"
- 192.168.2.1:7003[11]>
复制代码 6、 排除 192.168.2.1:7003 实例主从
- # 192.168.2.1:7003 查看主从信息
- redis-cli -h 192.168.2.1 -p 7003 -a 你的密码 info replication
- [root@master-slave ~]# redis-cli -h 192.168.2.1 -p 7003 -a 你的密码 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.2.1
- master_port:7001
- 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.2.1:7001 查看主从信息
- redis-cli -h 192.168.2.1 -p 7001 -a 你的密码 info replication
- [root@master-slave ~]# redis-cli -h 192.168.2.1 -p 7001 -a 你的密码 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.2.1,port=7002,state=online,offset=10157,lag=0
- slave1:ip=192.168.2.1,port=7003,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.2.1:7003 主从信息
- redis-cli -h 192.168.2.1 -p 7003 -a 你的密码 slaveof no one
- [root@master-slave ~]# redis-cli -h 192.168.2.1 -p 7003 -a 你的密码 slaveof no one
- Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
- OK
- # 再次查看 192.168.2.1:7001 查看主从信息
- redis-cli -h 192.168.2.1 -p 7001 -a 你的密码 info replication
- [root@master-slave ~]# redis-cli -h 192.168.2.1 -p 7001 -a 你的密码 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.2.1,port=7002,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.2.1:7003 添加回来
- redis-cli -h 192.168.2.1 -p 7003 -a 你的密码 slaveof 192.168.2.1 7001
- [root@master-slave ~]# redis-cli -h 192.168.2.1 -p 7003 -a 你的密码 slaveof 192.168.2.1 7001
- Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
- OK
- # 此时再看192.168.2.1:7001 查看主从信息
- redis-cli -h 192.168.2.1 -p 7001 -a 你的密码 info replication
- [root@master-slave ~]# redis-cli -h 192.168.2.1 -p 7001 -a 你的密码 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.2.1,port=7002,state=online,offset=10941,lag=0
- slave1:ip=192.168.2.1,port=7003,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 7Centos 7Centos 7内核版本3.10.0-957.21.3.el7.x86_643.10.0-957.21.3.el7.x86_643.10.0-957.21.3.el7.x86_64IP地址master: 16370 slave1: 16371 slave2: 16372master: 16370 slave1: 16371 slave2: 16372master: 16370 slave1: 16371 slave2: 16372Redis7.2.57.2.57.2.5 2. 修改设置文件
2.1 master
- # 创建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 你的密码
- # 以认证的方式连接到master。 如果master中使用了“密码保护”,slave必须交付正确的授权密码,才能连接成功。
- # 此配置项中值需要和master机器的“requirepass”保持一致
- masterauth 你的密码
- # 配置RDB持久化策略
- save 900 1
- save 300 10
- save 60 10000
- # 配置AOF持久化
- appendonly yes
- appendfsync everysec
- # 守护进程
- daemonize yes
- EOF
复制代码 2.2 slave 1
- # 创建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 你的密码
- # 以认证的方式连接到master。 如果master中使用了“密码保护”,slave必须交付正确的授权密码,才能连接成功。
- # 此配置项中值需要和master机器的“requirepass”保持一致
- masterauth 你的密码
- # 配置RDB持久化策略
- save 900 1
- save 300 10
- save 60 10000
- # 配置AOF持久化
- appendonly yes
- appendfsync everysec
- # 守护进程
- daemonize yes
- EOF
复制代码 2.3 slave 2
- # 创建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 你的密码
- # 以认证的方式连接到master。 如果master中使用了“密码保护”,slave必须交付正确的授权密码,才能连接成功。
- # 此配置项中值需要和master机器的“requirepass”保持一致
- masterauth 你的密码
- # 配置RDB持久化策略
- save 900 1
- save 300 10
- save 60 10000
- # 配置AOF持久化
- appendonly yes
- appendfsync everysec
- # 守护进程
- daemonize yes
- EOF
复制代码 3.启动Redis
3.1 master
- redis-server /usr/local/redis/conf/redis_16380.conf
复制代码 3.2 slave 1
- redis-server /usr/local/redis/conf/redis_16381.conf
复制代码 3.3 slave 2
- redis-server /usr/local/redis/conf/redis_16382.conf
复制代码 4. 开启主从
4.1 slave 1
- # 从库开启主从
- redis-cli -h 192.168.1.200 -p 16381 -a 你的密码 slaveof 192.168.1.100 16380
-
- [root@slave1 ~]# redis-cli -h 192.168.1.200 -p 16381 -a 你的密码 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 你的密码 info replication
- [root@slave1 ~]# redis-cli -h 192.168.1.200 -p 16381 -a 你的密码 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 你的密码 slaveof 192.168.1.100 16380
- [root@slave2 ~]# redis-cli -h 192.168.1.250 -p 16382 -a 你的密码 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 你的密码 info replication
- [root@slave2 ~]# redis-cli -h 192.168.1.250 -p 16382 -a 你的密码 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 你的密码 info replication
- [root@master ~]# redis-cli -h 192.168.1.100 -p 16380 -a 你的密码 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 你的密码 -n 11
- [root@master ~]# redis-cli -h 192.168.1.100 -p 16380 -a 你的密码 -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 zhangsan
- 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 你的密码 -n 11
- [root@slave1 ~]# redis-cli -h 192.168.1.200 -p 16381 -a 你的密码 -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
- "zhagnsan"
- 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 你的密码 -n 11
- [root@slave2 ~]# redis-cli -h 192.168.1.250 -p 16382 -a 你的密码 -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
- "zhangsan"
- 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.
复制代码 竣事
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |