2019年2月23日 星期六

手動安裝 Python 相關套件及設定 cx_Oracle

下載 Python 2.7.15
wget https://www.python.org/ftp/python/2.7.15/Python-2.7.15.tgz
tar -zxvf Python-2.7.15.tgz -C /tmp/
cd /tmp/Python-2.7.15

自定安裝路徑: /usr/local/python2.7
./configure --prefix=/usr/local/python2.7 --enable-shared --enable-optimizations
執行 # /usr/local/python2.7/bin/python -V 時,會出現下列錯誤
# /usr/local/python2.7/bin/python -V
/usr/local/python2.7/bin/python: error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory
會發生這錯誤,主要是因為 libpython2.7.so.1.0 這支 library 沒找到,因為我們使用了自定義安裝,所以要解決這個問題很簡單,先找看看目前系統有沒有這檔案,這邊我使用 find 這個指令
# find / -name libpython2.7.so.1.0 -type f
/tmp/Python-2.7.15/libpython2.7.so.1.0
/usr/local/python2.7/lib/libpython2.7.so.1.0
接著我們確認一下  LD_LIBRARY_PATH 的相關路徑有哪些,並且把我們剛剛找到的路徑也給加進去就行了。
# echo $LD_LIBRARY_PATH
會發現是空值,再來我們就加入到自已的 ~/.bashrc or ~/.bash_profile,這邊我自已的習慣是加入到 ~/.bashrc
# echo 'export LD_LIBRARY_PATH=/usr/local/python2.7/lib:$LD_LIBRARY_PATH' >> ~/.bashrc
# source ~/.bashrc
加入後,再次執行 /usr/local/python2.7/bin/python -V
# /usr/local/python2.7/bin/python -V
Python 2.7.15
  • 手動安裝 setuptools  pip  wheel  cx_Oracle
因為要用 Python 去連 Oracle DB,需要使用 cx_Oracle,測試一下目前的 python 有沒有這個 cx_Oracle 模組
# /usr/local/python2.7/bin/python -c "import cx_Oracle"
Traceback (most recent call last):
  File "", line 1, in 
ImportError: No module named cx_Oracle
先下載這四個 Tarball 檔,請連至 PYPI 搜尋並下載
解壓縮,並請依序安裝
# unzip /tmp/setuptools-40.8.0.zip
# cd /tmp/setuptools-40.8.0
# /usr/local/python2.7/bin/python setup.py install

# tar -zxvf /tmp/pip-19.0.2.tar.gz
# cd /tmp/pip-19.0.2
# /usr/local/python2.7/bin/python setup.py install

# tar -zxvf /tmp/wheel-0.33.1.tar.gz
# cd /tmp/wheel-0.33.1
# /usr/local/python2.7/bin/python setup.py install

# tar -zxvf /tmp/cx_Oracle-7.1.1.tar.gz
# cd /tmp/cx_Oracle-7.1.1/
# /usr/local/python2.7/bin/python setup.py install
  • 設定安裝 Oracle Client
參考 cx_Oracle 7 Installation 這官方文件來做安裝
由於我的伺服器沒有對外的連線,所以只好使用 Oracle Instant Client Zip Files 的安裝方法,請至 Oracle Instant Client 去下載 Instant Client for Linux x86-64,這邊案例我是下載這個檔 instantclient-basic-linux.x64-18.3.0.0.0dbru.zip,版本為 18.3.0.0.0
  • 解壓縮,並放到指定路徑下
# cd /tmp
# unzip instantclient-basic-linux.x64-18.3.0.0.0dbru.zip
# cp -r /tmp/instantclient_18_3 /usr/local/
  • 安裝套件
# yum install libaio
  •  instantclient_18_3 加入到自已的 ~/.bashrc
# sed '/LD_LIBRARY_PATH/d' -i ~/.bashrc
# echo 'export LD_LIBRARY_PATH=/usr/local/python2.7/lib:/usr/local/instantclient_18_3:$LD_LIBRARY_PATH' >> ~/.bashrc
# source ~/.bashrc
  • 再次執行
# /usr/local/python2.7/bin/python -c "import cx_Oracle"
完成上述步驟後,執行連接 Oracle 的程式,會出現下面的錯誤
Traceback (most recent call last):
  File "/tmp/Connect_Oracle.py", line 1, in 
    con = cx_Oracle.connect('Account/OraclePassWord')
