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

What about the garbled codes in MySQL?

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article is to share with you about how to deal with garbled codes 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.

Foreword:

MySQL is a very common data-based database in our project. But because we need to save Chinese characters in the database, we often encounter database garbled. Here is how to thoroughly solve the problem of Chinese garbled in the database.

1. Chinese garbled code

1.1. Chinese garbled code

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 encoding

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.1range of 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.

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

Drop database test;create database test;use test;create table user (name varchar (11)); show create table user\ G; Thank you for reading! This is the end of this article on "what to do about MySQL Chinese garbled code". 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

Database

Wechat

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

12
Report