Windows使用MySQL数据库管理系统中文乱码问题

时间:2022-05-13 22:05:02

声明:本文关于MySQL中文乱码问题的解决方案均基于Windows 10操作系统,如果是Linux系统会有较多不适用之处,请谨慎参考。 

一、MySQL中文乱码情况

1. sqlDevelper远程登陆MySQL客户端中文乱码

Windows使用MySQL数据库管理系统中文乱码问题

sqlDeveloper操作MySQL中文乱码  

2. command-line登陆本地MySQL客户端中文乱码

Windows使用MySQL数据库管理系统中文乱码问题

控制台操作MySQL中文乱码

二、MySQL中文乱码产生原因

  Windwos中文系统默认的字符编码集是gbk(扩展国标码,包括简体中文、繁体中文、朝鲜语、日本语等东亚语言),Linux系统默认的字符编码集为utf-8。然而当我们在安装MySQL的时候往往没注意,直接使用了MySQL的默认字符编码集latin1(仅支持英语即其它西欧语言),这就是导致我们在使用MySQL数据库查看记录时出现中文乱码的根本原因所在。

Windows使用MySQL数据库管理系统中文乱码问题

默认 MySQL Server Instance Configuration    

三、MySQL中文乱码可能解决方案

  1. 重新安装MySQL

  说说产生这种想法的原因:通过google搜索,尝试了大致10种左右互联网上提到的解决MySQL中文乱码方法,但是没有一种是能够完美解决我遇到问题的合适方案。经过不知道多少次修改MySQL配置文件my.ini文件的字符集,总是出现莫名奇妙的问题,用一句话说就是"按下葫芦起了瓢"。问题详细描述见 修改MySQL配置文件

  我们重新配置数据库,当然这种重新配置数据库实例是不会删除原来已存在的数据库数据,只是修改一下原有的数据库配置信息。

Windows使用MySQL数据库管理系统中文乱码问题

自定义 MySQL Server Instance Configuration 图1

Windows使用MySQL数据库管理系统中文乱码问题

自定义MySQL Server Instance Configuration 图2

  我们选择reconfigurate如上MySQL Server Instance Configuration图2所示,可以选择Best Support For Multilingualism(对多语言的最佳支持)或者Manual Selected Default Character Set/Collation(手动选择默认字符集/排序方式)中选择gbk或utf-8。本次选择的字符集是:utf-8

  然后经过与原来配置MySQL相同的步骤,然后启动MySQL服务(即运行mysqld)。接下去运行MySQL客户端,查看相应数据库情况,观察中文乱码问题是否解决。

Windows使用MySQL数据库管理系统中文乱码问题

MySQL客户端中文乱码

  不幸的是我们获得的仍然乱码。真的是我们的方案有问题?分析下本次尝试失败的原因: 虽然重新设置了默认的字符集为utf-8,即修改了Index.xml预编译字符集配置文件,也修改了my.ini启动配置文件(包括default-charater-set=utf-8和default-set-server=utf-8),但是需要读者明白的是原来数据库的字符集已经设定,并没有被改变。

  上述理论如何才能判断是正确的呢?那我就尝试一下重新建立一个MySQL数据库,数据库命名为teacher,表命名为therinfo。

Windows使用MySQL数据库管理系统中文乱码问题

  呵呵,又出错了。正当黔驴技穷的时候,又想到了命令行并不支持utf-8格式(中文版windows的命令行默认支持GBK或ascii)。那么明显,如果我们从命令行输入中文字符的时候,在MySQL服务器看来就是驴唇不对马嘴,存的内容天知道是什么玩意。为了验证中文windows命令行真的不支持utf-8这个理论,我又尝试了JDBC操作MySQL数据库,然后再命令行输出。输入情况如下所示。

Windows使用MySQL数据库管理系统中文乱码问题JDBC操作数据库在中文版windows命令行输出数据库内容

  可以输出,只不过输出的中文是乱码。显然这是命令行数据库客户端与数据库服务器端所支持编码不同导致。那么可以说,用JDBC操作数据库后,在MySQL数据中的内容确实是utf-8数据格式的,但是在命令行输出看成了GBK编码,导致仍然是乱码。

  (此部分建议先看MySQL原始配置文件)这样看来,似乎只要把客户端编码格式改为GBK就能够顺利解决乱码问题。为了避免修改配置文件出现的错误,我特意查看了Index.xml预编译字符集是否支持GBK 。查询结果如下所示。存在gbk编码,说明重新启动MySQL服务过程中是绝对不会出现1067错误的。

Windows使用MySQL数据库管理系统中文乱码问题

  修改的情况为[mysql] default-character-set=gbk,然后保持[mysqld] default-server-set=utf-8不变或改成gbk编码。显示的结果为正确,问题顺利解决。

Windows使用MySQL数据库管理系统中文乱码问题

