Search code examples
oraclegooracle-autonomous-db

Why is it failing to connect with oracle Autonomous Database from golang?


    dsn := `
user=ADMIN,
password=temp1!a,
(description= (retry_count=20)(retry_delay=3)(address=(protocol=tcps)(port=1522)(host=adb.ap-seoul-1.oraclecloud.com))(connect_data=(service_name=gee9edfb93f3cf6_redglqweyxqefhhf_high.adb.oraclecloud.com))(security=(ssl_server_cert_dn="CN=adb.ap-seoul-1.oraclecloud.com, OU=Oracle ADB SEOUL, O=Oracle Corporation, L=Redwood City, ST=California, C=US")))
`

db, err := sql.Open("godror", dsn)
if err != nil {
    fmt.Println(err)
    return
}
defer db.Close()

I created an autonomous database, and I try to connect with the above code in golang, but it is impossible to connect with the following error, how to connect, I cannot find a detailed manual, can you help me?

go run main.go
parsing parameters "user=ADMIN,\n\tpassword=temp1!a,\n\t(description= (retry_count=20)(retry_delay=3)(address=(protocol=tcps)(port=1522)(host=adb.ap-seoul-1.oraclecloud.com))(connect_data=(service_name=gee9edfb93f3cf6_redglqweyxqefhhf_high.adb.oraclecloud.com))(security=(ssl_server_cert_dn=\"CN=adb.ap-seoul-1.oraclecloud.com, OU=Oracle ADB SEOUL, O=Oracle Corporation, L=Redwood City, ST=California, C=US\")))": logfmt syntax error at pos 44 on line 3: unexpected '='

Solution

  • I am a new user of oracle and golang, everything is new, and I had difficulty finding documentation to connect oracle and golang. There is a manual, but I did not understand it well.

    If you post a question on stackoverflow, the answers are only links to the manual. It was really hard for beginners to experience a new environment

    I finally found a solution and my code is below. Even if you don't understand the code, it would be nice to think about it one by one while looking at the examples.

    package main
    
    import (
        "database/sql"
        "fmt"
    
        go_ora "github.com/sijms/go-ora/v2"
    )
    
    type DbInfo struct {
        Username string
        Password string
    }
    
    func main() {
        serverInfo := DbInfo{
            Username: "databaseUser",
            Password: "databaseUserPassword",
        }
        urlOptions := map[string]string{
            "TRACE FILE": "trace.log",
            "SSL VERIFY": "FALSE",
        }
        connectString := "(description= (retry_count=20)(retry_delay=3)(address=(protocol=tcps)(port=1522)(host=adb.ap-seoul-1.oraclecloud.com))(connect_data=(service_name=gvsd1dfc9c3cdf6_racc7y8cxsvb16f0z_medium.adb.oraclecloud.com))(security=(ssl_server_dn_match=yes)))"
    
        db, err := sql.Open("oracle", go_ora.BuildJDBC(serverInfo.Username, serverInfo.Password, connectString, urlOptions))
    
        if err != nil {
            panic(err.Error())
        }
        defer db.Close()
    
        r, err := db.Exec(`create table users (
            nickname varchar(20)
        )`)
        if err != nil {
            panic(err.Error())
        }
        fmt.Println(r.LastInsertId())
    }
    

    The code above does not require a wallet for tls access. I'll also upload how to access it through the wallet below, for beginners who need it.