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

    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?