How to store Emoji Character in MySQL Database

后端 未结 16 2005
迷失自我
迷失自我 2020-11-22 05:34

I am using Emoji character in my project. That characters are saved (??) into mysql database. I had used database Default collation in utf8mb4_general_ci. It sh

相关标签:
16条回答
  • 2020-11-22 06:29

    If you use command line interface for inserting sql file to database.

    Be sure your table charset utf8mb4 and column collation utf8mb4_unicode_ci or utf8mb4_bin

    mysql -u root -p123456 my_database < profiles.sql
    

    ERROR 1366 (HY000) at line 1679: Incorrect string value: '\xF0\x9F\x98\x87\xF0\x9F...' for column 'note' at row 328

    we can solve the problem with this parameter --default-character-set=name (Set the default character set)

    mysql -u root -p123456 --default-character-set=utf8mb4 my_database < profiles.sql
    
    0 讨论(0)
  • 2020-11-22 06:31

    For anyone trying to solve this on a managed MySQL instance (in my case on AWS RDS), the easiest way was to modify the parameter group and set the server character set and collation to be utf8mb4 and utf8mb4_bin, respectively. After rebooting the server, a quick query verifies the settings for system databases and any newly created ones:

    SELECT * FROM information_schema.SCHEMATA S;
    
    0 讨论(0)
  • 2020-11-22 06:32

    For Rails, next to the accepted answer, don't forget to add:

    encoding: utf8mb4
    collation: utf8mb4_bin
    

    to your database.yml

    0 讨论(0)
  • 2020-11-22 06:33

    Step 1, change your database's default charset:

    ALTER DATABASE database_name CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
    

    if the db is not created yet, create it with correct encodings:

    CREATE DATABASE database_name DEFAULT CHARSET = utf8mb4 DEFAULT COLLATE = utf8mb4_unicode_ci;
    

    Step 2, set charset when creating table:

    CREATE TABLE IF NOT EXISTS table_name (
    ...
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;
    

    or alter table

    ALTER TABLE table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
    ALTER TABLE table_name MODIFY field_name TEXT CHARSET utf8mb4;
    
    0 讨论(0)
  • 2020-11-22 06:33

    I have a good solution to save your time. I also meet the same problem but I could not solve this problem by the first answer.

    Your defualt character is utf-8. But emoji needs utf8mb4 to support it. If you have the permission to revise the configure file of mysql, you can follow this step.

    Therefore, do this following step to upgrade your character set ( from utf-8 to utf8mb4).

    step 1. open your my.cnf for mysql, add these following lines to your my.cnf.

    [mysqld]
    character-set-server = utf8mb4
    collation-server = utf8mb4_general_ci
    init_connect='SET NAMES utf8mb4'
    
    [mysql]
    default-character-set = utf8mb4
    
    
    [client]
    default-character-set = utf8mb4
    

    step2. stop your mysql service, and start mysql service

    mysql.server stop
    mysql.server start
    

    Finished! Then you can check your character are changed into utf8mb4.

    mysql> SHOW VARIABLES LIKE 'character_set%';
    +--------------------------+----------------------------------------------------------+
    | 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                                                     |
    | character_sets_dir       | /usr/local/Cellar/mysql@5.7/5.7.29/share/mysql/charsets/ |
    +--------------------------+----------------------------------------------------------+
    8 rows in set (0.00 sec)
    
    0 讨论(0)
  • 2020-11-22 06:34

    I have updated my database and table to upgraded from utf8 to utf8mb4. But nothing works for me. Then I tried to update column datatype to blob, luckily it worked for me and data has been saved. Even my database and table both are CHARACTER SET utf8 COLLATE utf8_unicode

    0 讨论(0)
提交回复
热议问题