Mysql查看編碼方式專題python
1、mysql
查看數據庫的字符集linux
show variables like 'character\_set\_%';nginx
輸出:sql
+--------------------------+--------+數據庫
| Variable_name | Value |django
+--------------------------+--------+服務器
| 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上面竟然是
2、經過命令修改其編碼
建立數據庫指定數據庫的字符集
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也亂碼
3、解決數據導入導出的亂碼問題
#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來設置的!只是說明當前鏈接的客戶端的編碼狀況並無影響到數據庫服務器自己的編碼狀況