go-sql-driver / mysql

Go MySQL Driver is a MySQL driver for Go's (golang) database/sql package
https://pkg.go.dev/github.com/go-sql-driver/mysql
Mozilla Public License 2.0
14.45k stars 2.3k forks source link

connect to doris panic("invalid connection') #1602

Closed 365taofang closed 2 months ago

365taofang commented 2 months ago

Issue description

i have an doris cluster, when i connect to it, db.Ping() will return an err: invalid connection. i try the code in window11 and ubuntu20, has the same error When i use the same code to connect mysql server, is fine. And i use python and php to connect doris, that's normal.

Example code

func InitDB() (err error) {
    dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", userName, password, ipAddress, port, dbName)
    db, err = sql.Open("mysql", dsn)
    if err != nil {
        return err
    }
    err = db.Ping()
    if err != nil {
        fmt.Println("invalid!", err)
        return err
    }
    db.SetMaxOpenConns(50)
    fmt.Println("connect to db success!")
    return nil
}

Error log

[mysql] 2024/06/27 15:25:51 connection.go:49: unexpected EOF

Configuration

Driver version (or git SHA): github.com/go-sql-driver/mysql v1.8.1

Go version: go version go1.22.4 windows/amd64

Server version: Doris 2.0.5

Server OS: Windows 11, ubuntu 20

methane commented 2 months ago

I never used doris. Have you reported it to doris? Please write a link to the issue on doris.

Error log from v1.8.1 has wrong filename:lineno. Please try @1.8 or @master.

365taofang commented 2 months ago

@methane i have changed to v1.8, err now is: "packets.go:37: unexpected EOF" and i track this errors, is raise from internal/poll/fd_windows.go:437 n, err = execIO(o, func(o *operation) error { return syscall.WSARecv(o.fd.Sysfd, &o.buf, 1, &o.qty, &o.flags, &o.o, nil) })

and i search this error, someone say it's an net bug? how can i solve it net: ExecIO invalid number of bytes written

365taofang commented 2 months ago

@methane i downgrade github.com/go-sql-driver/mysql to version v1.5.0, it's success!!! why higher version cause errors?

shogo82148 commented 2 months ago

I don't know why. we need more information. can you try git-bisect to find a commit that causes error?

Raaaaaaaay86 commented 2 months ago

having same issue by using GORM to connect Apache Doris. It becomes to no error after I downgrade github.com/go-sql-driver/mysql to v1.7.0.

I'm interested in fixing this issue but don't know how. Anyone can give me advice?

Raaaaaaaay86 commented 2 months ago
 go get github.com/go-sql-driver/mysql@924f8336da7226f4cd4bfac575d394ffa20aacb4

I think this is the commit cause the error.

 go get github.com/go-sql-driver/mysql@72e78ee26806a26405ee462c4cf82406f094a143

If I use 72e78e, the error is gone

methane commented 2 months ago

It seems bug in doris.

Try the version including the PR.

Raaaaaaaay86 commented 2 months ago

I saw those issue which you mentioned. It seems related to the parsing logic of connection attribute to cause this error.

func encodeConnectionAttributes(textAttributes string) string {
    connAttrsBuf := make([]byte, 0, 251)

    // default connection attributes
    connAttrsBuf = appendLengthEncodedString(connAttrsBuf, connAttrClientName)
    connAttrsBuf = appendLengthEncodedString(connAttrsBuf, connAttrClientNameValue)
    connAttrsBuf = appendLengthEncodedString(connAttrsBuf, connAttrOS)
    connAttrsBuf = appendLengthEncodedString(connAttrsBuf, connAttrOSValue)
    connAttrsBuf = appendLengthEncodedString(connAttrsBuf, connAttrPlatform)
    connAttrsBuf = appendLengthEncodedString(connAttrsBuf, connAttrPlatformValue)
    connAttrsBuf = appendLengthEncodedString(connAttrsBuf, connAttrPid)
    connAttrsBuf = appendLengthEncodedString(connAttrsBuf, strconv.Itoa(os.Getpid()))

    // user-defined connection attributes
    for _, connAttr := range strings.Split(textAttributes, ",") {
        attr := strings.SplitN(connAttr, ":", 2)
        if len(attr) != 2 {
            continue
        }
        for _, v := range attr {
            connAttrsBuf = appendLengthEncodedString(connAttrsBuf, v)
        }
    }

    return string(connAttrsBuf)
}

func newConnector(cfg *Config) (*connector, error) {
    encodedAttributes := encodeConnectionAttributes(cfg.ConnectionAttributes)
    if len(encodedAttributes) > 250 {
        return nil, fmt.Errorf("connection attributes are longer than 250 bytes: %dbytes (%q)", len(encodedAttributes), cfg.ConnectionAttributes)
    }
    return &connector{
        cfg:               cfg,
        encodedAttributes: encodedAttributes,
    }, nil
}

I discover that the connector is always adding the default connection attribute into the encodedAttributes. I use IDE debugger to set the encodedAttributes to empty string in runtime and the error is solved. Is it possible to not set any connection attribute when a new connector is built?

methane commented 2 months ago

Why do we need to support doris bug? How about just upgrade your doris? Doris is not our supported server. We do not use doris.

Raaaaaaaay86 commented 2 months ago

understand. I'm not asking for support. Just want to know if there's any existing way to achieve my goal. The cause is obvious occurs on the Doris side, I'll convince my colleagues to upgrade.

Raaaaaaaay86 commented 2 months ago

Issue description

i have an doris cluster, when i connect to it, db.Ping() will return an err: invalid connection. i try the code in window11 and ubuntu20, has the same error When i use the same code to connect mysql server, is fine. And i use python and php to connect doris, that's normal.

Example code

func InitDB() (err error) {
  dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", userName, password, ipAddress, port, dbName)
  db, err = sql.Open("mysql", dsn)
  if err != nil {
      return err
  }
  err = db.Ping()
  if err != nil {
      fmt.Println("invalid!", err)
      return err
  }
  db.SetMaxOpenConns(50)
  fmt.Println("connect to db success!")
  return nil
}

Error log

[mysql] 2024/06/27 15:25:51 connection.go:49: unexpected EOF

Configuration

Driver version (or git SHA): github.com/go-sql-driver/mysql v1.8.1

Go version: go version go1.22.4 windows/amd64

Server version: Doris 2.0.5

Server OS: Windows 11, ubuntu 20

Please try to upgrade to Doris 2.0.7. It might help : )