這兩天要實現一個用rust連接遠程的oracle數據庫的需求,所以就需要用rust連接oracle。
在github上面找到一個庫,地址:https://github.com/kubo/rust-oracle
直接使用時,發現報錯,打印報錯信息:
Err(DpiError(DbError { code: 0, offset: 3416999480, message: "DPI-1047: Cannot locate a 64-bit Oracle Client library: "libclntsh.so: cannot open shared object file: No such file or directory". See https://oracle.github.io/odpi/doc/installation.html#linux for help", fn_name: "dpiContext_createWithParams", action: "load library" }))
根據 https://oracle.github.io/odpi/doc/installation.html#linux 上的說明,大概查了一下,大概明白了。
這個rust-oracle不是直接連接遠程oracle的,而是需要在服務器上加載一個oracle客戶端的庫,然后調用這個庫才可以連接到oracle上。
本來是在macos m1上面開發的,但是m1是arm架構的,而官網只有x64架構的,所以就只能使用虛擬機上的ubuntu了。
下面是操作步驟:
1. sudo mkdir -p /opt/oracle #創建一個目錄來保存oracle客戶端文件
2. cd /opt/oracle #進入上面創建的這個目錄
3. wget https://download.oracle.com/otn_software/linux/instantclient/191000/instantclient-basic-linux.arm64-19.10.0.0.0dbru.zip #下載客戶端文件,我下載的是zip格式的,等下再解壓
4. unzip instantclient-basic-linux.arm64-19.10.0.0.0dbru.zip #解壓下載好的文件到當前目錄
解壓好之后,ls看一下,可以看到當前目錄多了一個目錄instantclient_19_10 這個目錄里面的文件就是需要的.so庫文件,剩下的任務就是要在rust里面能夠加載到這個庫.
//意思是把/opt/oracle/instantclient_19_10寫入到/etc/ld.so.conf.d/oracle-instantclient.conf文件,文件不存在則新建
5. sudo sh -c "echo /opt/oracle/instantclient_19_10 > /etc/ld.so.conf.d/oracle-instantclient.conf"
//用ldconfig 執行文件將 /etc/ld.so.conf(也就是/etc/ld.so.conf.d/文件夾下面的所有conf文件)的數據讀入高速緩存中,同時記錄到/etc/ld.so.cache文件中。
6.ldconfig
這樣的話,linux系統就能把客戶端庫加載到,然后rust-oracle就能連接到oracle了。
參考資料: