1
    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 '='
Adriaan
  • 17,741
  • 7
  • 42
  • 75
madol
  • 551
  • 2
  • 17
  • The dsn format is not right. – Stéphane Jeandeaux Jan 09 '23 at 10:26
  • 1
    Connection parameters should be placed into `connectString` key: see https://pkg.go.dev/github.com/godror/godror#readme-connection. Also please verify if commas can be parsed correctly as they are absent in the docs – astentx Jan 09 '23 at 10:26
  • db, error := sql.Open("godror", `user="scott" password="tiger" connectString="dbhost:1521/orclpdb1" I checked this part, what part should be missing in connectString?? – madol Jan 09 '23 at 11:49
  • Doesn't Oracle's db information need to be entered? How do I connect to my db in oracle? – madol Jan 09 '23 at 11:50
  • Cloud-based Oracle ADB requires the use of an Oracle wallet with certificates to support the TCPS protocol: you should be using the TNSNAMES alias for the connection, with all of the connect specifications provided by Oracle. Also, please tell me you didn't post the *actual* connection data, with username and password, in your original question... – pmdba Jan 09 '23 at 12:22
  • Can you show me a simple example? – madol Jan 09 '23 at 14:55
  • Please do not add answers to the question body itself. Instead, you should add it as an answer. [Answering your own question is allowed and even encouraged](https://stackoverflow.com/help/self-answer). – Adriaan Jan 11 '23 at 08:55

1 Answers1

0

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.

madol
  • 551
  • 2
  • 17