Steps to change mysql character set to UTF8 under Linux system

Steps to change mysql character set to UTF8 under Linux system

Change MySQL character encoding to UTF8 on Linux

I installed the MySQL service on Linux. During the later use, I suddenly found that some fields could not be in Chinese when inserting records. The error message was as follows:

mysql> insert into syudent values(1,'陶盼',21);
ERROR 1366 (HY000): Incorrect string value: '\xE9\x99\xB6\xE7\x9B\xBC' for column 'name' at row 1

insert image description here

After my persistent search for information, I finally solved the problem. Here I share with you my solution:

1. Check the MySQL status in the database:

# Check mysql statusmysql> status;
mysql> quit;

insert image description here

2. Modify the configuration file:

#Exit the database systemctl stop mysqld.service
#Edit the my.cnf configuration file vim /etc/my.cnf
[client] //If there is no [client] section, add it yourself default-character-set=utf8
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci

After editing the configuration file, restart the MySQL service;

systemctl start mysqld 

After the restart is successful, re-query the status of MySQL, and it will be displayed as follows:

mysql -u root -p 'your password'
mysql> status;

insert image description here

At this time, you can enter the database normally and insert the text segment data.

3. If it still fails, it may be due to the configuration when creating MySQL or the fact that MySQL service was not stopped in the previous step to edit the my.cnf configuration file.

Here we also need to set the character set for the table and field to utf8;

#Check the table creation processmysql> use ky15;
mysql> show create table syudent;

insert image description here

#Set the character set of the table to utf8
mysql> alter table table name character set utf8;
#Change the character set of the field where you want to insert Chinese characters to utf8
mysql> alter table table namechange name name char(20) character set utf8;
#View the character set of the table and the character set of the fieldmysql> show create table syudent;

insert image description here

#Test, insert Chinese mysql> insert into syudent values(1,'陶盼',21);
mysql> insert into syudent values(2,'王旭',21);
mysql> select * from syudent;

insert image description here

This is the end of this article about the steps to change the MySQL character set to UTF8 under Linux. For more relevant content about changing the MySQL character set in Linux, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Example of utf8mb4 collation in MySQL
  • MySQL encoding utf8 and utf8mb4 utf8mb4_unicode_ci and utf8mb4_general_ci
  • How to change the encoding of MySQL database to utf8mb4
  • mysql charset=utf8 do you really understand what it means
  • How to change MySQL character set utf8 to utf8mb4
  • mysql garbled characters latin1 characters converted to UTF8 details

<<:  What is HTML?

>>:  How to design and create adaptive web pages

Recommend

How to understand the difference between ref toRef and toRefs in Vue3

Table of contents 1. Basics 1.ref 2. toRef 3. toR...

Detailed steps to install Docker mongoDB 4.2.1 and collect springboot logs

1: Install mongodb in docker Step 1: Install mong...

What is Nginx load balancing and how to configure it

What is Load Balancing Load balancing is mainly a...

Example analysis of mysql shared lock and exclusive lock usage

This article uses examples to illustrate the usag...

Programs to query port usage and clear port usage in Windows operating system

In Windows operating system, the program to query...

Solution for Docker container not recognizing fonts such as Songti

Problem background: When using docker to deploy t...

Detailed process of installing and configuring MySQL and Navicat prenium

Prerequisite: Mac, zsh installed, mysql downloade...

MySQL independent index and joint index selection

There is often a lack of understanding of multi-c...

JS implements jQuery's append function

Table of contents Show Me The Code Test the effec...

Native JS to achieve draggable login box

This article shares a draggable login box impleme...

Implementing access control and connection restriction based on Nginx

Preface Nginx 's built-in module supports lim...

Summary of common Nginx techniques and examples

1. Priority of multiple servers For example, if e...

Detailed explanation of the process of building and running Docker containers

Simply pull the image, create a container and run...