標籤:
查看資料庫的字元集
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‘
Mysql查看編碼方式