connect oracle database in linux
build go file in linux server ,test connect to oracle db
在linux server上吃谣,編譯go代碼宫患,測(cè)試連接oracle數(shù)據(jù)庫(kù)。
I am coding on mac os and I will deploy it to a linux server安接。 cross compile break down! so~。
安裝golang 確保golang運(yùn)行正常护奈,可以跳過(guò)準(zhǔn)備工作次屠。
準(zhǔn)備工作
打算下載下go-oci8發(fā)現(xiàn)報(bào)錯(cuò)
go get github.com/mattn/go-oci8
嘗試了 curl,發(fā)現(xiàn)也報(bào)錯(cuò)
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
正在解析主機(jī) 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 請(qǐng)求,正在等待回應(yīng)... 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
正在解析主機(jī) 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 請(qǐng)求霞丧,正在等待回應(yīng)... 200 OK
長(zhǎng)度:2016830 (1.9M) [application/x-gzip]
正在保存至: “pkg-config-0.29.2.tar.gz”
60% [==============================================================================> ] 1,212,416 389K/s eta(英國(guó)中部時(shí)64% [====================================================================================> ] 1,310,720 393K/s eta(英國(guó)中部時(shí)70% [============================================================================================> ] 1,425,408 401K/s eta(英國(guó)中部時(shí)76% [===================================================================================================> ] 1,540,096 408K/s eta(英國(guó)中部時(shí)82% [===========================================================================================================> ] 1,654,784 414K/s eta(英國(guó)中部時(shí)87% [==================================================================================================================> ] 1,769,472 419K/s eta(英國(guó)中部時(shí)93% [==========================================================================================================================> ] 1,884,160 424K/s eta(英國(guó)中部時(shí)99% [=================================================================================================================================> ] 1,998,848 428K/s eta(英國(guó)中部時(shí)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
設(shè)置環(huán)境變量
[root@localhost ~]# vi .bash_profile
增加如下內(nèi)容
PKG_CONFIG_PATH=/root/instantclient_11_2
LD_LIBRARY_PATH=/root/instantclient_11_2
export PKG_CONFIG_PATH
export LD_LIBRARY_PATH
測(cè)試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
編寫(xiě)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)
}
}
直接運(yùn)行g(shù)o文件
[root@localhost ~]# go run oracle_db.go
2 + 2 always equals: 4
build,運(yùn)行可執(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