命令行客户端中文乱码问题解决结果

  从上述步骤中,我们小结一下情况:

  a) [mysql] default-character-set 是客户端默认字符集。如果采用命令行作为客户端,此字符集必须和命令行默认字符集能够匹配,否则出现中文乱码。

  b) [mysql]和[mysqld]数据字符集可以不同,因为MySQL服务器配置文件Index.xml如果有预编译编码,服务器是能够识别客户端数据编码并转换成服务器端编码储存在相应的表中。关键是要在Index.xml配置文件中存在此预编译字符集

  基于这套经过实践验证的理论,开始尝试解决sqlDevelopor远程登陆MySQL客户端出现中文乱码问题。关于sqlDevelopor远程登陆MySQL数据库步骤详情参考文章《sqlDeveloper远程登陆MySQL数据管理系统》。必须明确一点,sqlDeveloper首先连接MySQL客户端,然后通过客户端再连接MySQL服务器。因此,必须保证sqlDeveloper和MySQL客户端字符编码集相同。

  首先查看sqldevelopor的默认字符编码集:[帮助]-->[关于]-->[属性]-->[file.encoding],查到sqlDevelopor默认显示字符编码集为GBK。情况具体如下图所示。

Windows使用MySQL数据库管理系统中文乱码问题

Oracle SQL Developer 默认字符编码集

  Oracle SQL Developer客户端和其要远程登陆的Command line客户端编码相同,均为gbk,因此数据通讯以及显示没有问题。查询结果如下图所示。

Windows使用MySQL数据库管理系统中文乱码问题

Oracle SQL Developer客户端中文乱码问题解决结果

  2. 修改MySQL原始配置文件

