查看数据库的字符集
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来设置的!只是说明当前连接的客户端的编码情况并没有影响到数据库服务器本身的编码情况
CREATE TABLE student(
id TINYINT UNSIGNED AUTO_INCREMENT KEY,
username VARCHAR(20) NOT NULL UNIQUE,
age TINYINT UNSIGNED DEFAULT 18,
sex ENUM(‘男‘,‘女‘,‘保密‘) DEFAULT ‘男‘,
salary FLOAT(6,2),
addr VARCHAR(200) NOT NULL DEFAULT ‘北京‘,
hasHouse TINYINT(1) DEFAULT 0,
hasCar TINYINT(1) DEFAULT 0
);
这个表可以看到在创建的时候会出现错误
ERROR 1067 <42000>:Invalid default value for ‘sex‘
这就是编码的问题了,可以这样修改
alter DATABASE `DBname` DEFAULT CHARACTER SET utf8
或者查看一下
use Databasename SHOW VARIABLES LIKE ‘collation%‘
MySQL中,如何修改collation_server的latin1_swedish_ci为utf8_general_ci?
修改my.ini可以这样
[mysqld]
# The TCP/IP Port the MySQL Server will listen on
port=3306
#Path to installation directory. All paths are usually resolved relative to this.
basedir="D:\AppServ/MySQL"
#Path to the database root
datadir="D:\AppServ/MySQL/data/"
# The default character set that will be used when a new schema or table is
# created and no character set is defined
default-character-set = utf8
character-set-server = utf8
collation-server = utf8_general_ci
init_connect = ‘SET collation_connection = utf8_general_ci‘
init_connect = ‘SET NAMES utf8‘