mac安装MySQLdb时可能遇到如下问题:
copying MySQLdb/constants/REFRESH.py -> build/lib.macosx-10.13-intel-2.7/MySQLdb/constants
copying MySQLdb/constants/CLIENT.py -> build/lib.macosx-10.13-intel-2.7/MySQLdb/constants
running build_ext
building '_mysql' extension
reating build/temp.macosx-10.13-intel-2.7
cc -fno-strict-aliasing -fno-common -dynamic -arch x86_64 -arch i386 -g -Os -pipe -fno-common -fno-strict-aliasing -fwrapv -DENABLE_DTRACE -DMACOSX -DNDEBUG -Wall -Wstrict-prototypes -Wshorten-64-to-32 -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -arch i386 -arch x86_64 -pipe -Dversion_info=(1,2,5,'final',1) -D__version__=1.2.5 -I/usr/local/mysql/include -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c _mysql.c -o build/temp.macosx-10.13-intel-2.7/_mysql.o
_mysql.c:44:10: fatal error: 'my_config.h' file not found
#include "my_config.h"
^~~~~~~~~~~~~
1 error generated.
error: command 'cc' failed with exit status 1
--------------------------------------
解决方法来自Stackoverflow大神:
brew install mysql
brew unlink mysql
brew install mysql-connector-c #或者已经安装了的话 brew link mysql-connector-c
sed -i -e 's/libs="$libs -l "/libs="$libs -lmysqlclient -lssl -lcrypto"/g' /usr/local/bin/mysql_config
pip install MySQL-python
brew unlink mysql-connector-c
brew link --overwrite mysql
安装好MySQLdb后,在python环境中导入MySQLdb,可能遇到如下问题:
>>> import MySQLdb
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Python/2.7/site-packages/MySQLdb/__init__.py", line 19, in <module>
import _mysql
ImportError: dlopen(/Library/Python/2.7/site-packages/_mysql.so, 2): Library not loaded: @rpath/libmysqlclient.21.dylib
Referenced from: /Library/Python/2.7/site-packages/_mysql.so
Reason: image not found
为了解决上述问题,你可能需要:
otool -L /Library/Python/2.7/site-packages/_mysql.so
该命令的结果可能是:
/Library/Python/2.7/site-packages/_mysql.so:
@rpath/libmysqlclient.21.dylib (compatibility version 21.0.0, current version 21.0.0)
libssl.1.0.0.dylib (compatibility version 1.0.0, current version 1.0.0)
libcrypto.1.0.0.dylib (compatibility version 1.0.0, current version 1.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1252.50.4)
对此,你需要将前三个lib的位置重新写一下,具体命令:
sudo install_name_tool -change @rpath/libmysqlclient.21.dylib /usr/local/mysql/lib/libmysqlclient.21.dylib /Library/Python/2.7/site-packages/_mysql.so
sudo install_name_tool -change libssl.1.0.0.dylib /usr/local/mysql/lib/libssl.1.0.0.dylib /Library/Python/2.7/site-packages/_mysql.so
sudo install_name_tool -change libcrypto.1.0.0.dylib /usr/local/mysql/lib/libcrypto.1.0.0.dylib /Library/Python/2.7/site-packages/_mysql.so
最终,可以看到不再报错:
>>> import MySQLdb
>>>
>>>
此外,你可能遇到的问题:
>>> import MySQLdb
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Python/2.7/site-packages/MySQLdb/__init__.py", line 19, in <module>
import _mysql
ImportError: dlopen(/Library/Python/2.7/site-packages/_mysql.so, 2): Symbol not found: _mysql_affected_rows
Referenced from: /Library/Python/2.7/site-packages/_mysql.so
Expected in: flat namespace
in /Library/Python/2.7/site-packages/_mysql.so
这个问题的原因是,你的Pyhon和Mysql的版本不一致导致的。比如我的mysql版本是64位,mac自带的python版本是2.7.10(32 bit)。我自己的解决方法是重新下载了python2.7.15(64bit),并做了如下配置(vim ~/.bash_profile):
PATH="/usr/local/mysql/bin:${PATH}"
export PATH
export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/
export VERSIONER_PYTHON_PREFER_64_BIT=yes
export VERSIONER_PYTHON_PREFER_32_BIT=no