How MySql supports emoji expressions

foreword

During the coding process, this problem may be encountered. MySql or MariaDB needs to support emoji expressions. Share the steps below and record them by the way!

text

Modify the .cnf file

Since we are using the Docker image of MariaDB, we need to reprocess the original image, that is, replace the mariadb.cnf file in the original /etc/mysql/conf.d directory. If you don't know the specific directory of the modified file, you can use the original method, that is, start a MariaDB Docker image, then enter the container, and try to find it.

Find steps

1. Download the mirror

docker pull mariadb:latest

2. Start

docker run -it -e MYSQL_ROOT_PASSWORD=123456 mariadb:latest

MYSQL_ROOT_PASSWORD must be set here.
3. Enter the container

docker ps
docker exec -it ba593028e3a7 bash

Find the ID of the container according to docker ps, and then directly enter the specified container through the ID.

Modify the file

After finding the mariadb.cnf file, replace the following content to the specified location:

[client]
default-character-set = utf8mb4

[mysql]
default-character-set = utf8mb4

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

build image

1. Build the Dockerfile

FROM mariadb:latest
ADD mariadb.cnf /etc/mysql/conf.d/

2. Put the Dockerfile and mariadb.cnf in the same directory
3. Mirror

docker build -f ./Dockerfile -t mariadb-utf8mb4:latest .

4. Start a new image and verify the modification results. View the results of modifying variables
by using the following commands:

SHOW VARIABLES WHERE Variable_name LIKE 'character\_set\_%' OR Variable_name LIKE 'collation%';

Modify the encoding of databases, tables, and columns

Under normal circumstances, if the database is set to utf8mb4, there is no need to modify the encoding of the table separately, so the second step is not necessary!

ALTER DATABASE database_name CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
ALTER TABLE table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
ALTER TABLE table_name MODIFY column_name VARCHAR(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

Summarize

After the coding of the database, table and column is modified once, it can be imported into a new database by exporting the structure.

Guess you like

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