golang linux 连接oracle数据库
2018-07-19 本文已影响0人
luckyase
connect oracle database in linux
build go file in linux server ,test connect to oracle db
在linux server上,编译go代码,测试连接oracle数据库。
I am coding on mac os and I will deploy it to a linux server。 cross compile break down! so~。
安装golang 确保golang运行正常,可以跳过准备工作。
准备工作
打算下载下go-oci8发现报错
go get github.com/mattn/go-oci8
尝试了 curl,发现也报错
curl https://github.com/mattn/go-oci8
curl: (35) SSL connect error
yum安装nss发现404
yum update nss
更新 yum源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bac
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
yum clean all
yum makecache
再次更新nss,成功
yum update nss
再次安装go-oci8
go get github.com/mattn/go-oci8
下载instantclient
instantclient-basic-macos.x64-11.2.0.4.0
instantclient-sdk-macos.x64-11.2.0.4.0
download http://www.oracle.com/technetwork/topics/intel-macsoft-096467.html
解压instantclient
unzip instantclient-basic-linux.x64-11.2.0.4.0.zip
unzip instantclient-sdk-linux.x64-11.2.0.4.0.zip
[root@localhost instantclient_11_2]# pwd
/root/instantclient_11_2
[root@localhost instantclient_11_2]# ls -al
总用量 183540
drwxr-xr-x 3 root root 4096 7月 19 15:01 .
dr-xr-x---. 44 root root 4096 7月 19 15:01 ..
-rwxrwxr-x 1 root root 25420 8月 25 2013 adrci
-rw-rw-r-- 1 root root 439 8月 25 2013 BASIC_README
-rwxrwxr-x 1 root root 47860 8月 25 2013 genezi
-rwxrwxr-x 1 root root 53865194 8月 25 2013 libclntsh.so.11.1
-r-xr-xr-x 1 root root 7996693 8月 25 2013 libnnz11.so
-rwxrwxr-x 1 root root 1973074 8月 25 2013 libocci.so.11.1
-rwxrwxr-x 1 root root 118738042 8月 25 2013 libociei.so
-r-xr-xr-x 1 root root 164942 8月 25 2013 libocijdbc11.so
-r--r--r-- 1 root root 2091135 8月 25 2013 ojdbc5.jar
-r--r--r-- 1 root root 2739616 8月 25 2013 ojdbc6.jar
drwxrwxr-x 4 root root 4096 8月 25 2013 sdk
-rwxrwxr-x 1 root root 192365 8月 25 2013 uidrvci
-rw-rw-r-- 1 root root 66779 8月 25 2013 xstreams.jar
[root@localhost instantclient_11_2]#
[root@localhost instantclient_11_2]# cp libclntsh.so.11.1 libclntsh.so
[root@localhost instantclient_11_2]# ln libclntsh.so /usr/lib/libclntsh.so
[root@localhost instantclient_11_2]# ln libocci.so.11.1 /usr/lib/libocci.so
[root@localhost instantclient_11_2]# ln libociei.so /usr/lib/libociei.so
[root@localhost instantclient_11_2]# ln libnnz11.so /usr/lib/libnnz11.so
下载pkg-config
[root@localhost ~]# wget http://pkgconfig.freedesktop.org/releases/pkg-config-0.29.2.tar.gz
--2018-07-19 15:21:50-- http://pkgconfig.freedesktop.org/releases/pkg-config-0.29.2.tar.gz
正在解析主机 pkgconfig.freedesktop.org... 131.252.210.176, 2610:10:20:722:a800:ff:feda:470f
正在连接 pkgconfig.freedesktop.org|131.252.210.176|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 301 Moved Permanently
位置:https://pkg-config.freedesktop.org/releases/pkg-config-0.29.2.tar.gz [跟随至新的 URL]
--2018-07-19 15:21:51-- https://pkg-config.freedesktop.org/releases/pkg-config-0.29.2.tar.gz
正在解析主机 pkg-config.freedesktop.org... 131.252.210.176, 2610:10:20:722:a800:ff:feda:470f
正在连接 pkg-config.freedesktop.org|131.252.210.176|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:2016830 (1.9M) [application/x-gzip]
正在保存至: “pkg-config-0.29.2.tar.gz”
60% [==============================================================================> ] 1,212,416 389K/s eta(英国中部时64% [====================================================================================> ] 1,310,720 393K/s eta(英国中部时70% [============================================================================================> ] 1,425,408 401K/s eta(英国中部时76% [===================================================================================================> ] 1,540,096 408K/s eta(英国中部时82% [===========================================================================================================> ] 1,654,784 414K/s eta(英国中部时87% [==================================================================================================================> ] 1,769,472 419K/s eta(英国中部时93% [==========================================================================================================================> ] 1,884,160 424K/s eta(英国中部时99% [=================================================================================================================================> ] 1,998,848 428K/s eta(英国中部时100%[===================================================================================================================================>] 2,016,830 432K/s in 4.6s
2018-07-19 15:21:58 (432 KB/s) - 已保存 “pkg-config-0.29.2.tar.gz” [2016830/2016830])
[root@localhost ~]# tar xvf pkg-config-0.29.2.tar.gz
[root@localhost ~]# cd pkg-config-0.29.2
[root@localhost pkg-config-0.29.2]# ./configure --with-internal-glib
[root@localhost pkg-config-0.29.2]# make
[root@localhost pkg-config-0.29.2]# make install
新建oci8.pc
[root@localhost instantclient_11_2]# touch oci8.pc
设置环境变量
[root@localhost ~]# vi .bash_profile
增加如下内容
PKG_CONFIG_PATH=/root/instantclient_11_2
LD_LIBRARY_PATH=/root/instantclient_11_2
export PKG_CONFIG_PATH
export LD_LIBRARY_PATH
测试pkg-config oci8
[root@localhost instantclient_11_2]# source ~/.bash_profile
[root@localhost instantclient_11_2]# pkg-config --modversion oci8
12.2
[root@localhost instantclient_11_2]# pkg-config oci8
[root@localhost instantclient_11_2]# pkg-config --libs oci8
-L/root/instantclient_11_2 -lclntsh
编写oracle_db.go
package main
import (
"fmt"
_ "github.com/mattn/go-oci8"
"database/sql"
)
func main() {
db, err := sql.Open("oci8", "username/pwd@ip:1521/dbname")
if err != nil {
fmt.Println("abc", 123, err)
return
}
defer db.Close()
if err = db.Ping(); err != nil {
fmt.Printf("Error connecting to the database: %s\n", err)
return
}
rows, err := db.Query("select 2+2 from dual")
if err != nil {
fmt.Println("Error fetching addition")
fmt.Println(err)
return
}
defer rows.Close()
for rows.Next() {
var sum int
rows.Scan(&sum)
fmt.Printf("2 + 2 always equals: %d\n", sum)
}
}
直接运行go文件
[root@localhost ~]# go run oracle_db.go
2 + 2 always equals: 4
build,运行可执行文件
[root@localhost ~]# go build oracle_db.go
[root@localhost ~]# file oracle_db
oracle_db: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, not stripped
[root@localhost ~]# ./oracle_db
2 + 2 always equals: 4