Home Database Mysql Tutorial 查看修改mysql编码方式让它支持中文(gbk或者utf8)

查看修改mysql编码方式让它支持中文(gbk或者utf8)

Jun 07, 2016 pm 05:55 PM
coding

MySQL的默认编码是Latin1,不支持中文,要支持中文需要把数据库的默认编码修改为gbk或者utf8,真的是很麻烦啊,不过本文提供了详细的修改教程,感兴趣的你可不要走开啊,希望本文对你有所帮助

MySQL的默认编码是Latin1,不支持中文,要支持中文需要把数据库的默认编码修改为gbk或者utf8。

1、需要以root用户身份登陆才可以查看数据库编码方式(以root用户身份登陆的命令为:>mysql -u root –p,之后两次输入root用户的密码),查看数据库的编码方式命令为:
>show variables like 'character%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
从以上信息可知数据库的编码为latin1,需要修改为gbk或者是utf8;
其中,character_set_client为客户端编码方式;character_set_connection为建立连接使用的编码;character_set_database数据库的编码;
character_set_results结果集的编码;
character_set_server数据库服务器的编码;
只要保证以上四个采用的编码方式一样,就不会出现乱码问题。
另一个查看数据库编码的命令:
>show variables like ‘collation%';

2、linux系统下,修改MySQL数据库默认编码的步骤为:
ü 停止MySQL的运行
/etc/init.d/mysql start (stop) 为启动和停止服务器
ü MySQL主配置文件为my.cnf,一般目录为/etc/mysql
var/lib/mysql/ 放置的是数据库表文件夹,这里的mysql相当于windows下mysql的date文件夹
ü 当我们需要修改MySQL数据库的默认编码时,需要编辑my.cnf文件进行编码修改,在linux下修改mysql的配置文件my.cnf,文件位置默认/etc/my.cnf文件

找到客户端配置[client] 在下面添加
default-character-set=utf8 默认字符集为utf8
在找到[mysqld] 添加
default-character-set=utf8 默认字符集为utf8
init_connect='SET NAMES utf8' (设定连接mysql数据库时使用utf8编码,以让mysql数据库为utf8运行)

修改好后,重新启动mysql 即可,重新查询数据库编码可发现编码方式的改变:
>show variables like 'character%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

此方法用于标准mysql版本同样有效,对于/etc/my.cnf文件,需要从mysql/support-files的文件夹cp my-large.cnf一份到/etc/my.cnf

3、windows系统下可以删除MySQL数据库,并重新安装,在安装过程中可以直接用Mysql Server Instance Config Wizard 进行设置

4、当MySQL数据库服务器已经有数据不适合删除重装时,可以个别指定数据库的编码方式。MySQL指定编码的方式是非常灵活并多样化的,可以指定表级别的编码,行级别编码,甚至可以指定字段级别的编码。
以下示例给出创建数据库时指定编码的两种方式:
1)CREATE DATABASE ms_db CHARACTER SET utf8 COLLATE utf8_general_ci;
2)create database if not exists netctoss default character set utf8;
5、如果你采用的是外部接入的方式,可以在连接中确定请求的编码格式如:jdbc:mysql://localhost:3306 /mysql?useUnicode=true&characterEncoding=utf-8(注意:不要出现任何空格,否则出错)
6、执行脚本:指定编码格式set names gbk(注意,不是UTF-8)可以修改
执行前:
执行后:
从执行命令前后可知,set names gbk只可以修改character_set_client、character_set_connection、 character_set_results的编码方式,并且这种修改是窗口级别的,只针对本窗口有效,打开另外一个窗口修改无效。也可发现数据库底层的编码方式没有改变,插入数据后还是以utf8编码方式保持。
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 common classification feature encoding techniques 11 common classification feature encoding techniques Apr 12, 2023 pm 12:16 PM

Machine learning algorithms only accept numerical input, so if we encounter categorical features, we will encode the categorical features. This article summarizes 11 common categorical variable encoding methods. 1. ONE HOT ENCODING The most popular and commonly used encoding method is One Hot Enoding. A single variable with n observations and d distinct values ​​is converted into d binary variables with n observations, each binary variable is identified by a bit (0, 1). For example: the simplest implementation after coding is to use pandas' get_dummiesnew_df=pd.get_dummies(columns=[‘Sex’], data=df)2,

How many bytes do utf8 encoded Chinese characters occupy? How many bytes do utf8 encoded Chinese characters occupy? Feb 21, 2023 am 11:40 AM

