mysql命令列以及mysql workbence查詢結果中文亂碼的解決方案

來源:互聯網
上載者:User

標籤:

最近正在學習mysql,安裝環境是windows server 2003 32位作業系統+mysql 5.1.47
同時也安裝了mysql命令列以及mysql workbench
這裡是test資料庫
create  table fortest(empno int auto_increment,empname nvarchar(20) not null,constraint pk_fortest primary key(empno))default charset=utf8;
然後插入了一條資料
set names character utf8;
insert into fortest(empno)values(‘歐陽菲菲‘);
發現插入不了,總是報錯,具體錯誤如下:
這種情況主要是由於預設編碼設定錯誤引起的。
現在就查看一下mysql伺服器的配置情況吧。
具體命令就是
show variables like ‘character%‘;
查出的結果如下:


具體解決方案:

使用MySQL命令列可以實現許多我們需要的功能,不過在使用MySQL命令列的時候,有一個問題是在MySQL命令列插入中文資料或者查詢中文資料時出現亂碼,或者顯示不對。

在MySQL命令列輸入:show variables like ‘character_set_%’;

查看當前配置的編碼;

在MySQL命令列輸入 :set names utf8;

則等同於

set character_set_client=utf8;

set character_set_results=utf8;

set character_set_connection=utf8;

這三條命令

在命令列插入資料之前

執行命令set names gbk;或者set names gb2312;

再插入

查詢資料之前

執行命令set names gbk;或者set names gb2312;

完了之後再set names utf8;

這樣就能正常插入和查詢中文並且保持資料庫編碼為utf8


對於mysql workbench中的查詢出現中文亂碼主要是由於mysql伺服器的配置中預設設定的編碼不是utf8引起的,具體在windows作業系統下,要修改mysql安裝目錄下的my.ini
(linux下則是安裝目錄下的my.cnf檔案)
主要修改的部分如下:
[client]

port=3306

[mysql]

default-character-set=utf8

[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:/Program Files/MySQL/MySQL Server 5.1/"

#Path to the database root
datadir="D:/MySQL/MySQL Server 5.1/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

具體修改的部分就是紅色粗體字的文字部分。

 

轉載自:http://blog.sina.com.cn/s/blog_8a3a624901018j83.html

mysql命令列以及mysql workbence查詢結果中文亂碼的解決方案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.