7

golang 连接oracle 11g数据库(改编)

 2 years ago
source link: https://studygolang.com/articles/29808
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

1.安装Oracle的OCI套件(确定系统是否安装gcc和libaio1) 下载地址:https://www.oracle.com/technetwork/database/database-technologies/instant-client/overview/index.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(下载地址https://www.oracle.com/database/technologies/instant-client/linux-x86-64-downloads.html) 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 [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.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 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

欢迎关注我们的微信公众号,每天学习Go知识

FveQFjN.jpg!web

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK