MySQL配置不正确原因:不安全使用相对路径

时间:2015-07-10 14:26:27

标签: python mysql django dynamic-linking osx-elcapitan

我正在使用Django,当我运行python manage.py runserver时,我收到以下错误:

ImproperlyConfigured: Error loading MySQLdb module: dlopen(/Library/Python/2.7/site-packages/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib
  Referenced from: /Library/Python/2.7/site-packages/_mysql.so
  Reason: unsafe use of relative rpath libmysqlclient.18.dylib in /Library/Python/2.7/site-packages/_mysql.so with restricted binary

我不完全确定如何解决这个问题。我已经通过pip安装了MySQL-python。然后我提前this步了一步。

我还想指出这是与El Capitan Beta 3。

2 个答案:

答案 0 :(得分:76)

在OS X El Capitan(10.11)中,Apple添加了System Integrity Protection

这可以防止像/usr这样的受保护位置中的程序调用使用对另一个共享库的相对引用的共享库。对于_mysql.so,它包含对共享库libmysqlclient.18.dylib的相对引用。

将来可能会更新共享库_mysql.so。在此之前,您可以通过install_name_tool实用程序强制它使用绝对引用。

假设libmysqlclient.18.dylib位于/ usr / local / mysql / lib /中,则运行命令:

sudo install_name_tool -change libmysqlclient.18.dylib \
  /usr/local/mysql/lib/libmysqlclient.18.dylib \
  /Library/Python/2.7/site-packages/_mysql.so

答案 1 :(得分:0)

如果有很多相对路径需要修复(就像我在opencv库中发生的那样)。您可以使用以下代码段:

相应地更改ABSPATH和LIBPATHS。它将创建rPathChangeCmd.txt,您可以将其粘贴到终端中。如果出现任何错误,它还会创建rPathChangeErr.txt。我建议在粘贴命令之前检查错误文件(如果已创建)。

import glob
import subprocess
import os.path

ABSPATH = "/usr/local/lib/"  # absolute path to relative libraries
# libraries to correct
LIBPATHS = ['/usr/local/lib/python2.7/site-packages/cv2.so', '/usr/local/lib/libopencv*'] 

PREFIX = 'sudo install_name_tool -change '

assert(ABSPATH.startswith('/') and ABSPATH.endswith('/'), 
    'please provide absolute library path ending with /')

libs = []
for path in LIBPATHS:
  libs += glob.glob(path)

cmd =  []
err = []
for lib in libs:
  if not os.path.isfile(lib):
    err.append(lib+" library not found") # glob should take care
  datastr = subprocess.check_output(['otool','-l','-v', lib])
  data = datastr.split('\n') 
  for line in data:
    ll = line.split()
    if not ll: continue
    if (ll[0] == 'name' and ll[1].endswith('.dylib') and not ll[1].startswith('/')):
      libname = ll[1].split('/')[-1]
      if os.path.isfile(ABSPATH+libname):  
        cmd.append(PREFIX+ll[1]+" "+ABSPATH+libname+' '+lib)
      else:
        err.append(ABSPATH+libname+" does not exist, hence can't correct: "+ll[1]+" in: "+lib)

ohandle = open("rpathChangeCmd.txt", 'w')
for lib in cmd:
  ohandle.write(lib+'\n')
ohandle.close()

if err:
  ehandle = open("rpathChangeErr.txt", 'w')
  for e in err:
    ehandle.write(e+'\n')
  ehandle.close()