MySQLdb-python無法設定字元集的問題

來源:互聯網
上載者:User

今天寫個python指令碼從MySQL導資料,匯入和匯出的兩個資料庫的編碼不一致,需要建立兩個資料連線,分別使用不同的字元集,按MySQLdb的文檔說明,只需要在MySQLdb.Connect()設定charset參數即可

 

charset

If present, the connection character set will be changed to this character set, if they are not equal. Support for changing the character set requires MySQL-4.1 and later server; if the server is too old, UnsupportedError will be raised. This option implies use_unicode=True, but you can override this with use_unicode=False, though you probably shouldn't.

If not present, the default character set is used.

This must be a keyword parameter.

 

但並沒有如願設定成功,無論我傳給charset什麼參數,調用character_set_name()總是返回"latin1",因為在其他伺服器運行同樣的腳步沒問題,可以肯定是伺服器的配置原因

 

開始以為是MySQLdb-python庫的問題,於是重新下載並自己編譯,但編譯遇到一點問題,因為編譯時間需要運行mysql_config檢測MySQL的配置,但運行mysql_config沒有輸出任何資訊

 

仔細檢查一下這台伺服器的MySQL相關配置,很是古怪:

 

#rpm -qa|grep mysql

mysqlclient10-3.23.58-4.RHEL4.1

mysql-devel-4.1.22-2.el4_8.4

mysqlclient10-devel-3.23.58-4.RHEL4.1

 

#rpm -qa|grep MySQL

perl-DBD-MySQL-2.9004-3.1

MySQL-shared-standard-4.1.22-0.rhel3

MySQL-python-1.2.1_p2-1.el4.1

MySQL-server-standard-4.1.22-0.rhel3

MySQL-client-standard-4.1.22-0.rhel4

 

mysql開頭的幾個包是Red Hat Linux內建的包,MySQL開頭的幾個包是MySQL.com RPM包,混著裝一起了,難怪mysql_config無法檢測到任何資訊,考慮到MySQL-server正在運行,於是快刀斬亂麻,把那幾個Red Hat內建的包都清理了(rpm -e xxx),然後安裝MySQL.com的RPM包,也順便把rhel3的MySQL-shared和MySQL-server換成rhel4版本(伺服器啟動並執行是RHEL4系統),因為伺服器正在運行,所以採用更新的方式(rpm -Uvh xxx.rpm)。

 

謹慎起見,MySQL-server版本保留不變,其他包統一按MySQL-server的版本裝上4.1.22的包(順便說一下,我習慣在http://rpm.pbone.net找軟體包),現在看起來整齊多了:

 

#rpm -qa|grep MySQL

perl-DBD-MySQL-2.9004-3.1

MySQL-server-standard-4.1.22-0.rhel4

MySQL-shared-standard-4.1.22-0.rhel4

MySQL-client-standard-4.1.22-0.rhel4

MySQL-devel-standard-4.1.22-0.rhel4

 

這次可以正常編譯MySQLdb-python了(需要先安裝easy_install, MySQL-python-1.2.3的原始碼包已經提供了ez_setup.py指令碼,運行python ez_setup.py就可以安裝了,然後運行python setup.py build編譯MySQL-python, 再運行python setup.py install即可按egg方式把最新編譯的MySQLdb-python安裝到Python庫中)

 

杯具的是,問題依舊,character_set_name()還是返回"latin1"。

 

 

再仔細閱讀MySQLdb-python的文檔,裡面關於MySQL的要求提到:

 

* MySQL.com RPM packages:

 

    - MySQL-devel to compile

 

    - MySQL-shared if you want to use their shared

      library. Otherwise you'll get a statically-linked module,

      which may or may not be what you want.

 

    - MySQL-shared to run if you compiled with MySQL-shared installed

 

這裡並沒有提到版本的要求,抱著死馬當活馬醫的態度,把MySQL-devel和MySQL-client兩個封裝更新到較新的版本(謹慎一點,資料庫還是保留了原來的4.1.22版本,順便提一下,rpm的更新方式確實比較強大,版本跳躍較大的情況下更新起來也很順利),現在幾個MySQL的包安裝如下:

 

#rpm -qa|grep MySQL

perl-DBD-MySQL-2.9004-3.1

MySQL-server-standard-4.1.22-0.rhel4

MySQL-shared-standard-4.1.22-0.rhel4

MySQL-client-community-5.1.56-1.rhel4

MySQL-devel-community-5.1.56-1.rhel4

 

問題居然解決了,看來MySQLdb-python對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.