枫竹梦的环境是自己编译安装的MySQL,安装目录在/usr/local/mysql。
下载MySQLdb,由于网络上大多数的链接都是指向比较老的sourceforge上,而我们安装最新的1.2.5,https://github.com/farcepest/MySQLdb1/archive/MySQLdb-1.2.5.zip
解压
unzip MySQLdb-1.2.5.zip进行源码目录
cd MySQL-python-1.2.5编译
python setup.py build出现如下错误:
sh: 1: mysql_config: not found Traceback (most recent call last): File "setup.py", line 17, in <module> metadata, options = get_config() File "/home/furzoom/MySQLdb1-MySQLdb-1.2.5/setup_posix.py", line 43, in get_config libs = mysql_config("libs_r") File "/home/furzoom/MySQLdb1-MySQLdb-1.2.5/setup_posix.py", line 25, in mysql_config raise EnvironmentError("%s not found" % (mysql_config.path,)) EnvironmentError: mysql_config not found
由于MySQL是自行编译安装的,找不到mysql_config文件,编辑 MySQL-python-1.2.5目录下的site.cfg文件
将mysql_config配置行修改为以下内容,具体路径根据MySQL安装位置自行调整
mysql_config = /usr/local/mysql/bin/mysql_config
然后,继续编译
python setup.py build没有提示错误,表示安装成功,接着进行安装
sudo python setup.py install
一切正常。安装完毕。
写个脚本测试一下,提示如下错误:
Traceback (most recent call last): File "test.py", line 5, in <module> import MySQLdb File "build/bdist.linux-x86_64/egg/MySQLdb/__init__.py", line 19, in <module> File "build/bdist.linux-x86_64/egg/_mysql.py", line 7, in <module> File "build/bdist.linux-x86_64/egg/_mysql.py", line 6, in __bootstrap__ ImportError: libmysqlclient.so.20: cannot open shared object file: No such file or directory
第5行的代码仅仅是导入代码
import MySQLdb
原因是由于找不到libmysqlclient.so.20文件。其实这个文件在mysql的安装目录下,
/usr/local/mysql/lib/libmysqlclient.so.20
sudo ln -s /usr/local/mysql/lib/libmysqlclient.so.20 /usr/lib/libmysqlclient.so.20
至此,已经全部OK了。