MySQLdb操做mysql數據庫的兩個細節

1、安裝python

下載最新的mysqlclient-1.3.10.tar.gz解壓後:mysql

若是是apt方式安裝mysql(甚至不需下載源碼直接pip install mysqlclient),只需執行便可:linux

sudo python setup.py install

若是是源碼編譯,或二進制包安裝,假設安裝目錄爲:/opt/mysqlsql

一、修改mysqlclient-1.3.10/site.cfgbootstrap

# The path to mysql_config.
# Only use this if mysql_config is not on your PATH, or you have some weird
# setup that requires it.
#mysql_config = /usr/local/bin/mysql_config

改成:oracle

# The path to mysql_config.
# Only use this if mysql_config is not on your PATH, or you have some weird
# setup that requires it.
#mysql_config = /usr/local/bin/mysql_config
mysql_config = /opt/mysql/bin/mysql_config

2.安裝:ui

python setup.py build 
sudo python setup.py install

2、環境中動態連接庫搜索路徑中要有/opt/mysql/lib否則會有以下錯誤:this

$ python mysqltest1.py
Traceback (most recent call last):
  File "mysqltest1.py", line 2, in <module>
    import MySQLdb
  File "build/bdist.linux-x86_64/egg/MySQLdb/__init__.py", line 19, in <module>
  File "build/bdist.linux-x86_64/egg/_mysql.py", line 7, in <module>
  File "build/bdist.linux-x86_64/egg/_mysql.py", line 6, in __bootstrap__
ImportError: libmysqlclient.so.20: cannot open shared object file: No such file or directory

解決:.net

export  LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/mysql/lib
sudo ldconfig -v

若是是pycharm在運行/調試配置窗口設置environment variable,參考pycharm不能訪問oracle(sybase)的問題調試

相關文章
相關標籤/搜索