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

10 tips for designing useful, easy-to-use web applications

Here are 10 tips on how to design better-usable w...

A summary of the reasons why Mysql does not use date field index

Table of contents background explore Summarize ba...

React native ScrollView pull down refresh effect

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

Let's talk about the LIMIT statement in MySQL in detail

Table of contents question Server layer and stora...

How to start a Vue.js project

Table of contents 1. Node.js and Vue 2. Run the f...

Front-end advanced teaching you to use javascript storage function

Table of contents Preface Background Implementati...

How to implement horizontal bar chart with percentage in echarts

Table of contents Example Code Rendering Code Ana...

7 skills that web designers must have

Web design is both a science and an art. Web desi...

How to configure two or more sites using Apache Web server

How to host two or more sites on the popular and ...

Detailed explanation of MySQL alter ignore syntax

When I was at work today, the business side asked...

getdata table table data join mysql method

public function json_product_list($where, $order)...

Three steps to solve the IE address bar ICON display problem

<br />This web page production skills tutori...