Linux系统cluster集群 保姆?幼儿园级教学

害学习好难 太难了 只能背地里偷偷卷了 还得不被他们发现🥲

1.准备好3台虚拟机(克隆就完事了~ps:"前提是已经安装好redis" 如图👇🏻)

2.确保3台虚拟机里面都有这个文件(博主使用的是Xshell 代码编辑器!)

创建文件redis-new.sh:

#!/bin/sh

# 进入安装目录
# 第一个参是redis的端口
redis_port=$1
# 第二个参数是主数据的密码(从数据库需要设置)
master_auth_pwd=$2
# 第三个参数是本数据库的密码
my_auth_pwd=$3
# 第四个参数是否配置集群
is_cluster=$4
src_path=/usr/local/redis-5.0.5
redis_home=/usr/local/redis
bin_path=$redis_home/bin
conf_path=$redis_home/conf
data_path=$redis_home/data
log_path=$redis_home/log
exe_client=$bin_path/redis-cli
exe_server=$bin_path/redis-server

if [ -z "$redis_port" ] 
then
    # sh -x 可以查看命令执行语句
    echo "Usage: sh ~/redis-new.sh port [master_auth_pwd] [my_auth_pwd] [is_cluster]" >&2
    exit 1
fi

if [ ! -d $redis_home/ ] 
then
    echo "$redis_home directory not exist, you need install redis first." >&2
    exit 1
fi

# 创建配置目录
if [ ! -d $conf_path/ ] 
then
    echo "creating the conf directory..."
    mkdir -p $conf_path
fi
# 创建数据目录
if [ ! -d $data_path/ ] 
then
    echo "creating the data directory..."
    mkdir -p $data_path
fi
# 创建日志目录
if [ ! -d $log_path/ ] 
then
    echo "creating the log directory..."
    mkdir -p $log_path
fi

# 进入配置目录
cd $conf_path

# 复制配置文件
\cp -rf $src_path/redis.conf  $conf_path/redis_$redis_port.conf

# 修改端口
sed -i '0,/^\s*port.*$/s/^\s*port.*$/port '$redis_port'/' redis_$redis_port.conf

# 设置后台启动
sed -i '0,/^\s*daemonize\s*no\s*$/s/^\s*daemonize\s*no\s*$/daemonize yes/' redis_$redis_port.conf

# 去掉IP绑定
sed -i '0,/^\s*bind.*$/s/^\s*bind.*$/#bind 127.0.0.1/' redis_$redis_port.conf

# 开启集群模式
if [ -n "$is_cluster" ] 
then
    # 开启集群配置
    sed -i '0,/^#*\s*cluster-enabled.*$/s/^#*\s*cluster-enabled.*$/cluster-enabled yes/' redis_$redis_port.conf
    sed -i '0,/^#*\s*cluster-config-file.*$/s/^#*\s*cluster-config-file.*$/cluster-config-file nodes-'$redis_port'.conf/' redis_$redis_port.conf
    sed -i '0,/^#*\s*cluster-node-timeout.*$/s/^#*\s*cluster-node-timeout.*$/cluster-node-timeout 15000/' redis_$redis_port.conf
    # 集群要把保护模式取消掉
    sed -i '0,/^#*\s*protected-mode.*yes.*$/s/^\s*#*protected-mode.*yes.*$/protected-mode no/' redis_$redis_port.conf
fi

if [ -n "$my_auth_pwd" ] 
then
    # 增加密码(可选)
    sed -i '0,/^#*\s*requirepass.*$/s/^#*\s*requirepass.*$/requirepass '$my_auth_pwd'/' redis_$redis_port.conf
fi
if [ -n "$master_auth_pwd" ] 
then
    # 如果主机有密码,设置主机密码,否则无法复制
    sed -i '0,/^#*\s*masterauth.*$/s/^#*\s*masterauth.*$/masterauth '$master_auth_pwd'/' redis_$redis_port.conf
fi

# 修改pid文件路径
sed -i '0,/^pidfile\s*.*$/s/^pidfile\s*.*$/pidfile \/var\/run\/redis_'$redis_port'.pid/' redis_$redis_port.conf

