According to https://dev.mysql.com/doc/refman/5.6/en/charset-connection.html, When I connect to a mysql 5.6 server with the mysql 8.0 client using the command:
/usr/bin/mysql -h ${DB_HOST} -u ${DB_USER} -p --default-character-set=utf8mb4
I expect that the client will set up a utf8mb4 connection to the server. However, the connection is set to latin1:
mysql> SELECT * FROM INFORMATION_SCHEMA.SESSION_VARIABLES WHERE VARIABLE_NAME IN (
'character_set_client', 'character_set_connection', 'character_set_results', 'collation_connection' )
ORDER BY VARIABLE_NAME;
+--------------------------+-------------------+
| VARIABLE_NAME | VARIABLE_VALUE |
+--------------------------+-------------------+
| CHARACTER_SET_CLIENT | latin1 |
| CHARACTER_SET_CONNECTION | latin1 |
| CHARACTER_SET_RESULTS | latin1 |
| COLLATION_CONNECTION | latin1_swedish_ci |
+--------------------------+-------------------+
Using another character set, like:
/usr/bin/mysql -h ${DB_HOST} -u ${DB_USER} -p --default-character-set=koi8r
will result in the client connecting with the supplied character set:
mysql> SELECT * FROM INFORMATION_SCHEMA.SESSION_VARIABLES WHERE VARIABLE_NAME IN ( 'character_set_client', 'character_set_connection', 'character_set_results', 'collation_connection' ) ORDER BY VARIABLE_NAME;
+--------------------------+------------------+
| VARIABLE_NAME | VARIABLE_VALUE |
+--------------------------+------------------+
| CHARACTER_SET_CLIENT | koi8r |
| CHARACTER_SET_CONNECTION | koi8r |
| CHARACTER_SET_RESULTS | koi8r |
| COLLATION_CONNECTION | koi8r_general_ci |
+--------------------------+------------------+
The only way I can get the client connection to change is by executing charset utf8mb4
or SET NAMES utf8mb4
AFTER connecting to the server.
mysql> SET NAMES utf8mb4;
Query OK, 0 rows affected (0.01 sec)
mysql> SELECT * FROM INFORMATION_SCHEMA.SESSION_VARIABLES WHERE VARIABLE_NAME IN ( 'character_set_client', 'character_set_connection', 'character_set_results', 'collation_connection' ) ORDER BY VARIABLE_NAME;
+--------------------------+--------------------+
| VARIABLE_NAME | VARIABLE_VALUE |
+--------------------------+--------------------+
| CHARACTER_SET_CLIENT | utf8mb4 |
| CHARACTER_SET_CONNECTION | utf8mb4 |
| CHARACTER_SET_RESULTS | utf8mb4 |
| COLLATION_CONNECTION | utf8mb4_general_ci |
+--------------------------+--------------------+
Why will --default-character-set=utf8mb4 not work? I want to use other client tools like mysqldump and mysqlimport but without this flag I will get latin1 encoding instead of utf8mb4 encoding. Changing default server settings is not an option in this situation, it must be done from the client.
Some more information: I'm trying this from a 20.04 ubuntu WSL2 install, so there is no 5.6 or 5.7 client available. However, using a 5.6 or 5.7 windows mysql client will respect --default-character-set=utf8mb4, but the 8.0 windows client has the same behavior as the WSL2 client.
This behavior is explained in the 8.0 documentation: https://dev.mysql.com/doc/refman/8.0/en/charset-connection.html