How to modify the default encoding of mysql in Linux

How to modify the default encoding of mysql in Linux

During the development process, if garbled characters appear in the database data after restoring the MySQL database, you can solve this problem by changing the default encoding of the database.

The following example shows how to modify the default encoding of MySQL to UTF-8:

1. Check the mysql information first

# View the database installation location whereis mysql 
# Log in to the database mysql -u root -p Enter the password as prompted # View the mysql status mysql>status 

2. Modify the my.cnf file

The directory is /etc/my.cnf

If there is no my.cnf file in the system, you need to create this file. For specific steps, please see 3. If it exists, skip step 3 and go directly to step 4.

3. Find files ending with .cnf

Command find / -iname *.cnf -print

The result is as shown below:

Copy any file in the /usr/share/mysql directory in the search results above to the /etc/ directory

Command: cp /usr/share/mysql/my-large.cnf /etc/my.cnf

4. Edit the my.cnf file

Command: vi /etc/my,cnf

Add below [client]

default-character-set=utf8

Add below [mysqld]

character_set_server=utf8

The above modification method may not take effect due to different MySQL versions.

In actual use, basically most of the MySQL will take effect. I have only encountered one unsuccessful modification. The specific reason has not yet been found out due to the limitations of the conditions at the time.

If anyone knows the reason, please let me know, I would be very grateful.

In addition, if the above modification method does not work, please change the line under [mysqld] to the following line:

default-character-set=utf8

5. Restart mysql

service mysql restart 

At this point, after logging in to MySQL, you can view the modification results through show variables like "character%"; If the result is consistent with the modified target encoding, then congratulations, the modification is successful!

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:
  • The meaning and calculation method of QPS and TPS of MySQL database
  • How to backup and restore the mysql database if it is too large
  • PHP backend source code example for backing up MySQL database
  • How to use shell scripts to automatically back up multiple MySQL databases every day
  • Introduction to using mysqli's prepare to operate the database in PHP5
  • How to enable the slow query log function in MySQL
  • Two ways to operate MySQL database in Python [pymysql and pandas]
  • Summary of tips for setting the maximum number of connections in MySQL
  • A magical MySQL deadlock troubleshooting record
  • Detailed explanation of MySQL Limit performance optimization and paging data performance optimization

<<:  MySQL Order By Multi-Field Sorting Rules Code Example

>>:  JavaScript+HTML to implement student information management system

Recommend

Implementing access control and connection restriction based on Nginx

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

Some tips on speeding up the development of WeChat mini-programs

1. Create a page using app.json According to our ...

Implementation of postcss-pxtorem mobile adaptation

Execute the command to install the plugin postcss...

Mysql sorting to get ranking example code

The code looks like this: SELECT @i:=@i+1 rowNum,...

Detailed explanation based on event bubbling, event capture and event delegation

Event bubbling, event capturing, and event delega...

MySQL 5.7 JSON type usage details

JSON is a lightweight data exchange format that u...

Javascript Virtual DOM Detailed Explanation

Table of contents What is virtual dom? Why do we ...

Teach you how to build Tencent Cloud Server (graphic tutorial)

This article was originally written by blogger We...

Use image to submit the form instead of using button to submit the form

Copy code The code is as follows: <form method...

How to modify the forgotten password when installing MySQL on Mac

1. Install MySQL database on mac 1. Download MySQ...

The meaning of status code in HTTP protocol

A status code that indicates a provisional respon...

Vue parent component calls child component function implementation

Vue parent component calls the function of the ch...

How to create a MySQL master-slave database using Docker on MacOS

1. Pull the MySQL image Get the latest MySQL imag...

Vue implements adding watermark effect to the page

Recently, when I was working on a project, I was ...

MySQL view introduction and basic operation tutorial

Preface View is a very useful database object in ...