How to deal with garbled characters in Mysql database

How to deal with garbled characters in Mysql database

In MySQL, database garbled characters can generally be fixed by setting the character set, but garbled characters can appear at various stages, so this article summarizes the various stages of garbled characters and the corresponding methods.

Add charset settings when creating a database/table

Building a database

CREATE DATABASE 數據庫名稱DEFAULT CHARSET utf8 COLLATE utf8_general_ci;

Create a table

create table 表名(字段構成詳細列表信息) default charset=utf8;

Client display

locale

Check whether the locale setting is UTF8. Generally, there is no problem on the server side, only on the client side. Or there may be problems with some clients, which is often caused by problems with the client display settings.

確認命令:locale

mysql settings

Use show variables like '%character%' to confirm. Generally, modifying character_set_database and character_set_server can only modify the settings in the current session.

Use the mysql command to set the character set within the session

確認命令(mysql):show variables like '%character%'

Local settings

It only works in the current session. The setting method is:

set character_set_database=utf8

Global Settings

The setting method for multiple sessions to work is:

set global character_set_database=utf8

Of course, the session mode will become invalid after the database is restarted, and needs to be persisted. The same setting can be set in the MySQL configuration file my.cnf.

In this way, when creating a database or creating a database table, utf8 is specified by default.

If it is a pure display problem, it may often be due to the character_set_results setting.

Use show variables like '%character%' to confirm. If the setting is incorrect, you can use the following method to solve it

set character_set_results='utf8';

Database data export

Generally, the following command is used to export the database using mysqldump

mysqldump -u用戶名-p用戶密碼數據庫名稱>mysqlbackup.sql

If garbled characters appear, you can add the following Option

mysqldump -u用戶名-p用戶密碼--default-character-set=utf8 數據庫名稱>mysqlbackup.sql

If it contains blob type, you need to use the hex-blob option to export mysqldump

mysqldump -u用戶名-p用戶密碼--hex-blob 數據庫名稱>mysqlbackup.sql

Database data import

If there are problems when importing, you can also consider adding character-level settings

mysql -u用戶名-p用戶密碼--default-character-set=utf8 數據庫名稱<mysqlbackup.sql

You can also use the following method and execute the following command before importing

set names utf8;

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. If you want to learn more about this, please check out the following links

You may also be interested in:
  • Solution to the garbled code problem in MySQL 5.x
  • Solution to the Chinese garbled code problem in the decompressed version of MYSQL
  • How to solve the problem of Chinese garbled characters when using MySQL to obtain database data
  • Example solution for writing garbled Chinese characters into MySQL in PHP
  • MySQL character set garbled characters and solutions
  • Solution to MySQL garbled code problem under Linux
  • How to solve the problem of Chinese garbled characters when inserting table data into MySQL
  • Solve the problem of Chinese garbled characters when inserting data into MySQL by Tomcat under Linux
  • Summary of handling JDBC connection mysql garbled code exception problem
  • How to solve the DOS window garbled problem in MySQL

<<:  Detailed explanation of the new features of ES9: Async iteration

>>:  Detailed explanation of common commands for network configuration of containers in Docker

Recommend

Using react+redux to implement counter function and problems encountered

Redux is a simple state manager. We will not trac...

Use of MySQL trigger

Table of contents 1. Trigger Introduction 1. What...

Example of downloading files with vue+django

Table of contents 1. Overview 2. Django Project 3...

vite2.x implements on-demand loading of ant-design-vue@next components

1. Use version vite:2.0 ant-design-vue: 2.0.0-rc....

Tutorial on installing mysql under centos7

Recently, I plan to deploy a cloud disk on my hom...

Solve the problem of VScode configuration remote debugging Linux program

Let's take a look at the problem of VScode re...

Several ways to run Python programs in the Linux background

1. The first method is to use the unhup command d...

11 Examples of Advanced Usage of Input Elements in Web Forms

1. Cancel the dotted box when the button is press...

How to solve the problem of ERROR 2003 (HY000) when starting mysql

1. Problem Description When starting MYSQL, a pro...

Use vue to implement handwritten signature function

Personal implementation screenshots: Install: npm...

Native js to achieve accordion effect

In actual web page development, accordions also a...

Detailed explanation of data sharing between Vue components

Table of contents 1. In project development, the ...

Solution to forget password when installing MySQL on Linux/Mac

Preface This article mainly introduces the releva...

Detailed tutorial on installing Docker on CentOS 8

1. Previous versions yum remove docker docker-cli...