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 Chinese garbled codes in mysql database

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

This article mainly introduces the mysql database Chinese garbled code how to do, the article is very detailed, has a certain reference value, interested friends must read it!

Mysql Chinese garbled solutions: 1, find the my.cnf file in the linux environment and modify the code; 2, modify the my.ini file under the Windows; 3, force the character set coding in the process of use.

one。 Brief introduction

MySQL defaults to the latin1 (actually ISO-8859-1) character set. This obviously doesn't meet our needs, so we adjusted it to the UTF8 character set to be compatible with most character sets.

1. Let's first execute two SQL to detect the current MySQL default character set encoding:

Sql command:

Mysql > show variables like "colla%"

Sql command:

Mysql > show variables like "char%"

two。 Modify the character set to UTF-8

The MySQL configuration file my.cnf file needs to be modified, and the Windows environment is a my.ini file.

Usually under the linux environment, the my.cnf file is located in the / etc/mysql/my.cnf (/ etc/my.cnf) path, but due to the installation version or the system environment, we may not be able to find the file in this directory

Modify method under 1.Linux

Modify the configuration file of mysql, because the newly installed mysql may not have my.cnf under etc

It is necessary to create one yourself. Find a my-medium.cnf file and copy it to the etc folder. The command is: cp / usr/share/doc/MySQL-server-5.5.24/my-medium.cnf / etc/my.cnf

Shell Code:

# echo modify my.cnf file

# sudo vi / etc/my.cnf

Add the code under [client]:

Default-character-set=UTF8

Add the code under [mysqld]:

Character_set_server=utf8

Add the code under [mysql]:

Default-character-set=utf8

Some of the images are as follows:

Restart the mysql database after successful modification.

Shell command:

Service mysql restart

Sometimes the database service mysql start starts successfully, but you can only use the mysql command in root. It may be because the firewall is not turned off. Use the following command to solve the problem:

Service iptables stop

Then query the character set:

Show variables like'% character%'

Show variables like "colla%"

The success images are as follows:

Modify method under 2.Windows

Change my-default.ini to my.ini file under the MySQL installation directory

[client] node

Default-character-set=utf8 (added)

[mysql] node

Default-character-set=utf8 (modified)

[mysqld] node

Character_set_server=utf8 (modified)

Use after modification

Net stop mysql

Net start mysql

Restart the mysql service

Then query the character set:

Show variables like'% character%'

Show variables like "colla%"

3. Force to set character set encoding during use

In order to ensure the complete unity of the character set, it is necessary to forcibly set the unified character set when building tables and databases.

In addition, to make sure that it is foolproof to connect to MySQL through JDBC, you need to add the following parameters to the connection string:

Jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=UTF-8

The above is all the contents of how to garbled in mysql database. Thank you for your reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

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