# MySQL Server Instance Configuration File   
# 翻译:MySQL服务器实例配置文件
# ----------------------------------------------------------------------
# Generated by the MySQL Server Instance Configuration Wizard
# 翻译:由MySQL服务器实例配置向导自动生成
#
# Installation Instructions
# 翻译:安装指南
# ----------------------------------------------------------------------
#
# On Linux you can copy this file to /etc/my.cnf to set global options,
# mysql-data-dir/my.cnf to set server-specific options
# (@localstatedir@ for this installation) or to
# ~/.my.cnf to set user-specific options.
#
# On Windows you should keep this file in the installation directory
# of your server (e.g. C:\Program Files\MySQL\MySQL Server X.Y). To
# make sure the server reads the config file use the startup option
# "--defaults-file".
# 翻译:在Windows中必须将该文件(my.ini)保存在MySQL服务器的安装目录中。
# 确保服务器在使用启动项"--defaults-file"时入读该配置文件。
#
# To run the server from the command line, execute this in a
# command line shell, e.g.
# 翻译:为了能命令行运行服务器,需在命令行shell中执行如下命令。
# mysqld --defaults-file="C:\Program Files\MySQL\MySQL Server X.Y\my.ini"
# 注意:不同的安装路径,参数字符串内容一般不同。主要是找到my.ini绝对路径即可。
#
# To install the server as a Windows service manually, execute this in a
# command line shell, e.g.
# 翻译:为了能够将MySQL服务器手动安装成Windows服务,需在命令行shell中执行以下命令。
# mysqld --install MySQLXY --defaults-file="C:\Program Files\MySQL\MySQL Server X.Y\my.ini"
#
# And then execute this in a command line shell to start the server, e.g.
# 翻译:然后在命令行shell中执行如下命令启动MySQL服务器。
# net start MySQLXY
#
#
# Guildlines for editing this file
# 翻译:编辑本配置文件的指导路线
# ----------------------------------------------------------------------
#
# In this file, you can use all long options that the program supports.
# If you want to know the options a program supports, start the program
# with the "--help" option.
#
# More detailed information about the individual options can also be
# found in the manual.
#
#
# CLIENT SECTION
# 翻译:客户端部分
# ----------------------------------------------------------------------
#
# The following options will be read by MySQL client applications.
# Note that only client applications shipped by MySQL are guaranteed
# to read this section. If you want your own MySQL client program to
# honor these values, you need to specify it as an option during the
# MySQL client library initialization.
#
#
[client] port=3306 [mysql] default-character-set=latin1
# 注意:此处需修改为gbk或utf-8 # SERVER SECTION
# 翻译:服务器端部分
# ----------------------------------------------------------------------
#
# The following options will be read by the MySQL Server. Make sure that
# you have installed the server correctly (see above) so it reads this
# file.
# 翻译:下列选项将会被MySQL服务器读取。
# 为了MySQL服务器能够读取本文件,确保已经正确安装MySQL服务器。
[mysqld] # The TCP/IP Port the MySQL Server will listen on
# 翻译:MySQL服务器将会监听的TCP/IP端口
port=3306 #Path to installation directory. All paths are usually resolved relative to this.
# 翻译:安装目录路径。所有的路径解析通常相对的就是安装目录路径。
basedir="C:/Program Files/MySQL/MySQL Server 5.5/"
# 注意:basedir基础路径,即所有相对路径的基础路径。不同安装情况,具体有略微区别。
#Path to the database root
# 翻译:数据库root用户路径
datadir="C:/ProgramData/MySQL/MySQL Server 5.5/Data/" # The default character set that will be used when a new schema or table is
# created and no character set is defined
# 翻译:当一个新的schema或者tableb被创建,并且没有字符集被定义时将使用如下默认字符集定义。
character-set-server=latin1
# 注意:此处需要改动为gbk或utf-8。
# The default storage engine that will be used when create new tables when
# 翻译:在创建新的表时,如下默认储存引擎将会被使用
default-storage-engine=INNODB
# 注意:INNODB是MySQL的数据库引擎之一,为MySQL AB公司发布binary标准之一,原本由Innobase Oy公司所开发,后被Oracle并购。
# Set the SQL mode to strict
sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION" # The maximum amount of concurrent sessions the MySQL server will
# allow. One of these connections will be reserved for a user with
# SUPER privileges to allow the administrator to login even if the
# connection limit has been reached.
max_connections=100 # Query cache is used to cache SELECT results and later return them
# without actual executing the same query once again. Having the query
# cache enabled may result in significant speed improvements, if your
# have a lot of identical queries and rarely changing tables. See the
# "Qcache_lowmem_prunes" status variable to check if the current value
# is high enough for your load.
# Note: In case your tables change very often or if your queries are
# textually different every time, the query cache may result in a
# slowdown instead of a performance improvement.
query_cache_size=0 # The number of open tables for all threads. Increasing this value
# increases the number of file descriptors that mysqld requires.
# Therefore you have to make sure to set the amount of open files
# allowed to at least 4096 in the variable "open-files-limit" in
# section [mysqld_safe]
table_cache=256 # Maximum size for internal (in-memory) temporary tables. If a table
# grows larger than this value, it is automatically converted to disk
# based table This limitation is for a single table. There can be many
# of them.
tmp_table_size=18M # How many threads we should keep in a cache for reuse. When a client
# disconnects, the client's threads are put in the cache if there aren't
# more than thread_cache_size threads from before. This greatly reduces
# the amount of thread creations needed if you have a lot of new
# connections. (Normally this doesn't give a notable performance
# improvement if you have a good thread implementation.)
thread_cache_size=8 #*** MyISAM Specific options # The maximum size of the temporary file MySQL is allowed to use while
# recreating the index (during REPAIR, ALTER TABLE or LOAD DATA INFILE.
# If the file-size would be bigger than this, the index will be created
# through the key cache (which is slower).
myisam_max_sort_file_size=100G # If the temporary file used for fast index creation would be bigger
# than using the key cache by the amount specified here, then prefer the
# key cache method. This is mainly used to force long character keys in
# large tables to use the slower key cache method to create the index.
myisam_sort_buffer_size=35M # Size of the Key Buffer, used to cache index blocks for MyISAM tables.
# Do not set it larger than 30% of your available memory, as some memory
# is also required by the OS to cache rows. Even if you're not using
# MyISAM tables, you should still set it to 8-64M as it will also be
# used for internal temporary disk tables.
key_buffer_size=25M # Size of the buffer used for doing full table scans of MyISAM tables.
# Allocated per thread, if a full scan is needed.
read_buffer_size=64K
read_rnd_buffer_size=256K # This buffer is allocated when MySQL needs to rebuild the index in
# REPAIR, OPTIMZE, ALTER table statements as well as in LOAD DATA INFILE
# into an empty table. It is allocated per thread so be careful with
# large settings.
sort_buffer_size=256K #*** INNODB Specific options *** # Use this option if you have a MySQL server with InnoDB support enabled
# but you do not plan to use it. This will save memory and disk space
# and speed up some things.
# skip-innodb # Additional memory pool that is used by InnoDB to store metadata
# information. If InnoDB requires more memory for this purpose it will
# start to allocate it from the OS. As this is fast enough on most
# recent operating systems, you normally do not need to change this
# value. SHOW INNODB STATUS will display the current amount used.
innodb_additional_mem_pool_size=2M # If set to 1, InnoDB will flush (fsync) the transaction logs to the
# disk at each commit, which offers full ACID behavior. If you are
# willing to compromise this safety, and you are running small
# transactions, you may set this to 0 or 2 to reduce disk I/O to the
# logs. Value 0 means that the log is only written to the log file and
# the log file flushed to disk approximately once per second. Value 2
# means the log is written to the log file at each commit, but the log
# file is only flushed to disk approximately once per second.
innodb_flush_log_at_trx_commit=1 # The size of the buffer InnoDB uses for buffering log data. As soon as
# it is full, InnoDB will have to flush it to disk. As it is flushed
# once per second anyway, it does not make sense to have it very large
# (even with long transactions).
innodb_log_buffer_size=1M # InnoDB, unlike MyISAM, uses a buffer pool to cache both indexes and
# row data. The bigger you set this the less disk I/O is needed to
# access data in tables. On a dedicated database server you may set this
# parameter up to 80% of the machine physical memory size. Do not set it
# too large, though, because competition of the physical memory may
# cause paging in the operating system. Note that on 32bit systems you
# might be limited to 2-3.5G of user level memory per process, so do not
# set it too high.
innodb_buffer_pool_size=47M # Size of each log file in a log group. You should set the combined size
# of log files to about 25%-100% of your buffer pool size to avoid
# unneeded buffer pool flush activity on log file overwrite. However,
# note that a larger logfile size will increase the time needed for the
# recovery process.
innodb_log_file_size=24M # Number of threads allowed inside the InnoDB kernel. The optimal value
# depends highly on the application, hardware as well as the OS
# scheduler properties. A too high value may lead to thread thrashing.
innodb_thread_concurrency=10

  当默认字符集/排序方式为latin1时,修改配置文件中遇到的问题集锦

  2.1)MySQL默认字符集,客户端、服务器端均设置为gbk

  [mysql] default-charactter-set=gbk     [mysqld] default-set-server=gbk

  关闭MySQL服务后,再次启动MySQL服务正常。但是输入SQL语句SELECT * FROM XXX后显示的仍然为乱码。

  2.2)MySQL默认字符集,客户端设置为utf-8,服务器设置为gbk

  [mysql] default-charactter-set=utf-8    [mysqld] default-set-server=gbk

  关闭MySQL服务后,再次启动MySQL服务正常。但是在进入命令行客户端时报错,报错信息如下所示:

