首頁  >  文章  >  後端開發  >  Python配置mysql的教學(必看)

Python配置mysql的教學(必看)

黄舟
黄舟原創
2017-10-13 10:57:581984瀏覽

下面小編就為大家帶來一篇Python配置mysql的教學(推薦)。小編覺得蠻不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

Linux系統自帶Python,且根據系統自帶資源來對python配置mysql;安裝需要已配置好正確的yum源;

在python未配置mysql的情況下,直接import MySQLdb的提示如下


 >>> import MySQLdb
 Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 ImportError: No module named MySQLdb

Linux系統中沒有mysql-python的rpm安裝包,這個資源需要從網上下載:

https://sourceforge.net/projects/mysql-python

目前主流使用Python2.6或Python2.7版本,下載為MySQL-python-1.2.3c1 .tar.gz

下載後上傳到Linux機器,放入非中文目錄

tar -xf MySQL -python-1.2.3c1.tar.gz,解壓縮目錄如下


[root@localhost home]# cd MySQL-python-1.2.3c1/
[root@localhost MySQL-python-1.2.3c1]# ll
总用量 240
drwxr-xr-x. 5 root root  89 10月 12 12:27 build
-rw-r--r--. 1 tianF enosoft 59580 3月 31 2009 ChangeLog
drwxr-xr-x. 2 root root  57 10月 12 12:27 dist
drwxr-xr-x. 2 tianF enosoft 58 3月 31 2009 doc
-rw-r--r--. 1 tianF enosoft 9716 2月 6 2009 ez_setup.py
-rw-r--r--. 1 tianF enosoft 17989 2月 25 2007 GPL
-rw-r--r--. 1 tianF enosoft 2935 3月 4 2007 HISTORY
-rw-r--r--. 1 tianF enosoft 605 2月 11 2007 MANIFEST
-rw-r--r--. 1 tianF enosoft 272 3月 9 2009 MANIFEST.in
-rw-r--r--. 1 tianF enosoft 2098 3月 31 2009 metadata.cfg
-rw-r--r--. 1 tianF enosoft 75431 3月 31 2009 _mysql.c
drwxr-xr-x. 3 tianF enosoft 211 10月 12 12:28 MySQLdb
-rw-r--r--. 1 tianF enosoft 2306 4月 5 2006 _mysql_exceptions.py
-rw-r--r--. 1 root root  3791 10月 12 12:28 _mysql_exceptions.pyc
drwxr-xr-x. 2 tianF enosoft 90 3月 31 2009 MySQL_python.egg-info
-rw-r--r--. 1 tianF enosoft 1755 3月 31 2009 PKG-INFO
-rw-r--r--. 1 tianF enosoft 3203 4月 5 2006 pymemcompat.h
-rw-r--r--. 1 tianF enosoft 6696 10月 17 2008 README
-rw-r--r--. 1 tianF enosoft 380 3月 31 2009 setup.cfg
-rw-r--r--. 1 tianF enosoft 951 3月 8 2009 setup_common.py
-rw-r--r--. 1 root root  1520 10月 12 12:27 setup_common.pyc
-rw-r--r--. 1 tianF enosoft 2947 3月 8 2009 setup_posix.py
-rw-r--r--. 1 root root  2977 10月 12 12:27 setup_posix.pyc
-rw-r--r--. 1 tianF enosoft 495 10月 18 2008 setup.py
-rw-r--r--. 1 tianF enosoft 1547 3月 4 2007 setup_windows.py
-rw-r--r--. 1 tianF enosoft 592 10月 17 2008 site.cfg
drwxr-xr-x. 2 tianF enosoft 149 3月 31 2009 tests

在設定python-mysql之前,還需要安裝一些依賴項;否則會各種報錯缺失

python-devel系統自帶將查詢到的符合關鍵字名稱的套件逐一安裝mysql-servermysql-devel
#名稱 ##來源 #安裝方式

yum whatprovides python*


setuptools

系統自帶

http://pypi.python.org/pypi/setuptools

下載,根據python的版本選擇對應的setuptools版本或使用自帶套件yum install python-setuptools

#MySQL-python

網路下載


可使用yum whatprovides mysql-devel指令查看是否已安裝(舊版Linux系統下套件名稱為mysql-dev,如果mysql-devel提示找不到,則使用dev取代):若指令報錯,表示yum來源設定錯誤,或安裝光碟與系統不符等,請參考Linux下yum來源設定教學


