Mac OS 安裝mysqlclient 遇到的坑~

 最近在學習Python, 因爲Django連接mysql 需要安裝mysqlclient, 但Mac安裝遇到各種問題,這裏記錄一下,避免以後再踩坑。

1.   正常情況下,安裝mysqlclient ,只要執行命令: pip install mysqlclient 即可。

     但Mac如果沒有安裝過mysql驅動, 會提示如下報錯: mysql_config: command not found !!!

2. 查閱官網: https://pypi.org/project/mysqlclient/

   官網介紹到,安裝mysqlclient 之前,需要先安裝mysql-connector-c

 

    執行命令安裝mysql-connector-c

brew install mysql-connector-c

    如果安裝過程中出現如下pkg-config類似錯誤,     那麼說明需要安裝pkg-config

error: The pkg-config script could not be found or is too old.  Make sure it is in your PATH or set the PKG_CONFIG environment variable to the full path to pkg-config.

     下面是到官網下載pkg-config 並安裝, 如果沒有出現上面pkg-config的錯誤, 可以跳過這一步, 直接到第3步即可

      pkg-config 官網鏈接: https://www.freedesktop.org/wiki/Software/pkg-config/

    

      點擊version 0.29.2 進行下載, 下載完成後解壓,進入目錄順序執行下面3步。

./configure

make

make install

      如果不幸, 在執行第一步的時候就出現如下錯誤:

configure: error: Either a previously installed pkg-config or "glib-2.0 >= 2.16" could not be found. Please set GLIB_CFLAGS and GLIB_LIBS to the correct values or pass --with-internal-glib to configure to use the bundled copy.

    則可把第一步的命令改成

./configure --with-internal-glib

    安裝成功後,建議重啓電腦(我沒重啓電腦前,還是沒法安裝mysql-connector-c

3. 安裝mysql-connector-c成功後, 根據官方說明, 這東西在MacOS中居然有bug!!

 

     還好, 官網說明這東西還有解決版本,就是修改mysql_cofig配置文件,如果你不知道mysql_config在哪裏, 可以執行下面命令查找

which mysql_config

     然後修改mysql_config 裏面的112行, 不過which mysql_config 查找處理的文件可能只是mysql_config的一個鏈接(macOS 俗稱替身, 無法直接修改)

 所以我們要找到它的原身。 例如你which mysql_config 找到的路徑爲: /usr/local/bin/mysql_config

    那麼我們cd 到該路徑 /usr/local/bin/ 下, 然後執行 ls -l 查看文件信息

     可以看到mysql_config 的真實路徑是在../Cellar/mysql-connector-c/6.1.11/bin/mysql_config

     這樣我們就可以找到它,再進行如下修改了:

Change

# on macOS, on or about line 112:
# Create options
libs="-L$pkglibdir"
libs="$libs -l "

to

# Create options
libs="-L$pkglibdir"
libs="$libs -lmysqlclient -lssl -lcrypto"

      修改成功後,這時候我們就可以執行安裝mysqlclient了

pip install mysqlclient

 

   

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章