“Can't initialize character set utf8mb4” with Windows mysql-python

后端 未结 2 1180
野的像风
野的像风 2021-01-12 02:46

I\'m getting an error try to connect to a remote mysql database from a Windows 7 client via python 2.7 + MySQLdb 1.2.5 + sqlalchemy

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-12 02:55

    Consider the following checklist:

    1. Did you check your MySQL configuration file (/etc/my.cnf)? It should be:

      [client]
      default-character-set = utf8mb4
      
      [mysql]
      default-character-set = utf8mb4
      
      [mysqld]
      character-set-client-handshake = FALSE
      character-set-server = utf8mb4
      collation-server = utf8mb4_unicode_ci
      

      And you can verify them via:

      mysql> SHOW VARIABLES WHERE Variable_name LIKE 'character\_set\_%' OR Variable_name LIKE 'collation%';
      +--------------------------+--------------------+
      | Variable_name            | Value              |
      +--------------------------+--------------------+
      | character_set_client     | utf8mb4            |
      | character_set_connection | utf8mb4            |
      | character_set_database   | utf8mb4            |
      | character_set_filesystem | binary             |
      | character_set_results    | utf8mb4            |
      | character_set_server     | utf8mb4            |
      | character_set_system     | utf8               |
      | collation_connection     | utf8mb4_unicode_ci |
      | collation_database       | utf8mb4_unicode_ci |
      | collation_server         | utf8mb4_unicode_ci |
      +--------------------------+--------------------+
      10 rows in set (0.00 sec)
      

      -thanks to Mathias's blog post

    2. Enfore enforce UTF-8 to be used between Python and MySQL:

      # Connect to mysql.
      dbc = MySQLdb.connect(host='###', user='###', passwd='###', db='###', use_unicode=True)
      
      # Create a cursor.
      cursor = dbc.cursor()
      
      # Enforce UTF-8 for the connection.
      cursor.execute('SET NAMES utf8mb4')
      cursor.execute("SET CHARACTER SET utf8mb4")
      cursor.execute("SET character_set_connection=utf8mb4")
      
      # Do database stuff.
      
      # Commit data.
      dbc.commit()
      
      # Close cursor and connection.
      cursor.close()
      dbc.close()
      
      • thanks to Tomasz Nguyen's answer on stackoverflow
    3. Official tip from MySQL regarding Can't initialize character set:

      This error can have any of the following causes:

      • The character set is a multibyte character set and you have no support for the character set in the client. In this case, you need to recompile the client by running CMake with the -DDEFAULT_CHARSET=charset_name or -DWITH_EXTRA_CHARSETS=charset_name option. See Section 2.9.4, “MySQL Source-Configuration Options”.

      • All standard MySQL binaries are compiled with -DWITH_EXTRA_CHARSETS=complex, which enables support for all multibyte character sets. See Section 2.9.4, “MySQL Source-Configuration Options”.

      • The character set is a simple character set that is not compiled into mysqld, and the character set definition files are not in the place where the client expects to find them.

        In this case, you need to use one of the following methods to solve the problem:

        • Recompile the client with support for the character set. See Section 2.9.4, “MySQL Source-Configuration Options”.

        • Specify to the client the directory where the character set definition files are located. For many clients, you can do this with the --character-sets-dir option.

        • Copy the character definition files to the path where the client expects them to be.

提交回复
热议问题