Python的MySQL模块在2.6下的安装

2010-04-06 20:12:35 by sand, 2102 visits, Tags: Python, MySQL, Linux, Mac OS X,

操作系统:Linux Cent OS 5 / Max OS X 10.6 snow leopard
相关环境:Python 2.6.4 ; MySQL 5.1.45
安装版本:MySQL-python-1.2.3c1
 
MySQLdb是Python连接MySQL的模块,下面介绍一下源码方式安装MySQLdb:
  1. 首先要下载下载:请到官方网站http://sourceforge.net/projects/mysql-python/或者点击链接下载http://downloads.sourceforge.net/project/mysql-python/mysql-python-test/1.2.3c1/MySQL-python-1.2.3c1.tar.gz?use_mirror=nchc
     
  2. 解压:tar zxvf MySQL-python*
     
  3. 进入文件目录,运行以下命令:
    sudo python setup.py install 
     
  4. 安装完成,到你的python安装目录下的site-packages目录里检查以下文件是否存在,如果存在即代表安装成功了
    Linux:MySQL_python-1.2.3c1-py2.6-linux-i686.egg
    Mac OS X:MySQL_python-1.2.3c1-py2.6-macosx-10.4-x86_64.egg
    注:如果碰到mysql_config not found的问题,有两种方法解决:
    1)sudo ln -s /usr/local/mysql/bin/mysql_config /usr/local/bin/mysql_config
    将mysql_confi从你的安装目录链接到/usr/local/bin目录下,这样就可以在任意目录下访问了(也可以放到/usr/bin)
    2)编辑源码文件夹的site.cfg文件,去掉#mysql_config = /usr/local/bin/mysql_config前的注释#,修改后面的路径为你的mysql_config真正的目录就可以了。(如果不知道mysql_config在哪里,运行命令:whereis mysql_config)

  5. 测试方法
    1)运行命令python进入python运行环境
    2)输入以下python代码进行测试
    import MySQLdb
    test=MySQLdb.connect(db='mydb',host='myhost',user='u',passwd='p')
    cur = test.cursor()
    cur.execute('show databases;')
    for data in cur.fetchall():
        print data
    
    3)如果你在屏幕上看到了你几个数据库的库名的输出代表你安装成功了
     
  6. 可能碰到的问题
    1)问题:ImportError: libmysqlclient_r.so.16: cannot open shared object file: No such file or directory
    原因是python无法找到mysql目录下的libmysqlclient_r.so.16动态库,其实MySQLdb是调用mysql的c函数库.所以本机上首先得安装了mysql
    然后: export LD_LIBRARY_PATH=/usr/local/mysql/lib/mysql:$LD_LIBRARY_PATH
    并且将/usr/local/mysql5.1/lib/mysql 放入/etc/ld.so.conf中
    /etc/ld.so.conf改后内容为:
    include ld.so.conf.d/*.conf
    /usr/local/mysql5.1/lib/mysql
    最后重新再测试一下,就不会有上面的问题了
如有问题请留言,我一定尽力解答

 

相关评论 | Comments (6)

  1. ddd says: 2010-08-04 05:43:40
    你好 我是个新手 我在安装的过程中不知到为什么出现了下面的情况: DmatoMacBook-Pro:MySQL-python-1.2.3c1 D$ sudo python setup.py install Password: sh: mysql_config: command not found Traceback (most recent call last): File "setup.py", line 15, in metadata, options = get_config() File "/Users/D/Desktop/MySQL-python-1.2.3c1/setup_posix.py", line 43, in get_config libs = mysql_config("libs_r") File "/Users/D/Desktop/MySQL-python-1.2.3c1/setup_posix.py", line 24, in mysql_config raise EnvironmentError("%s not found" % (mysql_config.path,)) EnvironmentError: mysql_config not found DmatoMacBook-Pro:MySQL-python-1.2.3c1 D$ sudo python setup.py install sh: /usr/local/bin/mysql_config: No such file or directory Traceback (most recent call last): File "setup.py", line 15, in metadata, options = get_config() File "/Users/D/Desktop/MySQL-python-1.2.3c1/setup_posix.py", line 43, in get_config libs = mysql_config("libs_r") File "/Users/D/Desktop/MySQL-python-1.2.3c1/setup_posix.py", line 24, in mysql_config raise EnvironmentError("%s not found" % (mysql_config.path,)) EnvironmentError: /usr/local/bin/mysql_config not found DmatoMacBook-Pro:MySQL-python-1.2.3c1 D$ sudo ln -s /usr/local/mysql/bin/mysql_config /usr/local/bin/mysql_config DmatoMacBook-Pro:MySQL-python-1.2.3c1 D$ sudo python setup.py installrunning install /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/setuptools-0.6c8-py2.6.egg/setuptools/package_index.py:7: DeprecationWarning: the md5 module is deprecated; use hashlib instead running bdist_egg running egg_info writing MySQL_python.egg-info/PKG-INFO writing top-level names to MySQL_python.egg-info/top_level.txt writing dependency_links to MySQL_python.egg-info/dependency_links.txt reading manifest file 'MySQL_python.egg-info/SOURCES.txt' reading manifest template 'MANIFEST.in' writing manifest file 'MySQL_python.egg-info/SOURCES.txt' installing library code to build/bdist.macosx-10.3-fat/egg running install_lib running build_py creating build creating build/lib.macosx-10.3-fat-2.6 copying _mysql_exceptions.py -> build/lib.macosx-10.3-fat-2.6 creating build/lib.macosx-10.3-fat-2.6/MySQLdb copying MySQLdb/__init__.py -> build/lib.macosx-10.3-fat-2.6/MySQLdb copying MySQLdb/converters.py -> build/lib.macosx-10.3-fat-2.6/MySQLdb copying MySQLdb/connections.py -> build/lib.macosx-10.3-fat-2.6/MySQLdb copying MySQLdb/cursors.py -> build/lib.macosx-10.3-fat-2.6/MySQLdb copying MySQLdb/release.py -> build/lib.macosx-10.3-fat-2.6/MySQLdb copying MySQLdb/times.py -> build/lib.macosx-10.3-fat-2.6/MySQLdb creating build/lib.macosx-10.3-fat-2.6/MySQLdb/constants copying MySQLdb/constants/__init__.py -> build/lib.macosx-10.3-fat-2.6/MySQLdb/constants copying MySQLdb/constants/CR.py -> build/lib.macosx-10.3-fat-2.6/MySQLdb/constants copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.macosx-10.3-fat-2.6/MySQLdb/constants copying MySQLdb/constants/ER.py -> build/lib.macosx-10.3-fat-2.6/MySQLdb/constants copying MySQLdb/constants/FLAG.py -> build/lib.macosx-10.3-fat-2.6/MySQLdb/constants copying MySQLdb/constants/REFRESH.py -> build/lib.macosx-10.3-fat-2.6/MySQLdb/constants copying MySQLdb/constants/CLIENT.py -> build/lib.macosx-10.3-fat-2.6/MySQLdb/constants running build_ext building '_mysql' extension creating build/temp.macosx-10.3-fat-2.6 gcc-4.0 -fno-strict-aliasing -fno-common -dynamic -DNDEBUG -g -O3 -Dversion_info=(1,2,3,'gamma',1) -D__version__=1.2.3c1 -I/usr/local/mysql/include -I/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6 -c _mysql.c -o build/temp.macosx-10.3-fat-2.6/_mysql.o -g -Os -arch i386 -fno-common -D_P1003_1B_VISIBLE -DSIGNAL_WITH_VIO_CLOSE -DSIGNALS_DONT_BREAK_READ -DIGNORE_SIGHUP_SIGQUIT -DDONT_DECLARE_CXA_PURE_VIRTUAL In file included from /usr/include/architecture/i386/math.h:626, from /usr/include/math.h:28, from /Library/Frameworks/Python.framework/Versions/2.6/include/python2.6/pyport.h:235, from /Library/Frameworks/Python.framework/Versions/2.6/include/python2.6/Python.h:58, from pymemcompat.h:10, from _mysql.c:29: /usr/include/AvailabilityMacros.h:108:14: warning: #warning Building for Intel with Mac OS X Deployment Target < 10.4 is invalid. In file included from _mysql.c:36: /usr/local/mysql/include/my_config.h:1065:1: warning: "HAVE_WCSCOLL" redefined In file included from /Library/Frameworks/Python.framework/Versions/2.6/include/python2.6/Python.h:8, from pymemcompat.h:10, from _mysql.c:29: /Library/Frameworks/Python.framework/Versions/2.6/include/python2.6/pyconfig.h:805:1: warning: this is the location of the previous definition Compiling with an SDK that doesn't seem to exist: /Developer/SDKs/MacOSX10.4u.sdk Please check your Xcode installation gcc-4.0 -arch ppc -arch i386 -isysroot /Developer/SDKs/MacOSX10.4u.sdk -g -bundle -undefined dynamic_lookup build/temp.macosx-10.3-fat-2.6/_mysql.o -L/usr/local/mysql/lib -lmysqlclient_r -lz -lm -lmygcc -o build/lib.macosx-10.3-fat-2.6/_mysql.so ld: library not found for -lbundle1.o collect2: ld returned 1 exit status ld: library not found for -lbundle1.o collect2: ld returned 1 exit status lipo: can't open input file: /var/tmp//ccdObDpD.out (No such file or directory) error: command 'gcc-4.0' failed with exit status 1 似乎没有安装好mysql,我测试过你给出的测试代码 还是不行,你知道是怎么回事么 谢谢
  2. sand says: 2010-03-31 21:44:16
    你加我qq吧,看着有点乱
  3. skyma says: 2011-08-03 15:27:38
    博友,你好呀!看到你这篇文章给我带来很大的惊喜,结果我还是没有解决python下安装MySQLdb这个问题,希望指点一下,谢谢!
  4. ryan says: 2011-10-23 10:55:20
    ImportError: libmysqlclient_r.so.16: cannot open shared object file: No such file or directory 这个问题,貌似在lion下没有/etc/ld.so.conf,(10.6下没有这个问题)。 另外,如果是 export LD_LIBRARY_PATH=/usr/local/mysql/lib/mysql:$LD_LIBRARY_PATH 的话,貌似每次开机都需要输入一次? BTW,我也在深圳。握爪。
  5. ryan says: 2011-10-23 10:55:20
    ImportError: libmysqlclient_r.so.16: cannot open shared object file: No such file or directory 这个问题,貌似在lion下没有/etc/ld.so.conf,(10.6下没有这个问题)。 另外,如果是 export LD_LIBRARY_PATH=/usr/local/mysql/lib/mysql:$LD_LIBRARY_PATH 的话,貌似每次开机都需要输入一次? BTW,我也在深圳。握爪。
  6. 秦迷空间 says: 2012-05-17 09:04:28
    有windows的没有?Python都是在Linux的吗

发表评论

用户名(required)

验证码(required)

Email(不会显示在网页)(required)

您的站点