Golang 连接 Oracle

· · 1446 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

我们使用: https://github.com/rana/ora

原本我们使用下面的命令就可以装好:

go get gopkg.in/rana/ora.v4

但因为墙的原因,无法下载,这时可以到github.com中直接把包下载下来,然后再安装:

cd $GOPATH/src/gopkg.in/rana
git clone https://github.com/rana/ora.git ora.v4
cd ora.v4
git checkout -b v4.1.15 v4.1.15

然后再如下操作:

git install gopkg.in/rana/ora.v4

这时可能会缺一些依赖包,报如下错误:

[dbrpcsrv@rabbitmq01 ora.v4]$ go install gopkg.in/rana/ora.v4
num/ocinum.go:18:2: cannot find package "github.com/pkg/errors" in any of:
    /usr/local/go/src/github.com/pkg/errors (from $GOROOT)
    /home/dbrpcsrv/gopath/src/github.com/pkg/errors (from $GOPATH)
conn.go:12:2: cannot find package "golang.org/x/sync/errgroup" in any of:
    /usr/local/go/src/golang.org/x/sync/errgroup (from $GOROOT)
    /home/dbrpcsrv/gopath/src/golang.org/x/sync/errgroup (from $GOPATH)

需要把这几个依赖包装上:

cd $GOPATH/src/golang.org/x
git clone https://github.com/golang/sync.git sync
go install sync

go get github.com/pkg/errors

这时因为没有装oracle的客户端库,需要把客户端库装上,到
http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html
下载:

  • oracle-instantclient12.2-basic-12.2.0.1.0-1.x86_64.rpm
  • oracle-instantclient12.2-sqlplus-12.2.0.1.0-1.x86_64.rpm

然后安装:

rpm -ivh oracle-instantclient12.2-basic-12.2.0.1.0-1.x86_64.rpm
rpm -ivh oracle-instantclient12.2-sqlplus-12.2.0.1.0-1.x86_64

安装完后,驱动程序还不知道这些库在什么路径下,这个路径是靠$GOPATH/gopkg.in/rana/ora.v4路径下的contrib/oci8.pc文件指定的,按官方文档:

cp -aL contrib/oci8.pc /usr/local/lib/pkgconfig/oci8.pc

但上面是把文件拷贝到/usr/local/lib/pkgconfig目录下,这个目录不是工具pgk-config的默认目录,为了方便,我们直接把这个文件拷贝到pkg-config的默认目录/usr/lib64/pkgconfig目录下:

cd $GOPATH/gopkg.in/rana/ora.v4
cp -aL contrib/oci8.pc /usr/lib64/pkgconfig/.

因为我们下载的oracle instant版本是12.2版本,需要把/usr/lilb64/pkgconifg/oci8.pc中的的version=12.1改成version=12.2

再编译,会报如下错误:

[dbrpcsrv@rabbitmq01 ~]$ go install gopkg.in/rana/ora.v4
# gopkg.in/rana/ora.v4
/bin/ld: warning: libmql1.so, needed by /usr/lib/oracle/12.2/client64/lib/libclntsh.so, not found (try using -rpath or -rpath-link)
/bin/ld: warning: libipc1.so, needed by /usr/lib/oracle/12.2/client64/lib/libclntsh.so, not found (try using -rpath or -rpath-link)
/bin/ld: warning: libnnz12.so, needed by /usr/lib/oracle/12.2/client64/lib/libclntsh.so, not found (try using -rpath or -rpath-link)
/bin/ld: warning: libons.so, needed by /usr/lib/oracle/12.2/client64/lib/libclntsh.so, not found (try using -rpath or -rpath-link)
/bin/ld: warning: libclntshcore.so.12.1, needed by /usr/lib/oracle/12.2/client64/lib/libclntsh.so, not found (try using -rpath or -rpath-link)
/usr/lib/oracle/12.2/client64/lib/libclntsh.so: undefined reference to `ztcedec'
/usr/lib/oracle/12.2/client64/lib/libclntsh.so: undefined reference to `sltstan'
/usr/lib/oracle/12.2/client64/lib/libclntsh.so: undefined reference to `larch_query'
/usr/lib/oracle/12.2/client64/lib/libclntsh.so: undefined reference to `LhtStrSearch'
/usr/lib/oracle/12.2/client64/lib/libclntsh.so: undefined reference to `lpusets'
/usr/lib/oracle/12.2/client64/lib/libclntsh.so: undefined reference to `LdiDateFromString1'
/usr/lib/oracle/12.2/client64/lib/libclntsh.so: undefined reference to `ztcedchk'
/usr/lib/oracle/12.2/client64/lib/libclntsh.so: undefined reference to `lrmpaa'
...
...
...

这个错误是没有指定动态库的路径导致的,设置如下环境变量:

export LD_LIBRARY_PATH=/usr/lib/oracle/12.2/client64/lib

注:最好把上面的环境变量设置到.bash_profile文件中

再编译安装就OK了。

go install gopkg.in/rana/ora.v4

有疑问加站长微信联系(非本文作者)

本文来自:简书

感谢作者:

查看原文:Golang 连接 Oracle

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1446 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传