douyangqian5243 2017-02-28 05:52
浏览 345

golang MySQL“连接被拒绝”

I'm new in Go (Golang). I wrote a simple benchmark program to test the concurrent processing with MySQL. Keep getting "dial tcp 52.55.254.165:3306: getsockopt: connection refused", "unexpected EOF" errors when I increase the number of concurrent channels.

Each go routine is doing a batch insert of 1 to n number of row to a simple customer table. The program allows to set variable insert size (number of rows in a single statement) and number of parallel go routine (each go routine performs one insert above). Program works fine with small numbers row<100 and number go routines<100. But start getting Unexpected EOF errors when the numbers increase, especially the number of parallel go routines.

Did search for clues. Based on them, I've set the database max connection and 'max_allowed_packet' and 'max_connections'. I've also set the go program db.db.SetMaxOpenConns(200), db.SetConnMaxLifetime(200), db.SetMaxIdleConns(10). I've experimented with big numbers and small (from 10 to 2000). Nothing seems to solve the program.

I have one global db connection open. Code snippet below:

// main package

func main() {

    var err error
    db, err = sql.Open("mysql","usr:pwd@tcp(ip:3306)/gopoc")
    if err != nil {
        log.Panic(err)
    }
    db.SetMaxOpenConns(1000)
    db.SetConnMaxLifetime(1000)
    db.SetMaxIdleConns(10)

    // sql.DB should be long lived "defer" closes it once this function ends
    defer db.Close()

    if err = db.Ping(); err != nil {
        log.Panic(err)
    }

    http.HandleFunc("/addCust/", HFHandleFunc(addCustHandler))

    http.ListenAndServe(":8080", nil)
}

// add customer handler
func addCustHandler(w http.ResponseWriter, r *http.Request) {

    // experected url: /addCust/?num=3$pcnt=1
    num, _ := strconv.Atoi(r.URL.Query().Get("num"))
    pcnt, _ := strconv.Atoi(r.URL.Query().Get("pcnt"))

    ch := make([]chan string, pcnt) // initialize channel slice
    for i := range ch {
        ch[i] = make(chan string, 1)
    }

    var wg sync.WaitGroup

    for i, chans := range ch {
        wg.Add(1)
        go func(cha chan string, ii int) {
            defer wg.Done()
            addCust(num)
            cha <- "Channel[" + strconv.Itoa(ii) + "]
"
        }(chans, i)
    }

    wg.Wait()

    var outputstring string

    for i := 0; i < pcnt; i++ {
        outputstring = outputstring + <-ch[i]
    }

    fmt.Fprintf(w, "Output:
%s", outputstring)
}

func addCust(cnt int) sql.Result {
...
    sqlStr := "INSERT INTO CUST (idCUST, idFI, fName, state, country) VALUES "

    for i := 0; i < cnt; i++ {

        sqlStr += "(" + strconv.Itoa(FiIDpadding+r.Intn(CidMax)+1) + ", " + strconv.Itoa(FiID) +", 'fname', 'PA', 'USA), " 

    }

    //trim the last ,
    sqlStr = sqlStr[0:len(sqlStr)-2] + " on duplicate key update lname='dup';"

    res, err := db.Exec(sqlStr)
    if err != nil {
        panic("
Insert Statement error
" + err.Error()) 
    }

    return res
}
  • 写回答

1条回答 默认 最新

  • duanliaoyin3171 2017-02-28 07:17
    关注

    I suppose you are calling sql.Open in each of your routine?

    The Open function should be called just once. You should share your opened DB connection between your routines. The DB returned by the Open function can be used concurrently and has its own pool

    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)