Set the Mysql character set to UTF-8 to solve Chinese garbled.

Article directory When the public number is developed, the user nickname is garbled. The result of the investigation is the encoding problem of the d...

Article directory


When the public number is developed, the user nickname is garbled. The result of the investigation is the encoding problem of the default character set of the Mysql database. To solve the Chinese garbled, the default character set must be set to UTF-8.

Login to mysql and view the character set code by command.

mysql> show variables like "char%";

The results are as follows:

+--------------------------+----------------------------+ | Variable_name | Value | +--------------------------+----------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | latin1 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | latin1 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | +--------------------------+----------------------------+ 8 rows in set (0.00 sec)

It is found that the encoding of character_set_database and character_set_server is latin1 by default, so it needs to be modified to UTF-8.

Modify Mysql configuration file

vim /etc/my.cnf

Add character_set_server=utf8 below [mysqld], at the following location:

[mysqld] ... datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock character_set_server=utf8

When the setup is complete, save wq and restart mysql service

sudo systemctl restart mysqld

Look again at the database character set encoding and see that it has all been changed to UTF-8:

mysql> show variables like 'character%'; +--------------------------+----------------------------+ | Variable_name | Value | +--------------------------+----------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | utf8 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | utf8 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | +--------------------------+----------------------------+ 8 rows in set (0.01 sec)

(end)

Reference link:

http://www.2cto.com/database/201311/255324.html

3 October 2019, 02:33 | Views: 8019

Add new comment

For adding a comment, please log in
or create account

0 comments