UTF8 encoded Chinese characters occupy 3 bytes. In UTF-8 encoding, one Chinese character is equal to three bytes, and one Chinese punctuation mark occupies three bytes; while in Unicode encoding, one Chinese character (including traditional Chinese) is equal to two bytes. UTF-8 uses 1~4 bytes to encode each character. One US-ASCIl character only needs 1 byte to encode. Latin, Greek, Cyrillic, Armenian, and Hebrew with diacritical marks. , Arabic, Syriac and other letters require 2-byte encoding.

Knowledge graph: the ideal partner for large models Knowledge graph: the ideal partner for large models Jan 29, 2024 am 09:21 AM

Large language models (LLMs) have the ability to generate smooth and coherent text, bringing new prospects to areas such as artificial intelligence conversation and creative writing. However, LLM also has some key limitations. First, their knowledge is limited to patterns recognized from training data, lacking a true understanding of the world. Second, reasoning skills are limited and cannot make logical inferences or fuse facts from multiple data sources. When faced with more complex and open-ended questions, LLM's answers may become absurd or contradictory, known as "illusions." Therefore, although LLM is very useful in some aspects, it still has certain limitations when dealing with complex problems and real-world situations. In order to bridge these gaps, retrieval-augmented generation (RAG) systems have emerged in recent years. The core idea is

Several common encoding methods Several common encoding methods Oct 24, 2023 am 10:09 AM

Common encoding methods include ASCII encoding, Unicode encoding, UTF-8 encoding, UTF-16 encoding, GBK encoding, etc. Detailed introduction: 1. ASCII encoding is the earliest character encoding standard, using 7-bit binary numbers to represent 128 characters, including English letters, numbers, punctuation marks, control characters, etc.; 2. Unicode encoding is a method used to represent all characters in the world The standard encoding method of characters, which assigns a unique digital code point to each character; 3. UTF-8 encoding, etc.

How to solve the problem of encoding of php database query results How to solve the problem of encoding of php database query results Mar 21, 2023 am 11:49 AM

PHP is a popular web programming language that can be used to write dynamic web pages and applications. In practical applications, PHP often needs to interact with the database to query and process data. However, when using PHP to get results from a database, you may encounter encoding problems, which often result in garbled characters. So, how to solve the problem of encoding of PHP database query results?

PHP coding tips: How to generate a QR code with anti-counterfeiting verification function? PHP coding tips: How to generate a QR code with anti-counterfeiting verification function? Aug 17, 2023 pm 02:42 PM

PHP coding tips: How to generate a QR code with anti-counterfeiting verification function? With the development of e-commerce and the Internet, QR codes are increasingly used in various industries. In the process of using QR codes, in order to ensure product safety and prevent counterfeiting, it is very important to add anti-counterfeiting verification functions to the QR codes. This article will introduce how to use PHP to generate a QR code with anti-counterfeiting verification function, and attach corresponding code examples. Before starting, we need to prepare the following necessary tools and libraries: PHPQRCode: PHP

Learn how to improve coding performance based on GenAI in one article Learn how to improve coding performance based on GenAI in one article Apr 01, 2024 pm 06:49 PM

Hellofolks, my name is Luga, and today we will talk about technologies related to the artificial intelligence (AI) ecological field - GenAI. Facing the challenges of rapid technological innovation and differentiated business scenarios, traditional coding methods have begun to become acclimated and cannot fully cope with the growing demands. At the same time, emerging general-purpose GenAI (artificial intelligence technology) has great potential to meet this demand. As a representative of artificial intelligence technology, GenAI has begun to be widely used in all walks of life with its strong potential and capabilities. It can automatically learn and adapt to coding needs in different scenarios, greatly improving coding efficiency and quality. Through deep learning and model optimization, GenAI is able to accurately understand different

What are the hdb3 encoding rules? What are the hdb3 encoding rules? Aug 29, 2023 pm 01:38 PM

The coding rules are: 1. If the previous code is 0 and the current data bit is 0, the code is 0; 2. If the previous code is 0 and the current data bit is 1, the code is bipolar pulse (+A or - A), and the counter is increased by 1; 3. If the previous code is 1 and the current data bit is 1, the code is 0, and the counter is increased by 1; 4. If the previous code is 1, the current data bit is 0, The encoding method is determined based on the parity of the counter. If it is an even number, the encoding is (+B or -B). If it is an odd number, the encoding is zero level and the counter is cleared and so on.

See all articles