mysql: Character set 'utf-8' is not a compiled character set and is not specified in the 'C:\Program Files\MySQL\MySQL Server 5.5\share\charsets\Index.xml' file ERROR 2019 (HY000): Can't initialize character set utf-8 (path: C:\Program Files\MySQL\MySQL Server 5.5\share\charsets\)
mysql客户端(即用mysql -u root -p登录的客户端):字符集'utf-8'非已编译过字符集,在Index.xml配置文件中没有被指定。错误的原因是无法初始化'utf-8'。

  2.3)MySQL默认字符集,客户端设置为utf-8,服务器设置为utf-8

  [mysql] default-charactter-set=utf-8    [mysqld] default-set-server=utf-8

  关闭MySQL服务后,再次尝试启动MySQL服务,此时无法启动Windows MySQL服务,出现错误1067。连查询的是否为乱码的机会都没有给。

Windows使用MySQL数据库管理系统中文乱码问题

MySQL服务启动失败

  2.1~2.3问题分析详见 MySQL预编译字符集问题

  注意:本文所有修改配置文件的问题都是基于在修改配置文件重新启动(先关闭,再启动。启动的方式手动或自动)MySQL服务(即启动mysqld程序)。如下图所示:

Windows使用MySQL数据库管理系统中文乱码问题

MySQL服务

  3. 命令行修改MySQL字符集

  如果只需临时使用MySQL,还可以在命令行临时修改配置。这种方式在每次重新启动命令行后都必须重新修改,因此不推荐。google搜索出来的解决方案,很大一部分采用这种治标不治本的方法,当然这也不失为一种解决方案,因此文本也将其列为一种方法。再次强调:本文不推荐此方法

四、修改配置文件问题及解决方案

  1. 修改配置文件my.ini权限问题

  在修改完毕后,保存过程中出现"拒绝访问"问题。由于MySQL默认被安装在C盘,因此首先查看了权限,发现权限是运行写入的。就在这时候灵机一动,大不了把文件移到桌面修改,改完之后再放到相应文件夹下,这就省事多了。

  2. MySQL预编译字符集问题

  如果用默认的安装,即默认的字符集为latin1时,诸如gbk和utf-8在Index.xml是没有被定义的。当时我在没有重新配置MySQL前,就想在网上下载一份utf-8的Index.xml,可惜没找到。当经过我重新配置MySQL后,采用utf-8后,有了utf-8和gbk等字符集。采用utf-8后Index.xml如下所示情况。

 <?xml version='1.0' encoding="utf-8"?>

 <charsets max-id="99">

 <copyright>
