拨打tcp [:: 1]:6397:connectex:无法建立连接,因为目标计算机主动拒绝了它 [英] dial tcp [::1]:6397: connectex: No connection could be made because the target machine actively refused it

查看:177
本文介绍了拨打tcp [:: 1]:6397:connectex:无法建立连接,因为目标计算机主动拒绝了它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 程序包主要进口 ("fmt""net/http"文本/模板"" github.com/go-redis/redis"" github.com/gorilla/mux")var client * redis.Clientvar tmpl * template.Templatefunc init(){客户端= redis.NewClient(& redis.Options {地址:"localhost:6397",密码:",DB:0,})tmpl = template.Must(template.ParseGlob("./templates/*.gohtml"))pong,err:= client.Ping().Result()fmt.Println(pong,err)}func main(){路由器:= mux.NewRouter()router.HandleFunc("/",indexHandler).Methods("GET")http.Handle("/",路由器)http.ListenAndServe(:1234",无)}func indexHandler(w http.ResponseWriter,r * http.Request){评论,错误:= client.LRange("comments",0,10).Result()检查(错误)tmpl.ExecuteTemplate(w,"index.gohtml",注释)}func check(err error){如果err!= nil {fmt.Println(err)返回}} 

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

拨号tcp [:: 1]:6397:connectex:由于目标计算机主动拒绝连接,因此无法建立连接.

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

屏幕截图:

解决方案

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

将服务器地址更改为:

地址:"localhost:6379"

来自 Addr:"localhost:6397"

那应该可以解决您的问题.

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:

解决方案

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.

这篇关于拨打tcp [:: 1]:6397:connectex:无法建立连接,因为目标计算机主动拒绝了它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