IT story

Python mysqldb : 라이브러리가로드되지 않았습니다 : libmysqlclient.18.dylib

hot-time 2020. 5. 27. 07:38
반응형

Python mysqldb : 라이브러리가로드되지 않았습니다 : libmysqlclient.18.dylib


방금 Mac OS 10.6에서 Python 2.7 용 mysqldb를 컴파일하고 설치했습니다. 가져 오는 간단한 테스트 파일을 만들었습니다.

import MySQLdb as mysql

먼저,이 명령은 빨간색 밑줄로 표시되고 정보에 "미해결 가져 오기"가 표시됩니다. 그런 다음 다음 간단한 파이썬 코드를 실행하려고했습니다.

import MySQLdb as mysql

def main():
    conn = mysql.connect( charset="utf8", use_unicode=True, host="localhost",user="root", passwd="",db="" )

if __name__ == '__main__'():
    main()

그것을 실행할 때 다음과 같은 오류 메시지가 나타납니다.

Traceback (most recent call last):
  File "/path/to/project/Python/src/cvdv/TestMySQLdb.py", line 4, in <module>
    import MySQLdb as mysql
  File "build/bdist.macosx-10.6-intel/egg/MySQLdb/__init__.py", line 19, in <module>
    \namespace cvdv
  File "build/bdist.macosx-10.6-intel/egg/_mysql.py", line 7, in <module>
  File "build/bdist.macosx-10.6-intel/egg/_mysql.py", line 6, in __bootstrap__
