標籤:python mysql mysqldb
在Python串連和使用mysql時,需要調用MySQLdb 模組,是用於Python連結Mysql資料庫的介面,它實現了 Python 資料庫 API 規範 V2.0,基於 MySQL C API 上建立的。
MySQLdb模組在不同的平台下安裝方式不一樣,這裡主要介紹Linux下的安裝方式,https://pypi.python.org/pypi/MySQL-python 可以在這裡選擇合適的版本,解壓安裝。
安裝步驟:
1、選擇合適的版本以及檢查相關路徑這裡選擇版本為MySQL-python-1.2.5.zip,下面是下載路徑與過程。 wget https://pypi.python.org/packages/a5/e9/51b544da85a36a68debe7a7091f068d802fc515a3a202652828c73453cad/MySQL-python-1.2.5.zip--2016-10-08 21:34:56-- https://pypi.python.org/packages/a5/e9/51b544da85a36a68debe7a7091f068d802fc515a3a202652828c73453cad/MySQL-python-1.2.5.zipResolving pypi.python.org... 151.101.76.223, 2a04:4e42:12::223Connecting to pypi.python.org|151.101.76.223|:443... connected.HTTP request sent, awaiting response... 200 OKLength: 108935 (106K) [application/octet-stream]Saving to: “MySQL-python-1.2.5.zip”100%[======================================================>] 108,935 --.-K/s in 0.1s 2016-10-08 21:34:57 (1.00 MB/s) - “MySQL-python-1.2.5.zip” saved [108935/108935][[email protected] python]# ls mysql.py MySQL-python-1.2.5.zip解壓:[[email protected] python]# unzip MySQL-python-1.2.5.zip Archive: MySQL-python-1.2.5.zip inflating: MySQL-python-1.2.5/GPL-2.0 inflating: MySQL-python-1.2.5/HISTORY inflating: MySQL-python-1.2.5/INSTALL inflating: MySQL-python-1.2.5/MANIFEST.in inflating: MySQL-python-1.2.5/metadata.cfg inflating: MySQL-python-1.2.5/PKG-INFO inflating: MySQL-python-1.2.5/pymemcompat.h inflating: MySQL-python-1.2.5/README.md inflating: MySQL-python-1.2.5/setup.cfg inflating: MySQL-python-1.2.5/setup.py inflating: MySQL-python-1.2.5/setup_common.py inflating: MySQL-python-1.2.5/切換到解壓目錄安裝:使用命令 python setup.py build && python setup.py install 安裝報錯: [[email protected] MySQL-python-1.2.5]# python setup.py buildTraceback (most recent call last): File "setup.py", line 7, in <module> import setuptoolsImportError: No module named setuptools提示缺少setuptools 可以在 安裝命令和上面一樣,等最後執行完畢沒有報錯,安裝成功,我們繼續安裝上面的。 python setup.py buildrunning buildrunning build_pycreating buildcreating build/libcopying pkg_resources.py -> build/libcopying easy_install.py -> build/libcopying site.py -> build/libcreating build/lib/setuptoolscopying setuptools/package_index.py -> build/lib/setuptoolscopying setuptools/depends.py -> build/lib/setuptoolscopying setuptools/extension.py -> build/lib/setuptoolscopying setuptools/__init__.py -> build/lib/set。。。。。。Installed /usr/local/python2.7.11/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.eggProcessing dependencies for setuptools==0.6c11Finished processing dependencies for setuptools==0.6c11[[email protected] setuptools-0.6c11]# cd ..繼續安裝MySQL-python 出現error: command ‘gcc‘ failed with exit status 1可以用以下命令解決yum install gcc python-devel mysql-devel libxml2 libxml2-dev libxslt* zlib gcc openssl
安裝完成後的檢驗:
在shell視窗調用Python命令,然後執行,不報錯即可
import MySQLdb
Centos6.7下安裝python串連mysql環境故障解決