前言
本文使用的源碼安裝及其排錯,下列為其他幾種安裝方式。
yum安裝:
yum install MySQL-python -y
pip安裝:
pip install python-mysql
Python安裝MySQLdb模塊
【1】官網(wǎng)下載合適的版本
https://pypi.org/project/MySQL-python/
或
wget https://files.pythonhosted.org/packages/a5/e9/51b544da85a36a68debe7a7091f068d802fc515a3a202652828c73453cad/MySQL-python-1.2.5.zip
【2】解壓及安裝
安裝依賴包:
yum -y install rpm-build gcc-c++ mysql-devel python-devel
unzip MySQL-python-1.2.5.zip
cd MySQL-python-1.2.5
find / -name mysql_config
vim site.cfg(取消mysql_config前的注釋,并配置為你實際的數(shù)據(jù)庫該文件所在位置)
[options]
# embedded: link against the embedded server library
# threadsafe: use the threadsafe client
# static: link against a static library (probably required for embedded)
embedded = False
threadsafe = True
static = False
# The path to mysql_config.
# Only use this if mysql_config is not on your PATH, or you have some weird
# setup that requires it.
#mysql_config = /usr/local/bin/mysql_config
mysql_config = /app/mysql/bin/mysql_config
# http://stackoverflow.com/questions/1972259/mysql-python-install-problem-using-virtualenv-windows-pip
# Windows connector libs for MySQL. You need a 32-bit connector for your 32-bit Python build.
connector = C:\Program Files (x86)\MySQL\MySQL Connector C 6.0.2
python setup.py build
python setup.py install
【3】驗證
python中輸入import MySQLdb,無報錯即為成功
安裝過程中的報錯及解決
【1】ImportError: libmysqlclient.so.xx
原因:系統(tǒng)找不到名為libmysqlclient.so.xx的庫文件
解決方法:
<1>刪除生成的build目錄
cd MySQL-python-1.2.5
rm -rf build
<2>查找libmysqlclient.so.xx文件的位置
find / -name libmysqlclient.so.xx
<3>將文件發(fā)送到默認(rèn)lib庫位置處,可以做個軟鏈接
ln -s /usr/local/mysql/lib/libmysqlclient.so.xx /usr/lib/libmysqlclient.so.xx
<4> /etc/ld.so.conf添加庫的位置
include ld.so.conf.d/*.conf
/usr/lib/
/usr/local/lib/
/usr/local/mysql/lib/
重新加載配置:
ldconfig
【2】ImportError: /root/.python-eggs/MySQL_python-1.2.5-py2.7-linux-x86_64.egg-tmp/_mysql.so: undefined symbol: __cxa_pure_virtual
原因:在編譯的時候沒有成功加載zlib,更換編譯器gcc 為g++
解決方法:
<1>刪除生成的build目錄
cd MySQL-python-1.2.5
rm -rf build
<2>執(zhí)行build
python setup.py build
復(fù)制顯示的最后一行:
gcc -pthread -shared build/temp.linux-x86_64-2.7/_mysql.o -L/shang1/test/mysql/lib -lmysqlclient_r -lpthread -lz -lm -lrt -ldl -o build/lib.linux-x86_64-2.7/_mysql.so
<3>更換gcc為g++后執(zhí)行
g++ -pthread -shared build/temp.linux-x86_64-2.7/_mysql.o -L/shang1/test/mysql/lib -lmysqlclient_r -lpthread -lz -lm -lrt -ldl -o build/lib.linux-x86_64-2.7/_mysql.so
<4>無報錯后,執(zhí)行install
python setup.py install
【3】UserWarning: Module _mysql was already imported from…
原因:你是在解壓后的源碼包的位置執(zhí)行的import,模塊重復(fù)導(dǎo)致報錯。
解決方法:
切換的目錄執(zhí)行import即可。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
