【问题标题】:dial tcp [::1]:6397: connectex: No connection could be made because the target machine actively refused itdial tcp [::1]:6397: connectex: 由于目标机器主动拒绝,无法建立连接
【发布时间】:2019-02-18 03:38:51
【问题描述】:

我正在 Windows 上使用 Redis(第一次尝试 redis)编写一个简单的 Go Web 应用程序。我正在使用 go-redis 包连接到 Redis。

package main

import (
    "fmt"
    "net/http"
    "text/template"

    "github.com/go-redis/redis"
    "github.com/gorilla/mux"
)

var client *redis.Client
var tmpl *template.Template

func init() {
    client = redis.NewClient(&redis.Options{
        Addr:     "localhost:6397",
        Password: "",
        DB:       0,
    })
    tmpl = template.Must(template.ParseGlob("./templates/*.gohtml"))
    pong, err := client.Ping().Result()
    fmt.Println(pong, err)
}

func main() {
    router := mux.NewRouter()
    router.HandleFunc("/", indexHandler).Methods("GET")
    http.Handle("/", router)
    http.ListenAndServe(":1234", nil)
}

func indexHandler(w http.ResponseWriter, r *http.Request) {
    comments, err := client.LRange("comments", 0, 10).Result()
    check(err)
    tmpl.ExecuteTemplate(w, "index.gohtml", comments)
}

func check(err error) {
    if err != nil {
        fmt.Println(err)
        return
    }
}

但是当我运行这段代码时,我得到了

dial tcp [::1]:6397: connectex: 由于目标机器主动拒绝,无法建立连接。

我能找到的唯一答案是“启动 redis 服务器”。我的 redis 服务器已启动并正在运行(使用 redis 客户端中的“PING”命令检查它)。我也试过以管理员身份运行它,但没有运气。

截图:

【问题讨论】:

    标签: windows go redis


    【解决方案1】:

    这很可能是因为 Redis 服务器正在端口 6379(这是 Redis 服务器的默认端口)上运行,但您正在尝试连接到端口 6397

    将服务器地址更改为:

    Addr: "localhost:6379"

    Addr: "localhost:6397"

    这应该可以解决您的问题。

    【讨论】:

    • 非常感谢,已解决!不过真的很尴尬,考虑到我的问题有多愚蠢。再次感谢!
    • 发生了,没问题。
    猜你喜欢
    • 2022-01-26
    • 2021-11-13
    • 2021-07-15
    • 2013-05-18
    相关资源
    最近更新 更多