Python配置mysql的教程(推薦)
Linux系統(tǒng)自帶Python,且根據(jù)系統(tǒng)自帶資源來對(duì)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系統(tǒng)中沒有mysql-python的rpm安裝包,這個(gè)資源需要從網(wǎng)上下載:
https://sourceforge.net/projects/mysql-python
目前主流使用Python2.6或Python2.7版本,下載為 MySQL-python-1.2.3c1.tar.gz
下載后上傳到Linux機(jī)器,放入非中文目錄
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之前,還需要安裝一些依賴項(xiàng);否則會(huì)各種報(bào)錯(cuò)缺失
名稱 | 來源 | 安裝方式 |
python-devel | 系統(tǒng)自帶 |
yum whatprovides python* 將查詢到的符合關(guān)鍵字名稱的包逐個(gè)安裝 |
mysql-server | ||
mysql-devel | ||
setuptools | 系統(tǒng)自帶 | http://pypi.python.org/pypi/setuptools 下載,根據(jù)python的版本選擇對(duì)應(yīng)的setuptools版本或者使用自帶包 yum install python-setuptools |
MySQL-python | 網(wǎng)絡(luò)下載 |
可使用yum whatprovides mysql-devel命令查看是否已安裝(舊版本Linux系統(tǒng)下包名稱為 mysql-dev,如果mysql-devel提示找不到,則使用dev替代):
如果命令報(bào)錯(cuò),則表示yum源配置有誤,或安裝光盤與系統(tǒng)不匹配等,具體請(qǐng)參考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則表示已經(jīng)安裝;主要觀察mysql-devel關(guān)鍵字的包是否已安裝;如未安裝,則輸入yum install mysql-devel命令安裝
依次安裝mysql-devel、python-devel、python-setuptools,安裝過程不報(bào)錯(cuò)則繼續(xù);
以上依賴項(xiàng)安裝完成后,回到MySQL-python解壓出的MySQL-python-1.2.3c1/目錄;
>> python setup.py build
>> python setup.py install
以上兩項(xiàng)命令正確執(zhí)行,則表示python配置mysqldb成功,再次驗(yàn)證導(dǎo)入MySQLdb是否報(bào)錯(cuò)
[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成功
常見錯(cuò)誤:python setup.py build提示找不到mysql_config
這個(gè)問題是因?yàn)樵跇?gòu)建mysqldb時(shí),使用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的位置默認(rèn)在/usr/bin目錄,不同系統(tǒng)存在差異,具體可通過搜索文件獲取實(shí)際位置)
驗(yàn)證python-Mysql功能
根據(jù)需要配置Mysql數(shù)據(jù)庫,并修改好用戶名與密碼;
查看python-mysql基礎(chǔ)語法,鏈接mysql數(shù)據(jù)庫的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()
執(zhí)行結(jié)果如下:
[root@localhost python]# python mysql-conn.py the user table content is: ('%', 'root', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', '', '', '', '', 0L, 0L, 0L, 0L, <br>'mysql_native_password', '123456', 'Y', datetime.datetime(2017, 9, 14, 14, 40, 2), None, 'N') [root@localhost python]#
至此,Python配置Mysql驗(yàn)證通過!
以上這篇Python配置mysql的教程(推薦)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Python Django框架模板渲染功能示例
- Phantomjs抓取渲染JS后的網(wǎng)頁(Python代碼)
- 在Python中使用CasperJS獲取JS渲染生成的HTML內(nèi)容的教程
- VPS CENTOS 上配置python,mysql,nginx,uwsgi,django的方法詳解
- python 連接數(shù)據(jù)庫mysql解壓版安裝配置及遇到問題
- Linux下安裝Python3和django并配置mysql作為django默認(rèn)服務(wù)器方法
- Django1.7+python 2.78+pycharm配置mysql數(shù)據(jù)庫
- 在Debian下配置Python+Django+Nginx+uWSGI+MySQL的教程
- Django1.7+python 2.78+pycharm配置mysql數(shù)據(jù)庫教程
- python 動(dòng)態(tài)渲染 mysql 配置文件的示例
相關(guān)文章
python用循環(huán)新建多個(gè)列表的代碼實(shí)例
當(dāng)我們處理數(shù)據(jù)時(shí),有時(shí)候需要?jiǎng)?chuàng)建多個(gè)列表以存儲(chǔ)不同類型或不同條件下的數(shù)據(jù),在Python中,我們可以利用循環(huán)來快速、高效地創(chuàng)建這些列表,本文將介紹如何使用循環(huán)在Python中創(chuàng)建多個(gè)列表,并提供代碼實(shí)例,需要的朋友可以參考下2024-04-04Python 僅獲取響應(yīng)頭, 不獲取實(shí)體的實(shí)例
今天小編就為大家分享一篇Python 僅獲取響應(yīng)頭, 不獲取實(shí)體的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08在django中實(shí)現(xiàn)choices字段獲取對(duì)應(yīng)字段值
這篇文章主要介紹了在django中實(shí)現(xiàn)choices字段獲取對(duì)應(yīng)字段值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07淺談Python數(shù)據(jù)處理csv的應(yīng)用小結(jié)
這篇文章主要介紹了Python數(shù)據(jù)處理csv的簡單應(yīng)用,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01Python使用signal定時(shí)結(jié)束AsyncIOScheduler任務(wù)的問題
這篇文章主要介紹了Python使用signal定時(shí)結(jié)束AsyncIOScheduler任務(wù),在使用aiohttp結(jié)合apscheduler的AsyncIOScheduler模擬定點(diǎn)并發(fā)的時(shí)候遇到兩個(gè)問題,針對(duì)每個(gè)問題給大家詳細(xì)介紹,需要的朋友可以參考下2021-07-07python中csv文件創(chuàng)建、讀取及修改等操作實(shí)例
很多程序在處理數(shù)據(jù)時(shí)都會(huì)碰到csv這種格式的文件,下面這篇文章主要給大家介紹了關(guān)于python中csv文件創(chuàng)建、讀取及修改等操作的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05Python檢測(cè)數(shù)據(jù)類型的方法總結(jié)
在本篇文章里小編給大家整理了關(guān)于Python檢測(cè)數(shù)據(jù)類型的方法和相關(guān)實(shí)例代碼,需要的朋友們跟著學(xué)習(xí)下。2019-05-05