Mysql字元集問題

來源:互聯網
上載者:User

標籤:匯入資料   tin   儲存   重啟   quic   sql   完成   更改   localhost   

方法1:登入mysql,先做set names latin1,然後在執行更新語句或執行語句檔案mysql> set names latin1;方法2:在sql檔案中指定set names latin1;然後登入mysql,通過如下命令執行mysql> source test.sql;方法3:在sql檔案中指定set names latin1;然後通過mysql命令匯入資料mysql -uroot -p123456 test < test.sqlmysql -uroot -p123456 test -e "set names latin1;select * from test.test;"方法4:通過指定mysql命令的字元集參數實現--default-character-set=latin1cat test.sqlINSERT INTO test VALUES(9,‘張三‘);mysql -uroot -p123456 --default-character-set=‘latin1‘ test < test.sql方法5:在設定檔裡設定用戶端及服務端相關參數[client]default-character-set=latin1提示:無需重啟服務,退出登入就生效更改my.cnf參數[mysqld]default-character-set=latin1 適合5.1及以前版本character-set-server=latin1 適合5.5確保服務端和用戶端字元集相同 方法1:登入mysql,先做set names latin1,然後在執行更新語句或執行語句檔案mysql> set names latin1;方法2:在sql檔案中指定set names latin1;然後登入mysql,通過如下命令執行mysql> source test.sql;方法3:在sql檔案中指定set names latin1;然後通過mysql命令匯入資料mysql -uroot -p123456 test < test.sqlmysql -uroot -p123456 test -e "set names latin1;select * from test.test;"方法4:通過指定mysql命令的字元集參數實現--default-character-set=latin1cat test.sqlINSERT INTO test VALUES(9,‘張三‘);mysql -uroot -p123456 --default-character-set=‘latin1‘ test < test.sql方法5:在設定檔裡設定用戶端及服務端相關參數[client]default-character-set=latin1提示:無需重啟服務,退出登入就生效更改my.cnf參數[mysqld]default-character-set=latin1 適合5.1及以前版本character-set-server=latin1 適合5.5確保服務端和用戶端字元集相同 1、linux系統服務端cat /etc/sysconfig/i18nLANG="zh_CN.utf8"[[email protected] 3306]# echo $LANGzh_CN.UTF-8提示:linux用戶端也要更改為UTF-82、Mysql資料庫的用戶端臨時:mysql> set names utf8; 或者啟動時指定 mysql -uroot -p123456 --default-character-set=utf8永久:更改my.cnf用戶端的參數,可以實現set names utf8的效果,並且永久生效[client]default-character-set=utf8提示:設定檔修改無需重啟3、Mysql服務端更改my.cnf[mysqld]default-character-set=utf8 適合5.1及以前版本character-set-server=utf8 適合5.54、Mysql資料庫中建庫建表指定字元集create database test default character set utf8 collate utf8_general_ci;5、開發程式字元集:簡體UTF8 
#對於已有的資料庫向修改字元集不能直接通過"alter database character set ..." 或 "alter table tablename character set ..." 修改,這兩個命令都沒有更新已有記錄的字元集

而只是對於新建立的表或者記錄生效,已經有記錄的字元集的調整,必須要先將資料匯出,經過修改字元集後重新匯入後才可完成。


下面類比將latin1字元集的資料庫修改成GBK字元集的實際過程;

1、匯出表結構
mysqldump -uroot -p --default-character-set=latin1 -d dbname >alltabl.sql ##--default-character-set=gbk 表示以gbk字元集進行串連 -d 只導表結構

2、編輯alltable.sql將latin1改成gbk

3、確保資料庫不再更新,匯出所有資料
mysqldump -uroot -p --quick --no-create-info --extended-insert --default-character-set=latin1 dbname > alldata.sql

參數說明:
--quick:用於轉儲大的表,強制mysqldump從伺服器一次一行的檢索資料而不是檢索所有行

--no-create-info : 不建立create table 語句

--extended-insert : 使用包括幾個values列表的多行insert 文法,這樣檔案更小,IO也小,匯入資料時會非常快

--default-character-set=latin #按照原有的字元集匯出資料,這樣匯出的檔案中,所有中文都是可見的,不會儲存成亂碼

4、開啟alldata.sql 將 set names latin1 修改成set names gbk

5、建庫
create database dbname default charset gbk;

6、建立表,執行alldata.sql
mysql -uroot -p dbname < alltable.sql

7、匯入資料
mysql -uroot -p dbname < alldata.sql



 

Mysql字元集問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.