Bug #108346 MySQL Connector Python - Character set 'utf8' unsupported - MySQL 5.6
Submitted: 31 Aug 2022 10:59 Modified: 31 Aug 2022 13:34
Reporter: Steffen Pohlen Email Updates:
Status: Unsupported Impact on me:
None 
Category:Connector / Python Severity:S3 (Non-critical)
Version:8.0.30 OS:Debian
Assigned to: CPU Architecture:x86

[31 Aug 2022 10:59] Steffen Pohlen
Description:
I found that connecting to MySQL 5.6 with Connector 8.0.30 can result in different errors, depending on the Python version used:

| Python    |  MySQL  | No Charset          | latin1 | utf8mb3             | utf8                | utf8mb4             |
|-----------|---------|---------------------|--------|---------------------|---------------------|---------------------|
| 3.10.6    |  5.6.51 | ok                  | ok     | ok                  | Error 1273          | Error 1273          |
| 3.11.0rc1 |  5.6.51 | Error "unsupported" | ok     | Error "unsupported" | Error "unsupported" | Error "unsupported" | 

From 3.11 I get:

File "/usr/local/lib/python3.11/site-packages/mysql/connector/constants.py", line 775, in get_charset_info            
    info = cls.get_default_collation(charset)
File "/usr/local/lib/python3.11/site-packages/mysql/connector/constants.py", line 746, in get_default_collation       
    raise ProgrammingError(f"Character set '{charset}' unsupported")
    mysql.connector.errors.ProgrammingError: Character set 'utf8' unsupported

From 3.10:
   File "/usr/local/lib/python3.10/site-packages/mysql/connector/abstracts.py", line 1032, in set_charset_collation
     self._execute_query(f"SET NAMES '{charset_name}' COLLATE '{collation_name}'")
   File "/usr/local/lib/python3.10/site-packages/mysql/connector/connection_cext.py", line 563, in cmd_query
     raise get_mysql_exception(
   mysql.connector.errors.DatabaseError: 1273 (HY000): Unknown collation: 'utf8mb4_0900_ai_ci'

How to repeat:
import mysql.connector

charsets = [None, 'latin1', 'utf8mb3', 'utf8mb4', 'utf8']
for cs in charsets:
    print('-------------------------------------------------------------------------')
    print('attempting connect with charset ' + str(cs))
    dbconn=None
    try:
        if cs is None:
            dbconn = mysql.connector.connect(user='root', password='pwd'
                , database='schema', host='ipaddress', port=3306)
        elif cs == 'utf8mb3':
            dbconn = mysql.connector.connect(user='root', password='pwd'
                , database='schema', host='ipaddress', port=3306, charset=cs, collation='utf8mb3_general_ci')
        else:
            dbconn = mysql.connector.connect(user='root', password='pwd'
                , database='schema', host='ipaddress', port=3306, charset=cs)
    except mysql.connector.Error as err:
        print('ERROR: {}'.format(err))
    if not dbconn is None:
        dbconn.close()

Suggested fix:
I found that in constants.py you added a fallback for MySQL 5.7 to handle charsets:

# class CharacterSet(_Constants):
#    def set_mysql_version(cls, version):
#        if cls.mysql_version == (5, 7):
#            cls.desc = MYSQL_CHARACTER_SETS_57

Since the Connector version 8.0.30 claims to support also MySQL 5.5 and 5.6, I think this should be extended for those versions also.

After adding the lines
        if cls.mysql_version == (5, 6):
            cls.desc = MYSQL_CHARACTER_SETS_57
the results changed to this:

| Python    |  MySQL  | No Charset          | latin1 | utf8mb3             | utf8                | utf8mb4             |
|-----------|---------|---------------------|--------|---------------------|---------------------|---------------------|
| 3.10.6    |  5.6.51 | ok                  | ok     | Error "unsupported" | ok                  | ok                  |
| 3.11.0rc1 |  5.6.51 | ok                  | ok     | Error "unsupported" | ok                  | ok                  | 

I can't see why 3.11 showed errors different from 3.10, but the patch at least resulted in a more consistent behaviour. 
Probably it should also get the case for "(5, 5)" but I have no way to test that.
[31 Aug 2022 13:21] MySQL Verification Team
Hi Mr. Pohlen,

Thank you for taking the time to report a problem.  Unfortunately you are not using a current version of the product you reported a problem with -- the problem might already be fixed. Please download a new version from http://www.mysql.com/downloads/

Simply put, MySQL version 5.6 is not supported for quite a long time, already.

If you are able to reproduce the bug with one of the latest versions, please change the version on this bug report to the version you tested and we shall be happy to change the status back to "Open".  Again, thank you for your continued support of MySQL.
[31 Aug 2022 13:34] Steffen Pohlen
Hi, thanks for the quick reply.
However, on https://dev.mysql.com/doc/connector-python/en/connector-python-versions.html you state that Connector 8.0 supports MySQL Server Versions "8.0, 5.7, 5.6, 5.5". If that is no longer true, you should at least change that information.
[20 Sep 2022 15:58] Anye Li
Could you please reconsider the status of this bug?

1. This is about client code.  It is possible that the person running the client is not the server operator and cannot force them to upgrade.

2. There is a real bug here, which is that if you connect to a 5.7 server, then mysql.connector.constants.CharacterSet gets globally modified and then you start getting this error when trying to connect to 8.0 servers.  It's one thing to say, we don't support talking to old servers anymore; it's quite another for the act of connecting an old server to cripple the library for the remainder of the lifetime of the application.