ImportError: dlopen(/Users/toom/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.6-intel.egg-tmp/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib
  Referenced from: /Users/toom/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.6-intel.egg-tmp/_mysql.so
  Reason: image not found

내 문제에 대한 해결책은 무엇입니까?

편집 : 실제로 라이브러리가 / usr / local / mysql / lib에 있음을 알았습니다. 그래서 나는 pydev 이클립스 버전을 어디에서 찾을 수 있는지 말해야합니다. 이것을 어디에 설정합니까?


라이브러리에 대한 심볼릭 링크를 만들어 문제를 해결했습니다.

실제 라이브러리는

/usr/local/mysql/lib

그런 다음 심볼릭 링크를 만들었습니다.

/usr/lib

명령을 사용하여 :

sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

다음과 같은 매핑을 갖습니다.

ls -l libmysqlclient.18.dylib 
lrwxr-xr-x  1 root  wheel  44 16 Jul 14:01 libmysqlclient.18.dylib -> /usr/local/mysql/lib/libmysqlclient.18.dylib

그거였다. 그 후 모든 것이 잘 작동했습니다.

편집하다:

MacOS El Capitan 이후 시스템 무결성 보호 (SIP, "루트리스"라고도 함)는에서 링크를 만들지 못하게합니다 /usr/lib/. 다음 지침 에 따라 SIP를 비활성화 할 수 있지만 /usr/local/lib/대신 링크를 만들 수 있습니다 .

sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/local/lib/libmysqlclient.18.dylib

내가 선호하는 방법은 응용 프로그램 실행 방법에 따라 실제로 범위에 있거나 없을 수있는 환경 변수로 재생하는 대신 실제로 라이브러리를 수정하는 것입니다. 이것은 실제로 매우 간단한 과정입니다.

먼저 오류 출력을보고 문제가있는 python 모듈이있는 위치를 확인하십시오.

ImportError : dlopen (/Library/Python/2.7/site-packages/_mysql.so, 2) : 라이브러리가로드되지 않음 : libmysqlclient.18.dylib 참조 : /Library/Python/2.7/site-packages/_mysql.so 이유 : 이미지를 찾을 수 없습니다

문제가되는 파일은 /Library/Python/2.7/site-packages/_mysql.so입니다.

다음으로, _mysql.so가 libmysqlclient.18.dylib를 찾아야한다고 생각하는 위치를 찾으십시오.

% otool -L /Library/Python/2.7/site-packages/_mysql.so
/Library/Python/2.7/site-packages/_mysql.so:
    libmysqlclient.18.dylib (compatibility version 18.0.0, current version 18.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 169.3.0)

따라서 경로 정보가없는 libmysqlclient.18.dylib를 찾고 있습니다.

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

이제 _mysql.so는 라이브러리의 전체 경로를 알고 있으며 환경 변수에 관계없이 모든 것이 작동합니다.

% otool -L /Library/Python/2.7/site-packages/_mysql.so                                                                                           
/Library/Python/2.7/site-packages/_mysql.so:
    /usr/local/mysql/lib/libmysqlclient.18.dylib (compatibility version 18.0.0, current version 18.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 169.3.0)

심볼릭 링크를 만드는 것이 아니라이 문제에 대한 또 다른 해결책이 있다는 것을 알았습니다.

libmysqlclient.18.dylib가있는 디렉토리의 경로를 DYLD_LIBRARY_PATH 환경 변수로 설정하십시오. 내가 한 일은 내 .bash_profile에 다음 줄을 넣는 것입니다.

export DYLD_LIBRARY_PATH=/usr/local/mysql-5.5.15-osx10.6-x86/lib/:$DYLD_LIBRARY_PATH

그게 다야.


제 경우에는 Mac OS X 10.9 Mavericks에서 오류가 발생했습니다. DMG의 Oracle / MySQL 웹 사이트에서 직접 MySQL 커뮤니티 서버를 설치했습니다.

내가해야 할 일은 lib 파일을 / usr / local / lib 디렉토리에 심볼릭 링크하는 것입니다.

mkdir -p /usr/local/lib   
ln -s /usr/local/mysql/lib/libmysql* /usr/local/lib

Bonus: If you're running Mac OS X as well, there is a great tool to finding files like the libmysqlclient.18.dylib file, http://apps.tempel.org/FindAnyFile. This is how I originally found the location of the dylib file.


I found putting this in your .profile or .bashrc (whichever you use) is the easiest way to do it, sym links are messy compared to keeping paths in your source files.

Also compared to yoshisurfs answer, most of the time when mysql gets installed the mysql directory should be renamed to just mysql, not the whole file name, for ease of use.

export DYLD_LIBRARY_PATH=/usr/local/mysql/lib:$DYLD_LIBRARY_PATH

I've run into this with a couple virtual environments.

pip uninstall MySQL-python
pip install -U MySQL-python

Worked both times.


In pydev eclipse plugin, you may want to set the environment variable for DYLD. The path can be set as shown in

Install mysqldb on snow leopard


For those using homebrew you might fix this with:

$ brew link mysql

when you are in El Capitan, will get error: ln: /usr/lib/libmysqlclient.18.dylib: Operation not permitted need to close the "System Integrity Protection".

first, reboot and hold on cmd + R to enter the Recovery mode, then launch the terminal and type the command: csrutil disable, now you can reboot and try again.


On new El Capitan installation where SIP(rootless prevents access to usr/lib/) is on by default and you cannot create the symlink unless you are in recovery mode. As @yannisxu said you can disable SIP and do your symlink to /usr/lib/local and this will work.

you can use the following command on MAC OSX El Capitan instead of turning off SIP:

sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/local/lib/libmysqlclient.18.dylib

There used to be an option where you can login as root and this can disable SIP but in the final release that is now obsolete, you can read more about it here: https://forums.developer.apple.com/thread/4686

Question:

There is a nvram boot-args command available in Developer Beta 1 which can disable SIP when run with root privileges:

nvram boot-args="rootless=0"

Will this option of disabling SIP also be available in the El Capitan release version? Or is this strictly for the Developer Builds?

Answer:

This nvram boot-args command will be going away. It will not be available in the El Capitan release version and may disappear before the end of the Developer Betas. Keep an eye on the release notes for future Developer Betas.


In my Case, in El Capitan (OSX 10.11), I have to do following in ~/.bash_profile

export DYLD_LIBRARY_PATH="/usr/local/mysql/lib:${DYLD_LIBRARY_PATH}"
export PATH="/usr/local/mysql/lib:${PATH}"

I had this issue and it took me for a while to figure out how to fix that.

My case is slightly different. My MySQL server is of version 5.1.x. And somehow I upgraded my MySQL-python from 1.2.3 to 1.2.5. And I kept getting this issue since then event I added the following soft link.

libmysqlclient.18.dylib -> /usr/local/mysql/lib/libmysqlclient.18.dylib

It turns out that for MySQL 5.1.x there is no libmysqlclient.18.dylib, but only libmysqlclient.16.dylib. You can fix this issue either by downgrade your MySQL-python to 1.2.3 or upgrade your MySQL server to 5.6.x (I haven't tried 5.5.x.)

I downgraded the library to 1.2.3 since upgrading MySQL is not an option for me.


go to http://dev.mysql.com/downloads/connector/c/ and download MySQL Connector/C. after getting the package, make a new directory 'mysql', uncompress the Mysql Connector file under directory mysql, then under mysql, make another empty directory 'build'.we will use 'build' to build MySQL Connector/C. cd build && cmake ../your-MySQL-Connector-source-dir make && make install after make install, you will get a directory named mysql under /usr/local. it contains all the headers and libs you need.go to this dirctory, and copy the headers and libs to corresponding locations.


you can try:

sudo install_name_tool -change libmysqlclient.18.dylib /Users/toom/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.6-intel.egg-tmp/_mysql.so`

Note about bug of MySQL Connector/C on macOS (my current version is 10.13.2), fix the mysql_config and reinstall mysqlclient or MySQL-python, here is the detail

참고URL : https://stackoverflow.com/questions/6383310/python-mysqldb-library-not-loaded-libmysqlclient-18-dylib

반응형