概述
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
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之前,还需要安装一些依赖项;否则会各种报错缺失
yum whatprovides 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数据库的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: ('%','root','Y','',0L,<br>'MysqL_native_password','123456',datetime.datetime(2017,9,14,40,2),None,'N') [root@localhost python]#
至此,Python配置MysqL验证通过!
以上这篇Python配置MysqL的教程(推荐)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程小技巧。
总结
以上是编程之家为你收集整理的Python配置mysql的教程(推荐)全部内容,希望文章能够帮你解决Python配置mysql的教程(推荐)所遇到的程序开发问题。
如果您也喜欢它,动动您的小指点个赞吧