# 修改dir路径用于存放数据文件${变量//\//\\/}可以转义
sed -i '0,/^dir.*$/s/^dir.*$/dir '${data_path//\//\\/}'\/'$redis_port'\//' redis_$redis_port.conf

# 修改日志输出路径
sed -i '0,/^logfile.*$/s/^logfile.*$/logfile "'${log_path//\//\\/}'\/redis_'$redis_port'.log"/' redis_$redis_port.conf

# 开启AOF模式持久化(可选)
sed -i '0,/^\s*appendonly\s*no\s*$/s/^\s*appendonly\s*no\s*$/appendonly yes/' redis_$redis_port.conf

# 简化配置文件,只显示非注释内容
# egrep -v "#|^$"  redis_$redis_port.conf
# 仅查看未注释的内容
echo ------------------------ redis_$redis_port.conf 修改后配置 start -----------------------------------
egrep -E "^port|^logfile|^cluster|^daemonize|^bind|^requirepass|^masterauth|^pidfile|^dir|^appendonly|^protected-mode"  redis_$redis_port.conf
echo ------------------------ redis_$redis_port.conf 修改后配置 end -----------------------------------
# egrep -E "^port|^logfile|^cluster|^daemonize|^#?\s*?bind|^#?\s*requirepass|^#?\s*masterauth|^pidfile|^dir|^appendonly|^protected-mode"  redis_$redis_port.conf

# 创建数据文件目录
mkdir -p $data_path/$redis_port/

# 开启外网端口访问
firewall-cmd --zone=public --add-port=$redis_port/tcp --permanent >&2

if [ -n "$is_cluster" ] 
then
    # 开启集群总线端口
    firewall-cmd --zone=public --add-port=1$redis_port/tcp --permanent >&2
fi

# 重启防火墙生效
firewall-cmd --reload

# 查看防火墙信息
# firewall-cmd --list-all
# firewall-cmd --list-ports

# 移除某个端口
# firewall-cmd --zone=public --remove-port=$redis_port/tcp --permanent
# firewall-cmd --reload

                                                                                                                                             CV即可

必须确保每台下面都有:     

                                                                                                                                                 如图

3.启动redis服务查询该库中是否有数据如果有则清空(3台虚拟机redis都要清空)

 1.启动redis服务(redis_7001.conf是前面创建的虚拟机  3台主机都要执行 注意修改命令参数跟自己配置的一致)

/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis_7001.conf

 2.使用命令连接本机redis( -a后面的密码是安装redis配置的 -h 后面的ip是主机 -p则是启动的虚拟机端口)

/usr/local/redis/bin/redis-cli -a 123456  -h 127.0.0.1 -p 7001

 3.执行kesy *查询是否有数据

keys *
ps:返回空(empty array) 就是没有数据

  4.有数据则使用flushall清空数据

   !!!如果使用flushall报持久化错误则:

使用命令:config set stop-writes-on-bgsave-error no 再使用 flushall 命令

      欸嘿嘿我猜你肯定忘记了 3台都要执行这些操作(小声提醒~)

  5.执行命令删除一下文件

删除aop和rdb文件:
cd /usr/local/redis/conf
rm -rf *.aof
rm -rf *.rdb

 6.执行

rm -rf /usr/local/redis/data/700*/*

4.集群操作

 1.关闭redis服务准备配置cluster集群

pkill redis-sentinel
pkill redis-server

 2.创建主机和从机并进行配置

  ********************************************第一台7001执行**********************************************

sh ~/redis-new.sh 7001 123456 123456 true

sh ~/redis-new.sh 7004 123456 123456 true

sh ~/redis-new.sh 7007 123456 123456 true

/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis_7001.conf

/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis_7004.conf

/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis_7007.conf

**********************************************第二台7002执行**********************************************

sh ~/redis-new.sh 7002 123456 123456 true

sh ~/redis-new.sh 7005 123456 123456 true

sh ~/redis-new.sh 7008 123456 123456 true

/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis_7002.conf

/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis_7005.conf

/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis_7008.conf

**********************************************第三台7003执行**********************************************

sh ~/redis-new.sh 7003 123456 123456 true

sh ~/redis-new.sh 7006 123456 123456 true

sh ~/redis-new.sh 7009 123456 123456 true

/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis_7003.conf

/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis_7006.conf

/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis_7009.conf

创建完成就可以进行下一步了(ps:如果创建不了那就是创建redis的时候没有安装插件,可以手动定位到目入复制创建的虚拟机文件,粘贴重命名以此类推!我的位置是:/usr/local/redis/conf)

5.创建集群(顺便一台执行即可)

# -a 是创建redis配置的密码 

/usr/local/redis/bin/redis-cli -a 123456 --cluster create --cluster-replicas 2 \

#192.168.102.141是第一台虚拟机的ip 7001是要配置端口

#192.168.102.142是第二台虚拟机的ip 7002是要配置端口

#192.168.102.143是第三台虚拟机的ip 7003是要配置端口

#!!!改ip就行了

192.168.102.141:7001 192.168.102.142:7002 192.168.102.143:7003 \

192.168.102.141:7004 192.168.102.142:7005 192.168.102.143:7006 \

192.168.102.141:7007 192.168.102.142:7008 192.168.102.143:7009

 最后进行测试 确保虚拟机都处于启动状态

  找到自己电脑安装的redis文件位置 D:\DataBase\Redis-x64-3.2 直接cmd

执行:
redis-cli.exe -a 123456 -c -h 192.168.133.132 -p 7001

后面的自己去测试 set gei之类的

  • 23
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值