# 如何在Apple Silicon M1中安裝oracle-instant-client及oci8(for php) ###### tags: `教學` `oracle-instant-client` `oci8` ## Prerequirements:penguin: * 擁有一台M1 Mac:computer: * 安裝XAMPP:pig2: * php版本: PHP 8.1(如果不是8.1,OCI8要找到對應的版本) ## 安裝oracle-instant-client > 說明: https://developers.ascendcorp.com/how-to-install-oracle-instant-client-on-apple-silicon-m1-24b67f2dc743 - 去這裡 - https://www.oracle.com/database/technologies/instant-client/macos-intel-x86-downloads.html - 下載 - Basic Package (ZIP) - SQL*Plus Package (ZIP) - SDK Package (ZIP) - 解壓縮並全部丟到同一個資料夾 - 資料夾名稱: instantclient_19_8 - 路徑: /Applications/XAMPP/xamppfiles/lib/instantclient_19_8 - 文章中step 5和step 6不確定要不要做 - 原本有做,但後來刪除了,只保留上面的步驟而已 - 尚未完全確定重新開機後,oci8是否還正常 ## 安裝Rosetta 2 ```bash= #install rosetta /usr/sbin/softwareupdate --install-rosetta #You can also skip the license agreement by providing an additional flag: /usr/sbin/softwareupdate --install-rosetta --agree-to-license ``` ## 透過rosetta啟動x86 bash https://blog.davy.tw/posts/tips-on-m1-mac/ ``` arch -arch x86_64 bash ``` ## 透過x86 bash安裝oci8 https://stackoverflow.com/questions/63627619/how-to-setup-oracle-instant-client-with-xampp-in-macos-mojave https://blogs.oracle.com/opal/post/installing-xampp-on-macos-for-php-and-oracle-database #### 下載安裝檔 - 去這裡:https://pecl.php.net/package/oci8 - 下載 oci8-3.2.1.tgz - 解壓縮 - cd進去(要進去兩層喔),到有一堆檔案那層 #### 安裝oci8 ```bash= cd oci8-2.2.0 /Applications/XAMPP/xamppfiles/bin/phpize ./configure --with-php-config=/Applications/XAMPP/xamppfiles/bin/php-config --with-oci8=shared,instantclient,/Applications/XAMPP/xamppfiles/lib/instantclient_19_8 make sudo make install ``` Then, tell XAMPP to enable the extension: ```bash= sudo /Applications/XAMPP/xamppfiles/xampp oci8 #會詢問問題,輸入/Applications/XAMPP/xamppfiles/lib/instantclient_19_8 ``` ## 解決蘋果安全性問題 可能是因為是自行build的套件,所以執行會跳安全性的框框,無法執行,透過以下方法強制執行,之後就不用點了 https://app.yipee.cc/92743/%E3%80%90macos-%E6%95%99%E5%AD%B8%E3%80%91%E6%95%99%E4%BD%A0%E8%A7%A3%E6%B1%BA%E3%80%8C%E7%84%A1%E6%B3%95%E6%89%93%E9%96%8B-xxx%EF%BC%8C%E5%9B%A0%E7%82%BA%E5%AE%83%E4%BE%86%E8%87%AA%E6%9C%AA%E8%AD%98/ ## 安裝完成 ### 附錄: php連線範例程式 需更改帳號密碼與IP ```php= <?php $username = "YourAccount"; $password = "YourPassword"; $oracle_db=" (DESCRIPTION= (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP) (HOST = Your IP) (PORT = 1521) ) ) (CONNECT_DATA= (SID = orcl) ) )"; $encode = "AL32UTF8"; $sql="select NAME from TEST"; $conn=oci_connect($username,$password, $oracle_db,$encode); if(!$conn){ echo "db access error: ".oci_error(); } else{ echo "db access success"."<br>"; try{ $stid=oci_parse($conn,$sql); oci_define_by_name($stid,'NAME',$name); oci_execute($stid); while(($row=oci_fetch_array($stid,OCI_ASSOC+OCI_RETURN_NULLS))){ echo "NAME: ".$name.'<br>'; } } catch(Exception $e){ echo$e->getMessage(); } if($stid){oci_free_statement($stid);} if($conn){oci_close($conn);} } ?> ``` --- <br><br><br><br><br><br> <br><br><br><br><br><br> ## 其他參考資料 https://gist.github.com/nasrulhazim/308c9f59a2ddba364b4b908ee9238265 ## 無關的東西 https://dev.to/deborahtrez/how-to-get-xampp-to-work-on-m1-macbook-pro-47gi https://stackoverflow.com/questions/19996077/data-from-oracle-in-utf-8-with-php#:~:text=solution.%20Only%20add%20%27-,AL32UTF8,-%27%20to%20line%20if arch -x86_64 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)" https://developers.ascendcorp.com/how-to-install-oracle-instant-client-on-apple-silicon-m1-24b67f2dc743