Copyright (c) 2003-2005 MySQL AB
Use is subject to license terms This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License. This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
</copyright> <description>
This file lists all of the available character sets.
To make maintaining easier please:
- keep records sorted by collation number.
- change charsets.max-id when adding a new collation.
</description> <charset name="big5">
<family>Traditional Chinese</family>
<description>Big5 Traditional Chinese</description>
<alias>big-5</alias>
<alias>bigfive</alias>
<alias>big-five</alias>
<alias>cn-big5</alias>
<alias>csbig5</alias>
<collation name="big5_chinese_ci" id="1" order="Chinese">
<flag>primary</flag>
<flag>compiled</flag>
</collation>
<collation name="big5_bin" id="84" order="Binary">
<flag>binary</flag>
<flag>compiled</flag>
</collation>
</charset> <charset name="latin2">
<family>Central European</family>
<description>ISO 8859-2 Central European</description>
<alias>csisolatin2</alias>
<alias>iso-8859-2</alias>
<alias>iso-ir-101</alias>
<alias>iso_8859-2</alias>
<alias>iso_8859-2:1987</alias>
<alias>l2</alias>
<collation name="latin2_czech_cs" id="2" order="Czech" flag="compiled"/>
<collation name="latin2_general_ci" id="9" flag="primary">
<order>Hungarian</order>
<order>Polish</order>
<order>Romanian</order>
<order>Croatian</order>
<order>Slovak</order>
<order>Slovenian</order>
<order>Sorbian</order>
</collation>
<collation name="latin2_hungarian_ci" id="21" order="Hungarian"/>
<collation name="latin2_croatian_ci" id="27" order="Croatian"/>
<collation name="latin2_bin" id="77" order="Binary" flag="binary"/>
</charset> <charset name="dec8">
<family>Western</family>
<description>DEC West European</description>
<collation name="dec8_bin" id="69" order="Binary" flag="binary"/>
<collation name="dec8_swedish_ci" id="3" flag="primary">
<order>Dutch</order>
<order>English</order>
<order>French</order>
<order>German Duden</order>
<order>Italian</order>
<order>Latin</order>
<order>Portuguese</order>
<order>Spanish</order>
</collation>
</charset> <charset name="cp850">
<family>Western</family>
<description>DOS West European</description>
<alias>850</alias>
<alias>cspc850multilingual</alias>
<alias>ibm850</alias>
<collation name="cp850_general_ci" id="4" flag="primary">
<order>Dutch</order>
<order>English</order>
<order>French</order>
<order>German Duden</order>
<order>Italian</order>
<order>Latin</order>
<order>Portuguese</order>
<order>Spanish</order>
</collation>
<collation name="cp850_bin" id="80" order="Binary" flag="binary"/>
</charset> <charset name="latin1">
<family>Western</family>
<description>cp1252 West European</description>
<alias>csisolatin1</alias>
<alias>iso-8859-1</alias>
<alias>iso-ir-100</alias>
<alias>iso_8859-1</alias>
<alias>iso_8859-1:1987</alias>
<alias>l1</alias>
<alias>latin1</alias>
<collation name="latin1_german1_ci" id="5" order="German Duden"/>
<collation name="latin1_swedish_ci" id="8" order="Finnish, Swedish">
<flag>primary</flag>
<flag>compiled</flag>
</collation>
<collation name="latin1_danish_ci" id="15" order="Danish"/>
<collation name="latin1_german2_ci" id="31" order="German Phonebook" flag="compiled"/>
<collation name="latin1_spanish_ci" id="94" order="Spanish"/>
<collation name="latin1_bin" id="47" order="Binary">
<flag>binary</flag>
<flag>compiled</flag>
</collation>
<collation name="latin1_general_ci" id="48">
<order>Dutch</order>
<order>English</order>
<order>French</order>
<order>German Duden</order>
<order>Italian</order>
<order>Latin</order>
<order>Portuguese</order>
<order>Spanish</order>
</collation>
<collation name="latin1_general_cs" id="49">
<order>Dutch</order>
<order>English</order>
<order>French</order>
<order>German Duden</order>
<order>Italian</order>
<order>Latin</order>
<order>Portuguese</order>
<order>Spanish</order>
</collation>
</charset> <charset name="hp8">
<family>Western</family>
<description>HP West European</description>
<alias>hproman8</alias>
<collation name="hp8_bin" id="72" order="Binary" flag="binary"/>
<collation name="hp8_english_ci" id="6" flag="primary">
<order>Dutch</order>
<order>English</order>
<order>French</order>
<order>German Duden</order>
<order>Italian</order>
<order>Latin</order>
<order>Portuguese</order>
<order>Spanish</order>
</collation>
</charset> <charset name="koi8r">
<family>Cyrillic</family>
<description>KOI8-R Relcom Russian</description>
<alias>koi8-r</alias>
<alias>cskoi8r</alias>
<collation name="koi8r_general_ci" id="7" order="Russian" flag="primary"/>
<collation name="koi8r_bin" id="74" order="Binary" flag="binary"/>
</charset> <charset name="swe7">
<family>Western</family>
<description>7bit Swedish</description>
<alias>iso-646-se</alias>
<collation name="swe7_swedish_ci" id="10" order="Swedish" flag="primary"/>
<collation name="swe7_bin" id="82" order="Binary" flag="binary"/>
</charset> <charset name="ascii">
<family>Western</family>
<description>US ASCII</description>
<alias>us</alias>
<alias>us-ascii</alias>
<alias>csascii</alias>
<alias>iso-ir-6</alias>
<alias>iso646-us</alias>
<collation name="ascii_general_ci" id="11" order="English" flag="primary"/>
<collation name="ascii_bin" id="65" order="Binary" flag="binary"/>
</charset> <charset name="ujis">
<family>Japanese</family>
<description>EUC-JP Japanese</description>
<alias>euc-jp</alias>
<collation name="ujis_japanese_ci" id="12" order="Japanese">
<flag>primary</flag>
<flag>compiled</flag>
</collation>
<collation name="ujis_bin" id="91" order="Japanese">
<flag>binary</flag>
<flag>compiled</flag>
</collation>
</charset> <charset name="sjis">
<family>Japanese</family>
<description>Shift-JIS Japanese</description>
<alias>s-jis</alias>
<alias>shift-jis</alias>
<alias>x-sjis</alias>
<collation name="sjis_japanese_ci" id="13" order="Japanese">
<flag>primary</flag>
<flag>compiled</flag>
</collation>
<collation name="sjis_bin" id="88" order="Binary">
<flag>binary</flag>
<flag>compiled</flag>
</collation>
</charset> <charset name="cp1251">
<family>Cyrillic</family>
<description>Windows Cyrillic</description>
<alias>windows-1251</alias>
<alias>ms-cyr</alias>
<alias>ms-cyrillic</alias>
<collation name="cp1251_bulgarian_ci" id="14">
<order>Belarusian</order>
<order>Bulgarian</order>
<order>Macedonian</order>
<order>Russian</order>
<order>Serbian</order>
<order>*n</order>
<order>Ukrainian</order>
</collation>
<collation name="cp1251_ukrainian_ci" id="23" order="Ukrainian"/>
<collation name="cp1251_bin" id="50" order="Binary" flag="binary"/>
<collation name="cp1251_general_ci" id="51" flag="primary">
<order>Belarusian</order>
<order>Bulgarian</order>
<order>Macedonian</order>
<order>Russian</order>
<order>Serbian</order>
<order>*n</order>
<order>Ukrainian</order>
</collation>
<collation name="cp1251_general_cs" id="52">
<order>Belarusian</order>
<order>Bulgarian</order>
<order>Macedonian</order>
<order>Russian</order>
<order>Serbian</order>
<order>*n</order>
<order>Ukrainian</order>
</collation>
</charset> <charset name="hebrew">
<family>Hebrew</family>
<description>ISO 8859-8 Hebrew</description>
<alias>csisolatinhebrew</alias>
<alias>iso-8859-8</alias>
<alias>iso-ir-138</alias>
<collation name="hebrew_general_ci" id="16" order="Hebrew" flag="primary"/>
<collation name="hebrew_bin" id="71" order="Binary" flag="binary"/>
</charset> <charset name="tis620">
<family>Thai</family>
<description>TIS620 Thai</description>
<alias>tis-620</alias>
<collation name="tis620_thai_ci" id="18" order="Thai">
<flag>primary</flag>
<flag>compiled</flag>
</collation>
<collation name="tis620_bin" id="89" order="Binary">
<flag>binary</flag>
<flag>compiled</flag>
</collation>
</charset> <charset name="euckr">
<family>Korean</family>
<description>EUC-KR Korean</description>
<alias>euc_kr</alias>
<alias>euc-kr</alias>
<collation name="euckr_korean_ci" id="19" order="Korean">
<flag>primary</flag>
<flag>compiled</flag>
</collation>
<collation name="euckr_bin" id="85">
<flag>binary</flag>
<flag>compiled</flag>
</collation>
</charset> <charset name="latin7">
<family>Baltic</family>
<description>ISO 8859-13 Baltic</description>
<alias>BalticRim</alias>
<alias>iso-8859-13</alias>
<alias>l7</alias>
<collation name="latin7_estonian_cs" id="20">
<order>Estonian</order>
</collation>
<collation name="latin7_general_ci" id="41">
<order>Latvian</order>
<order>Lithuanian</order>
<flag>primary</flag>
</collation>
<collation name="latin7_general_cs" id="42">
<order>Latvian</order>
<order>Lithuanian</order>
</collation>
<collation name="latin7_bin" id="79" order="Binary" flag="binary"/>
</charset> <charset name="koi8u">
<family>Cyrillic</family>
<description>KOI8-U Ukrainian</description>
<alias>koi8-u</alias>
<collation name="koi8u_general_ci" id="22" order="Ukranian" flag="primary"/>
<collation name="koi8u_bin" id="75" order="Binary" flag="binary"/>
</charset> <charset name="gb2312">
<family>Simplified Chinese</family>
<description>GB2312 Simplified Chinese</description>
<alias>chinese</alias>
<alias>iso-ir-58</alias>
<collation name="gb2312_chinese_ci" id="24" order="Chinese">
<flag>primary</flag>
<flag>compiled</flag>
</collation>
<collation name="gb2312_bin" id="86">
<flag>binary</flag>
<flag>compiled</flag>
</collation>
</charset> <charset name="greek">
<family>Greek</family>
<description>ISO 8859-7 Greek</description>
<alias>csisolatingreek</alias>
<alias>ecma-118</alias>
<alias>greek8</alias>
<alias>iso-8859-7</alias>
<alias>iso-ir-126</alias>
<collation name="greek_general_ci" id="25" order="Greek" flag="primary"/>
<collation name="greek_bin" id="70" order="Binary" flag="binary"/>
</charset> <charset name="cp1250">
<family>Central European</family>
<description>Windows Central European</description>
<alias>ms-ce</alias>
<alias>windows-1250</alias>
<collation name="cp1250_general_ci" id="26" flag="primary">
<order>Hungarian</order>
<order>Polish</order>
<order>Romanian</order>
<order>Croatian</order>
<order>Slovak</order>
<order>Slovenian</order>
<order>Sorbian</order>
</collation>
<collation name="cp1250_croatian_ci" id="44">
<order>Croatian</order>
</collation>
<collation name="cp1250_polish_ci" id="99">
<order>Polish</order>
</collation>
<collation name="cp1250_czech_cs" id="34" order="Czech">
<flag>compiled</flag>
</collation>
<collation name="cp1250_bin" id="66" order="Binary" flag="binary"/>
</charset> <charset name="gbk">
<family>East Asian</family>
<description>GBK Simplified Chinese</description>
<alias>cp936</alias>
<collation name="gbk_chinese_ci" id="28" order="Chinese">
<flag>primary</flag>
<flag>compiled</flag>
</collation>
<collation name="gbk_bin" id="87" order="Binary">
<flag>binary</flag>
<flag>compiled</flag>
</collation>
</charset> <charset name="cp1257">
<family>Baltic</family>
<description>Windows Baltic</description>
<alias>WinBaltRim</alias>
<alias>windows-1257</alias>
<collation name="cp1257_lithuanian_ci" id="29" order="Lithuanian"/>
<collation name="cp1257_bin" id="58" order="Binary" flag="binary"/>
<collation name="cp1257_general_ci" id="59" flag="primary">
<order>Latvian</order>
<order>Lithuanian</order>
</collation>
<!--collation name="cp1257_ci" id="60"/-->
<!--collation name="cp1257_cs" id="61"/-->
</charset> <charset name="latin5">
<family>South Asian</family>
<description>ISO 8859-9 Turkish</description>
<alias>csisolatin5</alias>
<alias>iso-8859-9</alias>
<alias>iso-ir-148</alias>
<alias>l5</alias>
<alias>latin5</alias>
<alias>turkish</alias>
<collation name="latin5_turkish_ci" id="30" order="Turkish" flag="primary"/>
<collation name="latin5_bin" id="78" order="Binary" flag="binary"/>
</charset> <charset name="armscii8">
<family>South Asian</family>
<description>ARMSCII-8 Armenian</description>
<alias>armscii-8</alias>
<collation name="armscii8_general_ci" id="32" order="Armenian" flag="primary"/>
<collation name="armscii8_bin" id="64" order="Binary" flag="binary"/>
</charset> <charset name="utf8">
<family>Unicode</family>
<description>UTF-8 Unicode</description>
<alias>utf-8</alias>
<collation name="utf8_general_ci" id="33">
<flag>primary</flag>
<flag>compiled</flag>
</collation>
<collation name="utf8_bin" id="83">
<flag>binary</flag>
<flag>compiled</flag>
</collation>
</charset> <charset name="ucs2">
<family>Unicode</family>
<description>UCS-2 Unicode</description>
<collation name="ucs2_general_ci" id="35">
<flag>primary</flag>
<flag>compiled</flag>
</collation>
<collation name="ucs2_bin" id="90">
<flag>binary</flag>
<flag>compiled</flag>
</collation>
</charset> <charset name="cp866">
<family>Cyrillic</family>
<description>DOS Russian</description>
<alias>866</alias>
<alias>csibm866</alias>
<alias>ibm866</alias>
<alias>DOSCyrillicRussian</alias>
<collation name="cp866_general_ci" id="36" order="Russian" flag="primary"/>
<collation name="cp866_bin" id="68" order="Binary" flag="binary"/>
</charset> <charset name="keybcs2">
<family>Central European</family>
<description>DOS Kamenicky Czech-Slovak</description>
<collation name="keybcs2_general_ci" id="37" order="Czech" flag="primary"/>
<collation name="keybcs2_bin" id="73" order="Binary" flag="binary"/>
</charset> <charset name="macce">
<family>Central European</family>
<description>Mac Central European</description>
<alias>MacCentralEurope</alias>
<collation name="macce_general_ci" id="38" flag="primary">
<order>Hungarian</order>
<order>Polish</order>
<order>Romanian</order>
<order>Croatian</order>
<order>Slovak</order>
<order>Slovenian</order>
<order>Sorbian</order>
</collation>
<collation name="macce_bin" id="43" order="Binary" flag="binary"/>
</charset> <charset name="macroman">
<family>Western</family>
<description>Mac West European</description>
<alias>Mac</alias>
<alias>Macintosh</alias>
<alias>csmacintosh</alias>
<collation name="macroman_general_ci" id="39" flag="primary">
<order>Dutch</order>
<order>English</order>
<order>French</order>
<order>German Duden</order>
<order>Italian</order>
<order>Latin</order>
<order>Portuguese</order>
<order>Spanish</order>
</collation>
<collation name="macroman_bin" id="53" order="Binary" flag="binary"/>
<!--collation name="macroman_ci" id="54"/-->
<!--collation name="macroman_ci_ai" id="55"/-->
<!--collation name="macroman_cs" id="56"/-->
</charset> <charset name="cp852">
<family>Central European</family>
<description>DOS Central European</description>
<alias>852</alias>
<alias>cp852</alias>
<alias>ibm852</alias>
<collation name="cp852_general_ci" id="40" flag="primary">
<order>Hungarian</order>
<order>Polish</order>
<order>Romanian</order>
<order>Croatian</order>
<order>Slovak</order>
<order>Slovenian</order>
<order>Sorbian</order>
</collation>
<collation name="cp852_bin" id="81" order="Binary" flag="binary"/>
</charset> <charset name="cp1256">
<family>Arabic</family>
<description>Windows Arabic</description>
<alias>ms-arab</alias>
<alias>windows-1256</alias>
<collation name="cp1256_bin" id="67" order="Binary" flag="binary"/>
<collation name="cp1256_general_ci" id="57" order="Arabic" flag="primary">
<order>Arabic</order>
<order>Persian</order>
<order>Pakistani</order>
<order>Urdu</order>
</collation>
</charset> <charset name="geostd8">
<family>South Asian</family>
<description>GEOSTD8 Georgian</description>
<collation name="geostd8_general_ci" id="92" order="Georgian" flag="primary"/>
<collation name="geostd8_bin" id="93" order="Binary" flag="binary"/>
</charset> <charset name="binary">
<description>Binary pseudo charset</description>
<collation name="binary" id="63" order="Binary">
<flag>primary</flag>
<flag>compiled</flag>
</collation>
</charset> <charset name="cp932">
<family>Japanese</family>
<description>SJIS for Windows Japanese</description>
<alias>ms_cp932</alias>
<alias>sjis_cp932</alias>
<alias>sjis_ms</alias>
<collation name="cp932_japanese_ci" id="95" order="Japanese">
<flag>primary</flag>
<flag>compiled</flag>
</collation>
<collation name="cp932_bin" id="96" order="Binary">
<flag>binary</flag>
<flag>compiled</flag>
</collation>
</charset> <charset name="eucjpms">
<family>Japanese</family>
<description>UJIS for Windows Japanese</description>
<alias>eucjpms</alias>
<alias>eucJP_ms</alias>
<alias>ujis_ms</alias>
<alias>ujis_cp932</alias>
<collation name="eucjpms_japanese_ci" id="97" order="Japanese">
<flag>primary</flag>
<flag>compiled</flag>
</collation>
<collation name="eucjpms_bin" id="98" order="Japanese">
<flag>binary</flag>
<flag>compiled</flag>
</collation>
</charset> </charsets>

