Support for the storage of emoji expressions in MySQL

Due to the need to implement the function of emoji expression comments, the database needs to support the storage of emoji expressions. According to the queried data, this function is finally realized. Now the implementation process and some problems encountered in the process are recorded for your reference and communication.

A character encoded by mysql's utf8 is up to 3 bytes, but an emoji expression is 4 bytes, so utf8 does not support storing emoji expressions. But utf8mb4, a superset of utf8, can have up to 4 bytes per character, so it can support the storage of emoji expressions. The following introduces the specific method on how to modify the encoding format of the mysql database to utf8mb4.
The configuration file of MySQL in Linux system is my.cnf. (Note: The implementation process of changing the default encoding of mysql to utf8 is also recorded)
The configuration file in Windows is my.ini.
1. Modify the mysql configuration file

Find the my.cnf file in the /etc/mysql path, open the file and edit it with the vi command, and add the following configuration:
[client]
default-character-set=utf8mb4

[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'

[mysql]
default-character-set=utf8mb4

After modification, save and exit through wq
2. Modify the character set of database/table and column

Enter into mysql and execute the command as follows:
1) Modify the character set of database:
ALTER DATABASE database name CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
example:
ALTER DATABASE xxxdb CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
2) Step 1) After the execution is completed, you need to execute use database name to indicate the database whose character set needs to be modified at present; example: use xxxdb;
3) Modify the character set of the table:
ALTER TABLE table name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
Example:
ALTER TABLE user_comments CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
4) Modify the character set of the column:
ALTER TABLE table name CHANGE field name field name The original data type of the field CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode
Example:
ALTER TABLE user_comments CHANGE content content TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

After the above modification is completed, exit to exit mysql
3. When restarting mysql

here, I used service mysql restart at first, and finally found that this command did not work, and the restart was unsuccessful , resulting in the later viewing of the character set, the state of the desired character set has not been achieved.
Therefore, the following methods can be used to restart mysql correctly.
3.1 Stop the operation of

msql Execute the stop command through /etc/init.d/mysql
3.2 Start mysql

Execute the start command through /etc/init.d/mysql
4. Check the character set:

Enter mysql, use the SHOW VARIABLES WHERE Variable_name LIKE 'character\_set\_%' OR Variable_name LIKE 'collation%'; command to check the character set. After the

above steps are implemented, the emoji expression can be successfully stored in the mysql database function.

——————————————————————————————————————————————————— ——————————————
Since the mysql database needs to be backed up and restored, when importing and exporting .sql files with emoji expressions, you need to pay attention to the encoding format during execution. The following is the import Specific operations with export:

5. Import and export of sql files with emoji expressions (mainly export in windows)

(if the export in the server-side code is: mysqldump –default-character-set-utf8mb4 –u username –p database name> Export the physical path of the file; the imported command does not need to specify the encoding format, just need to execute the command normally)
5.1

Export the file with the suffix .sql, and the file data contains the emoji expression data, and back it up When exporting, do not use third-party software to export, but use the command line to perform the export action. The main reason is that when using a third-party to export the file, the default export encoding format is utf-8. , The encoding format only supports up to 3 bytes, while an emoji expression has 4 bytes, which will cause the data of the emoji expression to become garbled. Therefore, the specific operation steps when exporting locally are as follows:
1. Open cmd, first find the path where the mysqldump executable file is located;
2. Enter mysqldump –default-character-set-utf8mb4 –u username –p database name> after the path Export the physical path of the file, and press Enter to complete the export function. You can find the exported file in the exported physical path.
5.2 Import When importing the .sql file in

the Linux system to the local, do not use Navicat Premium to import, but use the physical path of the source .sql file in the command line as shown below to import as follows.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326351046&siteId=291194637