Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of Chinese garbled code in MySQL

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to solve the problem of Chinese garbled code in MySQL. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1. Chinese garbled 1.1Chinese garbled create table user (name varchar (11)); # create user table insert into table user ("carl"); # add data select * from user

Insert into user value ("")

Unable to insert Chinese characters:

1.2.View the table character code mysql > show create table user\ Gbomber * 1. Row * * Table: userCreate Table: CREATE TABLE `user` (`name` varchar (11) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=latin11 row in set (0.00 sec)

We can see that the default character set of the table is latin1.

So when we create the table, we need to specify the character set of the table:

Create table user (name varchar (11)) default charset=utf8

In this way, the table can be accessed and inserted and accessed in Linux.

1.3, database and operating system coding

Although Chinese can be displayed normally on the server side, garbled codes may be displayed on the client side. Because our server is UTF8.

And there are also problems with the coding of the database.

Here we can see that the character sets of character_sert_database and character_set_server are both latin1. Then in mysql database, the character set of server,database,table defaults to latin1. Let's take a look at how to solve mysql garbled.

2. Mysql sets the range of variables 2.1and session.

View the database code:

Show variables like'% char%'

Modify character encoding:

Set character_set_server=utf8;set character_set_database=utf8;show variables like'% char%'

We can see that the character set has been modified to utf8 in Chengdu. But there is a problem here, that is, if we reopen a command window and look at the data encoding, the following screen will appear:

2.2, global range

The scope of the mysql setting variable defaults to the session range. If you set the character set of multiple sessions, you need to set the global range: Set [global | session] variables...

Set global character_set_database=utf8;set global character_set_server=utf8;show variables like'% char%'

When we look at the mysql character set across sessions, we will see that it is all utf8. If you think everything is all right, you are dead wrong.

2.3. Set the global scope of data

When our database is restarted, you notice that the value that sets the global range has changed to latin1 again.

Service mysqld restartmysql-uroot-pyourpasswordshow variables like'% char%'

Don't be afraid, here's how to do it:

Modify the mysql configuration file / etc/my.cnf.

[mysqld] character-set-server=utf8 [client] default-character-set=utf8 [mysql] default-character-set=utf8

Please note the location of the configuration of these parameters, otherwise the mysql service may not be started:

OK . If you restart the mysql service, you will also find that its character set is utf8.

Drop database test;create database test;use test;create table user (name varchar (11)); show create table user\ G

And we don't need to specify the character encoding when we create the table, it defaults to utf8

Thank you for reading! This is the end of the article on "how to solve the problem of Chinese garbled code in MySQL". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report