pingcap / tidb

TiDB - the open-source, cloud-native, distributed SQL database designed for modern applications.
https://pingcap.com
Apache License 2.0
37.25k stars 5.84k forks source link

Invalid connection when using gorm to connect to ha-proxy -> tidb #39165

Open HoangTheQuyen opened 1 year ago

HoangTheQuyen commented 1 year ago

General Question

I have a few questions about bugs when i using ha-proxy + tiadb with golang to connect to Invalid connection using GORM has anyone seen it yet? Please helm me, tks all Bro Info : Go 1.18 gorm.io/driver/mysql v1.4.3 gorm.io/gorm v1.24.1 Currently wait_timeout configuration is 28800s(8h). Source golang to connecting to

func connect(dsn string) (*gorm.DB, error) {
    logMode := logger.Silent

    sqlDB, err := sql.Open("mysql", dsn)
    if err != nil {
        return nil, err
    }

    err = sqlDB.Ping()
    if err != nil {
        return nil, err
    }

    sqlDB.SetConnMaxLifetime(4 * time.Minute)
    sqlDB.SetMaxIdleConns(32)
    sqlDB.SetMaxOpenConns(8)

    gormDB, err := gorm.Open(mysql.New(mysql.Config{Conn: sqlDB}), &gorm.Config{
        Logger: logger.Default.LogMode(logMode),
    })

    if err != nil {
        return nil, err
    }

    return gormDB, nil
}
And HA Proxy config
global
    log /dev/log    local0
    log /dev/log    local1 notice
    chroot /var/lib/haproxy
    stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners
    stats timeout 30s
    user haproxy
    group haproxy
    daemon
        ulimit-n 550977
defaults
    log    global
    mode    http
    option    httplog
    option    dontlognull
        timeout connect 5000
        timeout client  50000
        timeout server  50000
    errorfile 400 /etc/haproxy/errors/400.http
    errorfile 403 /etc/haproxy/errors/403.http
    errorfile 408 /etc/haproxy/errors/408.http
    errorfile 500 /etc/haproxy/errors/500.http
    errorfile 502 /etc/haproxy/errors/502.http
    errorfile 503 /etc/haproxy/errors/503.http
    errorfile 504 /etc/haproxy/errors/504.http
listen tidbcluster49-101-103_mepprod
        bind *:4000
        mode tcp
        timeout client  30800s
        timeout server  30800s
        balance leastconn
        option tcplog
        maxconn 20000
        server tidb49101 10.11.23.100:4000 check inter 2000 rise 2 fall 3
        server tidb49102 10.11.23.101:4000 check inter 2000 rise 2 fall 3
        server tidb49103 10.11.23.102:4000 check inter 2000 rise 2 fall 3
ljluestc commented 6 months ago

package main

import (
    "database/sql"
    "log"
    "time"

    "gorm.io/driver/mysql"
    "gorm.io/gorm"
    "gorm.io/gorm/logger"
)

func connect(dsn string) *gorm.DB {
    logMode := logger.Silent

    sqlDB, err := sql.Open("mysql", dsn)
    if err != nil {
        log.Fatalf("Error opening database: %v", err)
    }

    err = sqlDB.Ping()
    if err != nil {
        log.Fatalf("Error pinging database: %v", err)
    }

    // Reduce connection max lifetime to handle potential proxy/connection aging
    sqlDB.SetConnMaxLifetime(2 * time.Minute)
    sqlDB.SetMaxIdleConns(10)
    sqlDB.SetMaxOpenConns(50)

    gormDB, err := gorm.Open(mysql.New(mysql.Config{Conn: sqlDB}), &gorm.Config{
        Logger: logger.Default.LogMode(logMode),
    })
    if err != nil {
        log.Fatalf("Error opening GORM database: %v", err)
    }

    return gormDB
}

global
    log /dev/log local0
    log /dev/log local1 notice
    chroot /var/lib/haproxy
    stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners
    stats timeout 30s
    user haproxy
    group haproxy
    daemon
    maxconn 20480
    ulimit-n 65536

defaults
    log global
    mode tcp
    option tcplog
    option dontlognull
    timeout connect 10s
    timeout client  3h
    timeout server  3h

listen tidbcluster
    bind *:4000
    balance leastconn
    option tcp-check
    timeout client  3h
    timeout server  3h
    maxconn 20000
    server tidb49101 10.11.23.100:4000 check inter 10s rise 2 fall 3
    server tidb49102 10.11.23.101:4000 check inter 10s rise 2 fall 3
    server tidb49103 10.11.23.102:4000 check inter 10s rise 2 fall 3