linux 上golang 串連oracle資料庫

來源:互聯網
上載者:User

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-oci8curl: (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.bacwget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repoyum clean allyum 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.zipunzip 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總用量 183540drwxr-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.jardrwxrwxr-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.6s2018-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 oci812.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 mainimport (    "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.go2 + 2 always equals: 4

build,運行可執行檔

[root@localhost ~]# go build oracle_db.go[root@localhost ~]# file oracle_dboracle_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_db2 + 2 always equals: 4
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.