macOS Sierra + Homebrew + PHP 7 + OCI8安裝

先這樣安裝PHP:php

brew install php71 --with-gmp --with-homebrew-curl --with-homebrew-libxml2 --with-imap --with-pear

而後去Oracle官網下載InstantClient相關的三個包,一個是Basic,一個是SQLPlus,還有一個是SDK:sql

instantclient-basic-macos.x64-11.2.0.3.0
instantclient-sqlplus-macos.x64-11.2.0.3.0
instantclient-sdk-macos.x64-11.2.0.3.0

下載後把這三個包裏的內容都解壓放到一個目錄下,假設是/usr/local/instantclient,而後創建幾個符號連接,以後的編譯和最後生成的oci8.so文件會用到:macos

ln -s /usr/local/instantclient/libclntsh.dylib.11.1 /usr/local/instantclient/libclntsh.dylib
ln -s /usr/local/instantclient/libclntsh.dylib.11.1 /usr/local/lib/
ln -s /usr/local/instantclient/libnnz11.dylib /usr/local/lib/
ln -s /usr/local/instantclient/libocci.dylib.11.1 /usr/local/lib/
ln -s /usr/local/instantclient/libociei.dylib /usr/local/lib/

注意:其中第一個符號連接若是不創建,編譯的時候會出錯。中間兩個符號連接若是不創建,oci8.so會加載失敗,提示找不到對應的dylib文件。後面兩個符號連接若是不創建,從此在執行PHP的過程當中oci_connect()函數會拋異常。若是從此依然遇到其餘失敗的信息,則以此類推,將instantclient目錄下的dylib文件都在/usr/local/lib下創建符號連接便可。若是想簡單粗暴一些,那也能夠把instantclient下的dylib文件都拷貝到/usr/local/lib下php7

而後安裝OCI8:curl

pecl install oci8

在提示輸入instantclient目錄位置的地方,輸入instantclient,/usr/local/instantclient,像這樣:ide

downloading oci8-2.1.3.tgz ...
Starting to download oci8-2.1.3.tgz (191,920 bytes)
.........................................done: 191,920 bytes
10 source files, building
running: phpize
Configuring for:
PHP Api Version:         20160303
Zend Module Api No:      20160303
Zend Extension Api No:   320160303
Please provide the path to the ORACLE_HOME directory. Use 'instantclient,/path/to/instant/client/lib' if you're compiling with Oracle Instant Client [autodetect] : instantclient,/usr/locall/instantclient

而後pecl就會編譯並安裝oci8.so文件。最後可能會遇到這個錯誤:函數

Build process completed successfully
Installing '/usr/local/Cellar/php71/7.1.1_12/lib/php/extensions/no-debug-non-zts-20160303/oci8.so'

Fatal error: Uncaught Error: Call to a member function getFilelist() on null in /usr/local/Cellar/php71/7.1.1_12/lib/php/PEAR/Command/Install.php:747
Stack trace:
#0 /usr/local/Cellar/php71/7.1.1_12/lib/php/PEAR/Command/Common.php(270): PEAR_Command_Install->doInstall('install', Array, Array)
#1 /usr/local/Cellar/php71/7.1.1_12/lib/php/pearcmd.php(316): PEAR_Command_Common->run('install', Array, Array)
#2 /usr/local/Cellar/php71/7.1.1_12/lib/php/peclcmd.php(31): require_once('/usr/local/Cell...')
#3 {main}
  thrown in /usr/local/Cellar/php71/7.1.1_12/lib/php/PEAR/Command/Install.php on line 747

不去管它,在/usr/local/etc/php/71/conf.d/目錄下手工建一個ext-oci8.ini文件:php-fpm

[oci8]
extension="/usr/local/Cellar/php71/7.1.1_12/lib/php/extensions/no-debug-non-zts-20160303/oci8.so"

注意:extension後面必定要寫pecl安裝的oci8.so文件所在的絕對路徑,我以前寫了相對路徑,加載失敗fetch

而後執行php -i | grep oci8來驗證是否安裝成功:ui

hongliang@macmini2012 ~ $ php -i | grep oci8
/usr/local/etc/php/7.1/conf.d/ext-oci8.ini,
oci8
oci8.connection_class => no value => no value
oci8.default_prefetch => 100 => 100
oci8.events => Off => Off
oci8.max_persistent => -1 => -1
oci8.old_oci_close_semantics => Off => Off
oci8.persistent_timeout => -1 => -1
oci8.ping_interval => 60 => 60
oci8.privileged_connect => Off => Off
oci8.statement_cache_size => 20 => 20

而後重啓php-fpm:

brew services restart php71

而後隨便寫一個php文件,輸出phpinfo()內容,若是看到oci8相關的參數,那就說明安裝成功了

另外,若是訪問Oracle數據發生could not generate unique server group name錯誤,則:

sudo vi /etc/hosts

而後加入:

127.0.0.1 hostname

Oracle要求本機的hostname必須能夠正確指向127.0.0.1本機IP。若是不肯定本機hostname是多少,則在命令行裏敲hostname就能夠了

相關文章
相關標籤/搜索