Ubuntu搭建Redis 集群

  1. 安装redis
    1. wget http://download.redis.io/releases/redis-7.2.1.tar.gz

  2. 解压redis
    1. tar xzf redis-7.2.1.tar.gz
      cd redis-7.2.1
      make && make install

  3. 安装ruby环境及ruby-redis插件
    1. 安装ruby
      apt-get install ruby
      进入 redis-7.2.1/src文件夹下,通过gem安装redis
      gem install redis
      

  4. 在 redis-7.2.1 同级目录创建目录
    1. 
      mkdir 7000 7001 7002 7003 7004 7005

  5. 在每个目录下创建 redis.conf 把下面配置添加进去
    1. bind 0.0.0.0                    # 绑定本机IP,多个IP由空格隔开
      daemonize yes                   # 后台启动
      port 7000                       # 分别是7001,7002,7003,7004,7005
      cluster-enabled yes             # 开启cluster
      cluster-config-file nodes.conf  # 节点配置文件,开启集群后会自动生成
      cluster-node-timeout 15000      # 超时时间
      appendonly yes                  # 开启aof
      protected-mode no               # 关闭安全模式

  6. 启动
    1. 把redis-7.2.1/src下的redis-service目录依次复制到 7000 7001 7002 7003 7004 7005下面
        并依次 ./redis-service redis.conf

  7. 创建集群
    1. (redis5以后 )回到  redis-7.2.1/src 下
      ./redis-cli --cluster create 0.0.0.0:7000 0.0.0.0:7001 0.0.0.0:7002 0.0.0.0:7003 0.0.0.0:7004 0.0.0.0:7005

  8. 测试连接
    1. golang代码
      1. func Redis() {
        	client := redis.NewClusterClient(&redis.ClusterOptions{
        		Addrs:    []string{"ip地址"},
        		Password: "密码", // no password set
        	})
        
        	pong, err := client.Ping(context.Background()).Result()
        	if err != nil {
        		panic(fmt.Sprintf("redis connect ping failed, err:%s", err.Error()))
        	} 
        }

    2. cli测试
      1. 活着通过redis-cli 测试
        通过集群模式连接其中一个节点:
        ./redis-cli -c -p 7000
        set test 11111
        get test 
        quit
        
        

  9. 遇到问题(redis-cli --cluster create报错解决)

    1. [ERR] Node 123.60.111.163:7000 DENIED Redis is running in protected mode because protected mode is enabled and no password is set for the default user. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Set up an authentication password for the default user. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.
      
      [ERR]节点123.60.111.163:7000 DENIED Redis在保护模式下运行,因为启用了保护模式,并且没有为默认用户设置密码。在这种模式下,只接受来自loopback接口的连接。如果你想从外部计算机连接到Redis,你可以采用以下解决方案之一:1)通过从服务器运行的同一主机连接到Redis,从loopback接口发送命令'CONFIG SET protected-mode no'来禁用保护模式,但是确保Redis不能从互联网公开访问,如果你这样做的话。使用CONFIG REWRITE使此更改永久生效。2)或者你可以通过编辑Redis配置文件禁用保护模式,并将保护模式选项设置为“no”,然后重新启动服务器。3)如果你手动启动服务器只是为了测试,用“——protected-mode no”选项重新启动它。4)为默认用户设置认证密码。注意:为了让服务器开始接受来自外部的连接,您只需要执行上述一项操作。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值