f8ac4e7edc62ab24560406d2ac7134f7.png

项目需求golang连接Orale数据库,使用mattn/go-oci8包,github地址

https://github.com/mattn/go-oci8
过程
  • Oracle Client和SDK
oracle官网下载https://www.oracle.com/database/technologies/instant-client/linux-x86-64-downloads.html[root@yunxuntech Soft]# ls -l oracle-instantclient12.2-* instantclient-*-rwxrwxrwx 1 root root   674743 Mar  1 16:05 instantclient-sdk-linux.x64-12.2.0.1.0.zip-rwxrwxrwx 1 root root 52826628 Mar  1 15:14 oracle-instantclient12.2-basic-12.2.0.1.0-1.x86_64.rpm-rwxrwxrwx 1 root root   606864 Mar  1 14:43 oracle-instantclient12.2-devel-12.2.0.1.0-1.x86_64.rpm客户端路径ORACLE_HOME=[root@yunxuntech ~]# ls -l /usr/lib/oracle/12.2/client64/total 0drwxr-xr-x 1 root root 4096 Mar  1 18:32 bindrwxr-xr-x 1 root root 4096 Mar  1 18:34 lib
  • mattn/go-oci8

GitHub下载放入$GOPATH/src

[root@yunxuntech ~]# ls -l $GOPATH/src/github.com/mattntotal 0drwxrwxrwx 1 root root 4096 Mar  1 19:04 go-oci8
  • 用户环境变量

用户环境变量需要添加PKG_CONFIG_PATH、ORACLE_HOME、LD_LIBRARY_PATH

export GOROOT=/mnt/d/Soft/goexport GOPATH=/mnt/d/Projects/yunmonexport PKG_CONFIG_PATH=/mnt/d/Projects/yunmonexport ORACLE_HOME=/usr/lib/oracle/12.2/client64export LD_LIBRARY_PATH=/usr/lib/oracle/12.2/client64/lib

PKG_CONFIG_PATH的路径可以自行指定,需要在该路径下添加一个oci8.pc文件,用于指定Oracle客户端的lib路径和sdk的include路径,go-oci8需要用到sdk的oci文件。

oci8.pc文件的内容

prefix=/usrincludedir=${prefix}/local/instantclient_12_2/sdk/include    //sdk路径下面的include文件夹libdir=${prefix}/lib/oracle/12.2/client64/lib  Name: oci8Description: Oracle instantclientVersion: 12.2Cflags: -I${includedir}Libs: -L${libdir} -lclntsh
40331c2a7bbaf4f43a0c3a1ece0f6f05.png

测试环境oci等lib是否正常

[root@yunxuntech yunmon]# pkg-config --cflags oci8 --libs-I/usr/local/instantclient_12_2/sdk/include  -L/usr/lib/oracle/12.2/client64/lib -lclntsh
脚本测试
b05feeb96ad3a6d9392f2fa2d1113d7a.png
[root@yunxuntech t7]# go run t7.goThe user SYS status is OPENThe user SYSTEM status is OPENThe user OUTLN status is EXPIRED & LOCKEDThe user MGMT_VIEW status is EXPIRED & LOCKEDThe user FLOWS_FILES status is EXPIRED & LOCKED

脚本正常工作,操作数据库正常。

编译后配置好Oracle客户端即可。