[推荐]Python 连接 Mysql 教程_MySQL, Oracle及数据库讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  MySQL, Oracle及数据库讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 2100 | 回复: 0   主题: [推荐]Python 连接 Mysql 教程        下一篇 
justfriend
注册用户
等级:新兵
经验:61
发帖:62
精华:0
注册:2011-11-21
状态:离线
发送短消息息给justfriend 加好友    发送短消息息给justfriend 发消息
发表于: IP:您无权察看 2014-11-21 10:47:38 | [全部帖] [楼主帖] 楼主

本文环境在centos 6.0下。首先需要确认你的环境有MySQLdb包。

可以输入

[root@mingming-dev python]# python
Python 2.6.6 (r266:84292, Dec 7 2011, 20:48:22)
[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named MySQLdb
>>>


出现No module named MySQLdb,说明没有这个包,需要手工加入,下载可以使用

wget "http://lazypeople.sinaapp.com/python/MySQL-python-1.2.3.tar.gz"


开始编译安装命令 python setup.py build

[root@mingming-dev mysqldb]# python setup.py build
running build
running build_py
creating build
creating build/lib.linux-x86_64-2.6
copying _mysql_exceptions.py -> build/lib.linux-x86_64-2.6
creating build/lib.linux-x86_64-2.6/MySQLdb
copying MySQLdb/__init__.py -> build/lib.linux-x86_64-2.6/MySQLdb
copying MySQLdb/converters.py -> build/lib.linux-x86_64-2.6/MySQLdb
copying MySQLdb/connections.py -> build/lib.linux-x86_64-2.6/MySQLdb
copying MySQLdb/cursors.py -> build/lib.linux-x86_64-2.6/MySQLdb
copying MySQLdb/release.py -> build/lib.linux-x86_64-2.6/MySQLdb
copying MySQLdb/times.py -> build/lib.linux-x86_64-2.6/MySQLdb
creating build/lib.linux-x86_64-2.6/MySQLdb/constants
copying MySQLdb/constants/__init__.py -> build/lib.linux-x86_64-2.6/MySQLdb/constants
copying MySQLdb/constants/CR.py -> build/lib.linux-x86_64-2.6/MySQLdb/constants
copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.linux-x86_64-2.6/MySQLdb/constants
copying MySQLdb/constants/ER.py -> build/lib.linux-x86_64-2.6/MySQLdb/constants
copying MySQLdb/constants/FLAG.py -> build/lib.linux-x86_64-2.6/MySQLdb/constants
copying MySQLdb/constants/REFRESH.py -> build/lib.linux-x86_64-2.6/MySQLdb/constants
copying MySQLdb/constants/CLIENT.py -> build/lib.linux-x86_64-2.6/MySQLdb/constants
running build_ext
building '_mysql' extension
creating build/temp.linux-x86_64-2.6
gcc -mno-cygwin -mdll -O -Wall -Dversion_info=(1,2,3,'final',0) -D__version__=1.2.3 -I/usr/include/mysql -I/usr/include/python2.6 -c _mysql.c -o build/temp.linux-x86_64-2.6/_mysql.o -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -fno-strict-aliasing -fwrapv -fPIC -DUNIV_LINUX -DUNIV_LINUX
cc1: error: unrecognized command line option "-mno-cygwin"
cc1: error: unrecognized command line option "-mdll"
error: command 'gcc' failed with exit status 1


发现出错了,怎么办呢?

检查发现是我的配置出现问题,删掉

[root@mingming-dev distutils]# rm distutils.cfg


再次

python setup.py build


发现

[root@mingming-dev mysqldb]# python setup.py build
running build
running build_py
copying MySQLdb/release.py -> build/lib.linux-x86_64-2.6/MySQLdb
running build_ext
building '_mysql' extension
gcc -pthread -fno-strict-aliasing -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -Dversion_info=(1,2,3,'final',0) -D__version__=1.2.3 -I/usr/include/mysql -I/usr/include/python2.6 -c _mysql.c -o build/temp.linux-x86_64-2.6/_mysql.o -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -fno-strict-aliasing -fwrapv -fPIC -DUNIV_LINUX -DUNIV_LINUX
In file included from _mysql.c:29:
pymemcompat.h:10:20: error: Python.h: No such file or directory
_mysql.c:30:26: error: structmember.h: No such file or directory
_mysql.c:62: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
_mysql.c:63: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
_mysql.c:64: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
_mysql.c:65: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
.....


出现了这个错误,经过google发现是没有安装python-dev

[root@mingming-dev mysqldb]# yum search python-dev
Loaded plugins: fastestmirror, presto
Repository base is listed more than once in the configuration
Repository updates is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
Repository contrib is listed more than once in the configuration
Loading mirror speeds from cached hostfile
base | 3.7 kB 00:00
extras | 3.0 kB 00:00
updates | 3.5 kB 00:00
=================================================================================== N/S Matched: python-dev ===================================================================================
dbus-python-devel.i686 : Libraries and headers for dbus-python
dbus-python-devel.x86_64 : Libraries and headers for dbus-python
gstreamer-python-devel.i686 : Headers for developing programs that will use gstreamer-python
gstreamer-python-devel.x86_64 : Headers for developing programs that will use gstreamer-python
python-devel.i686 : The libraries and header files needed for Python development
python-devel.x86_64 : The libraries and header files needed for Python development
Name and summary matches only, use "search all" for everything.
[root@mingming-dev mysqldb]# yum install python-devel.x86_64


然后就一路畅通无阻了~

[root@mingming-dev mysqldb]# python setup.py build
running build
running build_py
copying MySQLdb/release.py -> build/lib.linux-x86_64-2.6/MySQLdb
running build_ext
building '_mysql' extension
gcc -pthread -fno-strict-aliasing -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -Dversion_info=(1,2,3,'final',0) -D__version__=1.2.3 -I/usr/include/mysql -I/usr/include/python2.6 -c _mysql.c -o build/temp.linux-x86_64-2.6/_mysql.o -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -fno-strict-aliasing -fwrapv -fPIC -DUNIV_LINUX -DUNIV_LINUX
In file included from /usr/include/mysql/my_config.h:14,
from _mysql.c:36:
/usr/include/mysql/my_config_x86_64.h:1082:1: warning: "HAVE_WCSCOLL" redefined
In file included from /usr/include/python2.6/pyconfig.h:6,
from /usr/include/python2.6/Python.h:8,
from pymemcompat.h:10,
from _mysql.c:29:
/usr/include/python2.6/pyconfig-64.h:808:1: warning: this is the location of the previous definition
gcc -pthread -shared build/temp.linux-x86_64-2.6/_mysql.o -L/usr/lib64/mysql -L/usr/lib64 -lmysqlclient_r -lz -lpthread -lcrypt -lnsl -lm -lpthread -lssl -lcrypto -lpython2.6 -o build/lib.linux-x86_64-2.6/_mysql.so


--转自 北京联动北方科技有限公司




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论