星期日, 1月 15, 2012

PHP5.3, CentOS,裝Oci8, pdo_oci8

沒連過oracle,一開始要用還真搞不太懂
載了2個檔案,共2G回來,還是不會用
最後看只需載個client的basic,供oci連線即可
看完網路一裝教學,還是裝不起來,在交叉比對下,終於成功了~~~

os: CentOS 6.4
php: 5.3
download Instant Client Downloads
自己的環境是linux 64bit, 所以載了 Instant Client for Linux x86-64
  1. Install oracle instant client
    • oracle-instantclient11.2-basic-11.2.0.3.0-1.x86_64.rpm
    • oracle-instantclient11.2-devel-11.2.0.3.0-1.x86_64.rpm
    • oracle-instantclient11.2-sdk-11.2.0.1.0-1.x86_64.zip
    # rpm -ivh oracle-instantclient11.2-basic-11.2.0.3.0-1.x86_64.rpm
    # rpm -ivh oracle-instantclient11.2-devel-11.2.0.3.0-1.x86_64.rpm
  2. 設定ORACLE_HOME
    設定ORACLE_HOME的提示真是讓我暈倒,說要指到lib
    設好了,但在install oci時,會掛,最後試出只要到"HOME"就好了
    看下面設定 vi /etc/profile
    export ORACLE_HOME=/usr/lib/oracle/11.2/client64
    export ORACLE_OWNER=oracle
    export ORACLE_SID=ora1
    export LD_LIBRARY_PATH=$ORACLE_HOME/lib
    export PATH=$ORACLE_HOME/bin:$PATH
    export TNS_ADMIN=$ORACLE_HOME/network/admin
    export NLS_LANG="AMERICAN_AMERICA.AL32UTF8"
    啟動環境變數
    source /etc/profile
  3. 解壓sdk並把裡面的sdk裡的資料複製到/usr/include
    # unzip oracle-instantclient11.2-sdk-11.2.0.1.0-1.x86_64.zip 
    # cp -fr instantclient_11_2/sdk/include/* /usr/include
  4. install oci
    • Installing OCI8 from PECL 
      • Use 'pecl install oci8' to install for PHP 8.
      • Use 'pecl install oci8-2.2.0' to install for PHP 7.
      • Use 'pecl install oci8-2.0.12' to install for PHP 5.2 - PHP 5.6.
      • Use 'pecl install oci8-1.4.10' to install for PHP 4.3.9 - PHP 5.1.

      p.s. 基本上裝最新版即可,它有向下相融
            就這麼簡單,過程會要求輸入ORACLE_HOME的位置,如果第3步有做的話,就按enter即可 如果找不到pecl,就先安裝pear

      yum install php-pear
      #如果找不到的話,就先增加repository
      rpm -Uvh http://repo.webtatic.com/yum/centos/5/latest.rpm
      yum --enablerepo=webtatic install php-pear

  5. 設定php.ini
    vi /etc/php.ini , add the line extension=oci8.so
    再重開apache即可
    # service httpd restart

    這時下php -m 就會看到oci8了~ 開心
  6. 測試
    <?php
    $option = array(
        'ip'            => '10.0.3.20',
        'port'          => '15210',
        'account'       => 'web',
        'password'      => 'FEF00CB',
        'service_name'  => 'WXS4'
    );
    
        $db = "(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST={$option['ip']})(PORT={$option['port']}))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME={$option['service_name']})))";
    
        if(function_exists('ocilogon')){
            $c1 = ocilogon($option['account'],$option['password'],$db) or die('OCI Connect error, wrong connection string');
            echo 'OCI Connect pass. yes!!!';
        }else{
            echo 'ocilogon not exists, installment is not correct';
        }
有看到某些文章指出要安裝pdo,自己裝了也一直失敗
後來發現php5.3已經有了,所以不用裝了

全手動安裝

在公司封閉的網路,只能手動安裝可以參考這篇 PHP 5.3 連線 Oracle 的客戶端及 PDO_OCI 模組安裝

issue
  • pdo_oci unsupported oracle version 
由於oracle instantclient可能出了新版,而pdo_oci未更新,可以直接改pdo_oci/config.m4 ,自己加上要的版本,search 一下unsupported version字眼,就可以找到,詳細可參考這篇 

p.s. 改完記得要先phpize --clean,不然configure的內容不會生效哦!!!

沒有留言: