tidb-incubator / weir

Apache License 2.0
97 stars 40 forks source link

set names do not work #82

Open joeytang opened 4 months ago

joeytang commented 4 months ago

Bug Report

weir在首次建立与底层mysql连接的时候,没有设置字符集,只是把字符集设置成了属性,并没有实际调用set names 指令,可以看代码如下 https://github.com/tidb-incubator/weir/blob/89d789c4be27a704ca1163ab02088a7d2c034eb1/pkg/proxy/backend/client/conn.go#L70 应该改为 c.charset = constant.DefaultCharset 这段删掉 在 handshake之后调用SetCharset,改完如下: //c.charset = constant.DefaultCharset

// Apply configuration functions.
for i := range options {
    options[i](c)
}

if err = c.handshake(); err != nil {
    return nil, errors.Trace(err)
}
if err = c.SetCharset(constant.DefaultCharset); err != nil {
    return nil, errors.Trace(err)
}

为什么这么改呢? SetCharset这个函数中有判断 if c.charset == charset { return nil } 所以如果c.charset = constant.DefaultCharset进行了赋值,也就是这个连接没有调用set names utf8mb4指令,当我们主动调用SetCharset(ctx, "utf8mb4")的时候,因为c.charset已经是utf8mb4了,就不会再往下执行,也就是不会发送 set name utf8mb4指令,从而导致字符集一次都没有设置过。

What did you do?

What did you expect to see?

What did you see instead?

What version of Weir are you using (weir-proxy -V)?