macOS Sierra + Homebrew + PHP 7 + OCI8安装

先这样安装PHP:

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

然后去Oracle官网下载InstantClient相关的三个包,一个是Basic,一个是SQLPlus,还有一个是SDK:

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文件会用到:

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下

然后安装OCI8:

pecl install oci8

在提示输入instantclient目录位置的地方,输入instantclient,/usr/local/instantclient,像这样:

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文件:

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

注意:extension后面一定要写pecl安装的oci8.so文件所在的绝对路径,我之前写了相对路径,加载失败

然后执行php -i | grep oci8来验证是否安装成功:

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就可以了

    原文作者:洪亮
    原文地址: https://segmentfault.com/a/1190000008224468
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