MacBook安装Golang Oracle数据库驱动程序

能钟展
2023-12-01
Golang连接Oracle 需要安装Oracle Full Client或Instant Client

Oracle的Instant Client套件下载地址

#选择Instant Client for macOS (Intel x86)下载包如下:
instantclient-basic-macos.x64-19.8.0.0.0dbru.zip
instantclient-sdk-macos.x64-19.8.0.0.0dbru.zip
instantclient-sqlplus-macos.x64-19.8.0.0.0dbru.zip

#都解压到instantclient_19_8同一目录下
unzip instantclient-basic-macos.x64-19.8.0.0.0dbru.zip
unzip instantclient-sdk-macos.x64-19.8.0.0.0dbru.zip
unzip instantclient-sqlplus-macos.x64-19.8.0.0.0dbru.zip

#打印instantclient_19_8目录的路径
pwd
/Users/liang/software/oracle/instantclient_19_8
安装pkg-config

MacBook自带了pkg-config,这里不在安装

#打印pkg-config版本
pkg-config --version 
创建配置文件oci8.pc

vim /Users/liang/software/oracle/oci8.pc
oci8.pc文件内容如下:

prefixdir=/Users/liang/software/oracle/instantclient_19_8
libdir=${prefixdir}
includedir=${prefixdir}/sdk/include

Name: OCI
Description: Oracle database driver
Version: 19.8
Libs: -L${libdir} -lclntsh
Cflags: -I${includedir}
配置环境变量

我用的是zsh,没有使用zsh的,可以添加到~/.bashrc文件
vim ~/.zshrc
添加如下环境变量:

# Oracle的OCI套件
export LD_LIBRARY_PATH=/Users/liang/software/oracle/instantclient_19_8
# oci8.pc文件所在路径
export PKG_CONFIG_PATH=/Users/liang/software/oracle

使配置生效
source ~/.zshrc
运行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 /Users/liang/software/oracle/instantclient_19_8/libclntsh.dylib.19.1 /usr/local/lib/
注意

运行Golang时会提示 需要在系统偏好设置里–安全性和隐私 允许未知来源

 类似资料: