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

3.按顺序解压到同一目录

11g: instantclient_11_2

4.root权限移动文件夹到目录 /usr/lib 下

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

6.把 OCI路径加入系统加载动态库的路径中,并重新加载一次 11g:

echo /usr/lib/instantclient_11_2 >> /etc/ld.so.conf ldconfig

7.安装pkg-config

8.在/usr/lib 目录下创建pkgconfig 目录

9..在/usr/lib/pkgconfig 目录下创建文件oci8.pc,内容如下 11g:

prefix= /usr/lib/instantclient_11_2

libdir=${prefix}

includedir=${prefix}/sdk/include/

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)

} }

编译并运行测试文件

编译:go build –o sql sql.go

运行:./sql system/oracle@192.168.0.150:1521/testpdb