1.安裝Oracle的OCI套件(肯定系統是否安裝gcc和libaio1) 下載地址:https://www.oracle.com/technetwork/database/database-technologies/instant-client/overview/index.html html
2.下載版本實例 instantclient-basic-linux.x64-11.2.0.4.0.zip instantclient-sdk-linux.x64-11.2.0.4.0.zip instantclient-sqlplus-linux.x64-11.2.0.4.0.zip linux
3.按順序解壓到同一目錄
11g: instantclient_11_2 git
4.root權限移動文件夾到目錄 /usr/lib 下 github
5.root權限執行如下命令 11g:
ln /usr/lib/instantclient_11_2/libclntsh.so.11.1 /usr/lib/libclntsh.so
ln /usr/lib/instantclient_11_2/libocci.so.11.1 /usr/lib/libocci.so
ln /usr/lib/instantclient_11_2/libociei.so /usr/lib/libociei.so
ln /usr/lib/instantclient_11_2/libnnz11.so /usr/lib/libnnz12.so
如下兩條是爲了運行sqlplus
ln /usr/lib/instantclient_11_2/libsqlplusic.so /usr/lib/libsqlplusic.so
ln /usr/lib/instantclient_11_2/libsqlplus.so /usr/lib/libsqlplus.so sql
6.把 OCI路徑加入系統加載動態庫的路徑中,並從新加載一次 11g:
echo /usr/lib/instantclient_11_2 >> /etc/ld.so.conf ldconfig bash
7.安裝pkg-config 網絡
8.在/usr/lib 目錄下建立pkgconfig 目錄 oracle
9..在/usr/lib/pkgconfig 目錄下建立文件oci8.pc,內容以下 11g:
prefix= /usr/lib/instantclient_11_2
libdir=${prefix}
includedir=${prefix}/sdk/include/ide
Name: OCI
Description: Oracle database engine
Version: 11.2
Libs: -L${libdir} -lclntsh
Libs.private:
Cflags: -I${includedir}測試
10.在 .bashrc 文件中添加環境變量 11g: # OCI安裝目錄
export ORACLE_HOME=/usr/lib/instantclient_11_2
tnsnames.ora 文件地址須要建立network和admin目錄(此步驟也能夠省略)
export TNS_ADMIN=$ORACLE_HOME/network/admin
OCI安裝目錄加入動態庫加載路徑
export LD_LIBRARY_PATH=$ORACLE_HOME
oci8.pc文件所在路徑
export PKG_CONFIG_PATH=/usr/lib/pkgconfig
11.tnsnames.ora 文件的內容(若是export TNS_ADMIN=$ORACLE_HOME/network/admin 沒有設置則不需操做此步驟)內容以下:
awsdb=
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.0.126)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = awsdb)
)
)
EXTPROC_CONNECTION_DATA =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
)
(CONNECT_DATA =
(SID = PLSExtProc)
(PRESENTATION = RO)
)
)
安裝go-oci8 (準備條件爲:安裝了git和go語言運行環境)
可鏈接網絡:
go get github.com/mattn/go-oci8
不可鏈接網絡:
在Gopath 下建立sql.go 測試鏈接文件
Sql.go
package main
import (
"database/sql"
"fmt"
"log"
"os"
_ "github.com/mattn/go-oci8"
)
func main() {
if len(os.Args) != 2 {
log.Fatalln(os.Args[0] + " user/password@host:port/sid")
}
db, err := sql.Open("oci8", os.Args[1])
if err != nil {
log.Fatalln(err)
}
defer db.Close()
rows, err := db.Query("select user from dual")
if err != nil {
log.Fatalln(err)
}
defer rows.Close()
for rows.Next() {
var data string
rows.Scan(&data)
fmt.Println(data)
}
if err = rows.Err(); err != nil {
log.Fatalln(err)
} }