Connect Oracle Database in Linux
Build go file in Linux Server, test connect to Oracle DB
On the Linux server, compile the Go Code and test the connection to the Oracle database.
I am coding on Mac OS and I'll deploy it to a Linux server. Cross compile break down! so~.
Install Golang to make sure the Golang is working properly and you can skip the preparation.
Preparatory work
Intend to download the next Go-oci8 found error
go get github.com/mattn/go-oci8
I tried curl and found an error.
curl https://github.com/mattn/go-oci8curl: (35) SSL connect error
Yum install NSS Discovery 404
yum update nss
Update Yum Source
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
Update NSS again, successfully
yum update nss
再次安装go-oci8
go get github.com/mattn/go-oci8
Download 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
Decompression 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 Total dosage 183540drwxr-xr-x 3 root root 4096 July 15:01. Dr-xr-x---. Root root 4096 July 19 15:01.. -rwxrwxr-x 1 root root 25420 August adrci-rw-rw-r--1 root root 439 August basic_readme-rwxrwxr-x 1 root root 47860 August genezi-rwxrwxr-x 1 root root 53865194 August libclntsh.so.11.1-r-xr-xr-x 1 R Oot root 7996693 August libnnz11.so-rwxrwxr-x 1 root root 1973074 August libocci.so.11.1-rwxrwxr-x 1 roo T root 118738042 August libociei.so-r-xr-xr-x 1 root root 164942 August libocijdbc11.so-r--r--r--1 root Root 2091135 August ojdbc5.jar-r--r--r--1 root root 2739616 August ojdbc6.jardrwxrwxr-x 4 root root 4096 August sdk-rwxrwxr-x 1 root root 192365 August Uidrvci-rw-rw-r--1 root root 66779 August 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
Download 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 Parsing host pkgconfig.freedesktop.org ... 131.252.210.176, 2610:10:20:722:a800:ff:feda:470f is connecting pkgconfig.freedesktop.org|131.252.210.176|:80 ... is connected. An HTTP request has been made and is waiting for a response ... 301 Moved Permanently location: https://pkg-config.freedesktop.org/releases/pkg-config-0.29.2.tar.gz [follow to New url]-- 2018-07-19 15:21:51--https://pkg-config.freedesktop.org/releases/pkg-config-0.29.2.tar.gz is parsing the host Pkg-config.freedesktop.org ... 131.252.210.176, 2610:10:20:722:a800:ff:feda:470f is connecting pkg-config.freedesktop.org|131.252.210.176|:443 ... is connected. An HTTP request has been made and is waiting for a response ... $ OK Length: 2016830 (1.9M) [application/x-gzip] saving to: "pkg-config-0.29.2.tar.gz" 60% [================================= =============================================>] 1,212,416 389k/ S ETA (UK Central Time 64% [====================================================================================>] 1,310,720 393k/s ETA (70% [============================================================================================>, central United Kingdom ] 1,425,408 401k/s ETA (UK Central Time 76% [=========================================================== ========================================>] 1,540,096 408k/s ETA (UK Central Time 82% [========== =================================================================================================> ] 1,654,784 414k/s ETA (UK Central Time 87% [================================================================================= =================================>] 1,769,472 419k/s ETA (UK Central Time 93% [================================ ==========================================================================================>] 1,884,160 424K /s ETA (UK Central Time 99% [======================================================================================================================== =========>] 1,998,848 428k/s ETA (Central United Kingdom time 100%[=================================================================== ================================================================>] 2,016,830 432K/s in 4.6s2018-07-19 15:21:58 (4 KB/S)-Saved "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
New OCI8.PC
[root@localhost instantclient_11_2]# touch oci8.pc
Setting environment variables
[root@localhost ~]# vi .bash_profile
Add the following content
Pkg_config_path=/root/instantclient_11_2
Ld_library_path=/root/instantclient_11_2
Export Pkg_config_path
Export Ld_library_path
Test 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
Writing 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) }}
Run the go file directly
[root@localhost ~]# go run oracle_db.go2 + 2 always equals: 4
Build, run the executable file
[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[ro Ot@localhost ~]#./oracle_db2 + 2 always equals:4