- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Python配置mysql的教程(推荐)由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
Linux系统自带Python,且根据系统自带资源来对python配置mysql;安装需要已配置好正确的yum源; 。
在python未配置mysql的情形下,直接import MySQLdb的提示如下 。
1
2
3
4
|
>>> 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,解压目录如下 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
[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源配置教程 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
[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是否报错 。
1
2
3
4
5
6
7
|
[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; 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
[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表信息,代码如下,
1
2
3
4
5
6
7
8
9
10
|
#!/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()
|
执行结果如下:
1
2
3
|
[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的教程(推荐)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我.
原文链接:http://www.cnblogs.com/Wherebolezi/p/7657212.html 。
最后此篇关于Python配置mysql的教程(推荐)的文章就讲到这里了,如果你想了解更多关于Python配置mysql的教程(推荐)的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
嗨, 我是 Spark 的新手,我正在尝试使用 ML 推荐。 我的代码 df = sqlContext.createDataFrame( [(0, 0, 4.0), (0, 1, 2.0), (1,
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
平台工程是为软件开发人员创建高效生态系统的过程,帮助他们自主执行软件开发生命周期的端到端操作。平台工程旨在减少开发人员的整体认知负荷并消除流程中的瓶颈,让开发团队的体验更佳。平台工程工具通过改善开发
最近在ubuntu系统中使用自带的firefox浏览器,发现有写问题,比如登陆后,书签,历史记录等,原本在windows下同步的数据无法同步,添加书签的功能也无法使用。 经过查询资料后得知,unb
Remax是蚂蚁开源的一个用React来开发小程序的框架,采用运行时无语法限制的方案。整体研究下来主要分为三大部分:运行时原理、模板渲染原理、编译流程;看了下现有大部分文章主要集中在Reamx的运行
实验室拟态存储的项目需要通过lvs-nat模式通过lvs服务器来区隔内外网的服务,所以安全防护的重心则落在了lvs服务器之上。笔者最终选择通过firewalld放行端口的方式来实现需求,由于fire
如今,随着我们身体各类数据的指数级增长,人们需要接受的信息量越来越大,系统必须处理的难度也是越来越高。而这些正是我们需要通过交互式图表和仪表盘,来实现数据可视化的根本原因。在大幅节省用户的时间和精力
vsftpd 是“very secure FTP daemon”的缩写,安全性是它的一个最大的特点。 vsftpd 是一个 UNIX 类操作系统上运行的服务器的名字,它可以运行在诸如 Linux、
1、实现memcpy 将src所指向的内容拷贝到dst所指向的位置,拷贝len个字节。 memcpy是内存拷贝函数 memcpy在使用的时候不用考虑类型,以字节为单位进行拷贝
现在有3台服务器 s1(主),s2(从), s3(从)需要实现文件实时同步,我们可以安装Nfs服务端和客户端来实现! 1、安装 NFS 服务器所需的软件包:
本文基于Free Code Camp基本算法脚本“查找字符串中最长的单词”。 在此算法中,我们要查看每个单词并计算每个单词中有多少个字母。然后,比较计数以确定哪个单词的字符最多,并返回最长单词的长
I/O简介 I/O是Input/output的缩写,在java中,对于数据的输入和输出以流的方式进行。java.io包下提供了各种“流”类和接口,用以获取不同种类的数据,并通过标准的方法输入或输出
目录 docker容器源码部署httpd,用存储卷部署网站 创建一个httpd镜像 部署nfs 挂载 创建容器并映射
python代码如下: import webbrowser as wbimport foliumif __name__ == '__main__': loc = [30.679943, 104.0
近日,微软在 Github 上开源了一个 Python 静态类型检查工具:pyright ,引起了社区内的多方关注。 微软在开源项目上的参与力度是越来越大了,不说收购 Github 这种大的战略野
在编写多线程代码时,经常面临线程安全退出的问题。 一般情况下,选择检查标志位的方式: 在线程的while循环中,执行完例程后,都对标志位进行检查,如果标志位指示继续执行则再次执行例程,如果标志
前言 在程序中我们经常可以看到有很多的加密算法,比如说MD5 sha1等,今天我们就来了解下这下加密算法的吧,在了解之前我们需要知道一个模块嘛就是hashlib,他就是目前Python一个提供字符
java 泛型(generics)是 jdk 5 中引入的一个新特性, 泛型提供了编译时类型安全检测机制,该机制允许程序员在编译时检测到非法的类型。 泛型的本质是参数化类型,也就是说所操作的数据类型
在Python中,当我们有两个字典需要合并的时候,可以使用字典的 update 方法,例如: a = {'a': 1, 'b': 2} b = {'x': 3, 'y': 4}
有的时候我们在获取到目标电脑时候如果对方电脑又python 编译环境时可以利用python 反弹shell 主要用到python os库和sokect库 这里的服务端在目标机上运行
我是一名优秀的程序员,十分优秀!