如何build一个静态编译的MySQL-python
这里的静态是指静态链接mysqlclient lib。
MySQL-python是python访问MySQL数据库常用的一个模块,但是它的编译依赖os的很多环境,
build-requires = python-devel mysql-devel zlib-devel openssl-devel
要是能先静态编译好,就不需要依赖这些环境了。
修改site.cfg
static = True
python setup.py build
building '_mysql' extension
creating build/temp.linux-x86_64-2.6
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,5,'final',1) -D__version__=1.2.5 -I/usr/local/mysql/include -I/usr/include/python2.6 -c _mysql.c -o build/temp.linux-x86_64-2.6/_mysql.o -fPIC -g -fabi-version=2 -fno-omit-frame-pointer -fno-strict-aliasing
在包含自 _mysql.c:44 的文件中:
/usr/local/mysql/include/my_config.h:443:1: 警告:“HAVE_WCSCOLL”重定义
在包含自 /usr/include/python2.6/pyconfig.h:6 的文件中,
从 /usr/include/python2.6/Python.h:8,
从 _mysql.c:29:
/usr/include/python2.6/pyconfig-64.h:808:1: 警告:这是先前定义的位置
gcc -pthread -shared build/temp.linux-x86_64-2.6/_mysql.o /usr/local/mysql/lib/libmysqlclient.a -L/usr/local/mysql/lib -L/usr/lib64 -lpthread -lm -lrt -ldl -lpython2.6 -o build/lib.linux-x86_64-2.6/_mysql.so
看上面最后的gcc命令,链接的是MySQL静态lib: libmysqlclient.a, 如果site.cfg里配置了 thread_safe=True,
会链接 libmysqlclient_r.a。
gcc -pthread -shared build/temp.linux-x86_64-2.6/_mysql.o /usr/local/mysql/lib/libmysqlclient_r.a -L/usr/local/mysql/lib -L/usr/lib64 -lmysqlclient -lpthread -lm -lrt -ldl -lpython2.6 -o build/lib.linux-x86_64-2.6/_mysql.so
上面的命令还链接了 -lmysqlclient动态库,把这个去掉再执行。
build好之后,
python setup.py install
>>> import MySQLdb
/usr/lib64/python2.6/site-packages/MySQL_python-1.2.5-py2.6-linux-x86_64.egg/_mysql.py:3: UserWarning: Module _mysql was already imported from /usr/lib64/python2.6/site-packages/MySQL_python-1.2.5-py2.6-linux-x86_64.egg/_mysql.pyc, but /root/MySQL-python-1.2.5 is being added to sys.path
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "MySQLdb/__init__.py", line 19, in <module>
import _mysql
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: /root/.python-eggs/MySQL_python-1.2.5-py2.6-linux-x86_64.egg-tmp/_mysql.so: undefined symbol: __cxa_pure_virtual
import 报错,网络上搜一下,可以找到解决方案:
把gcc换成g++
g++ -pthread -shared build/temp.linux-x86_64-2.6/_mysql.o /usr/local/mysql/lib/libmysqlclient_r.a -L/usr/local/mysql/lib -L/usr/lib64 -lpthread -lm -lrt -ldl -lpython2.6 -o build/lib.linux-x86_64-2.6/_mysql.so
python setup.py install
这次安装之后,MySQLdb就可以正常访问,连接数据库了。
如何在其它机器上安装
以 python 2.6为例
把上面安装好的python egg文件(cp /usr/lib64/python2.6/site-packages/MySQL_python-1.2.5-py2.6-linux-x86_64.egg) copy到其他机器上对应的目录,
cp MySQL_python-1.2.5-py2.6-linux-x86_64.egg /usr/lib64/python2.6/site-packages/
echo MySQL_python-1.2.5-py2.6-linux-x86_64.egg > /usr/lib64/python2.6/site-packages/pythonmysql.pth
就可以直接使用MySQLdb模块了。不依赖机器上的mysqlclient。
时间: 2024-10-01 09:40:12