20170506|CentOS7中PHP连接ORACLE数据库的配置

【注意】最后更新于 May 6, 2017,文中内容可能已过时,请谨慎使用。

本次安装所需文件下载: https://pan.baidu.com/s/1gfeg2bx 密码: kdna

  1.  前期准备工作
    • yum -y install gcc #安装编译器
    • yum install php-devel #解决打开phpize时提示不存在的错误
  2. 前往ORACLE官网下载对应的客户端
    • 本例中使用的为10G版本的客户端(使用11G客户端安装后编译插件失败)
      • oracle-instantclient-basic-10.2.0.5-1.x86_64.rpm
      • oracle-instantclient-devel-10.2.0.5-1.x86_64.rpm
    • 下载PHP拓展文件
  3. 安装Oraclecient
    • rpm -qpl 可以查看rpm包会在哪些路径安装文件
    • 安装rpm包
      • rpm -ivh oracle-instantclient-basic-10.2.0.5-1.x86_64.rpm
      • rpm -ivh oracle-instantclient-devel-10.2.0.5-1.x86_64.rpm
    • 调试时可供参考的命令
      • rpm -qa | grep oracle //查看oracle是否安装
      • rpm -qa //查看所有已安装的人rpm包
      • rpm -e *.rpm //卸载已安装的rpm包
      • rpm -ivh –force *.rpm //强制安装rpm包
    • 配置
      • 修改/etc/ld.so.conf 或在ld.so.conf.d文件夹下添加oracle-x86_64.conf文件,写入安装oracle客户端的lib路径
        • vi /etc/ld.so.conf
        • /usr/lib/oracle/10.2.0.5/client64/lib/
        • (或者echo ‘/usr/lib/oracle/11.2/client64/lib/’ > /etc/ld.so.conf.d/oracle-x86_64.conf)
      • 64位系统需要创建32位的软链接(这里可能是一个遗留bug,不然后面编译会出问题)
        • ln -s /usr/lib/oracle/10.2.0.5/client64 /usr/lib/oracle/10.2.0.5/client
        • ln -s /usr/include/oracle/10.2.0.5/client64 /usr/include/oracle/10.2.0.5/client
      • 定义环境变量
        • vi etc/profile
        • 加入以下几行
          • export ORACLE_HOME=/usr/lib/oracle/10.2.0.5/client64/
          • export LD_LIBRARY_PATH=/usr/lib/oracle/10.2.0.5/client64:/usr/lib/oracle/10.2.0.5/client64/lib:
          • export NLS_LANG=“AMERICAN_AMERICA.AL32UTF8”
        • 使环境配置立即生效
          • source /etc/profile
  4. 安装OCI8
    • 如果需要 DTRACE
      • yum install systemtap-sdt-devel
      • export PHP_DTRACE=yes
    • 如果不需要 DTRACE
      • modify the file ‘php_oci8_int.h’, change the 48th line
      • #include “oci8_dtrace_gen.h” to #undef HAVE_OCI8_DTRACE
    • 编译安装
      • tar -xvf oci8-2.1.4.tgz
      • cd oci8-2.1.4
      • /usr/bin/phpize
      • ./configure –with-oci8=shared,instantclient,/usr/lib/oracle/10.2.0.5/client64/lib
      • make
      • make install
    • 在/etc/php.d目录下增加配置文件20-oci8.ini
      • extension=oci8.so
    • 查看是否成功
      • php -i | grep oci8
    • 查看到如下内容即可
      • /etc/php.d/oci8.ini,
      • oci8.default_prefetch => 100 => 100
      • oci8.events => On => On
      • 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 => On => On
      • oci8.statement_cache_size => 20 => 20
  5. 安装PDO_OCI
    • 解压安装文件
      • tar -xvf PDO_OCI-1.0.tgz
      • cd PDO_OCI-1.0
    • 防止pdo_oci对oracle11支持不足(pdo_oci可能不支持oracle11g,需要做个软链接成作为oracle10版本才能编译过去) *本次安装直接使用10g版本,不需要进行这个步骤
      • ln -s /usr/include/oracle/11.2 /usr/include/oracle/10.2.0.1
      • ln -s /usr/lib/oracle/11.2 /usr/lib/oracle/10.2.0.1
      • 还可以
        • Inside the PDO_OCI-1.0 folder, edit the file named config.m4.

          Find a pattern like this near line 10 and add these 2 lines:

          elif test -f $PDO_OCI_DIR/lib/libclntsh.$SHLIB_SUFFIX_NAME.11.2; then

          PDO_OCI_VERSION=11.2

          Find a pattern like this near line 101 and add these lines:

          11.2)

          PHP_ADD_LIBRARY(clntsh, 1, PDO_OCI_SHARED_LIBADD)

          ;;

    • 编译时还可能遇到其他问题(pdo_oci.c:34: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘\__attribute__’ before ‘pdo_oci_functions’)
      • 在pdo_oci.c文件中,将 function_entry 改成 zend_function_entry
    • 编译安装
      • /usr/bin/phpize
      • ./configure –with-pdo-oci=instantclient,/usr,10.2.0.5
      • make
      • make install
    • 引用拓展
      • 在 /etc/php.d 目录下 新建文件 30-pdo_oci.ini写入
        • extension=pdo_oci.so
    • 查看是否成功
      • php -i | grep oci
      • 查找是否有如下内容
        • /etc/php.d/pdo_oci.ini,
        • PDO drivers => oci, odbc, sqlite
  6. 运行phpinfo()
  7. 注意事项

也可以看看