pingcap / tidb

TiDB is an open-source, cloud-native, distributed, MySQL-Compatible database for elastic scale and real-time analytics. Try AI-powered Chat2Query free at : https://www.pingcap.com/tidb-serverless/

Home Page:https://pingcap.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

@@global.character_set_connection seems take no effect for new connections

lcwangchao opened this issue · comments

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

mysql> set @@global.character_set_connection="latin1";

Then schedule a new connection using MySQL client:

$ mysql --comments --host 127.0.0.1 -uroot -P4000
mysql> select @@global.character_set_connection, @@character_set_connection;

2. What did you expect to see? (Required)

I use a mysql client with version 8.0.33 to do this test

$ mysql --version
mysql  Ver 8.0.33 for macos13.3 on arm64 (Homebrew)

In mysql 8.0.33, the new connection has the same @@character_set_connection with global settings:

$ mysql --comments --host 127.0.0.1 -uroot -P3306
mysql> select @@global.character_set_connection, @@character_set_connection;
+-----------------------------------+----------------------------+
| @@global.character_set_connection | @@character_set_connection |
+-----------------------------------+----------------------------+
| latin1                            | latin1                     |
+-----------------------------------+----------------------------+
1 row in set (0.00 sec)

mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.33    |
+-----------+
1 row in set (0.01 sec)

3. What did you see instead (Required)

But for TiDB, @@global.character_set_connection does not take effect:

> mysql --comments --host 127.0.0.1 -uroot -P4001
mysql> select @@global.character_set_connection, @@character_set_connection;
+-----------------------------------+----------------------------+
| @@global.character_set_connection | @@character_set_connection |
+-----------------------------------+----------------------------+
| latin1                            | utf8mb4                    |
+-----------------------------------+----------------------------+
1 row in set (0.00 sec)

4. What is your TiDB version? (Required)

master

There are 3 places where the charset can be set:

When ExecuteStmt is called, it will call loadCommonGlobalVariablesIfNeeded to load the global variable to the current session. However, for character_set_connection, the skipInit is set as true which means this step will be skipped:

{Scope: ScopeGlobal | ScopeSession, Name: CollationConnection, Value: mysql.DefaultCollationName, skipInit: true, Validation: func(vars *SessionVars, normalizedValue string, originalValue string, scope ScopeFlag) (string, error) {

That's why the global settings of character_set_connection does not take effect.

  • Some clients will also run an extra statement SET NAMES ... after the handshake. Then the charset/collation in the current session will be reset too. That depends on the client and its configurations. For MySQL client with version 8.0.3, it does not do it.