How to store Emoji Character in MySQL Database

后端 未结 16 2184
迷失自我
迷失自我 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: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)
    

提交回复
热议问题