golang linux 連接oracle數(shù)據(jù)庫

connect oracle database in linux

build go file in linux server ,test connect to oracle db

在linux server上,編譯go代碼,測試連接oracle數(shù)據(jù)庫。

I am coding on mac os and I will deploy it to a linux server。 cross compile break down! so~。

安裝golang 確保golang運行正常,可以跳過準備工作。

準備工作

打算下載下go-oci8發(fā)現(xiàn)報錯

go get github.com/mattn/go-oci8

嘗試了 curl,發(fā)現(xiàn)也報錯

curl https://github.com/mattn/go-oci8
curl: (35) SSL connect error

yum安裝nss發(fā)現(xiàn)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... 已連接。
已發(fā)出 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... 已連接。
已發(fā)出 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

設置環(huán)境變量

[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,運行可執(zhí)行文件

[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
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • Astronomygonova - A wrapper for libnova -- Celestial Mech...
    JumboWu閱讀 9,424評論 0 41
  • 目的 搭建一個Oracle 11gR2支持數(shù)據(jù)熱備份的驗證環(huán)境,對應用系統(tǒng)進行集群部署驗證。 準備 1、CentO...
    席理加閱讀 7,115評論 1 8
  • 引言 cx_Oracle是Python環(huán)境下的一個(好像也是唯一的一個)用于操作Oracle的第三方模塊。最近在寫...
    bluexiii閱讀 9,703評論 3 13
  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,694評論 19 139
  • 近兩天在CentOS7上安裝了oracle,在網上找了一些資料,發(fā)現(xiàn)不是很全面,為了方便以后再次安裝,所以...
    即墨雨閱讀 6,992評論 1 11

友情鏈接更多精彩內容