Golang连接Oracle 需要安装Oracle Full Client或Instant Client
#选择Instant Client for Linux x86-64下载包如下:
instantclient-basic-linux.x64-21.1.0.0.0.zip
instantclient-sqlplus-linux.x64-21.1.0.0.0.zip
instantclient-sdk-linux.x64-21.1.0.0.0.zip
#都解压到 instantclient_21_1 这个目录,直接解压会解压到instantclient_21_1目录
unzip instantclient-basic-linux.x64-21.1.0.0.0.zip
unzip instantclient-sdk-linux.x64-21.1.0.0.0.zip
unzip instantclient-sqlplus-linux.x64-21.1.0.0.0.zip
#打印instantclient_21_1目录的路径
pwd
/home/oracle/instantclient_21_1
安装pkg-config
CentOS自带了pkg-config,这里不在安装
#打印pkg-config版本
pkg-config --version
创建配置文件oci8.pc
vim /home/oracle/oci8.pc
oci8.pc文件内容如下:
prefix=/home/oracle/instantclient_21_1
libdir=${prefix}
includedir=${prefix}/sdk/include/
glib_genmarshal=glib-genmarshal
gobject_query=gobject-query
glib_mkenums=glib-mkenums
Name: oci8
Description: oci8 library
Libs: -L${libdir} -lclntsh
Cflags: -I${includedir}
Version: 21.1
配置环境变量
vim /etc/profile
在最后添加如下环境变量
# Oracle的OCI套件
export LD_LIBRARY_PATH=/home/oracle/instantclient_21_1
# oci8.pc文件所在路径
export PKG_CONFIG_PATH=/home/oracle
使配置生效
source /etc/profile
运行Golang连接Oracle报错一:
# github.com/mattn/go-oci8
In file included from ../github.com/mattn/go-oci8/c_helpers.go:3:0:
./oci8.go.h:1:17: fatal error: oci.h: No such file or directory
#include
^
compilation terminated.
是因为配置的环境变量,oci8.pc文件所在路径不对,找不到oci8.pc文件导致的
注意这个路径配置对
export PKG_CONFIG_PATH=/home/oracle
运行Golang连接Oracle报错二:
dyld: Library not loaded: @rpath/libclntsh.dylib.19.1
Referenced from: /var/folders/_f/n3km_s7j6_9f8qzpkf9w395h0000gn/T/go-build114883360/b001/exe/oracleoci8
Reason: image not found
添加软链接,解决报错
ln -s /home/oracle/instantclient_21_1/libclntsh.so.21.1 /usr/lib/libclntsh.so
ln -s /home/oracle/instantclient_21_1/libocci.so.21.1 /usr/lib/libocci.so
ln -s /home/oracle/instantclient_21_1/libociei.so /usr/lib/libociei.so
ln -s /home/oracle/instantclient_21_1/libnnz21.so /usr/lib/libnnz12.so