Index.xml 

五、思考总结

  经过大致3~4天的理论积累以及实践操作,终于完成了这篇文章。可以这么说吧,这篇博客可能是我查询文献最多以及写作时间最长的一篇。处女座的我,最受不了网上那些只告诉你这么干,不告诉为什么这么干的,或者只是照搬照抄没有探索精神的文章。写下本文望能与后来者共勉。

  文章的排版可能不是特别合理,因为一直没有搞定博客园html的跳转问题,略微抱歉。

  1. 字符编码

  utf-8字符集包含GBK字符集,但是这种包含关系并不是说utf-8的字符集包含的GBK字符集完全是一致的,而是指GBK的所有字符集都能够在utf-8中找到相应的映射。例如,在GBK中中文字符占2字节,而在变长字符集utf-8中中文字符占3字节。不同字符集在MySQL数据管理系统内部能够自动映射,但是在客户端中显示时必须匹配合适的字符编码集,否则无法正确显示,即出现乱码问题。

  2. mysql与mysqld区别

  mysql指客户端;mysqld=mysql daemon指mysql守护进程,即mysql后台服务器端。当我们在命令行输入mysqld -install后,即安装完成mysqld,此时在服务中出现MySQL服务。启动MySQL服务,等价于启动mysqld,即启动mysql服务器。此后,才能在mysql客户端登陆操作mysql数据库。

  3. 数据管理系统字符集层次

  MySQL数据库字符集具有多层次。在配置文件my.ini中仅定义default-character-set、default-set-server,然而MySQL服务器中却存在多层次的字符集结构。

  character-set-client 客户端发送给服务器SQL语句字符集

  character-set-connection sokect连接字符集

  character-set-results 客户端通过SELECT语句显示数据库查询结果字符集

  这三种字符集一般是与default-character-set字符集相同的。当然也可以自定义设置。

  character-set-database 数据库储存的数据字符集

  character-set-server 服务器字符集

  character-set-system 系统字符集

  这三种字符集一般是与default-set-server字符集相同的。当然也可以自定义设置。

  character-set-filesystem 文件系统字符集

Windows使用MySQL数据库管理系统中文乱码问题

MySQL数据库字符集

  可以通过在命令行模糊查询:SHOW variables LIKE 'char%'; 显示上述图所示类似内容。

  本文系作者原创,转载需要通过作者本人同意。