zyllf2009 发表于 2015-12-30 14:32:35

mac os 上安装mysqldb血泪史

  昨天下午在mac上安装mysql-python一直未遂今天查了很多资料终于成功了 最后还是在stackoverflow点击打开链接(好网站啊,一般有什么技术问题在这都能找到)上找到了答案,废话少数:
  
首先,下载MySQLdb:http://sourceforge.net/projects/mysql-python/
下载MySQL-python-1.2.3.tar.gz

解压,运行setup.py:
python setup.py install
报错:

sh: mysql_config: command not found
Traceback (most recent call last):
File &quot;setup.py&quot;, line 15, in <module>
    metadata, options = get_config()
File &quot;/Users/***/Downloads/MySQL-python-1.2.3/setup_posix.py&quot;, line 43, in get_config
    libs = mysql_config(&quot;libs_r&quot;)
File &quot;/Users/***/Downloads/MySQL-python-1.2.3/setup_posix.py&quot;, line 24, in mysql_config
    raise EnvironmentError(&quot;%s not found&quot; % (mysql_config.path,))
EnvironmentError: mysql_config not found



参考:http://www.southsearepublic.org/article/2416/read/environmenterror_mysql_config_not_found/installing_mysqldb_for_python_on_mac_osx/
发现要修改site.cfg
mysql_config = /usr/local/mysql/bin/mysql_config

让mysql_config指向mysql在mac os中的安装位置。


之后再用高权限运行
sudo python setup.py install




如果运行的时候,还有问题:
>>> import MySQLdb
/Library/Python/2.7/site-packages/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg/_mysql.py:3: UserWarning: Module _mysql was already imported from /Library/Python/2.7/site-packages/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg/_mysql.pyc, but /Users/***/Downloads/MySQL-python-1.2.3is being added to sys.path
Traceback (most recent call last):
File &quot;<stdin>&quot;, line 1, in <module>
File &quot;MySQLdb/__init__.py&quot;, line 19, in <module>
    import _mysql
File &quot;build/bdist.macosx-10.7-intel/egg/_mysql.py&quot;, line 7, in <module>
File &quot;build/bdist.macosx-10.7-intel/egg/_mysql.py&quot;, line 6, in __bootstrap__
ImportError: dlopen(/Users/***/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg-tmp/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib
Referenced from: /Users/***/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg-tmp/_mysql.so
Reason: image not found



那么就需要解决动态引入的问题了(重新做一遍,并且设置环境变量):
  $ sudo python setup.py clean
  $ sudo python setup.py build
  $ sudo python setup.py install
  $ export DYLD_LIBRARY_PATH=/usr/local/mysql/lib:$DYLD_LIBRARY_PATH
  $python
  >>> import MySQLdb
/Library/Python/2.7/site-packages/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg/_mysql.py:3: UserWarning: Module _mysql was already imported from /Library/Python/2.7/site-packages/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg/_mysql.pyc, but /Users/***/Downloads/MySQL-python-1.2.3is being added to sys.path

  >>> conn=MySQLdb.connect(host=&quot;localhost&quot;,user=&quot;root&quot;,passwd=&quot;root&quot;,db=&quot;mysql&quot;)
>>> cursor =conn.cursor()
>>> sql =&quot;select * from user&quot;
>>> cursor.execute(sql)
7L
>>> row=cursor.fetchone()
>>> print row
('localhost', 'root', '*81F5E21E35407D884A6CD4A731AEBFB6AF209E1B', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', '', '', '', '', 0L, 0L, 0L, 0L, '', '')
  能够读取mysql.user表的信息了,说明已经mysqldb已经安装成功。
  参考:http://stackoverflow.com/questions/1465846/install-mysqldb-on-snow-leopard/6537345#6537345
  

  如果还是在eclipse中有问题,那就采用这里的办法 http://yanghao.org/blog/archives/76
  解决办法: eclipse->偏好设置->Pydev->Interpreter-Python->Environment里面增加下面内容,看图:http://yanghao.org/blog/wp-content/uploads/2011/03/%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7-2011-03-05-%E4%B8%8A%E5%8D%8812.49.19.png
  
  Eclipse中配置MySQLdb模块
  Window ——> Preferences ——> PyDev ——> Interpreter-Python ——> Forced Builtins ——> New... —— > 输入MySQLdb ——> OK
页: [1]
查看完整版本: mac os 上安装mysqldb血泪史