Help Center/ GaussDB(for MySQL)/ FAQs/ Database Parameter Modification/ How Do I Ensure that the Database Character Set of a GaussDB(for MySQL) Instance Is Correct?
Updated on 2024-09-05 GMT+08:00

How Do I Ensure that the Database Character Set of a GaussDB(for MySQL) Instance Is Correct?

UTF-8 supports 4 byte characters, but GaussDB(for MySQL) utf8 supports only 3 byte characters. Emojis and newly added Unicode characters cannot be stored using MySQL utf8 character set. MySQL released the utf8mb4 character set in 2010 and added the utf8mb4 code after 5.5.3 to be compatible with the 4-byte unicode. You only need to change utf8 to utf8mb4. No other conversion is required.

Huawei Cloud Data Admin Service (DAS) is a professional database management tool. You can view the database and system character sets through the DAS console.

Procedure

  1. Log in to the management console.
  2. Click in the upper left corner and select a region and project.
  3. Click in the upper left corner of the page, choose Databases > GaussDB(for MySQL).
  4. On the Instances page, locate the instance and click Log In in the Operation column.

    Alternatively, on the Instances page, click the instance name to go to the Basic Information page. Click Log In in the upper right corner of the page.

  5. On the displayed login page, enter the correct username and password and click Log In.
  6. On the top menu bar, choose SQL Operations > SQL Window.
  7. Run the following SQL statement in the SQL window to view the database character set:

    show variables like '%character%';

    Figure 1 SQL execution result

  8. Run the following SQL statement in the SQL window to view the database coding:

    show variables like 'collation%';

    Figure 2 SQL execution result

  9. Change the character set to utf8mb4.

    1. Run the following SQL statement to change the database character sets.

      ALTER DATABASE DATABASE_NAME DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

    2. Run the following SQL statement to change the table character sets.
      ALTER TABLE TABLE_NAME DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

      The SQL statement just changes the character sets of tables. The character sets of fields in the tables are not changed.

    3. Run the following SQL statement to change all the field character sets in tables:

      ALTER TABLE TABLE_NAME CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

    • character_set_client, character_set_connection, and character_set_results are the settings of the client.
    • character_set_system, character_set_server, and character_set_database are the settings of the server.
    • The priorities of the parameters on the server are as follows: character_set_database > character_set_server > character_set_system.