duanjiao8871 2018-09-13 11:48
浏览 1065
已采纳

拨打tcp [:: 1]:6397:connectex:无法建立连接,因为目标计算机主动拒绝了它

I am writing a simple go web application with redis (trying out redis for the first time) on windows. I'm using the go-redis package to connect to 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
    }
}

But when I run this code, I get "dial tcp [::1]:6397: connectex: No connection could be made because the target machine actively refused it."

The only answer that I could find was to "start the redis server". My redis server is up and running (Checked it by using the "PING" command in redis client).I have also tried running it as administrator, but no luck. Screenshot attached

Any help would be appreciated. Thanks in advanced!

  • 写回答

1条回答 默认 最新

  • dpfz27768 2018-09-13 12:29
    关注

    This is happening most likely because of the Redis server is running on port 6379 (that is the default port for Redis server) but you are trying to connect to the port 6397.

    Change the Server Address to:

    Addr: "localhost:6379"

    From Addr: "localhost:6397"

    And that should solve your problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名