搭建测试用的redis集群 访问失败小记

redis运行在virtual box虚拟机的ubuntu中。

golang编写的redis客户端代码运行在windows下。

首先在ubuntu下启动redis集群

下载redis源码,编译成功后,进入utils/create-cluster目录,运行

./create-cluster start

./create-cluster create

 在windows下编写访问redis代码

package main

import (
	"context"
	"errors"
	"fmt"

	"github.com/redis/go-redis/v9"
)

func NewRedisClient() (*redis.ClusterClient, error) {
	addr := []string{"192.168.2.6:30001", "192.168.2.6:30002", "192.168.2.6:30003", "192.168.2.6:30004", "192.168.2.6:30005", "192.168.2.6:30006"}

	redisCli := redis.NewClusterClient(&redis.ClusterOptions{
		Addrs:      addr,
		MaxRetries: 3,
		PoolSize:   30,
	})
	_, err := redisCli.Ping(context.Background()).Result()
	if err != nil {
		fmt.Printf("%v\n", err)
		return nil, errors.New("ping redis error")
	} else {
		fmt.Println("ping ok")
	}
	return redisCli, nil

}

func main() {
	NewRedisClient()
}

运行报错。

在linux上运行redis客户端程序,ping正常

./redis-cli -c -p 30001
127.0.0.1:30001> ping
PONG

在windows上 下载redis客户端,连接,运行,报错

 .\redis-cli.exe -c -h 192.168.2.6 -p 30001
192.168.2.6:30001> ping
(error) 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.

 看错误输出描述是非localhost访问开启了protected-mode模式,如果自己测试用,可以关闭此模式

vim打开redis目录下的utils/create-cluster文件,修改

PROTECTED_MODE=no

退出vim后运行 

./create-cluster restart

再在windows下访问linux的redis,成功。

 

你可能感兴趣的:(redis,golang)