Ubuntu 16.04 build oracle environment for golang development

Mirror version

Search oracle on docker hub and find a xe 11 image.

docker pull deepdiver/docker-oracle-xe-11g

The image size is about 1G, and it is about 2.7GB after decompression.

Function

docker run -d -p 1522:22 -p 1521:1521 --name oracle deepdiver/docker-oracle-xe-11g 

Use the above command to run oracle. Wait about 1 minute, use ssh root @localhost -p 1522 can enter the container with the password of admin.

After entering bash environment, execute the following command to solve the problem of Chinese scrambling.

export NLS_LANG="SIMPLIFIED CHINESE_CHINA.AL32UTF8"

The image has installed the sqlplus tool, which can be used to connect directly. The default password of the system user is oracle. When you log in, you will be prompted that the password is about to be obtained. Just change the password.

# Connecting oracle with sqlplus
sqlplus system/oracle@//localhost/xe
# Change Password
alter user system identified by oracle;

Ubuntu 16.04 install client

download

Download the basic and sdk client packages of the current version on the page. I download version 19.3 on my side. The files are instantclient-basic-linux.x64-19.3.0.0.0dbru.zip, instantclient-sdk-linux.x64-19.3.0.0.0dbru.zip.

Create the / opt/oracle directory, move the above two files to the / opt/oracle directory and extract them to get the directory / opt/oracle / instantclient

Configure oci8

You can use / usr / lib / PKG config to determine whether PKG config has been installed in Ubuntu 16.04. Add oci8.pc directly in the directory. The content is as follows:

prefix=/opt/oracle/instantclient_19_3
exec_prefix=${prefix}
libdir=${exec_prefix}
includedir=${prefix}/sdk/include

glib_genmarshal=glib-genmarshal
gobject_query=gobject-query
glib_mkenums=glib-mkenums

Name: oci8
Description: oci8 library
Libs: -L${libdir} -lclntsh
Cflags: -I${includedir}
Version: 19.3

Configure environment variables

Configure the following environment variables, otherwise the execution of golang will report an error

export LD_LIBRARY_PATH=/opt/oracle/instantclient_19_3

golang

Write unit test for oracle connection test, first import driver

import(
    _ "github.com/mattn/go-oci8"
)

Get connection

func getConn() (*sql.DB, error) {
	db, err := sql.Open("oci8", "system/oracle@localhost/xe")
	if err != nil {
		logrus.Errorf("init sql connection error:%s", err.Error())
		return db, err
	}
	// defer db.Close()
	if err = db.Ping(); err != nil {
		logrus.Errorf("open sql connection error:%s", err.Error())
		return db, err
	}
	return db, err
}

Call stored procedure

func TestOracleV2(t *testing.T) {
	db, err := getConn()
	if err != nil {
		return
	}
	loginStmt, err := db.Prepare(`begin PKG_SMS_INTERFACE.send_sms('ms','SMS platform password',:1,:2,1,sysdate,:3);end;`)
	var result string
	defer loginStmt.Close()
	_, err = loginStmt.Exec("181027", "Home for dinner", sql.Out{Dest: &result})
	if err != nil {
		logrus.Errorf("exec  error:%s", err.Error())
		return
	}
	fmt.Println(result)
}

Call result:

=== RUN   TestOracleV2
success
--- PASS: TestOracleV2 (0.03s)

Query the data in the table through sqlplus:

PHONE
--------------------
CONTENT
--------------------------------------------------------------------------------
SEND_TIME
--------------
10.20.0.5:3906,2019-05-17 08:55:06
17-5 month -19

181027
//Home for dinner
17-5 month -19

Keywords: Programming Oracle Docker SQL sqlplus

Added by greip on Fri, 08 Nov 2019 19:23:14 +0200