ocilib庫踩坑記

1.oracle帳戶與下載html

http://www.javashuo.com/article/p-opzbwrep-cu.htmllinux

https://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.htmlgit

本文下載了12.2.0.1.0版本的basic\devel\sqlplus包。github

2.oci環境搭建(ubuntu虛擬機中搭建oci開發環境)sql

http://www.javashuo.com/article/p-zwwyyeel-u.htmldocker

執行dpkg -i *.deb安裝oci環境,數據庫

inclue文件位於:/usr/include/oracle/12.2/client64ubuntu

ORACLE_HOME位於:/usr/lib/oracle/12.2/client64centos

3.ocilib編譯安裝(ubuntu虛擬機中編譯ocilib)bash

https://github.com/vrogier/ocilib

https://www.linuxidc.com/Linux/2016-05/131178.htm

./configure --with-oracle-lib-path=/usr/lib/oracle/12.2/client64/lib --with-oracle-headers-path=/usr/include/oracle/12.2/client64

make

sudo make install

4.ocilib測試(待定至最後一步)

#include "ocilib.h"

void err_handler(OCI_Error *err)
{
printf("%s\n", OCI_ErrorGetString(err));
}
int main(int argc, char *argv[])
{
    OCI_Connection* cn;
    OCI_Statement* st;
    OCI_Resultset* rs;
    // init with err_handler
    OCI_Initialize(err_handler, NULL, OCI_ENV_DEFAULT);
    // connect
    cn = OCI_ConnectionCreate("172.17.216.38:1521/rmbtbssvc", "MD", "MD", OCI_SESSION_DEFAULT);
// version
printf("Server major version : %i\n", OCI_GetServerMajorVersion(cn));
printf("Server minor version : %i\n", OCI_GetServerMinorVersion(cn));
printf("Server revision version : %i\n\n", OCI_GetServerRevisionVersion(cn));
printf("Connection version : %i\n\n", OCI_GetVersionConnection(cn));
// select
st = OCI_StatementCreate(cn);
    OCI_ExecuteStmt(st, "select intcol, strcol from table");
    rs = OCI_GetResultset(st);
    while (OCI_FetchNext(rs))
    {
        printf("%i - %s\n", OCI_GetInt(rs, 1), OCI_GetString(rs, 2));
    }
    // clean up
    OCI_Cleanup();
    return EXIT_SUCCESS;
} 

編譯:

gcc -o main ocilib-main.c -I/usr/include/oracle/12.2/client64 -L/usr/lib/oracle/12.2/client64/lib -lclntsh -locilib

5.在虛擬機centos中,藉助docker安裝oracle

https://docs.docker.com/install/linux/docker-ce/centos/#install-docker-ce-1

啓動docker,sudo systemctl start docker
測試docker,sudo docker run hello-world

https://my.oschina.net/u/3446722/blog/983745

https://my.oschina.net/Cleverdada/blog/801124

(1)下載oracle

docker pull wnameless/oracle-xe-11g(docker pull sath89/oracle-12c)

(2)查看鏡像

docker images

(3)開啓oracle(返回惟一標識符)

sudo docker run -d -v /home/centos/tmp/oracle:/data/oracle_data -p 8080:8080 -p 1521:1521 -e ORACLE_ALLOW_REMOTE=true wnameless/oracle-xe-11g

docker run :建立一個新的容器並運行一個命令。-d: 後臺運行容器,並返回容器ID;-p:主機與容器的端口映射(主機ip:容器ip);-v:主機與容器的目錄映射。

(4)查看docker運行線程(查詢出pid值,類hash的值)

docker ps

(5)結束docker進程(container id做爲參數)

docker stop 15dec6104665

(6)查看日誌(增長惟一標識符)

docker logs -f 08945b36818d80a9e466b96bf2a1d918c1d7fa9b30dacb9f6fa0f1abcc87d00d

(7)進入剛剛建立的container中

docker exec -it 08945b36818d /bin/bash

在此container中鏈接oracle(便可進入SQL提示符字面)

su oracle

$ORACLE_HOME/bin/sqlplus / as sysdba

執行SQL語句

SQL>conn system/oracle as sysdba;

SQL>create table t (id number);

SQL>insert into t values (100);

SQL>select * from t;

(8)安裝的oracle的默認參數

 
hostname: localhost
port: 1521
sid: xe
service name: xe.oracle.docker
username: system
password: oracle

  

6.在虛擬機ubuntu中,藉助sqlplus遠程鏈接centos的oracle
https://blog.csdn.net/zhangzl1012/article/details/50814545
(1)首先定位到centos下docker內的oracle,並查看遠程數據庫的ip、port、username、password,以及service_name。
sqlplus / as sysdba;
SQL>show parameter service_name;
(2)定位到ubuntu中(預先裝好client端oracle),執行遠程鏈接,若是測試成功則可進一步使用plsql了。
cd $ORACLE_HOME/bin
./sqlplus system/oracle@192.168.89.131:1521/XE

  

7.plsql遠程鏈接oracle
(1)下載instantclient_11_2.zip並解壓
(2)進入C:\instantclient_11_2,新建network/admin/tnsnames.ora
CENTOS-ORACLE = 
 (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.89.131)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = XE)
    )
  )

(3)下載安裝plsql,輸入用戶名system、密碼oracle,數據庫選擇CENTOS-ORACLE,鏈接選擇Normal

(4)進入,開啓SQL window,執行建表和插入數據

create table ocilib_table( id number, name varchar2(100));

insert into ocilib_table values (20, 'twenty');
insert into ocilib_table values (30, 'thirty');
insert into ocilib_table values (50, '五十');

select * from ocilib_table;

drop table ocilib_table;

(5)回到ubuntu開發環境,從新修改ocilib示例代碼(展現部分行)

cn = OCI_ConnectionCreate("192.168.89.131:1521/XE", "system", "oracle", OCI_SESSION_DEFAULT);

OCI_ExecuteStmt(st, "select id, name from ocilib_table");

 8.編寫複雜的ocilib邏輯代碼

https://en.wikipedia.org/wiki/OCILIB

https://vrogier.github.io/ocilib/doc/html/index.html

https://github.com/vrogier/ocilib

 

 

Enjoy it!

相關文章
相關標籤/搜索