pingcap / pingcap/tidb

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

Open
#39,165 1 comment 0 reactions 0 assignees View on GitHub
type/question
Dominant language
Go
Stars
40.5k
Forks
6.2k
PR merge metrics
PR metrics pending

Description

## 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](http://gorm.io/driver/mysql) v1.4.3
[gorm.io/gorm](http://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
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.