这几天要使用Django框架开发网站,使用到了MySQL数据库,禀着感觉linux牛逼的态度,所以我在Linux下安装了MySql数据库,并安装MySQLdb模块;不爽的是仅安装个MySQLdb模块就出了一堆错误,有点受不鸟;遂记录下来,so自己记录下痛苦的回忆。
a、下载MySQLdb,MySQL-python-1.2.4b4.tar.gz,下载地址http://sourceforge.net/projects/mysql-python/;
b、解压,进入,运行sudo python setup.py install
c、错误如下:
1、mysql_config not found
那就找到它,然后运行了一把whereis mysql_config,(甚至运行了find / -name mysql_config),尽然没有,网上google了一下,有人说是没有安装mysql-devel,于是
跑到MySql的官网下了个rpm安装包,运行rpm -ivh MySQL-devel-5.5.32-1.linux2.6.i386.rpm,在搜索一把,果然找到。
下面说怎么把mysql_config让python setup.py install 找到呢?这个要修改解压的MySQL-python-1.2.4b4.tar.gz下的site.cfg,将mysql_config的注释放开,禀修改值,我的是/usr/bin/mysql_config,修改保存;再运行;nnd,又出错了。。。
2、错误信息:
[shaohan@localhost MySQL-python-1.2.4b4]$ sudo python setup.py install
[sudo] password for shaohan:
running install
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.linux-i686/egg
running install_lib
running build_py
copying MySQLdb/release.py -> build/lib.linux-i686-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 -m32 -march=i686 -mtune=atom -fasynchronous-unwind-tables -D_GNU_SOURCE -fPIC -fwrapv -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m32 -march=i686 -mtune=atom -fasynchronous-unwind-tables -D_GNU_SOURCE -fPIC -fwrapv -fPIC -Dversion_info=(1,2,4,'beta',4) -D__version__=1.2.4b4 -I/usr/include/mysql -I/usr/include/python2.6 -c _mysql.c -o build/temp.linux-i686-2.6/_mysql.o -g -pipe -m32 -fPIC -g -static-libgcc -fno-omit-frame-pointer -fno-strict-aliasing -DMY_PTHREAD_FASTMUTEX=1
In file included from _mysql.c:44:
/usr/include/mysql/my_config.h:422:1: warning: "HAVE_WCSCOLL" redefined
In file included from /usr/include/python2.6/pyconfig.h:4,
from /usr/include/python2.6/Python.h:8,
from _mysql.c:29:
/usr/include/python2.6/pyconfig-32.h:808:1: warning: this is the location of the previous definition
gcc -pthread -shared build/temp.linux-i686-2.6/_mysql.o -L/usr/lib -L/usr/lib -lmysqlclient -lpthread -lm -lrt -ldl -lpython2.6 -o build/lib.linux-i686-2.6/_mysql.so
/usr/bin/ld: cannot find -lmysqlclient
collect2: ld returned 1 exit status
error: command 'gcc' failed with exit status 1
竟然找不到cannot find -lmysqlclient,于是又google了一把,运行find / -name 'libmysqlclient.so.*',我去竟然没有找到,所以问题就来了,哪去了;于是一狠心,就跑到MySQL的官网把其他的MySQL-shared-5.5.32-1.linux2.6.i386.rpm和MySQL-shared-compat-5.5.32-1.linux2.6.i386.rpm下载过来,安装了,在运行,woqu,成功了。
如果还是找不到,就运行find / -name 'libmysqlclient.so.*',;
[shaohan@localhost mysql]$ sudo find / -name 'libmysqlclient.so.*'
/usr/lib/libmysqlclient.so.10.0.0
/usr/lib/libmysqlclient.so.14
/usr/lib/libmysqlclient.so.14.0.0
/usr/lib/libmysqlclient.so.10
/usr/lib/libmysqlclient.so.15
/usr/lib/libmysqlclient.so.16
/usr/lib/libmysqlclient.so.15.0.0
/usr/lib/libmysqlclient.so.16.0.0
/usr/lib/libmysqlclient.so.12
/usr/lib/libmysqlclient.so.12.0.0
/usr/lib/libmysqlclient.so.18
/usr/lib/libmysqlclient.so.18.0.0
建立软链接,ln -s /usr/lib64/libmysqlclient.so.16.0.0 /usr/lib/libmysqlclient.so
总结:究其原因是MySQL是自己用rpm包装的,装得不完整,MySQL-client-5.5.32-1.linux2.6.i386.rpm,MySQL-devel-5.5.32-1.linux2.6.i386.rpm,MySQL-server-5.5.32-1.linux2.6.i386.rpm,MySQL-shared-5.5.32-1.linux2.6.i386.rpm,MySQL-shared-compat-5.5.32-1.linux2.6.i386.rpm,都安装就行了。
下面是汇总了其他人所遇到的错误:
错误:/usr/bin/ld: cannot find -lmysqlclient_r
解决:
切换到root用户,执行echo "/usr/local/mysql/lib/mysql" >> /etc/ld.so.conf#请主意这个/etc/ld.so.conf,这个是ld的默认配置文件,视系统不同而不同。/usr/local/mysql/lib/mysql与mysql安装目录相关。
强制刷新ld的配置文件。使用:ldconfig
进入mysql的lib目录如/usr/local/mysql/lib/mysql #与mysql安装目录相关。
如果没有libmysqlclient_r.so 就建立一个软连接 ln -s libmysqlclient.so libmysqlclient_r.so
退出root,切换到当前用户.
报错: _mysql.c:2810: error: expected declaration specifiers before 'init_mysql'
解决:安装python-dev,yum install python-devel