[root@localhost mysql-python]# <strong>yum whatprovides mysql-devel</strong>
Loaded plugins: product-id, refresh-packagekit, security, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
base        | 3.9 kB  00:00 ...
mysql-devel-5.1.66-2.el6_3.x86_64 : Files for development of MySQL applications
Repo  : base
Matched from:
 
mysql-devel-5.1.66-2.el6_3.i686 : Files for development of MySQL applications
Repo  : base
Matched from:
 
mysql-devel-5.1.66-2.el6_3.x86_64 : Files for development of MySQL applications
Repo  : installed
Matched from:
Other  : Provides-match: mysql-devel
 
mysql-devel-5.1.66-2.el6_3.i686 : Files for development of MySQL applications
Repo  : installed
Matched from:
Other  : Provides-match: mysql-devel

如上所示,Repo值為installed則表示已經安裝;主要觀察mysql-devel關鍵字的套件是否已安裝;如未安裝,則輸入yum install mysql-devel指令安裝

依序安裝mysql-devel、python-devel、python-setuptools,安裝程序不報錯則繼續;以上相依性安裝完成後,回到MySQL-python解壓縮出的MySQL-python-1.2.3c1 /目錄;

>> python setup.py build

>> python setup.py install#以上兩個指令正確執行,則表示python配置mysqldb成功,再次驗證匯入MySQLdb是否報錯

[root@localhost mysql-python]#
[root@localhost mysql-python]# python
Python 2.7.5 (default, Aug 4 2017, 00:39:18)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>>
如上表示設定MySQLdb成功

常見錯誤:python setup.py build提示找不到mysql_config

這個問題是因為在建置mysqldb時,使用MySQL-python-1.2.3c1/目錄下的site.cfg檔案中設定的mysql_config;

[root@localhost MySQL-python-1.2.3c1]# ls
build  doc   HISTORY  metadata.cfg _mysql_exceptions.py PKG-INFO  setup.cfg   setup_posix.py setup_windows.py
ChangeLog ez_setup.py MANIFEST  _mysql.c  _mysql_exceptions.pyc pymemcompat.h setup_common.py setup_posix.pyc site.cfg
dist  GPL   MANIFEST.in MySQLdb  MySQL_python.egg-info README   setup_common.pyc setup.py   tests
[root@localhost MySQL-python-1.2.3c1]# more site.cfg
[options]
# embedded: link against the embedded server library
# threadsafe: use the threadsafe client
# static: link against a static library (probably required for embedded)
 
embedded = False
threadsafe = True
static = False
 
# The path to mysql_config.
# Only use this if mysql_config is not on your PATH, or you have some weird
# setup that requires it.
#mysql_config = /usr/local/bin/mysql_config
 
# The Windows registry key for MySQL.
# This has to be set for Windows builds to work.
# Only change this if you have a different version.
registry_key = SOFTWARE\MySQL AB\MySQL Server 5.0
[root@localhost MySQL-python-1.2.3c1]#

如果mysql的安裝位置與site.cfg中配置的位置不符,則需要修改site.cfg檔案的

#mysql_config

#配置,取消前面的註釋,並配置為正確的位址。例如############mysql_config = /usr/bin/mysql_config###### #(未指定的情形下,mysql_config的位置預設在/usr/bin目錄,不同系統有差異,具體可透過搜尋檔案取得實際位置)############驗證python-Mysql功能############依需求設定Mysql資料庫,並修改好使用者名與密碼;######查看python-mysql基礎語法,連結mysql資料庫的mysql函式庫,取得user表信息,程式碼如下;############
#!/usr/bin/python
#encoding=utf8
 
import MySQLdb
conn=MySQLdb.connect("127.0.0.1","root","123456","mysql")
cursor=conn.cursor()
cursor.execute("select * from user")
getdata=cursor.fetchone()
print "the user table content is:",getdata
conn.close()
###執行結果如下:############
[root@localhost python]# python mysql-conn.py
the user table content is: (&#39;%&#39;, &#39;root&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;&#39;, &#39;&#39;, &#39;&#39;, &#39;&#39;, 0L, 0L, 0L, 0L, <br>&#39;mysql_native_password&#39;, &#39;123456&#39;, &#39;Y&#39;, datetime.datetime(2017, 9, 14, 14, 40, 2), None, &#39;N&#39;)
[root@localhost python]#
###至此,Python設定Mysql驗證通過! ###

以上是Python配置mysql的教學(必看)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn