How to change MySQL character set utf8 to utf8mb4

How to change MySQL character set utf8 to utf8mb4

For MySQL 5.5, if the character set is not set, the default character set of MySQL is latin1 Latin character set;

However, with the further development of various businesses, in addition to the native language characters of various countries, some emoticons often appear in applications. Before MySQL 5.5, UTF-8 encoding only supported 1-3 bytes and supported the Unicode encoding area of ​​​​the BMP part; starting from MySQL 5.5, it can support 4-byte UTF encoding utf8mb4. One character can support more character sets and more emoticons.

utf8mb4 is compatible with utf8 and can represent more characters than utf8. It is a superset of the utf8 character set. Therefore, some new businesses, such as ISO, will set the character set of the MySQL database to utf8mb4.

Today, when dealing with an application requirement, I encountered such a problem:

Of course, the best way to adjust it is to change the character set of the MySQL database to utf8mb4 on the client side, but this will cause a lot of changes, and if part of the client is changed to utf8 and part to utf8mb4, confusion is likely to occur.

After several tests, the character set configuration in my.cnf of the MySQL database was changed to the following configuration:

[client] 
default-character-set=utf8mb4 
 
[mysqld] 
character-set-server = utf8mb4 
collation-server = utf8mb4_unicode_ci 
init_connect = 'SET NAMES utf8mb4' 
skip-character-set-client-handshake = true 
 
[mysql] 
default-character-set = utf8mb4

Recommendations for using MySQL character sets

• When creating a database/table and performing database operations, try to explicitly specify the character set to be used, rather than relying on the MySQL default settings, otherwise it may cause great trouble when upgrading MySQL;

• Although the garbled characters can be solved in most cases when both the database and the connection character set use latin1, the disadvantage is that SQL operations cannot be performed in units of characters. Generally, it is a better choice to set both the database and the connection character set to utf8.

• When using the mysql C API, set the MYSQL_SET_CHARSET_NAME attribute to utf8 with mysql_options immediately after initializing the database handle. This way, you do not need to explicitly specify the connection character set with the SET NAMES statement, and using mysql_ping to reconnect to a disconnected long connection will also reset the connection character set to utf8.

• For MySQL PHP API, the total running time of page-level PHP programs is generally short. After connecting to the database, you can explicitly set the connection character set once using the SET NAMES statement. However, when using long connections, please be sure to keep the connection open and use the SET NAMES statement to explicitly reset the connection character set after disconnecting and reconnecting.

Comprehensive understanding of the difference between utf8 and utf8mb4 in MySQL: https://www.jb51.net/article/90037.htm

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • Steps to change mysql character set to UTF8 under Linux system
  • Example of utf8mb4 collation in MySQL
  • MySQL encoding utf8 and utf8mb4 utf8mb4_unicode_ci and utf8mb4_general_ci
  • How to change the encoding of MySQL database to utf8mb4
  • mysql charset=utf8 do you really understand what it means
  • mysql garbled characters latin1 characters converted to UTF8 details

<<:  Detailed explanation of the practical application of centos7 esxi6.7 template

>>:  WeChat applet implements simple calculator function

Recommend

Centos8 builds nfs based on kdc encryption

Table of contents Configuration nfs server (nfs.s...

How to use vue filter

Table of contents Overview Defining filters Use o...

Detailed analysis of the difference between Ref and Reactive in Vue3.0

Table of contents Ref and Reactive Ref Reactive T...

Tutorial on deploying the open source project Tcloud with Docker on CentOS8

1. Install Docker 1. I installed Centos7 in the v...

Introduction to Docker Architecture

Docker includes three basic concepts: Image: A Do...

Ubuntu 16.04 installation tutorial under VMware 12

This article shares with you the installation tut...

Detailed explanation of MySQL cursor concepts and usage

This article uses examples to explain the concept...

A brief discussion on CSS height collapse problem

Performance For example: HTML: <div class=&quo...

jQuery plugin to achieve image suspension

This article shares the specific code of the jQue...

How to quickly install Nginx in Linux

Table of contents What is nginx 1. Download the r...

Detailed analysis of the parameter file my.cnf of MySQL in Ubuntu

Preface Based on my understanding of MySQL, I thi...

N ways to achieve two-column layout with CSS

1. What is a two-column layout? There are two typ...