Python配置mysql的教程(推荐)


Posted in Python onOctober 13, 2017

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 系统自带 yum whatprovides python* 将查询到的符合关键字名称的包逐个安装
mysql-server
mysql-devel
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: ('%', '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验证通过!

以上这篇Python配置mysql的教程(推荐)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
简单的连接MySQL与Python的Bottle框架的方法
Apr 30 Python
Python的dict字典结构操作方法学习笔记
May 07 Python
Python自动发邮件脚本
Mar 31 Python
python 判断是否为正小数和正整数的实例
Jul 23 Python
pygame实现简易飞机大战
Sep 11 Python
python实现一行输入多个值和一行输出多个值的例子
Jul 16 Python
python机器学习包mlxtend的安装和配置详解
Aug 21 Python
tensorflow实现对张量数据的切片操作方式
Jan 19 Python
Jupyter Notebook打开任意文件夹操作
Apr 14 Python
为什么是 Python -m
Jun 19 Python
Pytest之测试命名规则的使用
Apr 16 Python
pytorch 如何把图像数据集进行划分成train,test和val
May 31 Python
基于Python数据可视化利器Matplotlib,绘图入门篇,Pyplot详解
Oct 13 #Python
python的paramiko模块实现远程控制和传输示例
Oct 13 #Python
基于python(urlparse)模板的使用方法总结
Oct 13 #Python
Python创建对称矩阵的方法示例【基于numpy模块】
Oct 12 #Python
Python中的浮点数原理与运算分析
Oct 12 #Python
python中获得当前目录和上级目录的实现方法
Oct 12 #Python
Python实现的十进制小数与二进制小数相互转换功能
Oct 12 #Python
You might like
150kHz到30Mhz完全冲浪手册
2020/03/20 无线电
PHP 将数组打乱 shuffle函数的用法及简单实例
2016/06/17 PHP
ThinkPHP 3.2.2实现事务操作的方法
2017/05/05 PHP
IE 条件注释详解总结(附实例代码)
2009/08/29 Javascript
来自qq的javascript面试题
2010/07/24 Javascript
理解Javascript_14_函数形式参数与arguments
2010/10/20 Javascript
自己写了一个展开和收起的多更能型的js效果
2013/03/05 Javascript
AngularJS入门知识之MVW类框架的编程思想探讨
2014/12/08 Javascript
jQuery源码分析之Callbacks详解
2015/03/13 Javascript
jquery分割字符串的方法
2015/06/24 Javascript
IE10中flexigrid无法显示数据的解决方法
2015/07/26 Javascript
详解JavaScript的变量和数据类型
2015/11/27 Javascript
基于react框架使用的一些细节要点的思考
2017/05/31 Javascript
js求数组中全部数字可拼接出的最大整数示例代码
2017/08/25 Javascript
web前端开发中常见的多列布局解决方案整理(一定要看)
2017/10/15 Javascript
webpack4+express+mongodb+vue实现增删改查的示例
2018/11/08 Javascript
vue-cli配置flexible过程详解
2019/07/04 Javascript
Vue+Element UI+vue-quill-editor富文本编辑器及插入图片自定义
2019/08/20 Javascript
vue webpack build资源相对路径的问题及解决方法
2020/06/04 Javascript
js实现轮播图效果 纯js实现图片自动切换
2020/08/09 Javascript
javascript实现左右缓动动画函数
2020/11/25 Javascript
python基础教程之自定义函数介绍
2014/08/29 Python
Python多线程编程(八):使用Event实现线程间通信
2015/04/05 Python
python使用xlrd模块读写Excel文件的方法
2015/05/06 Python
python中zip和unzip数据的方法
2015/05/27 Python
python结合API实现即时天气信息
2016/01/19 Python
pandas 取出表中一列数据所有的值并转换为array类型的方法
2018/04/11 Python
HTML5实现预览本地图片
2016/02/17 HTML / CSS
乐高官方旗舰店:LEGO积木玩具
2019/04/06 全球购物
2014三八妇女节活动总结范文四篇
2014/03/09 职场文书
资源环境与城乡规划管理专业自荐书
2014/09/26 职场文书
2019求职信:应届生求职信范文
2019/04/24 职场文书
2019年描写人生经典诗句大全
2019/07/08 职场文书
Java实现二维数组和稀疏数组之间的转换
2021/06/27 Java/Android
Java基于Dijkstra算法实现校园导游程序
2022/03/17 Java/Android
WCG2010 星际争霸决赛 Flash vs Goojila 1 星际经典比赛回顾
2022/04/01 星际争霸