一、查看数据库的字符集
show variables like 'collation%';
show variables like 'character\_set\_%';
输出:
+--------------------------+--------+
| 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 |
+--------------------------+--------+
结合以下的编码表我们发现当前的数据库系统的编码:
latin1_bin
西欧 (多语言), 二进制
binary
二进制
以上是我在linux环境中的查看的结果编码集了。我现在WIN平台上面查看编码集结果如:
+--------------------------+--------+
| 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 |
+--------------------------+--------+
为什么会出现不一致的情况呢?在我本机上面是显示的UTF-8.而在LINUX上面居然是
二、通过命令修改其编码
创建数据库指定数据库的字符集
mysql>create database mydb character set utf-8;#直接指定其编码
直接通过命令进行修改
set character_set_client=utf8;
set character_set_connection=utf8;
set character_set_database=utf8;
set character_set_results=utf8;
set character_set_server=utf8;
修改完了之后再查询
show variables like 'character\_set\_%';
+--------------------------+--------+
| 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 |
+--------------------------+--------+
结果全部都调整修改成UTF-8了!
修改完了之后我看 select * from address_address; 出现乱码了!Django也乱码
三、解决数据导入导出的乱码问题
#create database nginxdjango;
# use nginxdjango;
# show variables like 'character\_set\_%';
#打印输出居然是如下
+--------------------------+--------+
| 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 |
+--------------------------+--------+
依旧是latin编码的。
OK。我将其编码设置一下
set character_set_client=utf8;
set character_set_connection=utf8;
set character_set_database=utf8;
set character_set_results=utf8;
set character_set_server=utf8;
再查询一下其编码格式为: +--------------------------+--------+
| 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 |
+--------------------------+--------+
现在将数据导进来
source /python/django/sql/nginxdjango.sql;
其中的 nginxdjango.sql 其编码也是utf-8 格式的!
导进来 数据库查看居然还是乱码不过程序跑起来是正常了!
MySQL字符集编码的类型种类
gb2312_chinese_ci和gbk_chinese_ci以及gb2312_bin,gbk_bin的区别
gb2312_chinese_CI : 只支持简体中文
gb2312_BIN :而gb2312_bin可以说是gb2312_chinese_ci的一个子集,
而且gb2312_BIN是二进制存储.区分大小写数据库编码格式就意义不一样了
gbk_chinese_CI 支持简体中文和繁体
gbk_bin 解释同gb2312_BIN 对应gbk_chinese_CI
PS:GBK包括了简体与繁体两种类型
新篇:2010-03-09
MySQL中默认字符集的设置有四级:服务器级,数据库级,表级 。最终是字段级 的字符集设置。注意前三种均为默认设置,并不代码你的字段最终会使用这个字符集设置。所以我们建议要用show create table table ; 或show full fields from tableName; 来检查当前表中字段的字符集设置。
MySQL中关于连接环境的字符集设置有 Client端,connection, results 通过这些参数,MySQL就知道你的客户端工具用的是什么字符集,结果集应该是什么字符集。这样MySQL就会做必要的翻译,一旦这些参数有误,自然会导致字符串在转输过程中的转换错误。基本上99%的乱码由些造成。
1. 数据库表中字段的字符集设置 。show create table TableName 或 show full columns from tableName
mysql> show create table t1;
mysql> show full columns from t1; 查看列的编码类型
3. 查看数据库的编码格式
show create database test;
输出:CREATE DATABASE `test` /*!40100 DEFAULT CHARACTER SET utf8 */
2. 当前联接系统参数 show variables like 'char%'
mysql> show variables like 'char%';
1. 中文,请确保 表中该字段的字符集为中文兼容:
big5 | Big5 Traditional Chinese
gb2312 | GB2312 Simplified Chinese
gbk | GBK Simplified Chinese
utf8 | UTF-8 Unicode
[其它补充]
修改数据库的字符集
mysql>use mydb
mysql>alter database mydb character set utf-8;
创建数据库指定数据库的字符集
mysql>create database mydb character set utf-8;
2010-05-02 新添加进来
show variables like 'character\_set\_%'; 所查看到了几项中其中有这三项是受客户端影响
character_set_client
character_set_connection
character_set_results
而这三项是可以通过 set names utf8|set names gbk来设置的!只是说明当前连接的客户端的编码情况并没有影响到数据库服务器本身的编码情况
四. 修改数据库字符集和编码参数set names 'utf8';
或者:
SET CHARACTER_SET_CLIENT = utf8;
SET CHARACTER_SET_CONNECTION = utf8;
SET CHARACTER_SET_DATABASE = utf8;
SET CHARACTER_SET_RESULTS = utf8;
SET CHARACTER_SET_SERVER = utf8;
SET COLLATION_CONNECTION = utf8_general_ci;
SET COLLATION_DATABASE = utf8_general_ci;
SET COLLATION_SERVER = utf8_general_ci;修改完成后需要重启mysql
五. 查看字符集相关参数
show variables like 'collation%';
show variables like 'character\_set\_%';
status;