cx_Oracle.DatabaseError: DPI-1047: Cannot locate a 64-bit Oracle Client library: "/lib64/libc.so.6: version `GLIBC_2.9' not found (required by /usr/local/instantclient_18_3/libipc1.so)". See https://oracle.github.io/odpi/doc/installation.html#linux for help
上面這錯誤訊息是說,找不到 Oracle Client Library /lib64/libc.so.6 找不到版本 GLIBC_2.9
請執行 strings /lib64/libc.so.6 確認版本是否有支援
# strings /lib64/libc.so.6 | grep 'GLIBC_'
GLIBC_2.2.5
GLIBC_2.2.6
GLIBC_2.3
GLIBC_2.3.2
GLIBC_2.3.3
GLIBC_2.3.4
GLIBC_2.4
GLIBC_2.5
GLIBC_PRIVATE
觀察上面資訊後,確認沒有 `GLIBC_2.9 的版本,那就要去下載 Tarball 來安裝了。
找到自已所需要的版本
# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 5.7 (Tikanga)
確認 glibc 版本
# strings /lib64/libc.so.6 | grep 'GLIBC_'
GLIBC_2.2.5
GLIBC_2.2.6
GLIBC_2.3
GLIBC_2.3.2
GLIBC_2.3.3
GLIBC_2.3.4
GLIBC_2.4
GLIBC_2.5
GLIBC_PRIVATE
這裡要注意一下,要編譯前,請先執行 ls -la /lib64/libc.so.6
# ls -la /lib64/libc.so.6
lrwxrwxrwx 1 root root 11 Apr 21  2015 /lib64/libc.so.6 -> libc-2.5.so
先前不知道,就直接編輯下去了,結果重新要做軟連結時,會導致無法連進去伺服器,也不能執行任何指令,千萬不要重開機,如果真的不小心,執行指令有問題時,沒關係,請執行下面這個執行就可以了
# LD_PRELOAD=/lib64/libc-2.5.so ls
這樣就可以了,最後提醒一下,從這裡開始就要小心了~~~!!!!
先解壓縮
# cd /tmp/
# tar -zxvf /tmp/glibc-2.9.tar.gz
# cd glibc-2.9
# ./configure  --prefix=/usr --disable-profile --enable-add-ons --with-headers=/usr/include --with-binutils=/usr/bin
configure: error: you must configure in a separate build directory
這裡會出錯,請先在當前目錄中,建立一個 build 的目錄
# mkdir -p /tmp/glibc-2.9/build
# ../configure --prefix=/usr --disable-profile --enable-add-ons --with-headers=/usr/include --with-binutils=/usr/bin
...(省略)
checking LD_LIBRARY_PATH variable... contains current directory
configure: error:
*** LD_LIBRARY_PATH shouldn't contain the current directory when
*** building glibc. Please change the environment variable
*** and run configure again.
這邊又出一個錯誤,解法很簡單,執行 unset LD_LIBRARY_PATH,解法我是參考這篇 编译glibc(gcc)以及过程中遇到的一些错误
# unset LD_LIBRARY_PATH
再次編譯
# ../configure --prefix=/usr --disable-profile --enable-add-ons --with-headers=/usr/include --with-binutils=/usr/bin
# make
# make install
# ls -la /lib64/libc.so.6
lrwxrwxrwx 1 root root 11 Feb 23 15:52 /lib64/libc.so.6 -> libc-2.9.so
再次檢查 glibc 版本
# strings  /lib64/libc.so.6  | grep 'GLIBC_'
GLIBC_2.2.5
GLIBC_2.2.6
GLIBC_2.3
GLIBC_2.3.2
GLIBC_2.3.3
GLIBC_2.3.4
GLIBC_2.4
GLIBC_2.5
GLIBC_2.6
GLIBC_2.7
GLIBC_2.8
GLIBC_2.9
GLIBC_PRIVATE
再次執行連接 Oracle DB 的程式
# /usr/local/python2.7/bin/python /tmp/Connect_Oracle.py
/usr/local/python2.7/bin/python: error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory
會出現這錯誤,主要是我們剛剛在編譯時,有 unset LD_LIBRARY_PATH,導致 python 無法讀取到 libpython2.7.so.1.0,所以我們只要把 export LD_LIBRARY_PATH=/usr/local/python2.7/lib:/usr/local/instantclient_18_3:$LD_LIBRARY_PATH 執行一次就行了,或是直接執行 source ~/.bashrc 即可,先前有加進去了。
以上就是踩雷的過程,完工。

沒有留言:

張貼留言