2014-03-12

如何使用 Python3 連接 MySQL

相信大家想用 Python 來連接 MySQL 時一定也會搜尋一下網路
然後你可能會發現類似這個網站教導你如何用 MySQLdb 連MySQL

當然啦, 這並沒有錯, 不過有一點要注意的是, 如果你用的是 Python 3.x, 那麼, 很抱歉的是, MySQLdb 已經不再更新了, 你可能會在安裝時遇到以下訊息




在這裡建議你若你是使用 Python3 時請使用 mysql 官方的 mysql/python connector
不要再用網友們熱心提供或者 open source project 的 connector

所有平台的 MySQL official connector 在這裡 http://dev.mysql.com/doc/index-connectors.html Python 也不例外

MySQL Python connector 的 API guide 請看這裡 http://dev.mysql.com/doc/connector-python/en/connector-python-reference.html

connector 下載請到這裡  http://dev.mysql.com/downloads/connector/python/
例如 centos7 mysql connector 為 http://dev.mysql.com/get/Downloads/Connector-Python/mysql-connector-python-2.0.4-1.el7.noarch.rpm


相關 reference 請看這裡 http://dev.mysql.com/doc/connector-python/en/index.html (install, API, guidelines, examples...)


* 使用 pip 安裝注意事項
因為 pip 1.5 以後,若 library 未在 pip 官方主機上,則要用 --allow-external 。指令如下供參考
# pip install --allow-external mysql-connector-python mysql-connector-python


2015/11/19 註:
MySQLdb  雖然沒有維護了,但最近有人 fork 出一個新的分枝叫 mysqlclient.
而官網也在文件內作了說明,並且建議大家使用 mysqlclient
故現在建議大家使用新的 mysqlclient 吧
$ pip install mysqlclient

安裝過程中你可能還需要 mysql develop lib
$ yum install python-devel mysql-devel
使用 python3 時需要 
$ yum install python3-devel
7安裝附屬套件!!!
安裝 Shared compat libraries for MySQL ,我們程式 compile 所需 lib 及 python mysql connector 需要。
# yum install mysql-community-libs-compat
註:需要 libmysqlclient.so.18、libmysqlclient.so.20 ...時


詳細說明文件在此:https://docs.djangoproject.com/en/1.8/ref/databases/#mysql-db-api-drivers




I believe that you may also surf internet for references before connecting to MySQL using Python.

Therefore, you may also find websites like this which teaches you to use MySQLdb to connect to MySQL

Of course, this is not wrong, but one thing to be declared, if you are using Python 3.x, then,
It's a pity, MySQLdb is no longer be updated, you may encounter the following messages during installation.


How do you solve this problem?
Here I suggest you to use mysql official mysql/python connector if you are using Python3 or above.

Do not use private connectors that enthusiastic netizens provided or open source project for mysql.

MySQL official connectors for all platforms are here(http://dev.mysql.com/doc/index-connectors.html) and Python has no exception, you can find everything that python needed here

MySQL Python connector's API guide is here (http://dev.mysql.com/doc/connector-python/en/connector-python-reference.html)

Related reference is here (http://dev.mysql.com/doc/connector-python/en/index.html) (install, API, guidelines, examples ...)

with weapons above, you can use mysql without any problem, good luck.


* some important thing for  pip installation user.
It will fail if you use pip 1.5 or later version. The reason is that library is not  hosted at pip official machine. In this case, you need to add a parameter "--allow-external". Below instruction is for your reference.

# pip install --allow-external mysql-connector-python mysql-connector-python


沒有留言:

張貼留言